From 7c7eff2c01cc8d9a868e723f857f5c63832ff62f Mon Sep 17 00:00:00 2001
From: Jason Penilla <11360596+jpenilla@users.noreply.github.com>
Date: Wed, 26 Oct 2022 13:13:12 -0700
Subject: [PATCH] Fix EntityArgument and EntitySelectorParser permissions to
 align with EntitySelector#checkPermissions

Fixes where the user has permission for selectors but not their
suggestions, which especially matters when we force suggestions to
the server for this type
---
 .../commands/arguments/EntityArgument.java.patch   | 14 ++++++++++++++
 .../selector/EntitySelectorParser.java.patch       |  9 +++++++++
 2 files changed, 23 insertions(+)

diff --git a/paper-server/patches/sources/net/minecraft/commands/arguments/EntityArgument.java.patch b/paper-server/patches/sources/net/minecraft/commands/arguments/EntityArgument.java.patch
index 9e568bbf36..9c45cfda8f 100644
--- a/paper-server/patches/sources/net/minecraft/commands/arguments/EntityArgument.java.patch
+++ b/paper-server/patches/sources/net/minecraft/commands/arguments/EntityArgument.java.patch
@@ -36,3 +36,17 @@
          } else {
              return entityselector;
          }
+@@ -129,7 +135,12 @@
+             StringReader stringreader = new StringReader(suggestionsbuilder.getInput());
+ 
+             stringreader.setCursor(suggestionsbuilder.getStart());
+-            EntitySelectorParser argumentparserselector = new EntitySelectorParser(stringreader, EntitySelectorParser.allowSelectors(icompletionprovider));
++            // Paper start - Fix EntityArgument permissions
++            final boolean permission = object instanceof CommandSourceStack stack
++                    ? stack.bypassSelectorPermissions || stack.hasPermission(2, "minecraft.command.selector")
++                    : icompletionprovider.hasPermission(2);
++            EntitySelectorParser argumentparserselector = new EntitySelectorParser(stringreader, permission);
++            // Paper end - Fix EntityArgument permissions
+ 
+             try {
+                 argumentparserselector.parse();
diff --git a/paper-server/patches/sources/net/minecraft/commands/arguments/selector/EntitySelectorParser.java.patch b/paper-server/patches/sources/net/minecraft/commands/arguments/selector/EntitySelectorParser.java.patch
index bc6ec0856d..088ffce992 100644
--- a/paper-server/patches/sources/net/minecraft/commands/arguments/selector/EntitySelectorParser.java.patch
+++ b/paper-server/patches/sources/net/minecraft/commands/arguments/selector/EntitySelectorParser.java.patch
@@ -1,5 +1,14 @@
 --- a/net/minecraft/commands/arguments/selector/EntitySelectorParser.java
 +++ b/net/minecraft/commands/arguments/selector/EntitySelectorParser.java
+@@ -133,7 +133,7 @@
+         boolean flag;
+ 
+         if (source instanceof SharedSuggestionProvider icompletionprovider) {
+-            if (icompletionprovider.hasPermission(2)) {
++            if (source instanceof net.minecraft.commands.CommandSourceStack stack ? stack.bypassSelectorPermissions || stack.hasPermission(2, "minecraft.command.selector") : icompletionprovider.hasPermission(2)) { // Paper - Fix EntityArgument permissions
+                 flag = true;
+                 return flag;
+             }
 @@ -158,7 +158,7 @@
              axisalignedbb = this.createAabb(this.deltaX == null ? 0.0D : this.deltaX, this.deltaY == null ? 0.0D : this.deltaY, this.deltaZ == null ? 0.0D : this.deltaZ);
          }