fix parser string

This commit is contained in:
Darto KLoning
2026-09-06 16:19:01 +07:00
parent 5358bf9a8e
commit c8c8d1891b
4 changed files with 55 additions and 18 deletions
@@ -0,0 +1,35 @@
package id.dartokloning.lobbysystems;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
/**
* Brigadier argument type for Minecraft resource/dimension identifiers such as
* {@code minecraft:lobby}. Vanilla Brigadier's word() does not accept ':',
* while greedyString() consumes following arguments. This parser reads exactly
* one whitespace-delimited token and therefore works correctly in the middle
* of commands such as /ls gamemode set <dimension> <mode>.
*/
public final class DimensionIdArgumentType implements ArgumentType<String> {
private DimensionIdArgumentType() {
}
public static DimensionIdArgumentType dimensionId() {
return new DimensionIdArgumentType();
}
public static String getDimensionId(CommandContext<?> context, String name) {
return context.getArgument(name, String.class);
}
@Override
public String parse(StringReader reader) throws CommandSyntaxException {
int start = reader.getCursor();
while (reader.canRead() && !Character.isWhitespace(reader.peek())) {
reader.skip();
}
return reader.getString().substring(start, reader.getCursor());
}
}
@@ -148,26 +148,26 @@ public final class LobbySystemsMod implements ModInitializer {
true,
true))))
.then(Commands.literal("gamerule")
.then(Commands.argument("dimension", StringArgumentType.word())
.then(Commands.argument("dimension", DimensionIdArgumentType.dimensionId())
.suggests((ctx, builder) -> SharedSuggestionProvider.suggestResource(
ctx.getSource().getServer().levelKeys().stream().map(ResourceKey::identifier), builder))
.executes(ctx -> listGameRules(
ctx.getSource(),
ctx.getSource().getServer(),
StringArgumentType.getString(ctx, "dimension"),
DimensionIdArgumentType.getDimensionId(ctx, "dimension"),
1))
.then(Commands.literal("--page")
.then(Commands.argument("page", IntegerArgumentType.integer(1))
.executes(ctx -> listGameRules(
ctx.getSource(),
ctx.getSource().getServer(),
StringArgumentType.getString(ctx, "dimension"),
DimensionIdArgumentType.getDimensionId(ctx, "dimension"),
IntegerArgumentType.getInteger(ctx, "page")))))))
.then(Commands.literal("gamemode")
.then(Commands.literal("list")
.executes(ctx -> listDimensionGameModes(ctx.getSource())))
.then(Commands.literal("set")
.then(Commands.argument("dimension", StringArgumentType.word())
.then(Commands.argument("dimension", DimensionIdArgumentType.dimensionId())
.suggests((ctx, builder) -> SharedSuggestionProvider.suggestResource(
ctx.getSource().getServer().levelKeys().stream().map(ResourceKey::identifier), builder))
.then(Commands.argument("mode", StringArgumentType.word())
@@ -176,21 +176,21 @@ public final class LobbySystemsMod implements ModInitializer {
.executes(ctx -> setDimensionGameMode(
ctx.getSource(),
ctx.getSource().getServer(),
StringArgumentType.getString(ctx, "dimension"),
DimensionIdArgumentType.getDimensionId(ctx, "dimension"),
StringArgumentType.getString(ctx, "mode"))))))
.then(Commands.literal("remove")
.then(Commands.argument("dimension", StringArgumentType.word())
.then(Commands.argument("dimension", DimensionIdArgumentType.dimensionId())
.suggests((ctx, builder) -> SharedSuggestionProvider.suggestResource(
ctx.getSource().getServer().levelKeys().stream().map(ResourceKey::identifier), builder))
.executes(ctx -> removeDimensionGameMode(
ctx.getSource(),
StringArgumentType.getString(ctx, "dimension")))))
.then(Commands.argument("dimension", StringArgumentType.word())
DimensionIdArgumentType.getDimensionId(ctx, "dimension")))))
.then(Commands.argument("dimension", DimensionIdArgumentType.dimensionId())
.suggests((ctx, builder) -> SharedSuggestionProvider.suggestResource(
ctx.getSource().getServer().levelKeys().stream().map(ResourceKey::identifier), builder))
.executes(ctx -> showDimensionGameMode(
ctx.getSource(),
StringArgumentType.getString(ctx, "dimension")))))
DimensionIdArgumentType.getDimensionId(ctx, "dimension")))))
.then(Commands.literal("portal")
.then(Commands.literal("create")
.then(Commands.argument("name", StringArgumentType.word())