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