2024-12-11 22:26:55 +01:00
--- a/net/minecraft/commands/CommandSourceStack.java
+++ b/net/minecraft/commands/CommandSourceStack.java
2020-04-20 00:15:29 +02:00
@@ -45,8 +45,9 @@
2024-12-11 22:26:55 +01:00
import net.minecraft.world.level.dimension.DimensionType;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;
+import com.mojang.brigadier.tree.CommandNode; // CraftBukkit
2020-04-20 00:15:29 +02:00
-public class CommandSourceStack implements ExecutionCommandSource<CommandSourceStack>, SharedSuggestionProvider {
+public class CommandSourceStack implements ExecutionCommandSource<CommandSourceStack>, SharedSuggestionProvider, com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource { // Paper - Brigadier API
2024-12-11 22:26:55 +01:00
2020-04-20 00:15:29 +02:00
public static final SimpleCommandExceptionType ERROR_NOT_PLAYER = new SimpleCommandExceptionType(Component.translatable("permissions.requires.player"));
public static final SimpleCommandExceptionType ERROR_NOT_ENTITY = new SimpleCommandExceptionType(Component.translatable("permissions.requires.entity"));
2021-01-29 17:54:03 +01:00
@@ -65,6 +66,8 @@
2024-12-11 22:26:55 +01:00
private final Vec2 rotation;
private final CommandSigningContext signingContext;
private final TaskChainer chatMessageChainer;
2020-07-11 09:54:28 +02:00
+ public java.util.Map<Thread, CommandNode> currentCommand = new java.util.concurrent.ConcurrentHashMap<>(); // CraftBukkit // Paper - Thread Safe Vanilla Command permission checking
2021-01-29 17:54:03 +01:00
+ public boolean bypassSelectorPermissions = false; // Paper - add bypass for selector permissions
2024-12-11 22:26:55 +01:00
public CommandSourceStack(CommandSource output, Vec3 pos, Vec2 rot, ServerLevel world, int level, String name, Component displayName, MinecraftServer server, @Nullable Entity entity) {
this(output, pos, rot, world, level, name, displayName, server, entity, false, CommandResultCallback.EMPTY, EntityAnchorArgument.Anchor.FEET, CommandSigningContext.ANONYMOUS, TaskChainer.immediate(server));
2022-01-25 00:32:02 +01:00
@@ -169,11 +172,66 @@
2020-04-20 00:15:29 +02:00
return this.textName;
}
2024-12-11 22:26:55 +01:00
2020-04-20 00:15:29 +02:00
+ // Paper start - Brigadier API
2024-12-11 22:26:55 +01:00
@Override
2020-04-20 00:15:29 +02:00
+ public org.bukkit.entity.Entity getBukkitEntity() {
+ return getEntity() != null ? getEntity().getBukkitEntity() : null;
+ }
+
+ @Override
+ public org.bukkit.World getBukkitWorld() {
+ return getLevel() != null ? getLevel().getWorld() : null;
+ }
+
+ @Override
+ public org.bukkit.Location getBukkitLocation() {
+ Vec3 pos = getPosition();
+ org.bukkit.World world = getBukkitWorld();
+ Vec2 rot = getRotation();
+ return world != null && pos != null ? new org.bukkit.Location(world, pos.x, pos.y, pos.z, rot != null ? rot.y : 0, rot != null ? rot.x : 0) : null;
+ }
+ // Paper end - Brigadier API
+
+ @Override
2024-12-11 22:26:55 +01:00
public boolean hasPermission(int level) {
+ // CraftBukkit start
2020-07-11 09:54:28 +02:00
+ // Paper start - Thread Safe Vanilla Command permission checking
+ CommandNode currentCommand = this.currentCommand.get(Thread.currentThread());
2024-12-11 22:26:55 +01:00
+ if (currentCommand != null) {
+ return this.hasPermission(level, org.bukkit.craftbukkit.command.VanillaCommandWrapper.getPermission(currentCommand));
2020-07-11 09:54:28 +02:00
+ // Paper end - Thread Safe Vanilla Command permission checking
2024-12-11 22:26:55 +01:00
+ }
+ // CraftBukkit end
+
return this.permissionLevel >= level;
2021-01-29 17:54:03 +01:00
}
2020-04-20 00:15:29 +02:00
2022-01-25 00:32:02 +01:00
+ // Paper start - Fix permission levels for command blocks
+ private boolean forceRespectPermissionLevel() {
+ return this.source == CommandSource.NULL || (this.source instanceof final net.minecraft.world.level.BaseCommandBlock commandBlock && commandBlock.getLevel().paperConfig().commandBlocks.forceFollowPermLevel);
+ }
+ // Paper end - Fix permission levels for command blocks
+
2024-12-11 22:26:55 +01:00
+ // CraftBukkit start
+ public boolean hasPermission(int i, String bukkitPermission) {
2022-01-25 00:32:02 +01:00
+ // Paper start - Fix permission levels for command blocks
+ final java.util.function.BooleanSupplier hasBukkitPerm = () -> this.source == CommandSource.NULL /*treat NULL as having all bukkit perms*/ || this.getBukkitSender().hasPermission(bukkitPermission); // lazily check bukkit perms to the benefit of custom permission setups
+ // if the server is null, we must check the vanilla perm level system
+ // if ignoreVanillaPermissions is true, we can skip vanilla perms and just run the bukkit perm check
+ //noinspection ConstantValue
+ if (this.getServer() == null || !this.getServer().server.ignoreVanillaPermissions) { // server & level are null for command function loading
+ final boolean hasPermLevel = this.permissionLevel >= i;
+ if (this.forceRespectPermissionLevel()) { // NULL CommandSource and command blocks (if setting is enabled) should always pass the vanilla perm check
+ return hasPermLevel && hasBukkitPerm.getAsBoolean();
+ } else { // otherwise check vanilla perm first then bukkit perm, matching upstream behavior
+ return hasPermLevel || hasBukkitPerm.getAsBoolean();
+ }
+ }
+ return hasBukkitPerm.getAsBoolean();
+ // Paper end - Fix permission levels for command blocks
2021-01-29 17:54:03 +01:00
+ }
2024-12-11 22:26:55 +01:00
+ // CraftBukkit end
2020-04-20 00:15:29 +02:00
+
2024-12-11 22:26:55 +01:00
public Vec3 getPosition() {
return this.worldPosition;
2020-04-20 00:15:29 +02:00
}
2022-01-25 00:32:02 +01:00
@@ -302,21 +360,26 @@
2024-12-11 22:26:55 +01:00
while (iterator.hasNext()) {
ServerPlayer entityplayer = (ServerPlayer) iterator.next();
- if (entityplayer.commandSource() != this.source && this.server.getPlayerList().isOp(entityplayer.getGameProfile())) {
+ if (entityplayer.commandSource() != this.source && entityplayer.getBukkitEntity().hasPermission("minecraft.admin.command_feedback")) { // CraftBukkit
entityplayer.sendSystemMessage(ichatmutablecomponent);
}
}
2014-02-09 04:39:01 +01:00
}
- if (this.source != this.server && this.server.getGameRules().getBoolean(GameRules.RULE_LOGADMINCOMMANDS)) {
+ if (this.source != this.server && this.server.getGameRules().getBoolean(GameRules.RULE_LOGADMINCOMMANDS) && !org.spigotmc.SpigotConfig.silentCommandBlocks) { // Spigot
this.server.sendSystemMessage(ichatmutablecomponent);
}
2017-06-18 00:48:21 +02:00
}
public void sendFailure(Component message) {
+ // Paper start - Add UnknownCommandEvent
+ this.sendFailure(message, true);
+ }
+ public void sendFailure(Component message, boolean withStyle) {
+ // Paper end - Add UnknownCommandEvent
if (this.source.acceptsFailure() && !this.silent) {
- this.source.sendSystemMessage(Component.empty().append(message).withStyle(ChatFormatting.RED));
+ this.source.sendSystemMessage(withStyle ? Component.empty().append(message).withStyle(ChatFormatting.RED) : message); // Paper - Add UnknownCommandEvent
}
}
2022-01-25 00:32:02 +01:00
@@ -400,4 +463,10 @@
2024-12-11 22:26:55 +01:00
public boolean isSilent() {
return this.silent;
}
+
+ // CraftBukkit start
+ public org.bukkit.command.CommandSender getBukkitSender() {
+ return this.source.getBukkitSender(this);
+ }
+ // CraftBukkit end
}