-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v1.3.1. Refactor commands. Add getting and setting configs via comman…
…d again.
- Loading branch information
Showing
7 changed files
with
153 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
src/main/java/com/corosus/coroutil/command/CommandCoroConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<CommandSourceStack> 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<CommandSourceStack, ?> 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<CommandSourceStack, ?> 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<CommandSourceStack, ?> 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<CommandSourceStack, ?> 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<String> getConfigs() { | ||
return ConfigMod.lookupRegistryNameToConfig.values().stream().map((e) -> e.saveFilePath.replace("\\", "--") + ".toml").toList(); | ||
} | ||
|
||
public static Iterable<String> getConfigSettings(String config_name) { | ||
ModConfigData modConfigData = ConfigMod.lookupFilePathToConfig.get(config_name.replace("--", "\\")); | ||
if (modConfigData != null) { | ||
List<String> 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"); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/corosus/coroutil/command/CommandCoroConfigClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<CommandSourceStack> 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"; | ||
} | ||
} |
47 changes: 0 additions & 47 deletions
47
src/main/java/com/corosus/coroutil/command/CommandReloadConfig.java
This file was deleted.
Oops, something went wrong.
48 changes: 0 additions & 48 deletions
48
src/main/java/com/corosus/coroutil/command/CommandReloadConfigClient.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 4 additions & 17 deletions
21
src/main/java/com/corosus/modconfig/EventHandlerForge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters