From 5f753bafda106df5ff3b7430f2fb571efcd457bc Mon Sep 17 00:00:00 2001 From: FalsePattern <30945458+FalsePattern@users.noreply.github.com> Date: Tue, 30 Aug 2022 20:16:19 +0200 Subject: [PATCH] externalize event handling from config system to avoid classloading issues --- .../impl/config/ConfigurationManagerImpl.java | 62 +-------------- .../config/event/ClientEventHandlerPost.java | 50 ++++++++++++ .../config/event/ClientEventHandlerPre.java | 79 +++++++++++++++++++ .../CommonEventHandlerPost.java} | 23 ++---- .../config/event/CommonEventHandlerPre.java | 53 +++++++++++++ .../lib/internal/proxy/ClientProxy.java | 4 + .../lib/internal/proxy/CommonProxy.java | 8 +- 7 files changed, 199 insertions(+), 80 deletions(-) create mode 100644 src/main/java/com/falsepattern/lib/internal/impl/config/event/ClientEventHandlerPost.java create mode 100644 src/main/java/com/falsepattern/lib/internal/impl/config/event/ClientEventHandlerPre.java rename src/main/java/com/falsepattern/lib/internal/impl/config/{ConfigSyncEventHandler.java => event/CommonEventHandlerPost.java} (77%) create mode 100644 src/main/java/com/falsepattern/lib/internal/impl/config/event/CommonEventHandlerPre.java diff --git a/src/main/java/com/falsepattern/lib/internal/impl/config/ConfigurationManagerImpl.java b/src/main/java/com/falsepattern/lib/internal/impl/config/ConfigurationManagerImpl.java index 24dec8e..c6d6a37 100644 --- a/src/main/java/com/falsepattern/lib/internal/impl/config/ConfigurationManagerImpl.java +++ b/src/main/java/com/falsepattern/lib/internal/impl/config/ConfigurationManagerImpl.java @@ -20,19 +20,12 @@ */ package com.falsepattern.lib.internal.impl.config; -import com.falsepattern.lib.config.Config; import com.falsepattern.lib.config.ConfigException; import com.falsepattern.lib.config.event.AllConfigSyncEvent; import com.falsepattern.lib.config.event.ConfigSyncEvent; -import com.falsepattern.lib.config.event.ConfigValidationFailureEvent; import com.falsepattern.lib.internal.FalsePatternLib; import com.falsepattern.lib.internal.Share; -import com.falsepattern.lib.internal.config.LibraryConfig; import com.falsepattern.lib.internal.impl.config.net.SyncRequest; -import com.falsepattern.lib.text.FormattedText; -import com.falsepattern.lib.toasts.GuiToast; -import com.falsepattern.lib.toasts.SimpleToast; -import com.falsepattern.lib.toasts.icon.ToastBG; import com.falsepattern.lib.util.FileUtil; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; @@ -40,15 +33,9 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; import lombok.val; -import net.minecraft.util.EnumChatFormatting; -import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.config.Configuration; import cpw.mods.fml.client.config.IConfigElement; import cpw.mods.fml.client.event.ConfigChangedEvent; -import cpw.mods.fml.common.FMLCommonHandler; -import cpw.mods.fml.common.eventhandler.SubscribeEvent; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -74,12 +61,11 @@ import java.util.function.BiConsumer; * Do not read if you value your sanity. */ @NoArgsConstructor(access = AccessLevel.PRIVATE) -public class ConfigurationManagerImpl { +public final class ConfigurationManagerImpl { private static final Map configs = new HashMap<>(); private static final Map>> configToClassMap = new HashMap<>(); private static final Map, ParsedConfiguration> parsedConfigMap = new HashMap<>(); private static final BiMap> serializedNames = HashBiMap.create(); - private static final ConfigurationManagerImpl instance = new ConfigurationManagerImpl(); private static boolean initialized = false; private static Path configDir; @@ -256,19 +242,13 @@ public class ConfigurationManagerImpl { initialized = true; } - public static void registerBus() { - FMLCommonHandler.instance().bus().register(instance); - MinecraftForge.EVENT_BUS.register(instance); - } - - static void sendSyncRequest() throws IOException { + public static void sendSyncRequest() throws IOException { val event = new SyncRequest(); event.transmit(); FalsePatternLib.NETWORK.sendToServer(event); } - @SubscribeEvent - public void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent event) { + public static void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent event) { init(); val rawConfig = configs.get(event.modID); if (rawConfig == null) { @@ -280,40 +260,4 @@ public class ConfigurationManagerImpl { config.configChanged(); } } - - @SubscribeEvent - public void onValidationErrorLog(ConfigValidationFailureEvent e) { - if (LibraryConfig.CONFIG_ERROR_LOUDNESS != LibraryConfig.ValidationLogging.None) { - e.logWarn(); - } - } - - @SubscribeEvent - @SideOnly(Side.CLIENT) - public void onValidationErrorToast(ConfigValidationFailureEvent e) { - if (LibraryConfig.CONFIG_ERROR_LOUDNESS == LibraryConfig.ValidationLogging.LogAndToast) { - e.toast(); - } - } - - @SubscribeEvent - @SideOnly(Side.CLIENT) - public void onConfigSyncFinished(ConfigSyncEvent.End e) { - val cfg = e.configClass.getAnnotation(Config.class); - if (e.successful) { - GuiToast.add(new SimpleToast(ToastBG.TOAST_DARK, null, - FormattedText.parse(EnumChatFormatting.GREEN + "Synced config") - .toChatText() - .get(0), - FormattedText.parse(cfg.modid() + ":" + cfg.category()).toChatText().get(0), - false, 5000)); - } else { - GuiToast.add(new SimpleToast(ToastBG.TOAST_DARK, null, - FormattedText.parse(EnumChatFormatting.RED + "Failed to sync config") - .toChatText() - .get(0), - FormattedText.parse(cfg.modid() + ":" + cfg.category()).toChatText().get(0), - false, 5000)); - } - } } diff --git a/src/main/java/com/falsepattern/lib/internal/impl/config/event/ClientEventHandlerPost.java b/src/main/java/com/falsepattern/lib/internal/impl/config/event/ClientEventHandlerPost.java new file mode 100644 index 0000000..33c388f --- /dev/null +++ b/src/main/java/com/falsepattern/lib/internal/impl/config/event/ClientEventHandlerPost.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2022 FalsePattern + * All Rights Reserved + * + * The above copyright notice, this permission notice and the word "SNEED" + * shall be included in all copies or substantial portions of the Software. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.falsepattern.lib.internal.impl.config.event; + +import com.falsepattern.lib.config.event.ConfigSyncRequestEvent; +import com.falsepattern.lib.internal.impl.config.ConfigurationManagerImpl; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.SneakyThrows; + +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@SideOnly(Side.CLIENT) +public final class ClientEventHandlerPost { + private static final ClientEventHandlerPost INSTANCE = new ClientEventHandlerPost(); + + public static void registerBus() { + FMLCommonHandler.instance().bus().register(INSTANCE); + } + + @SideOnly(Side.CLIENT) + @SneakyThrows + @SubscribeEvent + public void onSyncRequestClient(ConfigSyncRequestEvent.Client e) { + ConfigurationManagerImpl.sendSyncRequest(); + } +} diff --git a/src/main/java/com/falsepattern/lib/internal/impl/config/event/ClientEventHandlerPre.java b/src/main/java/com/falsepattern/lib/internal/impl/config/event/ClientEventHandlerPre.java new file mode 100644 index 0000000..6427303 --- /dev/null +++ b/src/main/java/com/falsepattern/lib/internal/impl/config/event/ClientEventHandlerPre.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2022 FalsePattern + * All Rights Reserved + * + * The above copyright notice, this permission notice and the word "SNEED" + * shall be included in all copies or substantial portions of the Software. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.falsepattern.lib.internal.impl.config.event; + +import com.falsepattern.lib.config.Config; +import com.falsepattern.lib.config.event.ConfigSyncEvent; +import com.falsepattern.lib.config.event.ConfigValidationFailureEvent; +import com.falsepattern.lib.internal.config.LibraryConfig; +import com.falsepattern.lib.text.FormattedText; +import com.falsepattern.lib.toasts.GuiToast; +import com.falsepattern.lib.toasts.SimpleToast; +import com.falsepattern.lib.toasts.icon.ToastBG; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.val; + +import net.minecraft.util.EnumChatFormatting; +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@SideOnly(Side.CLIENT) +public class ClientEventHandlerPre { + private static final ClientEventHandlerPre INSTANCE = new ClientEventHandlerPre(); + + public static void registerBus() { + FMLCommonHandler.instance().bus().register(INSTANCE); + } + + @SubscribeEvent + @SideOnly(Side.CLIENT) + public void onValidationErrorToast(ConfigValidationFailureEvent e) { + if (LibraryConfig.CONFIG_ERROR_LOUDNESS == LibraryConfig.ValidationLogging.LogAndToast) { + e.toast(); + } + } + + @SubscribeEvent + @SideOnly(Side.CLIENT) + public void onConfigSyncFinished(ConfigSyncEvent.End e) { + val cfg = e.configClass.getAnnotation(Config.class); + if (e.successful) { + GuiToast.add(new SimpleToast(ToastBG.TOAST_DARK, null, + FormattedText.parse(EnumChatFormatting.GREEN + "Synced config") + .toChatText() + .get(0), + FormattedText.parse(cfg.modid() + ":" + cfg.category()).toChatText().get(0), + false, 5000)); + } else { + GuiToast.add(new SimpleToast(ToastBG.TOAST_DARK, null, + FormattedText.parse(EnumChatFormatting.RED + "Failed to sync config") + .toChatText() + .get(0), + FormattedText.parse(cfg.modid() + ":" + cfg.category()).toChatText().get(0), + false, 5000)); + } + } +} diff --git a/src/main/java/com/falsepattern/lib/internal/impl/config/ConfigSyncEventHandler.java b/src/main/java/com/falsepattern/lib/internal/impl/config/event/CommonEventHandlerPost.java similarity index 77% rename from src/main/java/com/falsepattern/lib/internal/impl/config/ConfigSyncEventHandler.java rename to src/main/java/com/falsepattern/lib/internal/impl/config/event/CommonEventHandlerPost.java index e88a0e3..ff11223 100644 --- a/src/main/java/com/falsepattern/lib/internal/impl/config/ConfigSyncEventHandler.java +++ b/src/main/java/com/falsepattern/lib/internal/impl/config/event/CommonEventHandlerPost.java @@ -19,10 +19,11 @@ * along with this program. If not, see . */ -package com.falsepattern.lib.internal.impl.config; +package com.falsepattern.lib.internal.impl.config.event; import com.falsepattern.lib.config.event.ConfigSyncRequestEvent; import com.falsepattern.lib.internal.FalsePatternLib; +import com.falsepattern.lib.internal.impl.config.ConfigurationManagerImpl; import com.falsepattern.lib.internal.impl.config.net.SyncPrompt; import lombok.AccessLevel; import lombok.NoArgsConstructor; @@ -35,19 +36,14 @@ import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.entity.EntityJoinWorldEvent; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.eventhandler.SubscribeEvent; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; @NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class ConfigSyncEventHandler { - private static ConfigSyncEventHandler instance; +public class CommonEventHandlerPost { + private static final CommonEventHandlerPost INSTANCE = new CommonEventHandlerPost(); public static void registerBus() { - if (instance == null) { - instance = new ConfigSyncEventHandler(); - MinecraftForge.EVENT_BUS.register(instance); - FMLCommonHandler.instance().bus().register(instance); - } + MinecraftForge.EVENT_BUS.register(INSTANCE); + FMLCommonHandler.instance().bus().register(INSTANCE); } @SneakyThrows @@ -58,13 +54,6 @@ public final class ConfigSyncEventHandler { } } - @SideOnly(Side.CLIENT) - @SneakyThrows - @SubscribeEvent - public void onSyncRequestClient(ConfigSyncRequestEvent.Client e) { - ConfigurationManagerImpl.sendSyncRequest(); - } - @SubscribeEvent public void onSyncRequestServer(ConfigSyncRequestEvent.Server e) { val players = e.getPlayers(); diff --git a/src/main/java/com/falsepattern/lib/internal/impl/config/event/CommonEventHandlerPre.java b/src/main/java/com/falsepattern/lib/internal/impl/config/event/CommonEventHandlerPre.java new file mode 100644 index 0000000..6425396 --- /dev/null +++ b/src/main/java/com/falsepattern/lib/internal/impl/config/event/CommonEventHandlerPre.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2022 FalsePattern + * All Rights Reserved + * + * The above copyright notice, this permission notice and the word "SNEED" + * shall be included in all copies or substantial portions of the Software. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.falsepattern.lib.internal.impl.config.event; + +import com.falsepattern.lib.config.event.ConfigValidationFailureEvent; +import com.falsepattern.lib.internal.config.LibraryConfig; +import com.falsepattern.lib.internal.impl.config.ConfigurationManagerImpl; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +import cpw.mods.fml.client.event.ConfigChangedEvent; +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class CommonEventHandlerPre { + private static final CommonEventHandlerPre INSTANCE = new CommonEventHandlerPre(); + + public static void registerBus() { + FMLCommonHandler.instance().bus().register(INSTANCE); + } + + @SubscribeEvent + public void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent event) { + ConfigurationManagerImpl.onConfigChanged(event); + } + + @SubscribeEvent + public void onValidationErrorLog(ConfigValidationFailureEvent e) { + if (LibraryConfig.CONFIG_ERROR_LOUDNESS != LibraryConfig.ValidationLogging.None) { + e.logWarn(); + } + } +} diff --git a/src/main/java/com/falsepattern/lib/internal/proxy/ClientProxy.java b/src/main/java/com/falsepattern/lib/internal/proxy/ClientProxy.java index ccb8b75..07fc05c 100644 --- a/src/main/java/com/falsepattern/lib/internal/proxy/ClientProxy.java +++ b/src/main/java/com/falsepattern/lib/internal/proxy/ClientProxy.java @@ -22,6 +22,8 @@ package com.falsepattern.lib.internal.proxy; import com.falsepattern.lib.internal.Share; import com.falsepattern.lib.internal.Tags; +import com.falsepattern.lib.internal.impl.config.event.ClientEventHandlerPost; +import com.falsepattern.lib.internal.impl.config.event.ClientEventHandlerPre; import com.falsepattern.lib.internal.impl.toast.GuiToastImpl; import com.falsepattern.lib.updates.UpdateChecker; import lombok.val; @@ -55,12 +57,14 @@ public class ClientProxy extends CommonProxy { @Override public void preInit(FMLPreInitializationEvent e) { super.preInit(e); + ClientEventHandlerPre.registerBus(); MinecraftForge.EVENT_BUS.register(this); } @Override public void postInit(FMLPostInitializationEvent e) { super.postInit(e); + ClientEventHandlerPost.registerBus(); chatFuture = updatesFuture.handleAsync((updates, exception) -> { if (exception != null || updates.isEmpty()) { return Collections.emptyList(); diff --git a/src/main/java/com/falsepattern/lib/internal/proxy/CommonProxy.java b/src/main/java/com/falsepattern/lib/internal/proxy/CommonProxy.java index 3e79af6..53e4d6c 100644 --- a/src/main/java/com/falsepattern/lib/internal/proxy/CommonProxy.java +++ b/src/main/java/com/falsepattern/lib/internal/proxy/CommonProxy.java @@ -23,8 +23,8 @@ package com.falsepattern.lib.internal.proxy; import com.falsepattern.lib.internal.FalsePatternLib; import com.falsepattern.lib.internal.Share; import com.falsepattern.lib.internal.config.LibraryConfig; -import com.falsepattern.lib.internal.impl.config.ConfigSyncEventHandler; -import com.falsepattern.lib.internal.impl.config.ConfigurationManagerImpl; +import com.falsepattern.lib.internal.impl.config.event.CommonEventHandlerPost; +import com.falsepattern.lib.internal.impl.config.event.CommonEventHandlerPre; import com.falsepattern.lib.internal.impl.config.net.SyncPrompt; import com.falsepattern.lib.internal.impl.config.net.SyncPromptHandler; import com.falsepattern.lib.internal.impl.config.net.SyncReply; @@ -55,7 +55,7 @@ public class CommonProxy { } public void preInit(FMLPreInitializationEvent e) { - ConfigurationManagerImpl.registerBus(); + CommonEventHandlerPre.registerBus(); if (LibraryConfig.ENABLE_UPDATE_CHECKER) { Share.LOG.info("Launching asynchronous update check."); updatesFuture = UpdateChecker.fetchUpdatesAsync(FalsePatternLib.UPDATE_URL).thenApplyAsync(updates -> { @@ -81,7 +81,7 @@ public class CommonProxy { } public void postInit(FMLPostInitializationEvent e) { - ConfigSyncEventHandler.registerBus(); + CommonEventHandlerPost.registerBus(); }