| 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.SimpleCommandExceptionType;
|
| 9 | import com.mojang.brigadier.suggestion.SuggestionProvider;
|
| 10 | import java.util.ArrayList;
|
| 11 | import java.util.Arrays;
|
| 12 | import java.util.Collection;
|
| 13 | import java.util.Collections;
|
| 14 | import java.util.List;
|
| 15 | import java.util.UUID;
|
| 16 | import java.util.function.Supplier;
|
| 17 | import net.minecraft.commands.CommandBuildContext;
|
| 18 | import net.minecraft.commands.CommandSourceStack;
|
| 19 | import net.minecraft.commands.SharedSuggestionProvider;
|
| 20 | import net.minecraft.commands.arguments.selector.EntitySelector;
|
| 21 | import net.minecraft.commands.arguments.selector.EntitySelectorParser;
|
| 22 | import net.minecraft.commands.synchronization.ArgumentTypeInfo;
|
| 23 | import net.minecraft.network.FriendlyByteBuf;
|
| 24 | import net.minecraft.network.chat.Component;
|
| 25 | import net.minecraft.server.MinecraftServer;
|
| 26 | import net.minecraft.server.level.ServerLevel;
|
| 27 | import net.minecraft.server.level.ServerPlayer;
|
| 28 | import net.minecraft.server.permissions.Permissions;
|
| 29 | import net.minecraft.world.entity.Entity;
|
| 30 | import net.minecraft.world.scores.ScoreHolder;
|
| 31 |
|
| 32 | public class ScoreHolderArgument implements ArgumentType<ScoreHolderArgument.Result> {
|
| 33 | public static final SuggestionProvider<CommandSourceStack> SUGGEST_SCORE_HOLDERS = (context, builder) -> {
|
| 34 | StringReader reader = new StringReader(builder.getInput());
|
| 35 | reader.setCursor(builder.getStart());
|
| 36 | EntitySelectorParser parser = new EntitySelectorParser(reader, context.getSource().permissions().hasPermission(Permissions.COMMANDS_ENTITY_SELECTORS));
|
| 37 |
|
| 38 | try {
|
| 39 | parser.parse();
|
| 40 | } catch (CommandSyntaxException var5) {
|
| 41 | }
|
| 42 |
|
| 43 | return parser.fillSuggestions(builder, suggestions -> SharedSuggestionProvider.suggest(context.getSource().getOnlinePlayerNames(), suggestions));
|
| 44 | };
|
| 45 | private static final Collection<String> EXAMPLES = Arrays.asList("Player", "0123", "*", "@e");
|
| 46 | private static final SimpleCommandExceptionType ERROR_NO_RESULTS = new SimpleCommandExceptionType(Component.translatable("argument.scoreHolder.empty"));
|
| 47 | private final boolean multiple;
|
| 48 |
|
| 49 | public ScoreHolderArgument(final boolean multiple) {
|
| 50 | this.multiple = multiple;
|
| 51 | }
|
| 52 |
|
| 53 | public static ScoreHolder getName(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
|
| 54 | return getNames(context, name).iterator().next();
|
| 55 | }
|
| 56 |
|
| 57 | public static Collection<ScoreHolder> getNames(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
|
| 58 | return getNames(context, name, Collections::emptyList);
|
| 59 | }
|
| 60 |
|
| 61 | public static Collection<ScoreHolder> getNamesWithDefaultWildcard(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
|
| 62 | return getNames(context, name, context.getSource().getServer().getScoreboard()::getTrackedPlayers);
|
| 63 | }
|
| 64 |
|
| 65 | public static Collection<ScoreHolder> getNames(
|
| 66 | final CommandContext<CommandSourceStack> context, final String name, final Supplier<Collection<ScoreHolder>> wildcard
|
| 67 | ) throws CommandSyntaxException {
|
| 68 | Collection<ScoreHolder> result = context.getArgument(name, ScoreHolderArgument.Result.class).getNames(context.getSource(), wildcard);
|
| 69 | if (result.isEmpty()) {
|
| 70 | throw EntityArgument.NO_ENTITIES_FOUND.create();
|
| 71 | } else {
|
| 72 | return result;
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | public static ScoreHolderArgument scoreHolder() {
|
| 77 | return new ScoreHolderArgument(false);
|
| 78 | }
|
| 79 |
|
| 80 | public static ScoreHolderArgument scoreHolders() {
|
| 81 | return new ScoreHolderArgument(true);
|
| 82 | }
|
| 83 |
|
| 84 | public ScoreHolderArgument.Result parse(final StringReader reader) throws CommandSyntaxException {
|
| 85 | return this.parse(reader, true);
|
| 86 | }
|
| 87 |
|
| 88 | public <S> ScoreHolderArgument.Result parse(final StringReader reader, final S source) throws CommandSyntaxException {
|
| 89 | return this.parse(reader, EntitySelectorParser.allowSelectors(source));
|
| 90 | }
|
| 91 |
|
| 92 | private ScoreHolderArgument.Result parse(final StringReader reader, final boolean allowSelectors) throws CommandSyntaxException {
|
| 93 | if (reader.canRead() && reader.peek() == '@') {
|
| 94 | EntitySelectorParser parser = new EntitySelectorParser(reader, allowSelectors);
|
| 95 | EntitySelector selector = parser.parse();
|
| 96 | if (!this.multiple && selector.getMaxResults() > 1) {
|
| 97 | throw EntityArgument.ERROR_NOT_SINGLE_ENTITY.createWithContext(reader);
|
| 98 | } else {
|
| 99 | return new ScoreHolderArgument.SelectorResult(selector);
|
| 100 | }
|
| 101 | } else {
|
| 102 | int start = reader.getCursor();
|
| 103 |
|
| 104 | while (reader.canRead() && reader.peek() != ' ') {
|
| 105 | reader.skip();
|
| 106 | }
|
| 107 |
|
| 108 | String text = reader.getString().substring(start, reader.getCursor());
|
| 109 | if (text.equals("*")) {
|
| 110 | return (sender, wildcard) -> {
|
| 111 | Collection<ScoreHolder> results = wildcard.get();
|
| 112 | if (results.isEmpty()) {
|
| 113 | throw ERROR_NO_RESULTS.create();
|
| 114 | } else {
|
| 115 | return results;
|
| 116 | }
|
| 117 | };
|
| 118 | }
|
| 119 |
|
| 120 | List<ScoreHolder> nameOnlyHolder = List.of(ScoreHolder.forNameOnly(text));
|
| 121 | if (text.startsWith("#")) {
|
| 122 | return (sender, wildcard) -> nameOnlyHolder;
|
| 123 | }
|
| 124 |
|
| 125 | try {
|
| 126 | UUID uuid = UUID.fromString(text);
|
| 127 | return (sender, wildcard) -> {
|
| 128 | MinecraftServer server = sender.getServer();
|
| 129 | ScoreHolder firstResult = null;
|
| 130 | List<ScoreHolder> moreResults = null;
|
| 131 |
|
| 132 | for (ServerLevel level : server.getAllLevels()) {
|
| 133 | Entity entity = level.getEntity(uuid);
|
| 134 | if (entity != null) {
|
| 135 | if (firstResult == null) {
|
| 136 | firstResult = entity;
|
| 137 | } else {
|
| 138 | if (moreResults == null) {
|
| 139 | moreResults = new ArrayList<>();
|
| 140 | moreResults.add(firstResult);
|
| 141 | }
|
| 142 |
|
| 143 | moreResults.add(entity);
|
| 144 | }
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 | if (moreResults != null) {
|
| 149 | return moreResults;
|
| 150 | } else {
|
| 151 | return firstResult != null ? List.of(firstResult) : nameOnlyHolder;
|
| 152 | }
|
| 153 | };
|
| 154 | } catch (IllegalArgumentException var7) {
|
| 155 | return (sender, wildcard) -> {
|
| 156 | MinecraftServer server = sender.getServer();
|
| 157 | ServerPlayer player = server.getPlayerList().getPlayerByName(text);
|
| 158 | return player != null ? List.of(player) : nameOnlyHolder;
|
| 159 | };
|
| 160 | }
|
| 161 | }
|
| 162 | }
|
| 163 |
|
| 164 | @Override
|
| 165 | public Collection<String> getExamples() {
|
| 166 | return EXAMPLES;
|
| 167 | }
|
| 168 |
|
| 169 | public static class Info implements ArgumentTypeInfo<ScoreHolderArgument, ScoreHolderArgument.Info.Template> {
|
| 170 | private static final byte FLAG_MULTIPLE = 1;
|
| 171 |
|
| 172 | public void serializeToNetwork(final ScoreHolderArgument.Info.Template template, final FriendlyByteBuf out) {
|
| 173 | int flags = 0;
|
| 174 | if (template.multiple) {
|
| 175 | flags |= 1;
|
| 176 | }
|
| 177 |
|
| 178 | out.writeByte(flags);
|
| 179 | }
|
| 180 |
|
| 181 | public ScoreHolderArgument.Info.Template deserializeFromNetwork(final FriendlyByteBuf in) {
|
| 182 | byte flags = in.readByte();
|
| 183 | boolean multiple = (flags & 1) != 0;
|
| 184 | return new ScoreHolderArgument.Info.Template(multiple);
|
| 185 | }
|
| 186 |
|
| 187 | public void serializeToJson(final ScoreHolderArgument.Info.Template template, final JsonObject out) {
|
| 188 | out.addProperty("amount", template.multiple ? "multiple" : "single");
|
| 189 | }
|
| 190 |
|
| 191 | public ScoreHolderArgument.Info.Template unpack(final ScoreHolderArgument argument) {
|
| 192 | return new ScoreHolderArgument.Info.Template(argument.multiple);
|
| 193 | }
|
| 194 |
|
| 195 | public final class Template implements ArgumentTypeInfo.Template<ScoreHolderArgument> {
|
| 196 | private final boolean multiple;
|
| 197 |
|
| 198 | private Template(final boolean multiple) {
|
| 199 | this.multiple = multiple;
|
| 200 | }
|
| 201 |
|
| 202 | public ScoreHolderArgument instantiate(final CommandBuildContext context) {
|
| 203 | return new ScoreHolderArgument(this.multiple);
|
| 204 | }
|
| 205 |
|
| 206 | @Override
|
| 207 | public ArgumentTypeInfo<ScoreHolderArgument, ?> type() {
|
| 208 | return Info.this;
|
| 209 | }
|
| 210 | }
|
| 211 | }
|
| 212 |
|
| 213 | @FunctionalInterface
|
| 214 | public interface Result {
|
| 215 | Collection<ScoreHolder> getNames(final CommandSourceStack sender, Supplier<Collection<ScoreHolder>> wildcard) throws CommandSyntaxException;
|
| 216 | }
|
| 217 |
|
| 218 | public static class SelectorResult implements ScoreHolderArgument.Result {
|
| 219 | private final EntitySelector selector;
|
| 220 |
|
| 221 | public SelectorResult(final EntitySelector selector) {
|
| 222 | this.selector = selector;
|
| 223 | }
|
| 224 |
|
| 225 | @Override
|
| 226 | public Collection<ScoreHolder> getNames(final CommandSourceStack sender, final Supplier<Collection<ScoreHolder>> wildcard) throws CommandSyntaxException {
|
| 227 | List<? extends Entity> entities = this.selector.findEntities(sender);
|
| 228 | if (entities.isEmpty()) {
|
| 229 | throw EntityArgument.NO_ENTITIES_FOUND.create();
|
| 230 | } else {
|
| 231 | return List.copyOf(entities);
|
| 232 | }
|
| 233 | }
|
| 234 | }
|
| 235 | }
|
| 236 |
|