2021-12-22 06:34:20 +01:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Sun, 22 Aug 2021 15:21:57 -0700
Subject: [PATCH] Fix entity type tags suggestions in selectors
This would really be better as a client fix because just to fix it
all EntityArguments have been told to ask the server for completions
when if this was fixed on the client, that wouldn't be needed.
Mojira Issue: https://bugs.mojang.com/browse/MC-235045
2021-12-30 08:11:40 +01:00
diff --git a/src/main/java/net/minecraft/commands/CommandSourceStack.java b/src/main/java/net/minecraft/commands/CommandSourceStack.java
2024-01-18 22:00:40 +01:00
index 79732086bc4cdbca8364d78eb60d68c758055966..e65d7980b7ebed60786bc31e2f8156fda3b59556 100644
2021-12-30 08:11:40 +01:00
--- a/src/main/java/net/minecraft/commands/CommandSourceStack.java
+++ b/src/main/java/net/minecraft/commands/CommandSourceStack.java
2023-12-06 16:17:00 +01:00
@@ -447,4 +447,20 @@ public class CommandSourceStack implements ExecutionCommandSource<CommandSourceS
2021-12-30 08:11:40 +01:00
return this.source.getBukkitSender(this);
}
// CraftBukkit end
2024-01-18 22:00:40 +01:00
+ // Paper start - tell clients to ask server for suggestions for EntityArguments
2021-12-30 08:11:40 +01:00
+ @Override
+ public Collection<String> getSelectedEntities() {
2022-06-09 10:51:45 +02:00
+ if (io.papermc.paper.configuration.GlobalConfiguration.get().commands.fixTargetSelectorTagCompletion && this.source instanceof ServerPlayer player) {
2023-06-27 23:46:27 +02:00
+ final Entity cameraEntity = player.getCamera();
+ final double pickDistance = player.gameMode.getGameModeForPlayer().isCreative() ? 6.0F : 4.5F;
+ final Vec3 min = cameraEntity.getEyePosition(1.0F);
+ final Vec3 viewVector = cameraEntity.getViewVector(1.0F);
+ final Vec3 max = min.add(viewVector.x * pickDistance, viewVector.y * pickDistance, viewVector.z * pickDistance);
+ final net.minecraft.world.phys.AABB aabb = cameraEntity.getBoundingBox().expandTowards(viewVector.scale(pickDistance)).inflate(1.0D, 1.0D, 1.0D);
+ final net.minecraft.world.phys.EntityHitResult hitResult = net.minecraft.world.entity.projectile.ProjectileUtil.getEntityHitResult(cameraEntity, min, max, aabb, (e) -> !e.isSpectator() && e.isPickable(), pickDistance * pickDistance);
2021-12-30 08:11:40 +01:00
+ return hitResult != null ? java.util.Collections.singletonList(hitResult.getEntity().getStringUUID()) : SharedSuggestionProvider.super.getSelectedEntities();
+ }
+ return SharedSuggestionProvider.super.getSelectedEntities();
+ }
2024-01-18 22:00:40 +01:00
+ // Paper end - tell clients to ask server for suggestions for EntityArguments
2021-12-30 08:11:40 +01:00
}
2021-12-22 06:34:20 +01:00
diff --git a/src/main/java/net/minecraft/commands/Commands.java b/src/main/java/net/minecraft/commands/Commands.java
2024-01-19 17:54:05 +01:00
index 2bad211edde8b494cae726915a332217ae232855..cb1898f6daa79a0ac379a5caa7283a9155a70a15 100644
2021-12-22 06:34:20 +01:00
--- a/src/main/java/net/minecraft/commands/Commands.java
+++ b/src/main/java/net/minecraft/commands/Commands.java
2023-12-06 16:17:00 +01:00
@@ -532,6 +532,7 @@ public class Commands {
2021-12-30 08:11:40 +01:00
private void fillUsableCommands(CommandNode<CommandSourceStack> tree, CommandNode<SharedSuggestionProvider> result, CommandSourceStack source, Map<CommandNode<CommandSourceStack>, CommandNode<SharedSuggestionProvider>> resultNodes) {
Iterator iterator = tree.getChildren().iterator();
2021-12-22 06:34:20 +01:00
2021-12-30 08:11:40 +01:00
+ boolean registeredAskServerSuggestionsForTree = false; // Paper - tell clients to ask server for suggestions for EntityArguments
while (iterator.hasNext()) {
CommandNode<CommandSourceStack> commandnode2 = (CommandNode) iterator.next();
2022-08-07 01:22:51 +02:00
// Paper start
2023-12-06 16:17:00 +01:00
@@ -558,6 +559,12 @@ public class Commands {
2021-12-30 08:11:40 +01:00
if (requiredargumentbuilder.getSuggestionsProvider() != null) {
requiredargumentbuilder.suggests(SuggestionProviders.safelySwap(requiredargumentbuilder.getSuggestionsProvider()));
+ // Paper start - tell clients to ask server for suggestions for EntityArguments
+ registeredAskServerSuggestionsForTree = requiredargumentbuilder.getSuggestionsProvider() == net.minecraft.commands.synchronization.SuggestionProviders.ASK_SERVER;
2022-06-09 10:51:45 +02:00
+ } else if (io.papermc.paper.configuration.GlobalConfiguration.get().commands.fixTargetSelectorTagCompletion && !registeredAskServerSuggestionsForTree && requiredargumentbuilder.getType() instanceof net.minecraft.commands.arguments.EntityArgument) {
2021-12-30 08:11:40 +01:00
+ requiredargumentbuilder.suggests(requiredargumentbuilder.getType()::listSuggestions);
+ registeredAskServerSuggestionsForTree = true; // You can only
+ // Paper end - tell clients to ask server for suggestions for EntityArguments
}
}
2021-12-22 06:34:20 +01:00
diff --git a/src/main/java/net/minecraft/commands/arguments/EntityArgument.java b/src/main/java/net/minecraft/commands/arguments/EntityArgument.java
2024-01-18 22:00:40 +01:00
index 850db283bf12345e9e7d7e8e590dbe8135c6dce1..d93698fd814683d451a3c8e8bcbb9cd475c79515 100644
2021-12-22 06:34:20 +01:00
--- a/src/main/java/net/minecraft/commands/arguments/EntityArgument.java
+++ b/src/main/java/net/minecraft/commands/arguments/EntityArgument.java
2022-06-08 14:33:46 +02:00
@@ -128,7 +128,7 @@ public class EntityArgument implements ArgumentType<EntitySelector> {
StringReader stringreader = new StringReader(suggestionsbuilder.getInput());
2021-12-22 06:34:20 +01:00
stringreader.setCursor(suggestionsbuilder.getStart());
- EntitySelectorParser argumentparserselector = new EntitySelectorParser(stringreader, icompletionprovider.hasPermission(2));
2024-01-18 22:00:40 +01:00
+ EntitySelectorParser argumentparserselector = new EntitySelectorParser(stringreader, icompletionprovider.hasPermission(2), true); // Paper - tell clients to ask server for suggestions for EntityArguments
2021-12-22 06:34:20 +01:00
try {
argumentparserselector.parse();
2023-06-11 03:15:43 +02:00
@@ -137,7 +137,19 @@ public class EntityArgument implements ArgumentType<EntitySelector> {
}
return argumentparserselector.fillSuggestions(suggestionsbuilder, (suggestionsbuilder1) -> {
- Collection<String> collection = icompletionprovider.getOnlinePlayerNames();
2024-01-18 22:00:40 +01:00
+ // Paper start - tell clients to ask server for suggestions for EntityArguments
2023-06-11 03:15:43 +02:00
+ final Collection<String> collection;
+ if (icompletionprovider instanceof CommandSourceStack commandSourceStack && commandSourceStack.getEntity() instanceof ServerPlayer sourcePlayer) {
+ collection = new java.util.ArrayList<>();
+ for (final ServerPlayer player : commandSourceStack.getServer().getPlayerList().getPlayers()) {
+ if (sourcePlayer.getBukkitEntity().canSee(player.getBukkitEntity())) {
+ collection.add(player.getGameProfile().getName());
+ }
+ }
+ } else {
+ collection = icompletionprovider.getOnlinePlayerNames();
+ }
2024-01-18 22:00:40 +01:00
+ // Paper end - tell clients to ask server for suggestions for EntityArguments
2023-06-11 03:15:43 +02:00
Iterable<String> iterable = this.playersOnly ? collection : Iterables.concat(collection, icompletionprovider.getSelectedEntities());
SharedSuggestionProvider.suggest((Iterable) iterable, suggestionsbuilder1);
2021-12-22 06:34:20 +01:00
diff --git a/src/main/java/net/minecraft/commands/arguments/selector/EntitySelectorParser.java b/src/main/java/net/minecraft/commands/arguments/selector/EntitySelectorParser.java
2024-01-18 22:00:40 +01:00
index 3d897ec6920eff6176ddac9f0442a997b9ef14fd..abaf1ea340c69c8bee80e64567b44b5ce66d5fa3 100644
2021-12-22 06:34:20 +01:00
--- a/src/main/java/net/minecraft/commands/arguments/selector/EntitySelectorParser.java
+++ b/src/main/java/net/minecraft/commands/arguments/selector/EntitySelectorParser.java
2022-12-07 22:35:34 +01:00
@@ -113,12 +113,19 @@ public class EntitySelectorParser {
2021-12-22 06:34:20 +01:00
private boolean hasScores;
private boolean hasAdvancements;
private boolean usesSelectors;
2024-01-18 22:00:40 +01:00
+ public boolean parsingEntityArgumentSuggestions; // Paper - tell clients to ask server for suggestions for EntityArguments
2021-12-22 06:34:20 +01:00
public EntitySelectorParser(StringReader reader) {
this(reader, true);
}
public EntitySelectorParser(StringReader reader, boolean atAllowed) {
2024-01-18 22:00:40 +01:00
+ // Paper start - tell clients to ask server for suggestions for EntityArguments
2021-12-22 06:34:20 +01:00
+ this(reader, atAllowed, false);
+ }
+ public EntitySelectorParser(StringReader reader, boolean atAllowed, boolean parsingEntityArgumentSuggestions) {
+ this.parsingEntityArgumentSuggestions = parsingEntityArgumentSuggestions;
2024-01-18 22:00:40 +01:00
+ // Paper end - tell clients to ask server for suggestions for EntityArguments
2021-12-22 06:34:20 +01:00
this.distance = MinMaxBounds.Doubles.ANY;
this.level = MinMaxBounds.Ints.ANY;
this.rotX = WrappedMinMaxBounds.ANY;
diff --git a/src/main/java/net/minecraft/commands/arguments/selector/options/EntitySelectorOptions.java b/src/main/java/net/minecraft/commands/arguments/selector/options/EntitySelectorOptions.java
2024-01-18 22:00:40 +01:00
index 7863833c9a761a2b9364ff572ec909ec5a4538c3..36e26929306c04af7a44012fd7fc9902027e9ea2 100644
2021-12-22 06:34:20 +01:00
--- a/src/main/java/net/minecraft/commands/arguments/selector/options/EntitySelectorOptions.java
+++ b/src/main/java/net/minecraft/commands/arguments/selector/options/EntitySelectorOptions.java
2023-09-22 16:58:35 +02:00
@@ -69,6 +69,19 @@ public class EntitySelectorOptions {
2021-12-22 06:34:20 +01:00
public static final DynamicCommandExceptionType ERROR_ENTITY_TYPE_INVALID = new DynamicCommandExceptionType((entity) -> {
2023-12-06 16:17:00 +01:00
return Component.translatableEscape("argument.entity.options.type.invalid", entity);
2021-12-22 06:34:20 +01:00
});
2024-01-18 22:00:40 +01:00
+ // Paper start - tell clients to ask server for suggestions for EntityArguments
2021-12-22 06:34:20 +01:00
+ public static final DynamicCommandExceptionType ERROR_ENTITY_TAG_INVALID = new DynamicCommandExceptionType((object) -> {
+ return io.papermc.paper.adventure.PaperAdventure
+ .asVanilla(net.kyori.adventure.text.Component
+ .text("Invalid or unknown entity type tag '" + object + "'")
+ .hoverEvent(net.kyori.adventure.text.event.HoverEvent
+ .showText(net.kyori.adventure.text.Component
+ .text("You can disable this error in 'paper.yml'")
+ )
+ )
+ );
+ });
2024-01-18 22:00:40 +01:00
+ // Paper end - tell clients to ask server for suggestions for EntityArguments
2021-12-22 06:34:20 +01:00
private static void register(String id, EntitySelectorOptions.Modifier handler, Predicate<EntitySelectorParser> condition, Component description) {
OPTIONS.put(id, new EntitySelectorOptions.Option(handler, condition, description));
2023-09-22 16:58:35 +02:00
@@ -316,6 +329,12 @@ public class EntitySelectorOptions {
2021-12-22 06:34:20 +01:00
if (reader.isTag()) {
2022-12-07 22:35:34 +01:00
TagKey<EntityType<?>> tagKey = TagKey.create(Registries.ENTITY_TYPE, ResourceLocation.read(reader.getReader()));
2024-01-18 22:00:40 +01:00
+ // Paper start - tell clients to ask server for suggestions for EntityArguments; throw error if invalid entity tag (only on suggestions to keep cmd success behavior)
2022-12-07 22:35:34 +01:00
+ if (reader.parsingEntityArgumentSuggestions && io.papermc.paper.configuration.GlobalConfiguration.get().commands.fixTargetSelectorTagCompletion && net.minecraft.core.registries.BuiltInRegistries.ENTITY_TYPE.getTag(tagKey).isEmpty()) {
2021-12-22 06:34:20 +01:00
+ reader.getReader().setCursor(i);
2022-02-28 23:16:17 +01:00
+ throw ERROR_ENTITY_TAG_INVALID.createWithContext(reader.getReader(), tagKey);
2021-12-22 06:34:20 +01:00
+ }
2024-01-18 22:00:40 +01:00
+ // Paper end - tell clients to ask server for suggestions for EntityArguments
2021-12-22 06:34:20 +01:00
reader.addPredicate((entity) -> {
2022-02-28 23:16:17 +01:00
return entity.getType().is(tagKey) != bl;
2021-12-22 06:34:20 +01:00
});