externalize event handling from config system to avoid classloading issues

This commit is contained in:
FalsePattern committed 2022-08-30 20:16:19 +02:00
1 parent 07a88af459
commit 5f753bafda
7 files changed
+199 -80

No files matched your search

@@ -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<String, Configuration> configs = new HashMap<>();
private static final Map<Configuration, Set<Class<?>>> configToClassMap = new HashMap<>();
private static final Map<Class<?>, ParsedConfiguration> parsedConfigMap = new HashMap<>();
private static final BiMap<String, Class<?>> 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));
}
}
}
@@ -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 <https://www.gnu.org/licenses/>.
*/
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();
}
}
@@ -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 <https://www.gnu.org/licenses/>.
*/
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));
}
}
}
@@ -19,10 +19,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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();
@@ -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 <https://www.gnu.org/licenses/>.
*/
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();
}
}
}
@@ -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();
@@ -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();
}