package net.minecraft.commands.arguments; import com.google.gson.JsonObject; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.arguments.ArgumentType; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType; import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; import it.unimi.dsi.fastutil.objects.Object2IntMap; import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; import java.util.Arrays; import java.util.Collection; import java.util.concurrent.CompletableFuture; import net.minecraft.commands.SharedSuggestionProvider; import net.minecraft.commands.synchronization.ArgumentTypeInfo; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.chat.Component; public class TimeArgument implements ArgumentType { private static final Collection EXAMPLES = Arrays.asList("0d", "0s", "0t", "0"); private static final SimpleCommandExceptionType ERROR_INVALID_UNIT = new SimpleCommandExceptionType(Component.translatable("argument.time.invalid_unit")); private static final Dynamic2CommandExceptionType ERROR_TICK_COUNT_TOO_LOW = new Dynamic2CommandExceptionType( (object, object2) -> Component.translatableEscape("argument.time.tick_count_too_low", object2, object) ); private static final Object2IntMap UNITS = new Object2IntOpenHashMap<>(); final int minimum; private TimeArgument(int minimum) { this.minimum = minimum; } public static TimeArgument time() { return new TimeArgument(0); } public static TimeArgument time(int minimum) { return new TimeArgument(minimum); } public Integer parse(StringReader reader) throws CommandSyntaxException { float f = reader.readFloat(); String string = reader.readUnquotedString(); int i = UNITS.getOrDefault(string, 0); if (i == 0) { throw ERROR_INVALID_UNIT.createWithContext(reader); } else { int j = Math.round(f * i); if (j < this.minimum) { throw ERROR_TICK_COUNT_TOO_LOW.createWithContext(reader, j, this.minimum); } else { return j; } } } @Override public CompletableFuture listSuggestions(CommandContext commandContext, SuggestionsBuilder suggestionsBuilder) { StringReader stringReader = new StringReader(suggestionsBuilder.getRemaining()); try { stringReader.readFloat(); } catch (CommandSyntaxException var5) { return suggestionsBuilder.buildFuture(); } return SharedSuggestionProvider.suggest(UNITS.keySet(), suggestionsBuilder.createOffset(suggestionsBuilder.getStart() + stringReader.getCursor())); } @Override public Collection getExamples() { return EXAMPLES; } static { UNITS.put("d", 24000); UNITS.put("s", 20); UNITS.put("t", 1); UNITS.put("", 1); } public static class Info implements ArgumentTypeInfo { public void serializeToNetwork(net.minecraft.commands.arguments.TimeArgument.Info.Template template, FriendlyByteBuf friendlyByteBuf) { friendlyByteBuf.writeInt(template.min); } public net.minecraft.commands.arguments.TimeArgument.Info.Template deserializeFromNetwork(FriendlyByteBuf friendlyByteBuf) { int i = friendlyByteBuf.readInt(); return new net.minecraft.commands.arguments.TimeArgument.Info.Template(this, i); } public void serializeToJson(net.minecraft.commands.arguments.TimeArgument.Info.Template template, JsonObject jsonObject) { jsonObject.addProperty("min", template.min); } public net.minecraft.commands.arguments.TimeArgument.Info.Template unpack(TimeArgument timeArgument) { return new net.minecraft.commands.arguments.TimeArgument.Info.Template(this, timeArgument.minimum); } } }