Skip to content

Commit

Permalink
v1.3.1. Refactor commands. Add getting and setting configs via comman…
Browse files Browse the repository at this point in the history
…d again.
  • Loading branch information
Corosauce committed Dec 1, 2023
1 parent f2a581a commit d9b9448
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 126 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
112 changes: 112 additions & 0 deletions src/main/java/com/corosus/coroutil/command/CommandCoroConfig.java
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");
}
}
}
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";
}
}

This file was deleted.

This file was deleted.

21 changes: 8 additions & 13 deletions src/main/java/com/corosus/modconfig/ConfigMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());*/
}


Expand Down Expand Up @@ -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;
Expand All @@ -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() {
Expand Down
21 changes: 4 additions & 17 deletions src/main/java/com/corosus/modconfig/EventHandlerForge.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit d9b9448

Please sign in to comment.