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.SimpleCommandExceptionType; import com.mojang.brigadier.suggestion.SuggestionProvider; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.UUID; import java.util.function.Supplier; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.SharedSuggestionProvider; import net.minecraft.commands.arguments.selector.EntitySelector; import net.minecraft.commands.arguments.selector.EntitySelectorParser; import net.minecraft.commands.synchronization.ArgumentTypeInfo; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.chat.Component; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.Entity; import net.minecraft.world.scores.ScoreHolder; public class ScoreHolderArgument implements ArgumentType { public static final SuggestionProvider SUGGEST_SCORE_HOLDERS = (commandContext, suggestionsBuilder) -> { StringReader stringReader = new StringReader(suggestionsBuilder.getInput()); stringReader.setCursor(suggestionsBuilder.getStart()); EntitySelectorParser entitySelectorParser = new EntitySelectorParser(stringReader, EntitySelectorParser.allowSelectors(commandContext.getSource())); try { entitySelectorParser.parse(); } catch (CommandSyntaxException var5) { } return entitySelectorParser.fillSuggestions( suggestionsBuilder, suggestionsBuilderx -> SharedSuggestionProvider.suggest(commandContext.getSource().getOnlinePlayerNames(), suggestionsBuilderx) ); }; private static final Collection EXAMPLES = Arrays.asList("Player", "0123", "*", "@e"); private static final SimpleCommandExceptionType ERROR_NO_RESULTS = new SimpleCommandExceptionType(Component.translatable("argument.scoreHolder.empty")); final boolean multiple; public ScoreHolderArgument(boolean multiple) { this.multiple = multiple; } public static ScoreHolder getName(CommandContext context, String name) throws CommandSyntaxException { return (ScoreHolder)getNames(context, name).iterator().next(); } /** * Gets one or more score holders, with no objectives list. */ public static Collection getNames(CommandContext context, String name) throws CommandSyntaxException { return getNames(context, name, Collections::emptyList); } /** * Gets one or more score holders, using the server's complete list of objectives. */ public static Collection getNamesWithDefaultWildcard(CommandContext context, String name) throws CommandSyntaxException { return getNames(context, name, context.getSource().getServer().getScoreboard()::getTrackedPlayers); } /** * Gets one or more score holders. */ public static Collection getNames(CommandContext context, String name, Supplier> objectives) throws CommandSyntaxException { Collection collection = context.getArgument(name, ScoreHolderArgument.Result.class) .getNames(context.getSource(), objectives); if (collection.isEmpty()) { throw EntityArgument.NO_ENTITIES_FOUND.create(); } else { return collection; } } public static ScoreHolderArgument scoreHolder() { return new ScoreHolderArgument(false); } public static ScoreHolderArgument scoreHolders() { return new ScoreHolderArgument(true); } public ScoreHolderArgument.Result parse(StringReader reader) throws CommandSyntaxException { return this.parse(reader, true); } public ScoreHolderArgument.Result parse(StringReader stringReader, S object) throws CommandSyntaxException { return this.parse(stringReader, EntitySelectorParser.allowSelectors(object)); } private ScoreHolderArgument.Result parse(StringReader reader, boolean allowSelectors) throws CommandSyntaxException { if (reader.canRead() && reader.peek() == '@') { EntitySelectorParser entitySelectorParser = new EntitySelectorParser(reader, allowSelectors); EntitySelector entitySelector = entitySelectorParser.parse(); if (!this.multiple && entitySelector.getMaxResults() > 1) { throw EntityArgument.ERROR_NOT_SINGLE_ENTITY.createWithContext(reader); } else { return new ScoreHolderArgument.SelectorResult(entitySelector); } } else { int i = reader.getCursor(); while (reader.canRead() && reader.peek() != ' ') { reader.skip(); } String string = reader.getString().substring(i, reader.getCursor()); if (string.equals("*")) { return (commandSourceStack, supplier) -> { Collection collection = (Collection)supplier.get(); if (collection.isEmpty()) { throw ERROR_NO_RESULTS.create(); } else { return collection; } }; } else { List list = List.of(ScoreHolder.forNameOnly(string)); if (string.startsWith("#")) { return (commandSourceStack, supplier) -> list; } else { try { UUID uUID = UUID.fromString(string); return (commandSourceStack, supplier) -> { MinecraftServer minecraftServer = commandSourceStack.getServer(); ScoreHolder scoreHolder = null; List list2 = null; for (ServerLevel serverLevel : minecraftServer.getAllLevels()) { Entity entity = serverLevel.getEntity(uUID); if (entity != null) { if (scoreHolder == null) { scoreHolder = entity; } else { if (list2 == null) { list2 = new ArrayList(); list2.add(scoreHolder); } list2.add(entity); } } } if (list2 != null) { return list2; } else { return scoreHolder != null ? List.of(scoreHolder) : list; } }; } catch (IllegalArgumentException var7) { return (commandSourceStack, supplier) -> { MinecraftServer minecraftServer = commandSourceStack.getServer(); ServerPlayer serverPlayer = minecraftServer.getPlayerList().getPlayerByName(string); return serverPlayer != null ? List.of(serverPlayer) : list; }; } } } } } @Override public Collection getExamples() { return EXAMPLES; } public static class Info implements ArgumentTypeInfo { private static final byte FLAG_MULTIPLE = 1; public void serializeToNetwork(net.minecraft.commands.arguments.ScoreHolderArgument.Info.Template template, FriendlyByteBuf friendlyByteBuf) { int i = 0; if (template.multiple) { i |= 1; } friendlyByteBuf.writeByte(i); } public net.minecraft.commands.arguments.ScoreHolderArgument.Info.Template deserializeFromNetwork(FriendlyByteBuf friendlyByteBuf) { byte b = friendlyByteBuf.readByte(); boolean bl = (b & 1) != 0; return new net.minecraft.commands.arguments.ScoreHolderArgument.Info.Template(this, bl); } public void serializeToJson(net.minecraft.commands.arguments.ScoreHolderArgument.Info.Template template, JsonObject jsonObject) { jsonObject.addProperty("amount", template.multiple ? "multiple" : "single"); } public net.minecraft.commands.arguments.ScoreHolderArgument.Info.Template unpack(ScoreHolderArgument scoreHolderArgument) { return new net.minecraft.commands.arguments.ScoreHolderArgument.Info.Template(this, scoreHolderArgument.multiple); } } @FunctionalInterface public interface Result { Collection getNames(CommandSourceStack commandSourceStack, Supplier> supplier) throws CommandSyntaxException; } public static class SelectorResult implements ScoreHolderArgument.Result { private final EntitySelector selector; public SelectorResult(EntitySelector selector) { this.selector = selector; } @Override public Collection getNames(CommandSourceStack commandSourceStack, Supplier> supplier) throws CommandSyntaxException { List list = this.selector.findEntities(commandSourceStack); if (list.isEmpty()) { throw EntityArgument.NO_ENTITIES_FOUND.create(); } else { return List.copyOf(list); } } } }