]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/commitdiff
Rework settings/config command
authorIrtimaled <irtimaled@gmail.com>
Mon, 18 May 2020 01:12:06 +0000 (18:12 -0700)
committerIrtimaled <irtimaled@gmail.com>
Mon, 18 May 2020 04:05:10 +0000 (21:05 -0700)
src/main/java/com/irtimaled/bbor/client/commands/Arguments.java
src/main/java/com/irtimaled/bbor/client/commands/CommandHelper.java
src/main/java/com/irtimaled/bbor/client/commands/ConfigCommand.java
src/main/java/com/irtimaled/bbor/client/config/AbstractSetting.java [deleted file]
src/main/java/com/irtimaled/bbor/client/config/ConfigManager.java
src/main/java/com/irtimaled/bbor/client/config/Configuration.java
src/main/java/com/irtimaled/bbor/client/config/Setting.java

index 72c3ca1ebe135f2c7bc69207954ba20d2c509606..5a71d2fee3739519161c65aa8cdffb390365c29e 100644 (file)
@@ -36,7 +36,7 @@ public class Arguments {
     }
 
     public static StringArgumentType string() {
-        return StringArgumentType.greedyString();
+        return StringArgumentType.string();
     }
 
     public static BoolArgumentType bool() {
@@ -67,11 +67,10 @@ public class Arguments {
         return getArgumentValueOrDefault(context, name, BoolArgumentType::getBool, () -> false);
     }
 
-
-    public static <T> T getArgumentValueOrDefault(CommandContext<CommandSource> context,
-                                                  String name,
-                                                  ArgumentFetcher<T> getValue,
-                                                  Supplier<T> defaultValue) throws CommandSyntaxException {
+    private static <T> T getArgumentValueOrDefault(CommandContext<CommandSource> context,
+                                                   String name,
+                                                   ArgumentFetcher<T> getValue,
+                                                   Supplier<T> defaultValue) throws CommandSyntaxException {
         try {
             return getValue.get(context, name);
         } catch (IllegalArgumentException exception) {
index cdb7260e1b6dd5c7738a3a67f546cf3aec433381..33918608ae27aed8f088b1b2ff19f830cd09ec67 100644 (file)
@@ -1,6 +1,8 @@
 package com.irtimaled.bbor.client.commands;
 
 import com.mojang.brigadier.context.CommandContext;
+import com.mojang.brigadier.tree.CommandNode;
+import com.mojang.brigadier.tree.LiteralCommandNode;
 import net.minecraft.command.CommandSource;
 import net.minecraft.util.text.TextComponentTranslation;
 
@@ -8,4 +10,18 @@ class CommandHelper {
     static void feedback(CommandContext<CommandSource> context, String format, Object... values) {
         context.getSource().sendFeedback(new TextComponentTranslation(format, values), false);
     }
+
+    static boolean lastNodeIsLiteral(CommandContext<CommandSource> context, String literal) {
+        CommandNode lastNode = getLastNode(context);
+        if (lastNode instanceof LiteralCommandNode) {
+            LiteralCommandNode literalCommandNode = (LiteralCommandNode) lastNode;
+            return literalCommandNode.getLiteral().equals(literal);
+        }
+        return false;
+    }
+
+    private static CommandNode getLastNode(CommandContext<CommandSource> context) {
+        CommandNode[] nodes = context.getNodes().keySet().toArray(new CommandNode[0]);
+        return nodes[nodes.length-1];
+    }
 }
index a371f1749d8e6b1302c1941ab62c85872adeeb9b..00ff4ea4dd53732e96d04f42436f6a7d2348abca 100644 (file)
@@ -3,7 +3,9 @@ package com.irtimaled.bbor.client.commands;
 import com.irtimaled.bbor.client.config.ConfigManager;
 import com.irtimaled.bbor.client.config.Setting;
 import com.irtimaled.bbor.client.gui.SettingsScreen;
+import com.mojang.brigadier.Command;
 import com.mojang.brigadier.CommandDispatcher;
+import com.mojang.brigadier.arguments.ArgumentType;
 import com.mojang.brigadier.builder.LiteralArgumentBuilder;
 import net.minecraft.command.CommandSource;
 import net.minecraft.command.Commands;
@@ -17,11 +19,18 @@ public class ConfigCommand {
     private static final String SAVE = "save";
     private static final String SHOW_GUI = "showGui";
     private static final String VALUE = "value";
+    private static final String RESET = "reset";
 
     public static void register(CommandDispatcher<ISuggestionProvider> commandDispatcher) {
         LiteralArgumentBuilder command = Commands.literal(COMMAND)
                 .then(buildCommands(GET, ConfigCommand::getCommandForSetting))
                 .then(buildCommands(ArgumentNames.SET, ConfigCommand::setCommandForSetting))
+                .then(buildCommands(RESET, ConfigCommand::resetCommandForSetting)
+                        .executes(context1 -> {
+                            ConfigManager.getSettings().forEach(Setting::reset);
+                            ConfigManager.saveConfig();
+                            return 0;
+                        }))
                 .then(Commands.literal(SAVE)
                         .executes(context -> {
                             ConfigManager.saveConfig();
@@ -36,11 +45,19 @@ public class ConfigCommand {
         commandDispatcher.register(command);
     }
 
+    private static LiteralArgumentBuilder<CommandSource> resetCommandForSetting(Setting<?> setting) {
+        return Commands.literal(setting.getName())
+                .executes(context -> {
+                    setting.reset();
+                    ConfigManager.saveConfig();
+                    return 0;
+                });
+    }
+
     private interface CommandBuilder extends Function<Setting<?>, LiteralArgumentBuilder<CommandSource>> {
     }
 
-    private static LiteralArgumentBuilder<CommandSource> buildCommands(String commandName,
-                                                                       CommandBuilder commandBuilder) {
+    private static LiteralArgumentBuilder<CommandSource> buildCommands(String commandName, CommandBuilder commandBuilder) {
         LiteralArgumentBuilder<CommandSource> command = Commands.literal(commandName);
         for (Setting<?> setting : ConfigManager.getSettings()) {
             command.then(commandBuilder.apply(setting));
@@ -60,20 +77,30 @@ public class ConfigCommand {
         LiteralArgumentBuilder<CommandSource> command = Commands.literal(setting.getName());
         switch (setting.getType()) {
             case 'B':
-                return command.then(Commands.argument(VALUE, Arguments.bool())
-                        .executes(context -> {
-                            boolean value = Arguments.getBool(context, VALUE);
-                            ((Setting<Boolean>) setting).set(value);
-                            return 0;
-                        }));
+                buildSetSettingCommand(command, (Setting<Boolean>) setting, Arguments.bool(), Boolean.class);
+                break;
             case 'I':
-                return command.then(Commands.argument(VALUE, Arguments.integer())
-                        .executes(context -> {
-                            int value = Arguments.getInteger(context, VALUE);
-                            ((Setting<Integer>) setting).set(value);
-                            return 0;
-                        }));
+                buildSetSettingCommand(command, (Setting<Integer>) setting, Arguments.integer(), Integer.class);
+                break;
+            case 'S':
+                buildSetSettingCommand(command, (Setting<String>) setting, Arguments.string(), String.class);
+                break;
         }
         return command;
     }
+
+    private static <T> void buildSetSettingCommand(LiteralArgumentBuilder<CommandSource> command,
+                                                   Setting<T> setting, ArgumentType<T> argument, Class<T> clazz) {
+        Command<CommandSource> setSettingCommand = context -> {
+            setting.set(context.getArgument(VALUE, clazz));
+            if (CommandHelper.lastNodeIsLiteral(context, SAVE)) {
+                ConfigManager.saveConfig();
+            }
+            return 0;
+        };
+        command.then(Commands.argument(VALUE, argument)
+                .executes(setSettingCommand)
+                .then(Commands.literal(SAVE)
+                        .executes(setSettingCommand)));
+    }
 }
diff --git a/src/main/java/com/irtimaled/bbor/client/config/AbstractSetting.java b/src/main/java/com/irtimaled/bbor/client/config/AbstractSetting.java
deleted file mode 100644 (file)
index 0d64876..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-package com.irtimaled.bbor.client.config;
-
-public abstract class AbstractSetting {
-    String comment;
-    String category;
-    String name;
-
-    private final char type;
-
-    AbstractSetting(char type) {
-        this.type = type;
-    }
-
-    public char getType() {
-        return type;
-    }
-
-    public String getName() { return name; }
-
-    abstract Object getValue();
-}
index 5742a3a80432fcf743e10231218fa1eeb916fa4b..bb0622a22cf3e46cc8397f9d138b02cc67afae9a 100644 (file)
@@ -146,6 +146,9 @@ public class ConfigManager {
         Setting<T> setting = config.get(category, settingName, defaultValue);
         setting.category = category;
         setting.name = settingName;
+        setting.defaultValue = defaultValue;
+        if (setting.get() == null)
+            setting.reset();
         setting.comment = comment + " (default: " + defaultValue.toString() + ")";
         settings.add(setting);
         return setting;
index 65f9a0d246263a6abb7fc0452d1da21f4bd55f07..432a076d446ca106427015a7a9969a704bd34616 100644 (file)
@@ -30,7 +30,7 @@ class Configuration {
                     first = false;
                     Setting setting = settings.get(settingName);
                     writer.write(String.format("    # %s\n", setting.comment));
-                    writer.write(String.format("    %s:%s=%s\n", setting.getType(), settingName, setting.getValue()));
+                    writer.write(String.format("    %s:%s=%s\n", setting.getType(), settingName, setting.get()));
                 }
                 writer.write("}\n");
             }
index f07f5fb191ce216d69b5bf7102c69e431c37b9ca..1c3a2234fc551b0517fbb26e834a788fc72832e5 100644 (file)
@@ -1,10 +1,15 @@
 package com.irtimaled.bbor.client.config;
 
-public class Setting<T> extends AbstractSetting {
+public class Setting<T> {
+    private final char type;
+    String comment;
+    String category;
+    String name;
     private T value;
+    T defaultValue;
 
     Setting(char type, T value) {
-        super(type);
+        this.type = type;
         this.value = value;
     }
 
@@ -16,8 +21,13 @@ public class Setting<T> extends AbstractSetting {
         this.value = value;
     }
 
-    @Override
-    Object getValue() {
-        return value;
+    public void reset() {
+        this.value = this.defaultValue;
+    }
+
+    public char getType() {
+        return type;
     }
+
+    public String getName() { return name; }
 }