]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/commands/ConfigCommand.java
Add config command to allow easier config editing
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / commands / ConfigCommand.java
1 package com.irtimaled.bbor.client.commands;
2
3 import com.irtimaled.bbor.client.gui.SettingsScreen;
4 import com.irtimaled.bbor.config.ConfigManager;
5 import com.irtimaled.bbor.config.Setting;
6 import com.mojang.brigadier.CommandDispatcher;
7 import com.mojang.brigadier.arguments.BoolArgumentType;
8 import com.mojang.brigadier.arguments.IntegerArgumentType;
9 import com.mojang.brigadier.builder.LiteralArgumentBuilder;
10 import net.minecraft.command.CommandSource;
11 import net.minecraft.command.Commands;
12 import net.minecraft.command.ISuggestionProvider;
13
14 import java.util.function.Function;
15
16 public class ConfigCommand {
17     private static final String COMMAND = "bbor:config";
18     private static final String GET = "get";
19     private static final String SET = "set";
20     private static final String SAVE = "save";
21     private static final String SHOW_GUI = "showGui";
22     private static final String VALUE = "value";
23
24     public static void register(CommandDispatcher<ISuggestionProvider> commandDispatcher) {
25         LiteralArgumentBuilder command = Commands.literal(COMMAND)
26                 .then(buildCommands(GET, ConfigCommand::getCommandForSetting))
27                 .then(buildCommands(SET, ConfigCommand::setCommandForSetting))
28                 .then(Commands.literal(SAVE)
29                         .executes(context -> {
30                             ConfigManager.saveConfig();
31                             return 0;
32                         }))
33                 .then(Commands.literal(SHOW_GUI)
34                         .executes(context -> {
35                             SettingsScreen.show();
36                             return 0;
37                         }));
38
39         commandDispatcher.register(command);
40     }
41
42     private interface CommandBuilder extends Function<Setting<?>, LiteralArgumentBuilder<CommandSource>> {
43     }
44
45     private static LiteralArgumentBuilder<CommandSource> buildCommands(String commandName,
46                                                                        CommandBuilder commandBuilder) {
47         LiteralArgumentBuilder<CommandSource> command = Commands.literal(commandName);
48         for (Setting<?> setting : ConfigManager.getSettings()) {
49             command.then(commandBuilder.apply(setting));
50         }
51         return command;
52     }
53
54     private static LiteralArgumentBuilder<CommandSource> getCommandForSetting(Setting<?> setting) {
55         return Commands.literal(setting.getName())
56                 .executes(context -> {
57                     CommandHelper.feedback(context, "%s: %s", setting.getName(), setting.get());
58                     return 0;
59                 });
60     }
61
62     private static LiteralArgumentBuilder<CommandSource> setCommandForSetting(Setting<?> setting) {
63         LiteralArgumentBuilder<CommandSource> command = Commands.literal(setting.getName());
64         switch (setting.getType()) {
65             case 'B':
66                 return command.then(Commands.argument(VALUE, BoolArgumentType.bool())
67                         .executes(context -> {
68                             boolean value = BoolArgumentType.getBool(context, VALUE);
69                             ((Setting<Boolean>) setting).set(value);
70                             return 0;
71                         }));
72             case 'I':
73                 return command.then(Commands.argument(VALUE, IntegerArgumentType.integer())
74                         .executes(context -> {
75                             int value = IntegerArgumentType.getInteger(context, VALUE);
76                             ((Setting<Integer>) setting).set(value);
77                             return 0;
78                         }));
79         }
80         return command;
81     }
82 }