From f014a396ed8069370619be4e6032decf405ead2b Mon Sep 17 00:00:00 2001 From: Ruben Taelman Date: Sat, 21 Sep 2024 15:43:30 +0200 Subject: [PATCH] Move config command to common --- .../org/cyclops/cyclopscore/GeneralConfig.java | 4 ++-- .../cyclopscore/command/CommandConfig.java | 15 ++++++--------- .../command/argument/ArgumentInfoMod.java | 9 +++++---- .../argument/ArgumentTypeConfigProperty.java | 8 ++++---- .../ArgumentTypeConfigPropertyConfig.java | 10 ++++++++++ .../config/ConfigurablePropertyData.java | 9 ++++++++- .../cyclops/cyclopscore/helper/ModBaseCommon.java | 3 +++ .../cyclops/cyclopscore/CyclopsCoreFabric.java | 2 ++ .../forgeconfig/ConfigHandlerFabricHandler.java | 5 ++++- .../org/cyclops/cyclopscore/CyclopsCoreForge.java | 2 ++ .../cyclopscore/config/ConfigHandlerForge.java | 5 ++++- .../java/org/cyclops/cyclopscore/CyclopsCore.java | 2 +- .../ArgumentTypeConfigPropertyConfig.java | 10 ---------- .../cyclopscore/config/ConfigHandlerNeoForge.java | 5 ++++- .../org/cyclops/cyclopscore/init/ModBase.java | 2 -- 15 files changed, 55 insertions(+), 36 deletions(-) rename {loader-neoforge => loader-common}/src/main/java/org/cyclops/cyclopscore/command/CommandConfig.java (81%) rename {loader-neoforge => loader-common}/src/main/java/org/cyclops/cyclopscore/command/argument/ArgumentInfoMod.java (84%) rename {loader-neoforge => loader-common}/src/main/java/org/cyclops/cyclopscore/command/argument/ArgumentTypeConfigProperty.java (91%) create mode 100644 loader-common/src/main/java/org/cyclops/cyclopscore/command/argument/ArgumentTypeConfigPropertyConfig.java delete mode 100644 loader-neoforge/src/main/java/org/cyclops/cyclopscore/command/argument/ArgumentTypeConfigPropertyConfig.java diff --git a/loader-common/src/main/java/org/cyclops/cyclopscore/GeneralConfig.java b/loader-common/src/main/java/org/cyclops/cyclopscore/GeneralConfig.java index 92b51d83ce..e5626dbf74 100644 --- a/loader-common/src/main/java/org/cyclops/cyclopscore/GeneralConfig.java +++ b/loader-common/src/main/java/org/cyclops/cyclopscore/GeneralConfig.java @@ -12,10 +12,10 @@ */ public class GeneralConfig extends DummyConfigCommon> { - @ConfigurablePropertyCommon(category = "general", comment = "When in a dev environment, if a button should be added to the main menu to open a dev world (shift-click creates a new world).", configLocation = ModConfigLocation.CLIENT) + @ConfigurablePropertyCommon(category = "general", comment = "When in a dev environment, if a button should be added to the main menu to open a dev world (shift-click creates a new world).", configLocation = ModConfigLocation.CLIENT, isCommandable = true) public static boolean devWorldButton = true; - @ConfigurablePropertyCommon(category = "general", comment = "When in a dev environment, if music should be disabled at startup.", configLocation = ModConfigLocation.CLIENT) + @ConfigurablePropertyCommon(category = "general", comment = "When in a dev environment, if music should be disabled at startup.", configLocation = ModConfigLocation.CLIENT, isCommandable = true) public static boolean devDisableMusic = true; /** diff --git a/loader-neoforge/src/main/java/org/cyclops/cyclopscore/command/CommandConfig.java b/loader-common/src/main/java/org/cyclops/cyclopscore/command/CommandConfig.java similarity index 81% rename from loader-neoforge/src/main/java/org/cyclops/cyclopscore/command/CommandConfig.java rename to loader-common/src/main/java/org/cyclops/cyclopscore/command/CommandConfig.java index 52b683a03e..5eb93133fb 100644 --- a/loader-neoforge/src/main/java/org/cyclops/cyclopscore/command/CommandConfig.java +++ b/loader-common/src/main/java/org/cyclops/cyclopscore/command/CommandConfig.java @@ -8,11 +8,9 @@ import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; import net.minecraft.network.chat.Component; -import net.neoforged.neoforge.common.ModConfigSpec; import org.cyclops.cyclopscore.command.argument.ArgumentTypeConfigProperty; import org.cyclops.cyclopscore.config.ConfigurablePropertyData; -import org.cyclops.cyclopscore.helper.Helpers; -import org.cyclops.cyclopscore.init.ModBase; +import org.cyclops.cyclopscore.init.IModBase; /** * Command for selecting and defining a {@link ConfigurablePropertyData}. @@ -21,10 +19,10 @@ */ public class CommandConfig implements Command { - private final ModBase mod; + private final IModBase mod; private final boolean valueSet; - public CommandConfig(ModBase mod, boolean valueSet) { + public CommandConfig(IModBase mod, boolean valueSet) { this.mod = mod; this.valueSet = valueSet; } @@ -36,10 +34,9 @@ public int run(CommandContext context) throws CommandSyntaxE context.getSource().getPlayerOrException().sendSystemMessage(Component.literal(property.getConfigProperty().get().toString())); } else { String value = context.getArgument("value", String.class); - Object newValue = Helpers.tryParse(value, property.getConfigProperty().get()); + Object newValue = mod.getModHelpers().getBaseHelpers().tryParse(value, property.getConfigProperty().get()); if(newValue != null) { - ((ModConfigSpec.ConfigValue) property.getConfigProperty()).set(newValue); - ((ModConfigSpec.ConfigValue) property.getConfigProperty()).save(); + property.getConfigPropertyUpdater().accept(newValue); context.getSource().getPlayerOrException().sendSystemMessage(Component.translatable("chat.cyclopscore.command.updatedValue", property.getName(), newValue.toString())); } else { @@ -50,7 +47,7 @@ public int run(CommandContext context) throws CommandSyntaxE return 0; } - public static LiteralArgumentBuilder make(ModBase mod) { + public static LiteralArgumentBuilder make(IModBase mod) { return Commands.literal("config") .requires((commandSource) -> commandSource.hasPermission(2)) .then(Commands.argument("property", new ArgumentTypeConfigProperty(mod)) diff --git a/loader-neoforge/src/main/java/org/cyclops/cyclopscore/command/argument/ArgumentInfoMod.java b/loader-common/src/main/java/org/cyclops/cyclopscore/command/argument/ArgumentInfoMod.java similarity index 84% rename from loader-neoforge/src/main/java/org/cyclops/cyclopscore/command/argument/ArgumentInfoMod.java rename to loader-common/src/main/java/org/cyclops/cyclopscore/command/argument/ArgumentInfoMod.java index 9dc78b058f..38224112a7 100644 --- a/loader-neoforge/src/main/java/org/cyclops/cyclopscore/command/argument/ArgumentInfoMod.java +++ b/loader-common/src/main/java/org/cyclops/cyclopscore/command/argument/ArgumentInfoMod.java @@ -5,7 +5,8 @@ import net.minecraft.commands.CommandBuildContext; import net.minecraft.commands.synchronization.ArgumentTypeInfo; import net.minecraft.network.FriendlyByteBuf; -import org.cyclops.cyclopscore.init.ModBase; +import org.cyclops.cyclopscore.helper.ModBaseCommon; +import org.cyclops.cyclopscore.init.IModBase; import org.cyclops.cyclopscore.network.PacketCodec; /** @@ -21,7 +22,7 @@ public void serializeToNetwork(ArgumentInfoMod.Template t, FriendlyByteBuf packe @Override public ArgumentInfoMod.Template deserializeFromNetwork(FriendlyByteBuf packetBuffer) { - return new Template(ModBase.get(packetBuffer.readUtf(PacketCodec.READ_STRING_MAX_LENGTH))); + return new Template(ModBaseCommon.getCommon(packetBuffer.readUtf(PacketCodec.READ_STRING_MAX_LENGTH))); } @Override @@ -35,9 +36,9 @@ public Template unpack(ArgumentTypeConfigProperty p_235372_) { } public final class Template implements ArgumentTypeInfo.Template { - private final ModBase mod; + private final IModBase mod; - Template(ModBase mod) { + Template(IModBase mod) { this.mod = mod; } diff --git a/loader-neoforge/src/main/java/org/cyclops/cyclopscore/command/argument/ArgumentTypeConfigProperty.java b/loader-common/src/main/java/org/cyclops/cyclopscore/command/argument/ArgumentTypeConfigProperty.java similarity index 91% rename from loader-neoforge/src/main/java/org/cyclops/cyclopscore/command/argument/ArgumentTypeConfigProperty.java rename to loader-common/src/main/java/org/cyclops/cyclopscore/command/argument/ArgumentTypeConfigProperty.java index 76e37761a2..b948b805b6 100644 --- a/loader-neoforge/src/main/java/org/cyclops/cyclopscore/command/argument/ArgumentTypeConfigProperty.java +++ b/loader-common/src/main/java/org/cyclops/cyclopscore/command/argument/ArgumentTypeConfigProperty.java @@ -11,7 +11,7 @@ import net.minecraft.commands.SharedSuggestionProvider; import net.minecraft.network.chat.Component; import org.cyclops.cyclopscore.config.ConfigurablePropertyData; -import org.cyclops.cyclopscore.init.ModBase; +import org.cyclops.cyclopscore.init.IModBase; import java.util.ArrayList; import java.util.Collection; @@ -23,13 +23,13 @@ */ public class ArgumentTypeConfigProperty implements ArgumentType { - private final ModBase mod; + private final IModBase mod; - public ArgumentTypeConfigProperty(ModBase mod) { + public ArgumentTypeConfigProperty(IModBase mod) { this.mod = mod; } - public ModBase getMod() { + public IModBase getMod() { return mod; } diff --git a/loader-common/src/main/java/org/cyclops/cyclopscore/command/argument/ArgumentTypeConfigPropertyConfig.java b/loader-common/src/main/java/org/cyclops/cyclopscore/command/argument/ArgumentTypeConfigPropertyConfig.java new file mode 100644 index 0000000000..b2a89ba996 --- /dev/null +++ b/loader-common/src/main/java/org/cyclops/cyclopscore/command/argument/ArgumentTypeConfigPropertyConfig.java @@ -0,0 +1,10 @@ +package org.cyclops.cyclopscore.command.argument; + +import org.cyclops.cyclopscore.config.extendedconfig.ArgumentTypeConfigCommon; +import org.cyclops.cyclopscore.init.IModBase; + +public class ArgumentTypeConfigPropertyConfig extends ArgumentTypeConfigCommon.Template, M> { + public ArgumentTypeConfigPropertyConfig(M mod) { + super(mod, "configprop", new ArgumentInfoMod<>(), ArgumentTypeConfigProperty.class); + } +} diff --git a/loader-common/src/main/java/org/cyclops/cyclopscore/config/ConfigurablePropertyData.java b/loader-common/src/main/java/org/cyclops/cyclopscore/config/ConfigurablePropertyData.java index da6395676b..533fb64eb5 100644 --- a/loader-common/src/main/java/org/cyclops/cyclopscore/config/ConfigurablePropertyData.java +++ b/loader-common/src/main/java/org/cyclops/cyclopscore/config/ConfigurablePropertyData.java @@ -4,6 +4,7 @@ import org.cyclops.cyclopscore.init.IModBase; import java.lang.reflect.Field; +import java.util.function.Consumer; import java.util.function.Supplier; /** @@ -31,6 +32,7 @@ public final class ConfigurablePropertyData { public final int maxValue; private Supplier configProperty; + private Consumer configPropertyUpdater; /** * Define a new configurable property. @@ -66,14 +68,19 @@ public ConfigurablePropertyData(IModBase mod, String category, String name, T de this.maxValue = maxValue; } - public void setConfigProperty(Supplier configProperty) { + public void setConfigProperty(Supplier configProperty, Consumer configPropertyUpdater) { this.configProperty = configProperty; + this.configPropertyUpdater = configPropertyUpdater; } public Supplier getConfigProperty() { return configProperty; } + public Consumer getConfigPropertyUpdater() { + return configPropertyUpdater; + } + /** * @return Can this property be changed through commands. */ diff --git a/loader-common/src/main/java/org/cyclops/cyclopscore/helper/ModBaseCommon.java b/loader-common/src/main/java/org/cyclops/cyclopscore/helper/ModBaseCommon.java index 37025dc053..ba16cd4a19 100644 --- a/loader-common/src/main/java/org/cyclops/cyclopscore/helper/ModBaseCommon.java +++ b/loader-common/src/main/java/org/cyclops/cyclopscore/helper/ModBaseCommon.java @@ -11,6 +11,7 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import org.apache.commons.lang3.tuple.Pair; +import org.cyclops.cyclopscore.command.CommandConfig; import org.cyclops.cyclopscore.config.ConfigHandlerCommon; import org.cyclops.cyclopscore.config.extendedconfig.CreativeModeTabConfigCommon; import org.cyclops.cyclopscore.init.IModBase; @@ -95,6 +96,8 @@ protected void loadModCompats(ModCompatLoader modCompatLoader) { protected LiteralArgumentBuilder constructBaseCommand(Commands.CommandSelection selection, CommandBuildContext context) { LiteralArgumentBuilder root = Commands.literal(this.getModId()); + root.then(CommandConfig.make(this)); + return root; } diff --git a/loader-fabric/src/main/java/org/cyclops/cyclopscore/CyclopsCoreFabric.java b/loader-fabric/src/main/java/org/cyclops/cyclopscore/CyclopsCoreFabric.java index f52db0a261..bc38faea45 100644 --- a/loader-fabric/src/main/java/org/cyclops/cyclopscore/CyclopsCoreFabric.java +++ b/loader-fabric/src/main/java/org/cyclops/cyclopscore/CyclopsCoreFabric.java @@ -14,6 +14,7 @@ import org.cyclops.cyclopscore.command.CommandDebug; import org.cyclops.cyclopscore.command.CommandIgnite; import org.cyclops.cyclopscore.command.CommandReloadResources; +import org.cyclops.cyclopscore.command.argument.ArgumentTypeConfigPropertyConfig; import org.cyclops.cyclopscore.command.argument.ArgumentTypeDebugPacketConfig; import org.cyclops.cyclopscore.command.argument.ArgumentTypeEnumConfig; import org.cyclops.cyclopscore.component.DataComponentCapacityConfig; @@ -85,6 +86,7 @@ protected void onConfigsRegister(ConfigHandlerCommon configHandler) { configHandler.addConfigurable(new GeneralConfig(this)); // Argument types + configHandler.addConfigurable(new ArgumentTypeConfigPropertyConfig<>(this)); configHandler.addConfigurable(new ArgumentTypeDebugPacketConfig<>(this)); configHandler.addConfigurable(new ArgumentTypeEnumConfig<>(this)); diff --git a/loader-fabric/src/main/java/org/cyclops/cyclopscore/modcompat/forgeconfig/ConfigHandlerFabricHandler.java b/loader-fabric/src/main/java/org/cyclops/cyclopscore/modcompat/forgeconfig/ConfigHandlerFabricHandler.java index cdfd31237d..f6e7adf7da 100644 --- a/loader-fabric/src/main/java/org/cyclops/cyclopscore/modcompat/forgeconfig/ConfigHandlerFabricHandler.java +++ b/loader-fabric/src/main/java/org/cyclops/cyclopscore/modcompat/forgeconfig/ConfigHandlerFabricHandler.java @@ -91,7 +91,10 @@ protected void onConfigPropertyInit(ConfigurablePropertyData configProper .comment(configPropertyData.comment) .translation(configPropertyData.getLanguageKey()) .define(configPropertyData.name, configPropertyData.defaultValue); - configPropertyData.setConfigProperty(configProperty); + configPropertyData.setConfigProperty(configProperty, (newValue) -> { + configProperty.set(newValue); + configProperty.save(); + }); configBuilder.pop(); } diff --git a/loader-forge/src/main/java/org/cyclops/cyclopscore/CyclopsCoreForge.java b/loader-forge/src/main/java/org/cyclops/cyclopscore/CyclopsCoreForge.java index 8b6b0d8053..52f50d16cd 100644 --- a/loader-forge/src/main/java/org/cyclops/cyclopscore/CyclopsCoreForge.java +++ b/loader-forge/src/main/java/org/cyclops/cyclopscore/CyclopsCoreForge.java @@ -16,6 +16,7 @@ import org.cyclops.cyclopscore.command.CommandDebug; import org.cyclops.cyclopscore.command.CommandIgnite; import org.cyclops.cyclopscore.command.CommandReloadResources; +import org.cyclops.cyclopscore.command.argument.ArgumentTypeConfigPropertyConfig; import org.cyclops.cyclopscore.command.argument.ArgumentTypeDebugPacketConfig; import org.cyclops.cyclopscore.command.argument.ArgumentTypeEnumConfig; import org.cyclops.cyclopscore.component.DataComponentCapacityConfig; @@ -94,6 +95,7 @@ protected void onConfigsRegister(ConfigHandlerCommon configHandler) { configHandler.addConfigurable(new GeneralConfig(this)); // Argument types + configHandler.addConfigurable(new ArgumentTypeConfigPropertyConfig<>(this)); configHandler.addConfigurable(new ArgumentTypeDebugPacketConfig<>(this)); configHandler.addConfigurable(new ArgumentTypeEnumConfig<>(this)); diff --git a/loader-forge/src/main/java/org/cyclops/cyclopscore/config/ConfigHandlerForge.java b/loader-forge/src/main/java/org/cyclops/cyclopscore/config/ConfigHandlerForge.java index fb25acc823..26d8e07374 100644 --- a/loader-forge/src/main/java/org/cyclops/cyclopscore/config/ConfigHandlerForge.java +++ b/loader-forge/src/main/java/org/cyclops/cyclopscore/config/ConfigHandlerForge.java @@ -148,7 +148,10 @@ protected void onConfigPropertyInit(ConfigurablePropertyData configProper .comment(configPropertyData.comment) .translation(configPropertyData.getLanguageKey()) .define(configPropertyData.name, configPropertyData.defaultValue); - configPropertyData.setConfigProperty(configProperty); + configPropertyData.setConfigProperty(configProperty, (newValue) -> { + configProperty.set(newValue); + configProperty.save(); + }); configBuilder.pop(); } diff --git a/loader-neoforge/src/main/java/org/cyclops/cyclopscore/CyclopsCore.java b/loader-neoforge/src/main/java/org/cyclops/cyclopscore/CyclopsCore.java index 74bdfd280b..a5709377a7 100644 --- a/loader-neoforge/src/main/java/org/cyclops/cyclopscore/CyclopsCore.java +++ b/loader-neoforge/src/main/java/org/cyclops/cyclopscore/CyclopsCore.java @@ -172,7 +172,7 @@ public void onConfigsRegister(ConfigHandlerCommon configHandler) { configHandler.addConfigurable(new ContainerInfoBookTestConfig()); // Argument types - configHandler.addConfigurable(new ArgumentTypeConfigPropertyConfig()); + configHandler.addConfigurable(new ArgumentTypeConfigPropertyConfig<>(this)); configHandler.addConfigurable(new ArgumentTypeDebugPacketConfig<>(this)); configHandler.addConfigurable(new ArgumentTypeEnumConfig<>(this)); diff --git a/loader-neoforge/src/main/java/org/cyclops/cyclopscore/command/argument/ArgumentTypeConfigPropertyConfig.java b/loader-neoforge/src/main/java/org/cyclops/cyclopscore/command/argument/ArgumentTypeConfigPropertyConfig.java deleted file mode 100644 index 825a070d0f..0000000000 --- a/loader-neoforge/src/main/java/org/cyclops/cyclopscore/command/argument/ArgumentTypeConfigPropertyConfig.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.cyclops.cyclopscore.command.argument; - -import org.cyclops.cyclopscore.CyclopsCore; -import org.cyclops.cyclopscore.config.extendedconfig.ArgumentTypeConfig; - -public class ArgumentTypeConfigPropertyConfig extends ArgumentTypeConfig.Template> { - public ArgumentTypeConfigPropertyConfig() { - super(CyclopsCore._instance, "blur", new ArgumentInfoMod<>(), ArgumentTypeConfigProperty.class); - } -} diff --git a/loader-neoforge/src/main/java/org/cyclops/cyclopscore/config/ConfigHandlerNeoForge.java b/loader-neoforge/src/main/java/org/cyclops/cyclopscore/config/ConfigHandlerNeoForge.java index 5f4272f0c1..dbd495c636 100644 --- a/loader-neoforge/src/main/java/org/cyclops/cyclopscore/config/ConfigHandlerNeoForge.java +++ b/loader-neoforge/src/main/java/org/cyclops/cyclopscore/config/ConfigHandlerNeoForge.java @@ -185,7 +185,10 @@ protected void onConfigPropertyInit(ConfigurablePropertyData configProper .comment(configPropertyData.comment) .translation(configPropertyData.getLanguageKey()) .define(configPropertyData.name, configPropertyData.defaultValue); - configPropertyData.setConfigProperty(configProperty); + configPropertyData.setConfigProperty(configProperty, (newValue) -> { + configProperty.set(newValue); + configProperty.save(); + }); configBuilder.pop(); } diff --git a/loader-neoforge/src/main/java/org/cyclops/cyclopscore/init/ModBase.java b/loader-neoforge/src/main/java/org/cyclops/cyclopscore/init/ModBase.java index ac32dfb005..4f3d0f54a9 100644 --- a/loader-neoforge/src/main/java/org/cyclops/cyclopscore/init/ModBase.java +++ b/loader-neoforge/src/main/java/org/cyclops/cyclopscore/init/ModBase.java @@ -26,7 +26,6 @@ import org.apache.logging.log4j.Level; import org.cyclops.cyclopscore.client.key.IKeyRegistry; import org.cyclops.cyclopscore.client.key.KeyRegistry; -import org.cyclops.cyclopscore.command.CommandConfig; import org.cyclops.cyclopscore.command.CommandVersion; import org.cyclops.cyclopscore.config.ConfigHandler; import org.cyclops.cyclopscore.config.ConfigHandlerCommon; @@ -206,7 +205,6 @@ protected IMCHandler constructIMCHandler() { protected LiteralArgumentBuilder constructBaseCommand(Commands.CommandSelection selection, CommandBuildContext context) { LiteralArgumentBuilder root = super.constructBaseCommand(selection, context); - root.then(CommandConfig.make(this)); root.then(CommandVersion.make(this)); return root;