Skip to content

Commit

Permalink
Add core:lifecycle_events
Browse files Browse the repository at this point in the history
  • Loading branch information
OroArmor committed Aug 16, 2024
1 parent 024f8b8 commit 1b13358
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 273 deletions.
4 changes: 4 additions & 0 deletions fabric-lifecycle-events-v1/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ loom {
}

moduleDependencies(project, ['fabric-api-base'])

dependencies {
modApi getQslModule("core", "lifecycle_events")
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
* Copyright 2024 The Quilt Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,7 +20,7 @@
import net.minecraft.client.MinecraftClient;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.fabricmc.fabric.impl.base.event.QuiltCompatEvent;

public final class ClientLifecycleEvents {
private ClientLifecycleEvents() {
Expand All @@ -30,23 +31,23 @@ private ClientLifecycleEvents() {
*
* <p>This occurs while the splash screen is displayed.
*/
public static final Event<ClientStarted> CLIENT_STARTED = EventFactory.createArrayBacked(ClientStarted.class, callbacks -> client -> {
for (ClientStarted callback : callbacks) {
callback.onClientStarted(client);
}
});
public static final Event<ClientStarted> CLIENT_STARTED = QuiltCompatEvent.fromQuilt(
org.quiltmc.qsl.lifecycle.api.client.event.ClientLifecycleEvents.READY,
event -> event::onClientStarted,
event -> event.get()::readyClient
);

/**
* Called when Minecraft's client begins to stop.
* This is caused by quitting while in game, or closing the game window.
*
* <p>This will be called before the integrated server is stopped if it is running.
*/
public static final Event<ClientStopping> CLIENT_STOPPING = EventFactory.createArrayBacked(ClientStopping.class, callbacks -> client -> {
for (ClientStopping callback : callbacks) {
callback.onClientStopping(client);
}
});
public static final Event<ClientStopping> CLIENT_STOPPING = QuiltCompatEvent.fromQuilt(
org.quiltmc.qsl.lifecycle.api.client.event.ClientLifecycleEvents.STOPPING,
event -> event::onClientStopping,
event -> event.get()::stoppingClient
);

@FunctionalInterface
public interface ClientStarted {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
* Copyright 2024 The Quilt Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,7 +21,7 @@
import net.minecraft.client.world.ClientWorld;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.fabricmc.fabric.impl.base.event.QuiltCompatEvent;

public final class ClientTickEvents {
private ClientTickEvents() {
Expand All @@ -29,40 +30,40 @@ private ClientTickEvents() {
/**
* Called at the start of the client tick.
*/
public static final Event<StartTick> START_CLIENT_TICK = EventFactory.createArrayBacked(StartTick.class, callbacks -> client -> {
for (StartTick event : callbacks) {
event.onStartTick(client);
}
});
public static final Event<StartTick> START_CLIENT_TICK = QuiltCompatEvent.fromQuilt(
org.quiltmc.qsl.lifecycle.api.client.event.ClientTickEvents.START,
event -> event::onStartTick,
event -> event.get()::startClientTick
);

/**
* Called at the end of the client tick.
*/
public static final Event<EndTick> END_CLIENT_TICK = EventFactory.createArrayBacked(EndTick.class, callbacks -> client -> {
for (EndTick event : callbacks) {
event.onEndTick(client);
}
});
public static final Event<EndTick> END_CLIENT_TICK = QuiltCompatEvent.fromQuilt(
org.quiltmc.qsl.lifecycle.api.client.event.ClientTickEvents.END,
event -> event::onEndTick,
event -> event.get()::endClientTick
);

/**
* Called at the start of a ClientWorld's tick.
*/
public static final Event<StartWorldTick> START_WORLD_TICK = EventFactory.createArrayBacked(StartWorldTick.class, callbacks -> world -> {
for (StartWorldTick callback : callbacks) {
callback.onStartTick(world);
}
});
public static final Event<StartWorldTick> START_WORLD_TICK = QuiltCompatEvent.fromQuilt(
org.quiltmc.qsl.lifecycle.api.client.event.ClientWorldTickEvents.START,
event -> (client, world) -> event.onStartTick(world),
event -> (world) -> event.get().startWorldTick(MinecraftClient.getInstance(), world)
);

/**
* Called at the end of a ClientWorld's tick.
*
* <p>End of world tick may be used to start async computations for the next tick.
*/
public static final Event<EndWorldTick> END_WORLD_TICK = EventFactory.createArrayBacked(EndWorldTick.class, callbacks -> world -> {
for (EndWorldTick callback : callbacks) {
callback.onEndTick(world);
}
});
public static final Event<EndWorldTick> END_WORLD_TICK = QuiltCompatEvent.fromQuilt(
org.quiltmc.qsl.lifecycle.api.client.event.ClientWorldTickEvents.END,
event -> (client, world) -> event.onEndTick(world),
event -> (world) -> event.get().endWorldTick(MinecraftClient.getInstance(), world)
);

@FunctionalInterface
public interface StartTick {
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
"ClientPlayNetworkHandlerMixin",
"ClientTagLoaderMixin",
"ClientWorldClientEntityHandlerMixin",
"ClientWorldMixin",
"MinecraftClientMixin",
"WorldChunkMixin"
],
"injectors": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
* Copyright 2024 The Quilt Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +24,7 @@

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.fabricmc.fabric.impl.base.event.QuiltCompatEvent;

public final class ServerLifecycleEvents {
private ServerLifecycleEvents() {
Expand All @@ -33,22 +35,22 @@ private ServerLifecycleEvents() {
*
* <p>This occurs before the {@link PlayerManager player manager} and any worlds are loaded.
*/
public static final Event<ServerStarting> SERVER_STARTING = EventFactory.createArrayBacked(ServerStarting.class, callbacks -> server -> {
for (ServerStarting callback : callbacks) {
callback.onServerStarting(server);
}
});
public static final Event<ServerStarting> SERVER_STARTING = QuiltCompatEvent.fromQuilt(
org.quiltmc.qsl.lifecycle.api.event.ServerLifecycleEvents.STARTING,
event -> event::onServerStarting,
event -> event.get()::startingServer
);

/**
* Called when a Minecraft server has started and is about to tick for the first time.
*
* <p>At this stage, all worlds are live.
*/
public static final Event<ServerStarted> SERVER_STARTED = EventFactory.createArrayBacked(ServerStarted.class, (callbacks) -> (server) -> {
for (ServerStarted callback : callbacks) {
callback.onServerStarted(server);
}
});
public static final Event<ServerStarted> SERVER_STARTED = QuiltCompatEvent.fromQuilt(
org.quiltmc.qsl.lifecycle.api.event.ServerLifecycleEvents.READY,
event -> event::onServerStarted,
event -> event.get()::readyServer
);

/**
* Called when a Minecraft server has started shutting down.
Expand All @@ -58,11 +60,11 @@ private ServerLifecycleEvents() {
*
* <p>All worlds are still present and can be modified.
*/
public static final Event<ServerStopping> SERVER_STOPPING = EventFactory.createArrayBacked(ServerStopping.class, (callbacks) -> (server) -> {
for (ServerStopping callback : callbacks) {
callback.onServerStopping(server);
}
});
public static final Event<ServerStopping> SERVER_STOPPING = QuiltCompatEvent.fromQuilt(
org.quiltmc.qsl.lifecycle.api.event.ServerLifecycleEvents.STOPPING,
event -> event::onServerStopping,
event -> event.get()::stoppingServer
);

/**
* Called when a Minecraft server has stopped.
Expand All @@ -71,11 +73,11 @@ private ServerLifecycleEvents() {
* <p>For example, an {@link net.fabricmc.api.EnvType#CLIENT integrated server} will begin stopping, but its client may continue to run.
* Meanwhile, for a {@link net.fabricmc.api.EnvType#SERVER dedicated server}, this will be the last event called.
*/
public static final Event<ServerStopped> SERVER_STOPPED = EventFactory.createArrayBacked(ServerStopped.class, callbacks -> server -> {
for (ServerStopped callback : callbacks) {
callback.onServerStopped(server);
}
});
public static final Event<ServerStopped> SERVER_STOPPED = QuiltCompatEvent.fromQuilt(
org.quiltmc.qsl.lifecycle.api.event.ServerLifecycleEvents.STOPPED,
event -> event::onServerStopped,
event -> event.get()::exitServer
);

/**
* Called when a Minecraft server is about to send tag and recipe data to a player.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
* Copyright 2024 The Quilt Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,7 +21,7 @@
import net.minecraft.server.world.ServerWorld;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.fabricmc.fabric.impl.base.event.QuiltCompatEvent;

public final class ServerTickEvents {
private ServerTickEvents() {
Expand All @@ -29,40 +30,40 @@ private ServerTickEvents() {
/**
* Called at the start of the server tick.
*/
public static final Event<StartTick> START_SERVER_TICK = EventFactory.createArrayBacked(StartTick.class, callbacks -> server -> {
for (StartTick event : callbacks) {
event.onStartTick(server);
}
});
public static final Event<StartTick> START_SERVER_TICK = QuiltCompatEvent.fromQuilt(
org.quiltmc.qsl.lifecycle.api.event.ServerTickEvents.START,
event -> event::onStartTick,
event -> event.get()::startServerTick
);

/**
* Called at the end of the server tick.
*/
public static final Event<EndTick> END_SERVER_TICK = EventFactory.createArrayBacked(EndTick.class, callbacks -> server -> {
for (EndTick event : callbacks) {
event.onEndTick(server);
}
});
public static final Event<EndTick> END_SERVER_TICK = QuiltCompatEvent.fromQuilt(
org.quiltmc.qsl.lifecycle.api.event.ServerTickEvents.END,
event -> event::onEndTick,
event -> event.get()::endServerTick
);

/**
* Called at the start of a ServerWorld's tick.
*/
public static final Event<StartWorldTick> START_WORLD_TICK = EventFactory.createArrayBacked(StartWorldTick.class, callbacks -> world -> {
for (StartWorldTick callback : callbacks) {
callback.onStartTick(world);
}
});
public static final Event<StartWorldTick> START_WORLD_TICK = QuiltCompatEvent.fromQuilt(
org.quiltmc.qsl.lifecycle.api.event.ServerWorldTickEvents.START,
event -> (server, world) -> event.onStartTick(world),
event -> world -> event.get().startWorldTick(world.getServer(), world)
);

/**
* Called at the end of a ServerWorld's tick.
*
* <p>End of world tick may be used to start async computations for the next tick.
*/
public static final Event<EndWorldTick> END_WORLD_TICK = EventFactory.createArrayBacked(EndWorldTick.class, callbacks -> world -> {
for (EndWorldTick callback : callbacks) {
callback.onEndTick(world);
}
});
public static final Event<EndWorldTick> END_WORLD_TICK = QuiltCompatEvent.fromQuilt(
org.quiltmc.qsl.lifecycle.api.event.ServerWorldTickEvents.END,
event -> (server, world) -> event.onEndTick(world),
event -> world -> event.get().endWorldTick(world.getServer(), world)
);

@FunctionalInterface
public interface StartTick {
Expand Down
Loading

0 comments on commit 1b13358

Please sign in to comment.