--- a/net/minecraft/world/level/block/entity/CommandBlockEntity.java
+++ b/net/minecraft/world/level/block/entity/CommandBlockEntity.java
@@ -7,7 +7,9 @@
 import net.minecraft.core.component.DataComponentMap;
 import net.minecraft.core.component.DataComponents;
 import net.minecraft.nbt.CompoundTag;
+import net.minecraft.network.chat.Component;
 import net.minecraft.server.level.ServerLevel;
+import net.minecraft.world.entity.Entity;
 import net.minecraft.world.level.BaseCommandBlock;
 import net.minecraft.world.level.block.Block;
 import net.minecraft.world.level.block.Blocks;
@@ -17,10 +19,18 @@
 import net.minecraft.world.phys.Vec3;
 
 public class CommandBlockEntity extends BlockEntity {
+
     private boolean powered;
     private boolean auto;
     private boolean conditionMet;
     private final BaseCommandBlock commandBlock = new BaseCommandBlock() {
+        // CraftBukkit start
+        @Override
+        public org.bukkit.command.CommandSender getBukkitSender(CommandSourceStack wrapper) {
+            return new org.bukkit.craftbukkit.command.CraftBlockCommandSender(wrapper, CommandBlockEntity.this);
+        }
+        // CraftBukkit end
+
         @Override
         public void setCommand(String command) {
             super.setCommand(command);
@@ -29,13 +39,14 @@
 
         @Override
         public ServerLevel getLevel() {
-            return (ServerLevel)CommandBlockEntity.this.level;
+            return (ServerLevel) CommandBlockEntity.this.level;
         }
 
         @Override
         public void onUpdated() {
-            BlockState blockState = CommandBlockEntity.this.level.getBlockState(CommandBlockEntity.this.worldPosition);
-            this.getLevel().sendBlockUpdated(CommandBlockEntity.this.worldPosition, blockState, blockState, 3);
+            BlockState iblockdata = CommandBlockEntity.this.level.getBlockState(CommandBlockEntity.this.worldPosition);
+
+            this.getLevel().sendBlockUpdated(CommandBlockEntity.this.worldPosition, iblockdata, iblockdata, 3);
         }
 
         @Override
@@ -45,18 +56,9 @@
 
         @Override
         public CommandSourceStack createCommandSourceStack() {
-            Direction direction = CommandBlockEntity.this.getBlockState().getValue(CommandBlock.FACING);
-            return new CommandSourceStack(
-                this,
-                Vec3.atCenterOf(CommandBlockEntity.this.worldPosition),
-                new Vec2(0.0F, direction.toYRot()),
-                this.getLevel(),
-                2,
-                this.getName().getString(),
-                this.getName(),
-                this.getLevel().getServer(),
-                null
-            );
+            Direction enumdirection = (Direction) CommandBlockEntity.this.getBlockState().getValue(CommandBlock.FACING);
+
+            return new CommandSourceStack(this, Vec3.atCenterOf(CommandBlockEntity.this.worldPosition), new Vec2(0.0F, enumdirection.toYRot()), this.getLevel(), this.getLevel().paperConfig().commandBlocks.permissionsLevel, this.getName().getString(), this.getName(), this.getLevel().getServer(), (Entity) null); // Paper - configurable command block perm level
         }
 
         @Override
@@ -109,26 +111,32 @@
     }
 
     public void setAutomatic(boolean auto) {
-        boolean bl = this.auto;
+        boolean flag1 = this.auto;
+
         this.auto = auto;
-        if (!bl && auto && !this.powered && this.level != null && this.getMode() != CommandBlockEntity.Mode.SEQUENCE) {
+        if (!flag1 && auto && !this.powered && this.level != null && this.getMode() != CommandBlockEntity.Mode.SEQUENCE) {
             this.scheduleTick();
         }
+
     }
 
     public void onModeSwitch() {
-        CommandBlockEntity.Mode mode = this.getMode();
-        if (mode == CommandBlockEntity.Mode.AUTO && (this.powered || this.auto) && this.level != null) {
+        CommandBlockEntity.Mode tileentitycommand_type = this.getMode();
+
+        if (tileentitycommand_type == CommandBlockEntity.Mode.AUTO && (this.powered || this.auto) && this.level != null) {
             this.scheduleTick();
         }
+
     }
 
     private void scheduleTick() {
         Block block = this.getBlockState().getBlock();
+
         if (block instanceof CommandBlock) {
             this.markConditionMet();
             this.level.scheduleTick(this.worldPosition, block, 1);
         }
+
     }
 
     public boolean wasConditionMet() {
@@ -138,10 +146,12 @@
     public boolean markConditionMet() {
         this.conditionMet = true;
         if (this.isConditional()) {
-            BlockPos blockPos = this.worldPosition.relative(this.level.getBlockState(this.worldPosition).getValue(CommandBlock.FACING).getOpposite());
-            if (this.level.getBlockState(blockPos).getBlock() instanceof CommandBlock) {
-                BlockEntity blockEntity = this.level.getBlockEntity(blockPos);
-                this.conditionMet = blockEntity instanceof CommandBlockEntity && ((CommandBlockEntity)blockEntity).getCommandBlock().getSuccessCount() > 0;
+            BlockPos blockposition = this.worldPosition.relative(((Direction) this.level.getBlockState(this.worldPosition).getValue(CommandBlock.FACING)).getOpposite());
+
+            if (this.level.getBlockState(blockposition).getBlock() instanceof CommandBlock) {
+                BlockEntity tileentity = this.level.getBlockEntity(blockposition);
+
+                this.conditionMet = tileentity instanceof CommandBlockEntity && ((CommandBlockEntity) tileentity).getCommandBlock().getSuccessCount() > 0;
             } else {
                 this.conditionMet = false;
             }
@@ -151,25 +161,21 @@
     }
 
     public CommandBlockEntity.Mode getMode() {
-        BlockState blockState = this.getBlockState();
-        if (blockState.is(Blocks.COMMAND_BLOCK)) {
-            return CommandBlockEntity.Mode.REDSTONE;
-        } else if (blockState.is(Blocks.REPEATING_COMMAND_BLOCK)) {
-            return CommandBlockEntity.Mode.AUTO;
-        } else {
-            return blockState.is(Blocks.CHAIN_COMMAND_BLOCK) ? CommandBlockEntity.Mode.SEQUENCE : CommandBlockEntity.Mode.REDSTONE;
-        }
+        BlockState iblockdata = this.getBlockState();
+
+        return iblockdata.is(Blocks.COMMAND_BLOCK) ? CommandBlockEntity.Mode.REDSTONE : (iblockdata.is(Blocks.REPEATING_COMMAND_BLOCK) ? CommandBlockEntity.Mode.AUTO : (iblockdata.is(Blocks.CHAIN_COMMAND_BLOCK) ? CommandBlockEntity.Mode.SEQUENCE : CommandBlockEntity.Mode.REDSTONE));
     }
 
     public boolean isConditional() {
-        BlockState blockState = this.level.getBlockState(this.getBlockPos());
-        return blockState.getBlock() instanceof CommandBlock && blockState.getValue(CommandBlock.CONDITIONAL);
+        BlockState iblockdata = this.level.getBlockState(this.getBlockPos());
+
+        return iblockdata.getBlock() instanceof CommandBlock ? (Boolean) iblockdata.getValue(CommandBlock.CONDITIONAL) : false;
     }
 
     @Override
     protected void applyImplicitComponents(BlockEntity.DataComponentInput components) {
         super.applyImplicitComponents(components);
-        this.commandBlock.setCustomName(components.get(DataComponents.CUSTOM_NAME));
+        this.commandBlock.setCustomName((Component) components.get(DataComponents.CUSTOM_NAME));
     }
 
     @Override
@@ -185,8 +191,9 @@
     }
 
     public static enum Mode {
-        SEQUENCE,
-        AUTO,
-        REDSTONE;
+
+        SEQUENCE, AUTO, REDSTONE;
+
+        private Mode() {}
     }
 }
