Skip to content

Commit 5f753ba

Browse files
committed
externalize event handling from config system to avoid classloading issues
1 parent 07a88af commit 5f753ba

File tree

7 files changed

+199
-80
lines changed

7 files changed

+199
-80
lines changed

src/main/java/com/falsepattern/lib/internal/impl/config/ConfigurationManagerImpl.java

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,22 @@
2020
*/
2121
package com.falsepattern.lib.internal.impl.config;
2222

23-
import com.falsepattern.lib.config.Config;
2423
import com.falsepattern.lib.config.ConfigException;
2524
import com.falsepattern.lib.config.event.AllConfigSyncEvent;
2625
import com.falsepattern.lib.config.event.ConfigSyncEvent;
27-
import com.falsepattern.lib.config.event.ConfigValidationFailureEvent;
2826
import com.falsepattern.lib.internal.FalsePatternLib;
2927
import com.falsepattern.lib.internal.Share;
30-
import com.falsepattern.lib.internal.config.LibraryConfig;
3128
import com.falsepattern.lib.internal.impl.config.net.SyncRequest;
32-
import com.falsepattern.lib.text.FormattedText;
33-
import com.falsepattern.lib.toasts.GuiToast;
34-
import com.falsepattern.lib.toasts.SimpleToast;
35-
import com.falsepattern.lib.toasts.icon.ToastBG;
3629
import com.falsepattern.lib.util.FileUtil;
3730
import com.google.common.collect.BiMap;
3831
import com.google.common.collect.HashBiMap;
3932
import lombok.AccessLevel;
4033
import lombok.NoArgsConstructor;
4134
import lombok.val;
4235

43-
import net.minecraft.util.EnumChatFormatting;
44-
import net.minecraftforge.common.MinecraftForge;
4536
import net.minecraftforge.common.config.Configuration;
4637
import cpw.mods.fml.client.config.IConfigElement;
4738
import cpw.mods.fml.client.event.ConfigChangedEvent;
48-
import cpw.mods.fml.common.FMLCommonHandler;
49-
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
50-
import cpw.mods.fml.relauncher.Side;
51-
import cpw.mods.fml.relauncher.SideOnly;
5239

