diff --git a/src/main/java/carpet/api/settings/CarpetRule.java b/src/main/java/carpet/api/settings/CarpetRule.java index 98fec623..70edf7ab 100644 --- a/src/main/java/carpet/api/settings/CarpetRule.java +++ b/src/main/java/carpet/api/settings/CarpetRule.java @@ -100,15 +100,14 @@ public interface CarpetRule { /** *

Sets this rule's value to the provided {@link String}, after first converting the {@link String} into a suitable type.

* - *

This methods run any required validation on the value first, and throws {@link InvalidRuleValueException} if the value is not suitable - * for this rule, regardless of whether it was impossible to convert the value to the required type, the rule doesn't accept the - * value, or the rule is immutable.

+ *

This methods run any required validation on the value first, and throws {@link InvalidRuleValueException} if the value is not suitable for + * this rule, regardless of whether it was impossible to convert the value to the required type, the rule doesn't accept the value, or the rule is + * immutable.

* *

Implementations of this method must notify their {@link SettingsManager} by calling - * {@link SettingsManager#notifyRuleChanged(CommandSourceStack, CarpetRule, String)}, who is responsible for - * notifying the {@link ServerNetworkHandler} (if the rule isn't restricted from being synchronized with clients) - * and the {@link CarpetEventServer.Event#CARPET_RULE_CHANGES#onCarpetRuleChanges(CarpetRule, CommandSourceStack)} Scarpet event - * in case the value of the rule was changed because of the invocation.

+ * {@link SettingsManager#notifyRuleChanged(CommandSourceStack, CarpetRule, String)}, who is responsible for notifying the + * {@link ServerNetworkHandler} (if the rule isn't restricted from being synchronized with clients) and other rule observers like the Scarpet + * event, in case the value of the rule was changed because of the invocation.

* *

This method must not throw any exception other than the documented {@link InvalidRuleValueException}.

* diff --git a/src/main/java/carpet/api/settings/SettingsManager.java b/src/main/java/carpet/api/settings/SettingsManager.java index 8dec18d6..b8a0430f 100644 --- a/src/main/java/carpet/api/settings/SettingsManager.java +++ b/src/main/java/carpet/api/settings/SettingsManager.java @@ -25,8 +25,6 @@ import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; -import carpet.script.CarpetEventServer; -import carpet.script.value.StringValue; import com.google.common.collect.Sets; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.StringArgumentType; @@ -259,39 +257,12 @@ public class SettingsManager { rules.put(rule.name(), rule); } - public static final CarpetEventServer.Event CARPET_RULE_CHANGES = new CarpetEventServer.Event("carpet_rule_changes", 2, true) - { - @Override - public void handleAny(final Object ... args) - { - final CarpetRule rule = (CarpetRule) args[0]; - final CommandSourceStack source = (CommandSourceStack) args[1]; - final String id = rule.settingsManager().identifier(); - final String namespace; - if (!id.equals("carpet")) - { - namespace = id + ":"; - } - else - { - namespace = ""; - } - handler.call( - () -> Arrays.asList( - new StringValue(namespace + rule.name()), - new StringValue(RuleHelper.toRuleString(rule.value())) - ), () -> source - ); - } - }; - public void notifyRuleChanged(CommandSourceStack source, CarpetRule rule, String userInput) { observers.forEach(observer -> observer.ruleChanged(source, rule, userInput)); staticObservers.forEach(observer -> observer.ruleChanged(source, rule, userInput)); ServerNetworkHandler.updateRuleWithConnectedClients(rule); switchScarpetRuleIfNeeded(source, rule); //TODO move into rule - if (CARPET_RULE_CHANGES.isNeeded()) CARPET_RULE_CHANGES.handleAny(rule, source); } /** diff --git a/src/main/java/carpet/script/CarpetEventServer.java b/src/main/java/carpet/script/CarpetEventServer.java index be9b1540..4a13a869 100644 --- a/src/main/java/carpet/script/CarpetEventServer.java +++ b/src/main/java/carpet/script/CarpetEventServer.java @@ -437,6 +437,11 @@ public class CarpetEventServer return events; } + static + { + Carpet.initCarpetEvents(); + } + public static final Event START = new Event("server_starts", 0, true) { @Override diff --git a/src/main/java/carpet/script/external/Carpet.java b/src/main/java/carpet/script/external/Carpet.java index 26472677..700e7b97 100644 --- a/src/main/java/carpet/script/external/Carpet.java +++ b/src/main/java/carpet/script/external/Carpet.java @@ -11,6 +11,7 @@ import carpet.helpers.TickSpeed; import carpet.logging.HUDController; import carpet.network.ServerNetworkHandler; import carpet.patches.EntityPlayerMPFake; +import carpet.script.CarpetEventServer; import carpet.script.CarpetExpression; import carpet.script.CarpetScriptHost; import carpet.script.CarpetScriptServer; @@ -40,6 +41,7 @@ import java.io.IOException; import java.nio.file.FileVisitOption; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -214,6 +216,33 @@ public class Carpet throw new LoadException(String.format("%s requires a version of mod '%s' matching '%s', which is missing!", host.getName(), requiredModId, stringPredicate)); } + // to be ran once during CarpetEventServer.Event static init + public static void initCarpetEvents() { + CarpetEventServer.Event carpetRuleChanges = new CarpetEventServer.Event("carpet_rule_changes", 2, true) + { + @Override + public void handleAny(final Object... args) + { + CarpetRule rule = (CarpetRule) args[0]; + CommandSourceStack source = (CommandSourceStack) args[1]; + String id = rule.settingsManager().identifier(); + String namespace; + if (!id.equals("carpet")) + { + namespace = id + ":"; + } else + { + namespace = ""; + } + handler.call(() -> Arrays.asList( + new StringValue(namespace + rule.name()), + new StringValue(RuleHelper.toRuleString(rule.value())) + ), () -> source); + } + }; + SettingsManager.registerGlobalRuleObserver((source, changedRule, userInput) -> carpetRuleChanges.handleAny(changedRule, source)); + } + public static class ScarpetAppStoreValidator extends Validator { @Override