From 7927c61a224204fca7df1ba43ccfbee6f93ef08d Mon Sep 17 00:00:00 2001 From: EdwinMindcraft Date: Fri, 19 Aug 2022 15:18:45 +0200 Subject: [PATCH] All: Attempt to fix all known bugs. --- .../calio/client/CalioClientEventHandler.java | 7 +++++ .../calio/client/util/ClientHelper.java | 4 +++ .../calio/common/CalioEventHandler.java | 2 +- .../registry/CalioDynamicRegistryManager.java | 30 ++++++------------- 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/main/java/io/github/edwinmindcraft/calio/client/CalioClientEventHandler.java b/src/main/java/io/github/edwinmindcraft/calio/client/CalioClientEventHandler.java index 2a7836d..f9c298b 100644 --- a/src/main/java/io/github/edwinmindcraft/calio/client/CalioClientEventHandler.java +++ b/src/main/java/io/github/edwinmindcraft/calio/client/CalioClientEventHandler.java @@ -2,8 +2,10 @@ import io.github.apace100.calio.resource.OrderedResourceListenerManager; import io.github.edwinmindcraft.calio.api.CalioAPI; +import io.github.edwinmindcraft.calio.common.registry.CalioDynamicRegistryManager; import net.minecraft.server.packs.PackType; import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.client.event.ClientPlayerNetworkEvent; import net.minecraftforge.client.event.RegisterClientReloadListenersEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod; @@ -14,4 +16,9 @@ public class CalioClientEventHandler { public void addClientResources(RegisterClientReloadListenersEvent event) { OrderedResourceListenerManager.getInstance().addResources(PackType.CLIENT_RESOURCES, event::registerReloadListener); } + + @SubscribeEvent + public void onDisconnecting(ClientPlayerNetworkEvent.LoggingOut event) { + CalioDynamicRegistryManager.removeClientInstance(); + } } diff --git a/src/main/java/io/github/edwinmindcraft/calio/client/util/ClientHelper.java b/src/main/java/io/github/edwinmindcraft/calio/client/util/ClientHelper.java index 9a00488..68ea0e2 100644 --- a/src/main/java/io/github/edwinmindcraft/calio/client/util/ClientHelper.java +++ b/src/main/java/io/github/edwinmindcraft/calio/client/util/ClientHelper.java @@ -2,14 +2,18 @@ import net.minecraft.client.Minecraft; import net.minecraft.core.RegistryAccess; +import net.minecraftforge.server.ServerLifecycleHooks; import org.jetbrains.annotations.Nullable; public class ClientHelper { public static boolean isServerContext(@Nullable RegistryAccess access) { Minecraft instance = Minecraft.getInstance(); + //This is wrong in a data context, and annotations are busted; + //noinspection ConstantConditions if (instance == null) return true; // Data Context if (instance.getConnection() == null) return true; // Outside a world + if (ServerLifecycleHooks.getCurrentServer() == null) return false; // No server. return access != null && access != instance.getConnection().registryAccess(); } } diff --git a/src/main/java/io/github/edwinmindcraft/calio/common/CalioEventHandler.java b/src/main/java/io/github/edwinmindcraft/calio/common/CalioEventHandler.java index 8fbba40..bba55ef 100644 --- a/src/main/java/io/github/edwinmindcraft/calio/common/CalioEventHandler.java +++ b/src/main/java/io/github/edwinmindcraft/calio/common/CalioEventHandler.java @@ -48,7 +48,7 @@ public static void onServerReload(AddReloadListenerEvent event) { @SubscribeEvent public static void onServerStopped(ServerStoppedEvent event) { CalioAPI.LOGGER.info("Removing Dynamic Registries for: " + event.getServer()); - CalioDynamicRegistryManager.removeInstance(event.getServer().registryAccess()); + CalioDynamicRegistryManager.removeServerInstance(); } @SubscribeEvent diff --git a/src/main/java/io/github/edwinmindcraft/calio/common/registry/CalioDynamicRegistryManager.java b/src/main/java/io/github/edwinmindcraft/calio/common/registry/CalioDynamicRegistryManager.java index 4092db4..7dfc89a 100644 --- a/src/main/java/io/github/edwinmindcraft/calio/common/registry/CalioDynamicRegistryManager.java +++ b/src/main/java/io/github/edwinmindcraft/calio/common/registry/CalioDynamicRegistryManager.java @@ -29,13 +29,9 @@ import net.minecraft.server.packs.resources.ResourceManager; import net.minecraft.util.GsonHelper; import net.minecraft.util.profiling.ProfilerFiller; -import net.minecraftforge.api.distmarker.Dist; -import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.fml.DistExecutor; -import net.minecraftforge.fml.ModList; import net.minecraftforge.fml.ModLoader; -import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; import net.minecraftforge.network.PacketDistributor; import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.tuple.Pair; @@ -59,8 +55,6 @@ public class CalioDynamicRegistryManager implements ICalioDynamicRegistryManager { private static final Gson GSON = new GsonBuilder().create(); private static final int FILE_SUFFIX_LENGTH = ".json".length(); - - private static final Map INSTANCES = new ConcurrentHashMap<>(); private static CalioDynamicRegistryManager clientInstance = null; private static CalioDynamicRegistryManager serverInstance = null; private boolean lock; @@ -184,33 +178,27 @@ public static boolean isServerContext(RegistryAccess access) { } public static CalioDynamicRegistryManager getInstance(RegistryAccess server) { - if (isServerContext(server)) - return addInstance(server); + return isServerContext(server) ? getServerInstance() : getClientInstance(); + } + public static CalioDynamicRegistryManager getClientInstance() { if (clientInstance == null) - initializeClient(); + clientInstance = new CalioDynamicRegistryManager(); return clientInstance; } - private static CalioDynamicRegistryManager addInstance(RegistryAccess server) { + public static CalioDynamicRegistryManager getServerInstance() { if (serverInstance == null) serverInstance = new CalioDynamicRegistryManager(); - return serverInstance;//INSTANCES.computeIfAbsent(server, s -> new CalioDynamicRegistryManager()); + return serverInstance; } - public static void removeInstance(RegistryAccess server) { - //INSTANCES.remove(server); + public static void removeServerInstance() { serverInstance = null; } - @OnlyIn(Dist.CLIENT) - public static void initializeClient() { - clientInstance = new CalioDynamicRegistryManager(); - } - - @OnlyIn(Dist.CLIENT) - public static void setClientInstance(CalioDynamicRegistryManager clientInstance) { - CalioDynamicRegistryManager.clientInstance = clientInstance; + public static void removeClientInstance() { + clientInstance = null; } public static CalioDynamicRegistryManager decode(FriendlyByteBuf buffer) {