| 1 | package net.minecraft.commands.arguments;
|
| 2 |
|
| 3 | import com.google.gson.JsonObject;
|
| 4 | import com.mojang.brigadier.StringReader;
|
| 5 | import com.mojang.brigadier.arguments.ArgumentType;
|
| 6 | import com.mojang.brigadier.context.CommandContext;
|
| 7 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 8 | import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType;
|
| 9 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 10 | import com.mojang.brigadier.suggestion.Suggestions;
|
| 11 | import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
| 12 | import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
| 13 | import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
| 14 | import java.util.Arrays;
|
| 15 | import java.util.Collection;
|
| 16 | import java.util.concurrent.CompletableFuture;
|
| 17 | import net.minecraft.commands.CommandBuildContext;
|
| 18 | import net.minecraft.commands.SharedSuggestionProvider;
|
| 19 | import net.minecraft.commands.synchronization.ArgumentTypeInfo;
|
| 20 | import net.minecraft.network.FriendlyByteBuf;
|
| 21 | import net.minecraft.network.chat.Component;
|
| 22 |
|
| 23 | public class TimeArgument implements ArgumentType<Integer> {
|
| 24 | private static final Collection<String> EXAMPLES = Arrays.asList("0d", "0s", "0t", "0");
|
| 25 | private static final SimpleCommandExceptionType ERROR_INVALID_UNIT = new SimpleCommandExceptionType(Component.translatable("argument.time.invalid_unit"));
|
| 26 | private static final Dynamic2CommandExceptionType ERROR_TICK_COUNT_TOO_LOW = new Dynamic2CommandExceptionType(
|
| 27 | (value, limit) -> Component.translatableEscape("argument.time.tick_count_too_low", limit, value)
|
| 28 | );
|
| 29 | private static final Object2IntMap<String> UNITS = new Object2IntOpenHashMap<>();
|
| 30 | private final int minimum;
|
| 31 |
|
| 32 | private TimeArgument(final int minimum) {
|
| 33 | this.minimum = minimum;
|
| 34 | }
|
| 35 |
|
| 36 | public static TimeArgument time() {
|
| 37 | return new TimeArgument(0);
|
| 38 | }
|
| 39 |
|
| 40 | public static TimeArgument time(final int minimum) {
|
| 41 | return new TimeArgument(minimum);
|
| 42 | }
|
| 43 |
|
| 44 | public Integer parse(final StringReader reader) throws CommandSyntaxException {
|
| 45 | float value = reader.readFloat();
|
| 46 | String unit = reader.readUnquotedString();
|
| 47 | int factor = UNITS.getOrDefault(unit, 0);
|
| 48 | if (factor == 0) {
|
| 49 | throw ERROR_INVALID_UNIT.createWithContext(reader);
|
| 50 | } else {
|
| 51 | int ticks = Math.round(value * factor);
|
| 52 | if (ticks < this.minimum) {
|
| 53 | throw ERROR_TICK_COUNT_TOO_LOW.createWithContext(reader, ticks, this.minimum);
|
| 54 | } else {
|
| 55 | return ticks;
|
| 56 | }
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
|
| 62 | StringReader reader = new StringReader(builder.getRemaining());
|
| 63 |
|
| 64 | try {
|
| 65 | reader.readFloat();
|
| 66 | } catch (CommandSyntaxException ignored) {
|
| 67 | return builder.buildFuture();
|
| 68 | }
|
| 69 |
|
| 70 | return SharedSuggestionProvider.suggest(UNITS.keySet(), builder.createOffset(builder.getStart() + reader.getCursor()));
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public Collection<String> getExamples() {
|
| 75 | return EXAMPLES;
|
| 76 | }
|
| 77 |
|
| 78 | static {
|
| 79 | UNITS.put("d", 24000);
|
| 80 | UNITS.put("s", 20);
|
| 81 | UNITS.put("t", 1);
|
| 82 | UNITS.put("", 1);
|
| 83 | }
|
| 84 |
|
| 85 | public static class Info implements ArgumentTypeInfo<TimeArgument, TimeArgument.Info.Template> {
|
| 86 | public void serializeToNetwork(final TimeArgument.Info.Template template, final FriendlyByteBuf out) {
|
| 87 | out.writeInt(template.min);
|
| 88 | }
|
| 89 |
|
| 90 | public TimeArgument.Info.Template deserializeFromNetwork(final FriendlyByteBuf in) {
|
| 91 | int min = in.readInt();
|
| 92 | return new TimeArgument.Info.Template(min);
|
| 93 | }
|
| 94 |
|
| 95 | public void serializeToJson(final TimeArgument.Info.Template template, final JsonObject out) {
|
| 96 | out.addProperty("min", template.min);
|
| 97 | }
|
| 98 |
|
| 99 | public TimeArgument.Info.Template unpack(final TimeArgument argument) {
|
| 100 | return new TimeArgument.Info.Template(argument.minimum);
|
| 101 | }
|
| 102 |
|
| 103 | public final class Template implements ArgumentTypeInfo.Template<TimeArgument> {
|
| 104 | private final int min;
|
| 105 |
|
| 106 | private Template(final int min) {
|
| 107 | this.min = min;
|
| 108 | }
|
| 109 |
|
| 110 | public TimeArgument instantiate(final CommandBuildContext context) {
|
| 111 | return TimeArgument.time(this.min);
|
| 112 | }
|
| 113 |
|
| 114 | @Override
|
| 115 | public ArgumentTypeInfo<TimeArgument, ?> type() {
|
| 116 | return Info.this;
|
| 117 | }
|
| 118 | }
|
| 119 | }
|
| 120 | }
|
| 121 |
|