|
| 1 | +package dev.xpple.seedmapper.command.arguments; |
| 2 | + |
| 3 | +import com.mojang.brigadier.StringReader; |
| 4 | +import com.mojang.brigadier.arguments.ArgumentType; |
| 5 | +import com.mojang.brigadier.context.CommandContext; |
| 6 | +import com.mojang.brigadier.exceptions.CommandSyntaxException; |
| 7 | +import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; |
| 8 | +import com.mojang.brigadier.suggestion.Suggestions; |
| 9 | +import com.mojang.brigadier.suggestion.SuggestionsBuilder; |
| 10 | +import dev.xpple.seedmapper.command.CustomClientCommandSource; |
| 11 | +import it.unimi.dsi.fastutil.objects.Object2IntMap; |
| 12 | +import it.unimi.dsi.fastutil.objects.Object2IntMaps; |
| 13 | +import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; |
| 14 | +import net.minecraft.commands.SharedSuggestionProvider; |
| 15 | +import net.minecraft.network.chat.Component; |
| 16 | +import net.minecraft.util.TimeUtil; |
| 17 | +import net.minecraft.util.Util; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.List; |
| 22 | +import java.util.concurrent.CompletableFuture; |
| 23 | +import java.util.function.Consumer; |
| 24 | + |
| 25 | +public class DurationArgument implements ArgumentType<Duration> { |
| 26 | + |
| 27 | + private static final Collection<String> EXAMPLES = List.of("5s", "10m", "3h"); |
| 28 | + |
| 29 | + private static final SimpleCommandExceptionType INVALID_UNIT_EXCEPTION = new SimpleCommandExceptionType(Component.translatable("argument.time.invalid_unit")); |
| 30 | + |
| 31 | + public static DurationArgument duration() { |
| 32 | + return new DurationArgument(); |
| 33 | + } |
| 34 | + |
| 35 | + public static DurationArgument getDuration(CommandContext<CustomClientCommandSource> context, String name) { |
| 36 | + return context.getArgument(name, DurationArgument.class); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public Duration parse(StringReader reader) throws CommandSyntaxException { |
| 41 | + return new Parser(reader).parse(); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) { |
| 46 | + StringReader reader = new StringReader(builder.getInput()); |
| 47 | + reader.setCursor(builder.getStart()); |
| 48 | + |
| 49 | + Parser parser = new Parser(reader); |
| 50 | + |
| 51 | + try { |
| 52 | + parser.parse(); |
| 53 | + } catch (CommandSyntaxException ignored) { |
| 54 | + } |
| 55 | + |
| 56 | + if (parser.suggester != null) { |
| 57 | + parser.suggester.accept(builder); |
| 58 | + } |
| 59 | + |
| 60 | + return builder.buildFuture(); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Collection<String> getExamples() { |
| 65 | + return EXAMPLES; |
| 66 | + } |
| 67 | + |
| 68 | + private static final class Parser { |
| 69 | + |
| 70 | + private static final Object2IntMap<String> UNITS = Util.make(() -> { |
| 71 | + Object2IntMap<String> temp = new Object2IntOpenHashMap<>(); |
| 72 | + |
| 73 | + temp.put("s", 1); |
| 74 | + temp.put("m", TimeUtil.SECONDS_PER_MINUTE); |
| 75 | + temp.put("h", (int) TimeUtil.SECONDS_PER_HOUR); |
| 76 | + |
| 77 | + return Object2IntMaps.unmodifiable(temp); |
| 78 | + }); |
| 79 | + |
| 80 | + private final StringReader reader; |
| 81 | + private Consumer<SuggestionsBuilder> suggester; |
| 82 | + |
| 83 | + private Parser(StringReader reader) { |
| 84 | + this.reader = reader; |
| 85 | + } |
| 86 | + |
| 87 | + private Duration parse() throws CommandSyntaxException { |
| 88 | + float durationRaw = reader.readFloat(); |
| 89 | + int cursor = reader.getCursor(); |
| 90 | + suggester = builder -> { |
| 91 | + SuggestionsBuilder newBuilder = builder.createOffset(cursor); |
| 92 | + SharedSuggestionProvider.suggest(UNITS.keySet(), newBuilder); |
| 93 | + builder.add(newBuilder); |
| 94 | + }; |
| 95 | + String string = reader.readUnquotedString(); |
| 96 | + int unit = UNITS.getOrDefault(string, -1); |
| 97 | + if (unit == -1) { |
| 98 | + throw INVALID_UNIT_EXCEPTION.create(); |
| 99 | + } |
| 100 | + int durationSeconds = Math.round(durationRaw * unit); |
| 101 | + return Duration.ofSeconds(durationSeconds); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments