mirror of
https://github.com/FabricMC/fabric.git
synced 2025-03-28 07:40:02 -04:00
Add an event for when the server is about to stop
This may allow mods to perform any additional clean up of resources, or what not.
This commit is contained in:
parent
19bc18104d
commit
ac954f6e08
4 changed files with 28 additions and 1 deletions
src
main/java/net/fabricmc/fabric
test
|
@ -24,6 +24,7 @@ import java.util.function.Consumer;
|
|||
|
||||
public final class ServerEvent {
|
||||
public static final HandlerRegistry<Consumer<MinecraftServer>> START = new HandlerArray<>(Consumer.class);
|
||||
public static final HandlerRegistry<Consumer<MinecraftServer>> STOP = new HandlerArray<>(Consumer.class);
|
||||
|
||||
private ServerEvent() {
|
||||
|
||||
|
|
|
@ -34,4 +34,14 @@ public class MixinMinecraftServer {
|
|||
handler.accept((MinecraftServer) (Object) this);
|
||||
}
|
||||
}
|
||||
|
||||
@Inject(
|
||||
at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;exit()V", shift = At.Shift.BY, by = -2, ordinal = 0),
|
||||
method = "run"
|
||||
)
|
||||
public void beforeExitServer(CallbackInfo info) {
|
||||
for (Consumer<MinecraftServer> handler : ((HandlerArray<Consumer<MinecraftServer>>) ServerEvent.STOP).getBackingArray()) {
|
||||
handler.accept((MinecraftServer) (Object) this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
15
src/test/java/net/fabricmc/fabric/events/ServerEventMod.java
Normal file
15
src/test/java/net/fabricmc/fabric/events/ServerEventMod.java
Normal file
|
@ -0,0 +1,15 @@
|
|||
package net.fabricmc.fabric.events;
|
||||
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class ServerEventMod implements ModInitializer {
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
ServerEvent.START.register(server -> LOGGER.info("Server starting (" + server + ")"));
|
||||
ServerEvent.STOP.register(server -> LOGGER.info("Server stopped (" + server + ")"));
|
||||
}
|
||||
}
|
|
@ -6,6 +6,7 @@
|
|||
"description": "A series of test mods to check Fabric works.",
|
||||
"license": "Apache-2.0",
|
||||
"initializers": [
|
||||
"net.fabricmc.fabric.colormapper.ColorProviderMod"
|
||||
"net.fabricmc.fabric.colormapper.ColorProviderMod",
|
||||
"net.fabricmc.fabric.events.ServerEventMod"
|
||||
]
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue