|
| 1 | +package dev.xpple.seedmapper.command.arguments; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableMap; |
| 4 | +import com.mojang.brigadier.StringReader; |
| 5 | +import com.mojang.brigadier.arguments.ArgumentType; |
| 6 | +import com.mojang.brigadier.arguments.LongArgumentType; |
| 7 | +import com.mojang.brigadier.context.CommandContext; |
| 8 | +import com.mojang.brigadier.exceptions.CommandSyntaxException; |
| 9 | +import com.mojang.brigadier.exceptions.DynamicCommandExceptionType; |
| 10 | +import com.mojang.brigadier.suggestion.Suggestions; |
| 11 | +import com.mojang.brigadier.suggestion.SuggestionsBuilder; |
| 12 | +import dev.xpple.betterconfig.util.CheckedFunction; |
| 13 | +import dev.xpple.seedmapper.util.SeedIdentifier; |
| 14 | +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; |
| 15 | +import net.minecraft.commands.SharedSuggestionProvider; |
| 16 | +import net.minecraft.network.chat.Component; |
| 17 | + |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.HashSet; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Set; |
| 23 | +import java.util.concurrent.CompletableFuture; |
| 24 | +import java.util.function.Consumer; |
| 25 | + |
| 26 | +public class SeedIdentifierArgument implements ArgumentType<SeedIdentifier> { |
| 27 | + |
| 28 | + private static final Collection<String> EXAMPLES = List.of("8052710360952744907", "-123 --version 1.17.1", "-123 --generatorFlags large_biomes --version 1.21.11"); |
| 29 | + |
| 30 | + private static final DynamicCommandExceptionType UNKNOWN_SEED_ARGUMENT_EXCEPTION = new DynamicCommandExceptionType(arg -> Component.translatable("commands.exceptions.unknownSeedArgument", arg)); |
| 31 | + |
| 32 | + public static SeedIdentifierArgument seedIdentifier() { |
| 33 | + return new SeedIdentifierArgument(); |
| 34 | + } |
| 35 | + |
| 36 | + public SeedIdentifier getSeedIdentifier(CommandContext<FabricClientCommandSource> context, String name) { |
| 37 | + return context.getArgument(name, SeedIdentifier.class); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public SeedIdentifier parse(StringReader reader) throws CommandSyntaxException { |
| 42 | + return new Parser(reader).parse(); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) { |
| 47 | + StringReader reader = new StringReader(builder.getInput()); |
| 48 | + reader.setCursor(builder.getStart()); |
| 49 | + |
| 50 | + Parser parser = new Parser(reader); |
| 51 | + |
| 52 | + try { |
| 53 | + parser.parse(); |
| 54 | + } catch (CommandSyntaxException ignored) { |
| 55 | + } |
| 56 | + |
| 57 | + if (parser.suggester != null) { |
| 58 | + parser.suggester.accept(builder); |
| 59 | + } |
| 60 | + |
| 61 | + return builder.buildFuture(); |
| 62 | + } |
| 63 | + |
| 64 | + private static class Parser { |
| 65 | + private final StringReader reader; |
| 66 | + private Consumer<SuggestionsBuilder> suggester; |
| 67 | + |
| 68 | + private final Map<String, CheckedFunction<SeedIdentifier, SeedIdentifier, CommandSyntaxException>> ARGUMENTS = ImmutableMap.<String, CheckedFunction<SeedIdentifier, SeedIdentifier, CommandSyntaxException>>builder() |
| 69 | + .put("--version", this::parseVersion) |
| 70 | + .put("--generatorFlag", this::parseGeneratorFlag) |
| 71 | + .build(); |
| 72 | + |
| 73 | + private Parser(StringReader reader) { |
| 74 | + this.reader = reader; |
| 75 | + } |
| 76 | + |
| 77 | + private SeedIdentifier parse() throws CommandSyntaxException { |
| 78 | + long seed = LongArgumentType.longArg().parse(this.reader); |
| 79 | + SeedIdentifier identifier = new SeedIdentifier(seed); |
| 80 | + |
| 81 | + if (!this.reader.canRead()) { |
| 82 | + return identifier; |
| 83 | + } |
| 84 | + |
| 85 | + this.reader.expect(' '); |
| 86 | + this.reader.skipWhitespace(); |
| 87 | + |
| 88 | + Set<String> remainingArguments = new HashSet<>(ARGUMENTS.keySet()); |
| 89 | + while (!remainingArguments.isEmpty()) { |
| 90 | + int cursor = this.reader.getCursor(); |
| 91 | + this.suggester = suggestions -> { |
| 92 | + SuggestionsBuilder builder = suggestions.createOffset(cursor); |
| 93 | + SharedSuggestionProvider.suggest(remainingArguments, builder); |
| 94 | + suggestions.add(builder); |
| 95 | + }; |
| 96 | + if (!this.reader.canRead()) { |
| 97 | + break; |
| 98 | + } |
| 99 | + String argumentString = this.reader.readUnquotedString(); |
| 100 | + CheckedFunction<SeedIdentifier, SeedIdentifier, CommandSyntaxException> parser = ARGUMENTS.get(argumentString); |
| 101 | + if (parser == null) { |
| 102 | + this.reader.setCursor(cursor); |
| 103 | + throw UNKNOWN_SEED_ARGUMENT_EXCEPTION.create(argumentString); |
| 104 | + } |
| 105 | + this.reader.expect(' '); |
| 106 | + this.reader.skipWhitespace(); |
| 107 | + identifier = parser.apply(identifier); |
| 108 | + if (!argumentString.equals("--generatorFlag")) { // allow multiple flags |
| 109 | + remainingArguments.remove(argumentString); |
| 110 | + } |
| 111 | + if (this.reader.canRead()) { |
| 112 | + this.reader.expect(' '); |
| 113 | + this.reader.skipWhitespace(); |
| 114 | + } else { |
| 115 | + break; |
| 116 | + } |
| 117 | + } |
| 118 | + return identifier; |
| 119 | + } |
| 120 | + |
| 121 | + private SeedIdentifier parseVersion(SeedIdentifier seed) throws CommandSyntaxException { |
| 122 | + int cursor = this.reader.getCursor(); |
| 123 | + this.suggester = builder -> { |
| 124 | + SuggestionsBuilder newBuilder = builder.createOffset(cursor); |
| 125 | + SharedSuggestionProvider.suggest(VersionArgument.VERSIONS.keySet(), newBuilder); |
| 126 | + builder.add(newBuilder); |
| 127 | + }; |
| 128 | + int version = VersionArgument.version().parse(this.reader); |
| 129 | + return seed.withVersion(version); |
| 130 | + } |
| 131 | + |
| 132 | + private SeedIdentifier parseGeneratorFlag(SeedIdentifier identifier) throws CommandSyntaxException { |
| 133 | + int cursor = this.reader.getCursor(); |
| 134 | + this.suggester = builder -> { |
| 135 | + SuggestionsBuilder newBuilder = builder.createOffset(cursor); |
| 136 | + SharedSuggestionProvider.suggest(GeneratorFlagArgument.GENERATOR_FLAGS.keySet(), newBuilder); |
| 137 | + builder.add(newBuilder); |
| 138 | + }; |
| 139 | + int generatorFlag = GeneratorFlagArgument.generatorFlag().parse(this.reader); |
| 140 | + return identifier.withGeneratorFlag(generatorFlag); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public Collection<String> getExamples() { |
| 146 | + return EXAMPLES; |
| 147 | + } |
| 148 | +} |
0 commit comments