Vanilla command permission fixes

Fixes permission checks for vanilla commands which don't have a
requirement, as well as for namespaced vanilla commands.

== AT ==
public-f com.mojang.brigadier.tree.CommandNode requirement
This commit is contained in:
Jason Penilla 2021-08-25 13:19:53 -07:00
parent 33aad47ee1
commit 6dafeceebd
3 changed files with 61 additions and 17 deletions

View File

@ -0,0 +1,21 @@
--- a/com/mojang/brigadier/builder/ArgumentBuilder.java
+++ b/com/mojang/brigadier/builder/ArgumentBuilder.java
@@ -14,9 +14,17 @@
import java.util.function.Predicate;
public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> {
+ // Paper start - Vanilla command permission fixes
+ private static final Predicate<Object> DEFAULT_REQUIREMENT = s -> true;
+
+ @SuppressWarnings("unchecked")
+ public static <S> Predicate<S> defaultRequirement() {
+ return (Predicate<S>) DEFAULT_REQUIREMENT;
+ }
+ // Paper end - Vanilla command permission fixes
private final RootCommandNode<S> arguments = new RootCommandNode<>();
private Command<S> command;
- private Predicate<S> requirement = s -> true;
+ private Predicate<S> requirement = defaultRequirement(); // Paper - Vanilla command permission fixes
private CommandNode<S> target;
private RedirectModifier<S> modifier = null;
private boolean forks;

View File

@ -1,10 +1,9 @@
--- a/net/minecraft/commands/Commands.java
+++ b/net/minecraft/commands/Commands.java
@@ -138,6 +138,14 @@
import net.minecraft.world.flag.FeatureFlags;
@@ -139,6 +139,14 @@
import net.minecraft.world.level.GameRules;
import org.slf4j.Logger;
+
+// CraftBukkit start
+import com.google.common.base.Joiner;
+import java.util.Collection;
@ -12,9 +11,10 @@
+import org.bukkit.event.player.PlayerCommandSendEvent;
+import org.bukkit.event.server.ServerCommandEvent;
+// CraftBukkit end
+
public class Commands {
private static final ThreadLocal<ExecutionContext<CommandSourceStack>> CURRENT_EXECUTION_CONTEXT = new ThreadLocal();
@@ -151,6 +159,7 @@
private final com.mojang.brigadier.CommandDispatcher<CommandSourceStack> dispatcher = new com.mojang.brigadier.CommandDispatcher();
@ -23,19 +23,28 @@
AdvancementCommands.register(this.dispatcher);
AttributeCommand.register(this.dispatcher, commandRegistryAccess);
ExecuteCommand.register(this.dispatcher, commandRegistryAccess);
@@ -252,6 +261,11 @@
PublishCommand.register(this.dispatcher);
}
@@ -250,8 +259,20 @@
if (environment.includeIntegrated) {
PublishCommand.register(this.dispatcher);
+ }
+
+ // Paper start - Vanilla command permission fixes
+ for (final CommandNode<CommandSourceStack> node : this.dispatcher.getRoot().getChildren()) {
+ if (node.getRequirement() == com.mojang.brigadier.builder.ArgumentBuilder.<CommandSourceStack>defaultRequirement()) {
+ node.requirement = stack -> stack.source == CommandSource.NULL || stack.getBukkitSender().hasPermission(org.bukkit.craftbukkit.command.VanillaCommandWrapper.getPermission(node));
+ }
}
+ // Paper end - Vanilla command permission fixes
+ // CraftBukkit start
+ }
+
+ public Commands() {
+ // CraftBukkkit end
this.dispatcher.setConsumer(ExecutionCommandSource.resultConsumer());
}
@@ -262,30 +276,78 @@
@@ -262,30 +283,78 @@
return new ParseResults(commandcontextbuilder1, parseResults.getReader(), parseResults.getExceptions());
}
@ -123,7 +132,7 @@
StackTraceElement[] astacktraceelement = exception.getStackTrace();
for (int i = 0; i < Math.min(astacktraceelement.length, 3); ++i) {
@@ -298,7 +360,7 @@
@@ -298,7 +367,7 @@
}));
if (SharedConstants.IS_RUNNING_IN_IDE) {
commandlistenerwrapper.sendFailure(Component.literal(Util.describeError(exception)));
@ -132,7 +141,7 @@
}
} finally {
Profiler.get().pop();
@@ -307,18 +369,22 @@
@@ -307,18 +376,22 @@
}
@Nullable
@ -161,7 +170,7 @@
});
if (i > 10) {
@@ -333,8 +399,18 @@
@@ -333,8 +406,18 @@
}
ichatmutablecomponent.append((Component) Component.translatable("command.context.here").withStyle(ChatFormatting.RED, ChatFormatting.ITALIC));
@ -181,7 +190,7 @@
return null;
}
@@ -368,7 +444,7 @@
@@ -368,7 +451,7 @@
executioncontext1.close();
} finally {
@ -190,7 +199,7 @@
}
} else {
callback.accept(executioncontext);
@@ -377,22 +453,89 @@
@@ -377,22 +460,89 @@
}
public void sendCommands(ServerPlayer player) {
@ -285,7 +294,7 @@
argumentbuilder.requires((icompletionprovider) -> {
return true;
@@ -415,12 +558,12 @@
@@ -415,12 +565,12 @@
argumentbuilder.redirect((CommandNode) resultNodes.get(argumentbuilder.getRedirect()));
}
@ -300,7 +309,7 @@
}
}
}
@@ -481,7 +624,7 @@
@@ -481,7 +631,7 @@
}
private <T> HolderLookup.RegistryLookup.Delegate<T> createLookup(final HolderLookup.RegistryLookup<T> original) {

View File

@ -91,7 +91,21 @@ public final class VanillaCommandWrapper extends BukkitCommand {
}
public static String getPermission(CommandNode<CommandSourceStack> vanillaCommand) {
return "minecraft.command." + ((vanillaCommand.getRedirect() == null) ? vanillaCommand.getName() : vanillaCommand.getRedirect().getName());
// Paper start - Vanilla command permission fixes
while (vanillaCommand.getRedirect() != null) {
vanillaCommand = vanillaCommand.getRedirect();
}
final String commandName = vanillaCommand.getName();
return "minecraft.command." + stripDefaultNamespace(commandName);
}
private static String stripDefaultNamespace(final String maybeNamespaced) {
final String prefix = "minecraft:";
if (maybeNamespaced.startsWith(prefix)) {
return maybeNamespaced.substring(prefix.length());
}
return maybeNamespaced;
// Paper end - Vanilla command permission fixes
}
private String toDispatcher(String[] args, String name) {