From ac954f6e08dab5b8dd5789f7cab0adb80a91d90e Mon Sep 17 00:00:00 2001 From: SquidDev Date: Mon, 17 Dec 2018 22:57:59 +0000 Subject: [PATCH] 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. --- .../net/fabricmc/fabric/events/ServerEvent.java | 1 + .../mixin/events/server/MixinMinecraftServer.java | 10 ++++++++++ .../fabricmc/fabric/events/ServerEventMod.java | 15 +++++++++++++++ src/test/resources/mod.json | 3 ++- 4 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 src/test/java/net/fabricmc/fabric/events/ServerEventMod.java diff --git a/src/main/java/net/fabricmc/fabric/events/ServerEvent.java b/src/main/java/net/fabricmc/fabric/events/ServerEvent.java index a02322f7c..b6a67f1e2 100644 --- a/src/main/java/net/fabricmc/fabric/events/ServerEvent.java +++ b/src/main/java/net/fabricmc/fabric/events/ServerEvent.java @@ -24,6 +24,7 @@ import java.util.function.Consumer; public final class ServerEvent { public static final HandlerRegistry> START = new HandlerArray<>(Consumer.class); + public static final HandlerRegistry> STOP = new HandlerArray<>(Consumer.class); private ServerEvent() { diff --git a/src/main/java/net/fabricmc/fabric/mixin/events/server/MixinMinecraftServer.java b/src/main/java/net/fabricmc/fabric/mixin/events/server/MixinMinecraftServer.java index 07f174d90..ce4689aab 100644 --- a/src/main/java/net/fabricmc/fabric/mixin/events/server/MixinMinecraftServer.java +++ b/src/main/java/net/fabricmc/fabric/mixin/events/server/MixinMinecraftServer.java @@ -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 handler : ((HandlerArray>) ServerEvent.STOP).getBackingArray()) { + handler.accept((MinecraftServer) (Object) this); + } + } } diff --git a/src/test/java/net/fabricmc/fabric/events/ServerEventMod.java b/src/test/java/net/fabricmc/fabric/events/ServerEventMod.java new file mode 100644 index 000000000..910df8f12 --- /dev/null +++ b/src/test/java/net/fabricmc/fabric/events/ServerEventMod.java @@ -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 + ")")); + } +} diff --git a/src/test/resources/mod.json b/src/test/resources/mod.json index c43aa5f21..16e1e41dd 100644 --- a/src/test/resources/mod.json +++ b/src/test/resources/mod.json @@ -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" ] }