diff --git a/gradle.properties b/gradle.properties index 2872ee7f..a4bc586c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -48,7 +48,7 @@ mod_name=CoroUtil # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. mod_license=All Rights Reserved # The mod version. See https://semver.org/ -mod_version=1.20.1-1.3.0 +mod_version=1.20.1-1.3.1 # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # This should match the base package used for the mod sources. # See https://maven.apache.org/guides/mini/guide-naming-conventions.html diff --git a/src/main/java/com/corosus/coroutil/command/CommandCoroConfig.java b/src/main/java/com/corosus/coroutil/command/CommandCoroConfig.java new file mode 100644 index 00000000..12aff076 --- /dev/null +++ b/src/main/java/com/corosus/coroutil/command/CommandCoroConfig.java @@ -0,0 +1,112 @@ +package com.corosus.coroutil.command; + +import com.corosus.coroconfig.CoroConfigTracker; +import com.corosus.coroconfig.CoroModConfig; +import com.corosus.coroutil.util.CULog; +import com.corosus.modconfig.ConfigMod; +import com.corosus.modconfig.ModConfigData; +import com.mojang.brigadier.Command; +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.ArgumentBuilder; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; +import net.minecraft.commands.SharedSuggestionProvider; +import net.minecraft.network.chat.Component; +import net.minecraftforge.fml.config.ConfigTracker; +import net.minecraftforge.fml.config.ModConfig; +import net.minecraftforge.fml.loading.FMLPaths; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import static net.minecraft.commands.Commands.literal; + +public class CommandCoroConfig { + public static void register(final CommandDispatcher dispatcher) { + dispatcher.register( + Commands.literal(getCommandName()) + .then(literal("config") + .then(literal("common") + .then(argumentReload(ModConfig.Type.COMMON)) + .then(argumentSave()) + .then(argumentGet()) + .then(argumentSet()) + ) + ) + ); + } + + public static ArgumentBuilder argumentReload(ModConfig.Type type) { + return literal("reload").executes(c -> { + CULog.log("reloading all mods common configurations from disk"); + ConfigTracker.INSTANCE.loadConfigs(type, FMLPaths.CONFIGDIR.get()); + CoroConfigTracker.INSTANCE.loadConfigs(type == ModConfig.Type.COMMON ? CoroModConfig.Type.COMMON : CoroModConfig.Type.CLIENT, FMLPaths.CONFIGDIR.get()); + c.getSource().sendSuccess(() -> Component.literal("Reloading all common configs from disk"), true); + return Command.SINGLE_SUCCESS; + }); + } + + public static ArgumentBuilder argumentSave() { + return literal("save").executes(c -> { + CULog.log("saving all coro mods runtime configs to disk"); + ConfigMod.forceSaveAllFilesFromRuntimeSettings(); + c.getSource().sendSuccess(() -> Component.literal("Saving all common coro configs to disk"), true); + return Command.SINGLE_SUCCESS; + }); + } + + public static ArgumentBuilder argumentGet() { + return literal("get").then(Commands.argument("file_name", StringArgumentType.word()).suggests((p_136339_, p_136340_) -> SharedSuggestionProvider.suggest(getConfigs(), p_136340_)) + .then(Commands.argument("setting_name", StringArgumentType.word()).suggests((p_136339_, p_136340_) -> SharedSuggestionProvider.suggest(getConfigSettings(StringArgumentType.getString(p_136339_, "file_name")), p_136340_)) + .executes(c -> { + String fileName = StringArgumentType.getString(c, "file_name").replace("--", "\\"); + String configName = ConfigMod.lookupFilePathToConfig.get(fileName).configID; + String settingName = StringArgumentType.getString(c, "setting_name"); + Object obj = ConfigMod.getField(configName, settingName); + c.getSource().sendSuccess(() -> Component.literal(settingName + " = " + obj + " in " + fileName), true); + return Command.SINGLE_SUCCESS; + }))); + } + + public static ArgumentBuilder argumentSet() { + return literal("set").then(Commands.argument("file_name", StringArgumentType.word()).suggests((p_136339_, p_136340_) -> SharedSuggestionProvider.suggest(getConfigs(), p_136340_)) + .then(Commands.argument("setting_name", StringArgumentType.word()).suggests((p_136339_, p_136340_) -> SharedSuggestionProvider.suggest(getConfigSettings(StringArgumentType.getString(p_136339_, "file_name")), p_136340_)) + .then(Commands.argument("value", StringArgumentType.string()) + .executes(c -> { + String fileName = StringArgumentType.getString(c, "file_name").replace("--", "\\"); + String configName = ConfigMod.lookupFilePathToConfig.get(fileName).configID; + String settingName = StringArgumentType.getString(c, "setting_name"); + String value = StringArgumentType.getString(c, "value"); + ConfigMod.updateField(configName, settingName, value); + Object obj = ConfigMod.getField(configName, settingName); + ConfigMod.forceSaveAllFilesFromRuntimeSettings(); + c.getSource().sendSuccess(() -> Component.literal("Set " + settingName + " to " + obj + " in " + fileName), true); + return Command.SINGLE_SUCCESS; + })))); + } + + public static String getCommandName() { + return "coro"; + } + + public static Iterable getConfigs() { + return ConfigMod.lookupRegistryNameToConfig.values().stream().map((e) -> e.saveFilePath.replace("\\", "--") + ".toml").toList(); + } + + public static Iterable getConfigSettings(String config_name) { + ModConfigData modConfigData = ConfigMod.lookupFilePathToConfig.get(config_name.replace("--", "\\")); + if (modConfigData != null) { + List joinedList = new ArrayList<>(); + joinedList.addAll(modConfigData.valsString.keySet()); + joinedList.addAll(modConfigData.valsInteger.keySet()); + joinedList.addAll(modConfigData.valsDouble.keySet()); + joinedList.addAll(modConfigData.valsBoolean.keySet()); + Collections.sort(joinedList); + return joinedList; + } else { + return List.of("<-- invalid config name"); + } + } +} diff --git a/src/main/java/com/corosus/coroutil/command/CommandCoroConfigClient.java b/src/main/java/com/corosus/coroutil/command/CommandCoroConfigClient.java new file mode 100644 index 00000000..98e582d3 --- /dev/null +++ b/src/main/java/com/corosus/coroutil/command/CommandCoroConfigClient.java @@ -0,0 +1,28 @@ +package com.corosus.coroutil.command; + +import com.mojang.brigadier.CommandDispatcher; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; +import net.minecraftforge.fml.config.ModConfig; + +import static net.minecraft.commands.Commands.literal; + +public class CommandCoroConfigClient { + public static void register(final CommandDispatcher dispatcher) { + dispatcher.register( + Commands.literal(getCommandName()) + .then(literal("config") + .then(literal("client") + .then(CommandCoroConfig.argumentReload(ModConfig.Type.CLIENT)) + .then(CommandCoroConfig.argumentSave()) + .then(CommandCoroConfig.argumentGet()) + .then(CommandCoroConfig.argumentSet()) + ) + ) + ); + } + + public static String getCommandName() { + return "coro"; + } +} diff --git a/src/main/java/com/corosus/coroutil/command/CommandReloadConfig.java b/src/main/java/com/corosus/coroutil/command/CommandReloadConfig.java deleted file mode 100644 index 80d48b6a..00000000 --- a/src/main/java/com/corosus/coroutil/command/CommandReloadConfig.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.corosus.coroutil.command; - -import com.corosus.coroconfig.CoroConfigTracker; -import com.corosus.coroconfig.CoroModConfig; -import com.corosus.coroutil.util.CULog; -import com.corosus.modconfig.ConfigMod; -import com.mojang.brigadier.Command; -import com.mojang.brigadier.CommandDispatcher; -import net.minecraft.commands.CommandSourceStack; -import net.minecraft.commands.Commands; -import net.minecraft.network.chat.Component; -import net.minecraftforge.fml.config.ConfigTracker; -import net.minecraftforge.fml.config.ModConfig; -import net.minecraftforge.fml.loading.FMLPaths; - -import static net.minecraft.commands.Commands.literal; - -public class CommandReloadConfig { - public static void register(final CommandDispatcher dispatcher) { - dispatcher.register( - Commands.literal(getCommandName()) - .then(literal("config") - .then(literal("reload") - .then(Commands.literal("common").executes(c -> { - CULog.log("reloading all mods common configurations from disk"); - ConfigTracker.INSTANCE.loadConfigs(ModConfig.Type.COMMON, FMLPaths.CONFIGDIR.get()); - CoroConfigTracker.INSTANCE.loadConfigs(CoroModConfig.Type.COMMON, FMLPaths.CONFIGDIR.get()); - c.getSource().sendSuccess(() -> Component.literal("Reloading all common configs from disk"), true); - return Command.SINGLE_SUCCESS; - })) - ) - .then(literal("save") - .then(Commands.literal("common").executes(c -> { - CULog.log("saving all coro mods runtime configs to disk"); - ConfigMod.forceSaveAllFilesFromRuntimeSettings(); - c.getSource().sendSuccess(() -> Component.literal("Saving all common coro configs to disk"), true); - return Command.SINGLE_SUCCESS; - })) - ) - ) - ); - } - - public static String getCommandName() { - return "coro"; - } -} diff --git a/src/main/java/com/corosus/coroutil/command/CommandReloadConfigClient.java b/src/main/java/com/corosus/coroutil/command/CommandReloadConfigClient.java deleted file mode 100644 index 48473b37..00000000 --- a/src/main/java/com/corosus/coroutil/command/CommandReloadConfigClient.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.corosus.coroutil.command; - -import com.corosus.coroconfig.CoroConfigTracker; -import com.corosus.coroconfig.CoroModConfig; -import com.corosus.coroutil.util.CULog; -import com.corosus.modconfig.ConfigMod; -import com.mojang.brigadier.Command; -import com.mojang.brigadier.CommandDispatcher; -import net.minecraft.commands.CommandSourceStack; -import net.minecraft.commands.Commands; -import net.minecraft.network.chat.Component; -import net.minecraftforge.fml.config.ConfigTracker; -import net.minecraftforge.fml.config.ModConfig; -import net.minecraftforge.fml.loading.FMLPaths; - -import static net.minecraft.commands.Commands.literal; - -public class CommandReloadConfigClient { - public static void register(final CommandDispatcher dispatcher) { - dispatcher.register( - Commands.literal(getCommandName()) - .then(literal("config") - .then(literal("reload") - .then(Commands.literal("client").executes(c -> { - CULog.log("reloading all mods client configurations from disk"); - ConfigTracker.INSTANCE.loadConfigs(ModConfig.Type.CLIENT, FMLPaths.CONFIGDIR.get()); - CoroConfigTracker.INSTANCE.loadConfigs(CoroModConfig.Type.CLIENT, FMLPaths.CONFIGDIR.get()); - c.getSource().sendSuccess(() -> Component.literal("Reloading all client configs from disk"), true); - return Command.SINGLE_SUCCESS; - })) - ) - .then(literal("save") - .then(Commands.literal("client").executes(c -> { - CULog.log("saving all coro mods runtime configs to disk"); - /** dummy literal for autocomplete sake, see EventHandlerForge.clientChat for what actually "intercepts" this */ - ConfigMod.forceSaveAllFilesFromRuntimeSettings(); - c.getSource().sendSuccess(() -> Component.literal("Saving all client coro configs to disk"), true); - return Command.SINGLE_SUCCESS; - })) - ) - ) - ); - } - - public static String getCommandName() { - return "coro"; - } -} diff --git a/src/main/java/com/corosus/modconfig/ConfigMod.java b/src/main/java/com/corosus/modconfig/ConfigMod.java index 48cc910c..3a53dc34 100644 --- a/src/main/java/com/corosus/modconfig/ConfigMod.java +++ b/src/main/java/com/corosus/modconfig/ConfigMod.java @@ -47,20 +47,8 @@ public ConfigMod() { EventHandlerForge eventHandlerForge = new EventHandlerForge(); MinecraftForge.EVENT_BUS.register(eventHandlerForge); - IEventBus modBus = FMLJavaModLoadingContext.get().getModEventBus(); - //modBus.addListener(EventHandlerForge::serverStart); - - System.out.println("ConfigCoroUtil.useLoggingDebug: " + ConfigCoroUtil.useLoggingDebug); - new File("./config/CoroUtil").mkdirs(); ConfigMod.addConfigFile(MODID, new ConfigCoroUtil()); - - System.out.println("ConfigCoroUtil.useLoggingDebug: " + ConfigCoroUtil.useLoggingDebug); - - //now loaded as its registered - /*DistExecutor.unsafeRunWhenOn(Dist.CLIENT, - () -> () -> CoroConfigTracker.INSTANCE.loadConfigs(CoroModConfig.Type.CLIENT, FMLPaths.CONFIGDIR.get())); - CoroConfigTracker.INSTANCE.loadConfigs(CoroModConfig.Type.COMMON, FMLPaths.CONFIGDIR.get());*/ } @@ -186,7 +174,8 @@ public static String getComment(String configID, String name) { public static boolean updateField(String configID, String name, Object obj) { if (lookupRegistryNameToConfig.get(configID).setFieldBasedOnType(name, obj)) { //writeHashMapsToFile(); - lookupRegistryNameToConfig.get(configID).writeConfigFile(true); + //invalid here now it just fully re-registers configs which is bad, we just force save outside here now + //lookupRegistryNameToConfig.get(configID).writeConfigFile(true); return true; } return false; @@ -198,6 +187,12 @@ public static void forceSaveAllFilesFromRuntimeSettings() { //data.writeConfigFile(true); data.updateConfigFileWithRuntimeValues(); } + + //TODO: theres a bug where it takes 2 tries, find out why + for (ModConfigData data : lookupRegistryNameToConfig.values()) { + //data.writeConfigFile(true); + data.updateConfigFileWithRuntimeValues(); + } } public static void forceLoadRuntimeSettingsFromFile() { diff --git a/src/main/java/com/corosus/modconfig/EventHandlerForge.java b/src/main/java/com/corosus/modconfig/EventHandlerForge.java index 988525c1..c0e22dc8 100644 --- a/src/main/java/com/corosus/modconfig/EventHandlerForge.java +++ b/src/main/java/com/corosus/modconfig/EventHandlerForge.java @@ -1,35 +1,22 @@ package com.corosus.modconfig; -import com.corosus.coroconfig.CoroConfigTracker; -import com.corosus.coroconfig.CoroModConfig; -import com.corosus.coroutil.command.CommandReloadConfig; -import com.corosus.coroutil.command.CommandReloadConfigClient; -import com.corosus.coroutil.config.ConfigCoroUtil; -import net.minecraft.server.MinecraftServer; -import net.minecraft.world.level.storage.LevelResource; +import com.corosus.coroutil.command.CommandCoroConfig; +import com.corosus.coroutil.command.CommandCoroConfigClient; import net.minecraftforge.client.event.RegisterClientCommandsEvent; import net.minecraftforge.event.RegisterCommandsEvent; -import net.minecraftforge.event.entity.living.LivingEvent; import net.minecraftforge.event.server.ServerAboutToStartEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; -import net.minecraftforge.fml.config.ConfigTracker; -import net.minecraftforge.fml.config.ModConfig; -import net.minecraftforge.fml.loading.FMLPaths; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; public class EventHandlerForge { @SubscribeEvent public void registerCommands(RegisterCommandsEvent event) { - CommandReloadConfig.register(event.getDispatcher()); + CommandCoroConfig.register(event.getDispatcher()); } @SubscribeEvent public void registerCommandsClient(RegisterClientCommandsEvent event) { - CommandReloadConfigClient.register(event.getDispatcher()); + CommandCoroConfigClient.register(event.getDispatcher()); } /*@SubscribeEvent