}
public static StringArgumentType string() {
- return StringArgumentType.greedyString();
+ return StringArgumentType.string();
}
public static BoolArgumentType bool() {
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) {
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;
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];
+ }
}
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;
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();
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));
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)));
+ }
}