From 323a1e58c21a1c90024a8ddb2a3defd137e258af Mon Sep 17 00:00:00 2001 From: Cedric Date: Thu, 10 Oct 2024 17:30:26 +0200 Subject: [PATCH] ref: better config system --- .../signalbox/config/SignalConfig.java | 41 ++++++++++++++----- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/troblecodings/signals/signalbox/config/SignalConfig.java b/src/main/java/com/troblecodings/signals/signalbox/config/SignalConfig.java index adf621447..362bf8e0b 100644 --- a/src/main/java/com/troblecodings/signals/signalbox/config/SignalConfig.java +++ b/src/main/java/com/troblecodings/signals/signalbox/config/SignalConfig.java @@ -11,6 +11,9 @@ import com.troblecodings.signals.contentpacks.ChangeConfigParser; import com.troblecodings.signals.contentpacks.OneSignalNonPredicateConfigParser; import com.troblecodings.signals.contentpacks.OneSignalPredicateConfigParser; +import com.troblecodings.signals.core.LoadHolder; +import com.troblecodings.signals.core.SignalStateListener; +import com.troblecodings.signals.core.SignalStateLoadHoler; import com.troblecodings.signals.enums.PathType; import com.troblecodings.signals.handler.SignalStateHandler; import com.troblecodings.signals.handler.SignalStateInfo; @@ -18,6 +21,9 @@ public final class SignalConfig { + private static final LoadHolder> LOAD_HOLDER = new LoadHolder<>( + SignalConfig.class); + private SignalConfig() { } @@ -58,7 +64,7 @@ public static void reset(final ResetInfo info) { .get(info.current.signal); if (resetValues == null) return; - SignalStateHandler.runTaskWhenSignalLoaded(info.current, (stateInfo, oldProperties, _u) -> { + loadSignalAndRunTask(info.current, (stateInfo, oldProperties, _u) -> { final Map, Object> object = new HashMap<>(); object.put(Boolean.class, info.isRepeater); object.put(Map.class, oldProperties); @@ -73,6 +79,7 @@ public static void reset(final ResetInfo info) { }); if (!propertiesToSet.isEmpty()) SignalStateHandler.setStates(info.current, propertiesToSet); + unloadSignal(stateInfo); }); } @@ -85,16 +92,17 @@ public static void loadDisable(final ConfigInfo info) { } private static void changeIfPresent(final List values, final ConfigInfo info) { - SignalStateHandler.runTaskWhenSignalLoaded(info.currentinfo, - (stateInfo, oldProperties, _u) -> { - if (info.nextinfo != null) { - SignalStateHandler.runTaskWhenSignalLoaded(info.nextinfo, - (nextInfo, nextProperties, _u2) -> changeSignals(values, info, - oldProperties, nextProperties)); - } else { - changeSignals(values, info, oldProperties, null); - } + loadSignalAndRunTask(info.currentinfo, (stateInfo, oldProperties, _u) -> { + if (info.nextinfo != null) { + loadSignalAndRunTask(info.nextinfo, (nextInfo, nextProperties, _u2) -> { + changeSignals(values, info, oldProperties, nextProperties); + unloadSignal(nextInfo); }); + } else { + changeSignals(values, info, oldProperties, null); + } + unloadSignal(stateInfo); + }); } @@ -121,7 +129,7 @@ private static void changeSignals(final List values, final Confi private static void loadWithoutPredicate(final List values, final SignalStateInfo current) { if (values != null) { - SignalStateHandler.runTaskWhenSignalLoaded(current, (info, oldProperties, _u) -> { + loadSignalAndRunTask(current, (info, oldProperties, _u) -> { final Map propertiesToSet = new HashMap<>(); values.forEach(property -> { propertiesToSet.putAll(property.state.entrySet().stream() @@ -130,7 +138,18 @@ private static void loadWithoutPredicate(final List values, }); if (!propertiesToSet.isEmpty()) SignalStateHandler.setStates(current, propertiesToSet); + unloadSignal(info); }); } } + + private static void loadSignalAndRunTask(final SignalStateInfo info, + final SignalStateListener task) { + SignalStateHandler.loadSignal(new SignalStateLoadHoler(info, LOAD_HOLDER)); + SignalStateHandler.runTaskWhenSignalLoaded(info, task); + } + + private static void unloadSignal(final SignalStateInfo info) { + SignalStateHandler.unloadSignal(new SignalStateLoadHoler(info, LOAD_HOLDER)); + } }