5340
import java.io.ByteArrayInputStream;
5441
import java.io.ByteArrayOutputStream;
@@ -74,12 +61,11 @@
7461
* Do not read if you value your sanity.
7562
*/
7663
@NoArgsConstructor(access = AccessLevel.PRIVATE)
77-
public class ConfigurationManagerImpl {
64+
public final class ConfigurationManagerImpl {
7865
private static final Map<String, Configuration> configs = new HashMap<>();
7966
private static final Map<Configuration, Set<Class<?>>> configToClassMap = new HashMap<>();
8067
private static final Map<Class<?>, ParsedConfiguration> parsedConfigMap = new HashMap<>();
8168
private static final BiMap<String, Class<?>> serializedNames = HashBiMap.create();
82-
private static final ConfigurationManagerImpl instance = new ConfigurationManagerImpl();
8369
private static boolean initialized = false;
8470
private static Path configDir;
8571

@@ -256,19 +242,13 @@ private static void init() {
256242
initialized = true;
257243
}
258244

259-
public static void registerBus() {
260-
FMLCommonHandler.instance().bus().register(instance);
261-
MinecraftForge.EVENT_BUS.register(instance);
262-
}
263-
264-
static void sendSyncRequest() throws IOException {
245+
public static void sendSyncRequest() throws IOException {
265246
val event = new SyncRequest();
266247
event.transmit();
267248
FalsePatternLib.NETWORK.sendToServer(event);
268249
}
269250

270-
@SubscribeEvent
271-
public void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent event) {
251+
public static void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent event) {
272252
init();
273253
val rawConfig = configs.get(event.modID);
274254
if (rawConfig == null) {
@@ -280,40 +260,4 @@ public void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent event) {
280260
config.configChanged();
281261
}
282262
}
283-
284-
@SubscribeEvent
285-
public void onValidationErrorLog(ConfigValidationFailureEvent e) {
286-
if (LibraryConfig.CONFIG_ERROR_LOUDNESS != LibraryConfig.ValidationLogging.None) {
287-
e.logWarn();
288-
}
289-
}
290-
291-
@SubscribeEvent
292-
@SideOnly(Side.CLIENT)
293-
public void onValidationErrorToast(ConfigValidationFailureEvent e) {
294-
if (LibraryConfig.CONFIG_ERROR_LOUDNESS == LibraryConfig.ValidationLogging.LogAndToast) {
295-
e.toast();
296-
}
297-
}
298-
299-
@SubscribeEvent
300-
@SideOnly(Side.CLIENT)
301-
public void onConfigSyncFinished(ConfigSyncEvent.End e) {
302-
val cfg = e.configClass.getAnnotation(Config.class);
303-
if (e.successful) {
304-
GuiToast.add(new SimpleToast(ToastBG.TOAST_DARK, null,
305-
FormattedText.parse(EnumChatFormatting.GREEN + "Synced config")
306-
.toChatText()
307-
.get(0),
308-
FormattedText.parse(cfg.modid() + ":" + cfg.category()).toChatText().get(0),
309-
false, 5000));
310-
} else {
311-
GuiToast.add(new SimpleToast(ToastBG.TOAST_DARK, null,
312-
FormattedText.parse(EnumChatFormatting.RED + "Failed to sync config")
313-
.toChatText()
314-
.get(0),
315-
FormattedText.parse(cfg.modid() + ":" + cfg.category()).toChatText().get(0),
316-
false, 5000));
317-
}
318-
}
319263
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (C) 2022 FalsePattern
3+
* All Rights Reserved
4+
*
5+
* The above copyright notice, this permission notice and the word "SNEED"
6+
* shall be included in all copies or substantial portions of the Software.
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Lesser General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*/
21+
22+
package com.falsepattern.lib.internal.impl.config.event;
23+
24+
import com.falsepattern.lib.config.event.ConfigSyncRequestEvent;
25+
import com.falsepattern.lib.internal.impl.config.ConfigurationManagerImpl;
26+
import lombok.AccessLevel;
27+
import lombok.NoArgsConstructor;
28+
import lombok.SneakyThrows;
29+
30+
import cpw.mods.fml.common.FMLCommonHandler;
31+
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
32+
import cpw.mods.fml.relauncher.Side;
33+
import cpw.mods.fml.relauncher.SideOnly;
34+
35+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
36+
@SideOnly(Side.CLIENT)
37+
public final class ClientEventHandlerPost {
38+
private static final ClientEventHandlerPost INSTANCE = new ClientEventHandlerPost();
39+
40+
public static void registerBus() {
41+
FMLCommonHandler.instance().bus().register(INSTANCE);
42+
}
43+
44+
@SideOnly(Side.CLIENT)
45+
@SneakyThrows
46+
@SubscribeEvent
47+
public void onSyncRequestClient(ConfigSyncRequestEvent.Client e) {
48+
ConfigurationManagerImpl.sendSyncRequest();
49+
}
50+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright (C) 2022 FalsePattern
3+
* All Rights Reserved
4+
*
5+
* The above copyright notice, this permission notice and the word "SNEED"
6+
* shall be included in all copies or substantial portions of the Software.
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Lesser General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*/
21+
22+
package com.falsepattern.lib.internal.impl.config.event;
23+
24+
import com.falsepattern.lib.config.Config;
25+
import com.falsepattern.lib.config.event.ConfigSyncEvent;
26+
import com.falsepattern.lib.config.event.ConfigValidationFailureEvent;
27+
import com.falsepattern.lib.internal.config.LibraryConfig;
28+
import com.falsepattern.lib.text.FormattedText;
29+
import com.falsepattern.lib.toasts.GuiToast;
30+
import com.falsepattern.lib.toasts.SimpleToast;
31+
import com.falsepattern.lib.toasts.icon.ToastBG;
32+
import lombok.AccessLevel;
33+
import lombok.NoArgsConstructor;
34+
import lombok.val;
35+
36+
import net.minecraft.util.EnumChatFormatting;
37+
import cpw.mods.fml.common.FMLCommonHandler;
38+
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
39+
import cpw.mods.fml.relauncher.Side;
40+
import cpw.mods.fml.relauncher.SideOnly;
41+
42+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
43+
@SideOnly(Side.CLIENT)
44+
public class ClientEventHandlerPre {
45+
private static final ClientEventHandlerPre INSTANCE = new ClientEventHandlerPre();
46+
47+
public static void registerBus() {
48+
FMLCommonHandler.instance().bus().register(INSTANCE);
49+
}
50+
51+
@SubscribeEvent
52+
@SideOnly(Side.CLIENT)
53+
public void onValidationErrorToast(ConfigValidationFailureEvent e) {
54+
if (LibraryConfig.CONFIG_ERROR_LOUDNESS == LibraryConfig.ValidationLogging.LogAndToast) {
55+
e.toast();
56+
}
57+
}
58+
59+
@SubscribeEvent
60+
@SideOnly(Side.CLIENT)
61+
public void onConfigSyncFinished(ConfigSyncEvent.End e) {
62+
val cfg = e.configClass.getAnnotation(Config.class);
63+
if (e.successful) {
64+
GuiToast.add(new SimpleToast(ToastBG.TOAST_DARK, null,
65+
FormattedText.parse(EnumChatFormatting.GREEN + "Synced config")
66+
.toChatText()
67+
.get(0),
68+
FormattedText.parse(cfg.modid() + ":" + cfg.category()).toChatText().get(0),
69+
false, 5000));
70+
} else {
71+
GuiToast.add(new SimpleToast(ToastBG.TOAST_DARK, null,
72+
FormattedText.parse(EnumChatFormatting.RED + "Failed to sync config")
73+
.toChatText()
74+
.get(0),
75+
FormattedText.parse(cfg.modid() + ":" + cfg.category()).toChatText().get(0),
76+
false, 5000));
77+
}
78+
}
79+
}

src/main/java/com/falsepattern/lib/internal/impl/config/ConfigSyncEventHandler.java renamed to src/main/java/com/falsepattern/lib/internal/impl/config/event/CommonEventHandlerPost.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2020
*/
2121

22-
package com.falsepattern.lib.internal.impl.config;
22+
package com.falsepattern.lib.internal.impl.config.event;
2323

2424
import com.falsepattern.lib.config.event.ConfigSyncRequestEvent;
2525
import com.falsepattern.lib.internal.FalsePatternLib;
26+
import com.falsepattern.lib.internal.impl.config.ConfigurationManagerImpl;
2627
import com.falsepattern.lib.internal.impl.config.net.SyncPrompt;
2728
import lombok.AccessLevel;
2829
import lombok.NoArgsConstructor;
@@ -35,19 +36,14 @@
3536
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
3637
import cpw.mods.fml.common.FMLCommonHandler;
3738
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
38-
import cpw.mods.fml.relauncher.Side;
39-
import cpw.mods.fml.relauncher.SideOnly;
4039

4140
@NoArgsConstructor(access = AccessLevel.PRIVATE)
42-
public final class ConfigSyncEventHandler {
43-
private static ConfigSyncEventHandler instance;
41+
public class CommonEventHandlerPost {
42+
private static final CommonEventHandlerPost INSTANCE = new CommonEventHandlerPost();
4443

4544
public static void registerBus() {
46-
if (instance == null) {
47-
instance = new ConfigSyncEventHandler();
48-
MinecraftForge.EVENT_BUS.register(instance);
49-
FMLCommonHandler.instance().bus().register(instance);
50-
}
45+
MinecraftForge.EVENT_BUS.register(INSTANCE);
46+
FMLCommonHandler.instance().bus().register(INSTANCE);
5147
}
5248

5349
@SneakyThrows
@@ -58,13 +54,6 @@ public void onJoinWorld(EntityJoinWorldEvent e) {
5854
}
5955
}
6056

61-
@SideOnly(Side.CLIENT)
62-
@SneakyThrows
63-
@SubscribeEvent
64-
public void onSyncRequestClient(ConfigSyncRequestEvent.Client e) {
65-
ConfigurationManagerImpl.sendSyncRequest();
66-
}
67-
6857
@SubscribeEvent
6958
public void onSyncRequestServer(ConfigSyncRequestEvent.Server e) {
7059
val players = e.getPlayers();
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (C) 2022 FalsePattern
3+
* All Rights Reserved
4+
*
5+
* The above copyright notice, this permission notice and the word "SNEED"
6+
* shall be included in all copies or substantial portions of the Software.
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Lesser General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*/
21+
22+
package com.falsepattern.lib.internal.impl.config.event;
23+
24+
import com.falsepattern.lib.config.event.ConfigValidationFailureEvent;
25+
import com.falsepattern.lib.internal.config.LibraryConfig;
26+
import com.falsepattern.lib.internal.impl.config.ConfigurationManagerImpl;
27+
import lombok.AccessLevel;
28+
import lombok.NoArgsConstructor;
29+
30+
import cpw.mods.fml.client.event.ConfigChangedEvent;
31+
import cpw.mods.fml.common.FMLCommonHandler;
32+
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
33+
34+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
35+
public class CommonEventHandlerPre {
36+
private static final CommonEventHandlerPre INSTANCE = new CommonEventHandlerPre();
37+
38+
public static void registerBus() {
39+
FMLCommonHandler.instance().bus().register(INSTANCE);
40+
}
41+
42+
@SubscribeEvent
43+
public void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent event) {
44+
ConfigurationManagerImpl.onConfigChanged(event);
45+
}
46+
47+
@SubscribeEvent
48+
public void onValidationErrorLog(ConfigValidationFailureEvent e) {
49+
if (LibraryConfig.CONFIG_ERROR_LOUDNESS != LibraryConfig.ValidationLogging.None) {
50+
e.logWarn();
51+
}
52+
}
53+
}

src/main/java/com/falsepattern/lib/internal/proxy/ClientProxy.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import com.falsepattern.lib.internal.Share;
2424
import com.falsepattern.lib.internal.Tags;
25+
import com.falsepattern.lib.internal.impl.config.event.ClientEventHandlerPost;
26+
import com.falsepattern.lib.internal.impl.config.event.ClientEventHandlerPre;
2527
import com.falsepattern.lib.internal.impl.toast.GuiToastImpl;
2628
import com.falsepattern.lib.updates.UpdateChecker;
2729
import lombok.val;
@@ -55,12 +57,14 @@ public void construct(FMLConstructionEvent e) {
5557
@Override
5658
public void preInit(FMLPreInitializationEvent e) {
5759
super.preInit(e);
60+
ClientEventHandlerPre.registerBus();
5861
MinecraftForge.EVENT_BUS.register(this);
5962
}
6063

6164
@Override
6265
public void postInit(FMLPostInitializationEvent e) {
6366
super.postInit(e);
67+
ClientEventHandlerPost.registerBus();
6468
chatFuture = updatesFuture.handleAsync((updates, exception) -> {
6569
if (exception != null || updates.isEmpty()) {
6670
return Collections.emptyList();

0 commit comments

Comments
 (0)