Skip to content

Commit

Permalink
Game rule command now tells you when the value is wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro270707 committed Jan 16, 2024
1 parent 19f35f4 commit 4f659d9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package net.pedroricardo.commander.content.arguments;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.core.data.gamerule.GameRule;
import net.pedroricardo.commander.content.exceptions.CommanderExceptions;

public class GenericGameRuleArgumentType implements ArgumentType<Object> {
private final GameRule<?> gameRule;

private GenericGameRuleArgumentType(GameRule<?> gameRule) {
this.gameRule = gameRule;
}

public static GenericGameRuleArgumentType gameRule(GameRule<?> gameRule) {
return new GenericGameRuleArgumentType(gameRule);
}

@Override
public Object parse(StringReader reader) throws CommandSyntaxException {
StringBuilder read = new StringBuilder();
Object value = null;
while (reader.canRead() && (StringReader.isQuotedStringStart(reader.peek()) || StringReader.isAllowedInUnquotedString(reader.peek())) && value == null) {
read.append(reader.readString());
value = this.gameRule.parseFromString(read.toString());
}
System.out.println(read);
if (value != null) return value;
throw CommanderExceptions.invalidGameRuleValue().create();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.minecraft.core.data.gamerule.GameRuleBoolean;
import net.minecraft.core.data.registry.Registries;
import net.pedroricardo.commander.content.CommanderCommandSource;
import net.pedroricardo.commander.content.arguments.GenericGameRuleArgumentType;
import net.pedroricardo.commander.content.exceptions.CommanderExceptions;

@SuppressWarnings("unchecked")
Expand All @@ -27,9 +28,9 @@ public static void register(CommandDispatcher<CommanderCommandSource> dispatcher
return Command.SINGLE_SUCCESS;
});
} else {
gameRuleValueArgument = RequiredArgumentBuilder.<CommanderCommandSource, String>argument("value", StringArgumentType.greedyString())
gameRuleValueArgument = RequiredArgumentBuilder.<CommanderCommandSource, Object>argument("value", GenericGameRuleArgumentType.gameRule(gameRule))
.executes(c -> {
Object o = gameRule.parseFromString(StringArgumentType.getString(c, "value"));
Object o = c.getArgument("value", Object.class);
if (o == null) throw CommanderExceptions.invalidGameRuleValue().create();
c.getSource().getWorld().getLevelData().getGameRules().setValue((GameRule<? super Object>) gameRule, o);
c.getSource().sendTranslatableMessage("commands.commander.gamerule.set", gameRule.getKey(), o);
Expand Down

0 comments on commit 4f659d9

Please sign in to comment.