From 3548ae533e74ace51c950cb8da6f8804331776ea Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 1 May 2024 22:20:11 +0200 Subject: [PATCH] Don't crash when switching local worlds (#123) * Don't crash on server/world switch * prettier --- .../org/geysermc/floodgate/FabricMod.java | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/geysermc/floodgate/FabricMod.java b/src/main/java/org/geysermc/floodgate/FabricMod.java index a2da132..090ee15 100644 --- a/src/main/java/org/geysermc/floodgate/FabricMod.java +++ b/src/main/java/org/geysermc/floodgate/FabricMod.java @@ -1,5 +1,7 @@ package org.geysermc.floodgate; +import net.fabricmc.api.EnvType; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents; import org.geysermc.floodgate.inject.fabric.FabricInjector; import com.google.inject.Guice; import com.google.inject.Injector; @@ -10,6 +12,9 @@ import org.geysermc.floodgate.module.*; public class FabricMod implements ModInitializer { + + private boolean started; + @Override public void onInitialize() { FabricInjector.setInstance(new FabricInjector()); @@ -30,19 +35,28 @@ public void onInitialize() { // This can probably be Guice-i-fied but that is beyond me MinecraftServerHolder.set(server); - platform.enable( - new FabricAddonModule(), - new FabricListenerModule(), - new PluginMessageModule() - ); + if (!started) { + platform.enable( + new FabricAddonModule(), + new FabricListenerModule(), + new PluginMessageModule() + ); + started = true; + } long endCtm = System.currentTimeMillis(); injector.getInstance(FloodgateLogger.class) .translatedInfo("floodgate.core.finish", endCtm - ctm); }); - ServerLifecycleEvents.SERVER_STOPPING.register((server) -> { - platform.disable(); - }); + if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { + ClientLifecycleEvents.CLIENT_STOPPING.register(($) -> { + platform.disable(); + }); + } else { + ServerLifecycleEvents.SERVER_STOPPING.register((server) -> { + platform.disable(); + }); + } } }