From 97128bbcaec914ac44f4d0888224a94a86f0c741 Mon Sep 17 00:00:00 2001 From: Hexeption Date: Fri, 23 Aug 2019 12:14:57 +0100 Subject: [PATCH] Finished Cleaning up and adding permissions to default commands --- .../command/CommandHandler.java.patch | 71 +++++++---- .../org/bukkit/command/CommandWrapper.java | 116 ++++++++++++++++++ .../craftbukkit/event/CraftEventFactory.java | 69 ++++++----- 3 files changed, 202 insertions(+), 54 deletions(-) create mode 100644 src/main/java/org/bukkit/command/CommandWrapper.java diff --git a/patches/net/minecraft/command/CommandHandler.java.patch b/patches/net/minecraft/command/CommandHandler.java.patch index c993cb6..172333f 100644 --- a/patches/net/minecraft/command/CommandHandler.java.patch +++ b/patches/net/minecraft/command/CommandHandler.java.patch @@ -13,10 +13,11 @@ import net.minecraft.entity.Entity; import net.minecraft.server.MinecraftServer; import net.minecraft.util.math.BlockPos; -@@ -16,7 +10,16 @@ +@@ -16,26 +10,33 @@ import net.minecraft.util.text.TextFormatting; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; ++import org.bukkit.command.CommandWrapper; +import org.bukkit.craftbukkit.command.CraftSimpleCommandMap; +import org.bukkit.craftbukkit.command.ModCustomCommand; @@ -30,7 +31,11 @@ public abstract class CommandHandler implements ICommandManager { private static final Logger LOGGER = LogManager.getLogger(); -@@ -27,10 +30,10 @@ + private final Map commandMap = Maps.newHashMap(); ++ private final Map commandMod = Maps.newHashMap(); + private final Set commandSet = Sets.newHashSet(); + + public int executeCommand(ICommandSender sender, String rawCommand) { rawCommand = rawCommand.trim(); @@ -38,46 +43,54 @@ - { - rawCommand = rawCommand.substring(1); - } -+// if (rawCommand.startsWith("/")) -+// { -+// rawCommand = rawCommand.substring(1); -+// } - +- String[] astring = rawCommand.split(" "); String s = astring[0]; -@@ -144,9 +147,8 @@ + astring = dropFirstString(astring); + ICommand icommand = this.commandMap.get(s); ++ if (icommand == null) icommand = CommandWrapper.toNMSCommand(sender, s); + int i = 0; + try +@@ -145,20 +146,43 @@ protected abstract MinecraftServer getServer(); -- public ICommand registerCommand(ICommand command) + public ICommand registerCommand(ICommand command) - { - this.commandMap.put(command.getName(), command); -+ public ICommand registerCommand(ICommand command) { -+ /*this.commandMap.put(command.getName(), command); - this.commandSet.add(command); - - for (String s : command.getAliases()) -@@ -159,6 +161,34 @@ +- this.commandSet.add(command); +- +- for (String s : command.getAliases()) + { +- ICommand icommand = this.commandMap.get(s); +- +- if (icommand == null || !icommand.getName().equals(s)) ++ String pre = command.getClass().getName().substring(command.getClass().getName().lastIndexOf('.')+1).toLowerCase() + "." + command.getName(); ++ if (command instanceof CommandBase) ++ { ++ if (!((CommandBase)command).permissionNode.equals("minecraft")) + { +- this.commandMap.put(s, command); ++ pre = ((CommandBase)command).permissionNode; ++ this.commandMod.put(command.getName(), command); } } - -+ return command;*/ -+ // register commands with permission nodes, defaulting to class name -+ return registerCommand(command, command.getClass().getName()); ++ return registerCommand(command, pre); + } -+ -+ private ICommand registerCommand(String permissionGroup, ICommand command) { -+ return registerCommand(command, permissionGroup + "." + command.getName()); + ++ public ICommand registerCommand(String permissionGroup, ICommand par1ICommand) ++ { ++ return registerCommand(par1ICommand, permissionGroup + "." + par1ICommand.getName()); + } + + public ICommand registerCommand(ICommand command, String permissionNode) { ++ List list = command.getAliases(); + this.commandMap.put(command.getName(), command); + this.commandSet.add(command); + // register vanilla commands with Bukkit to support permissions. + CraftSimpleCommandMap commandMap = MinecraftServer.getServerCB().server.getCraftCommandMap(); + ModCustomCommand customCommand = new ModCustomCommand(command.getName()); + customCommand.setPermission(permissionNode); -+ List list = command.getAliases(); + if (list != null) customCommand.setAliases(list); + commandMap.register(command.getName(), customCommand); + LogManager.getLogger().info("Registered command " + command.getName() + " with permission node " + permissionNode); @@ -92,3 +105,15 @@ return command; } +@@ -224,6 +248,11 @@ + return this.commandMap; + } + ++ public Map getCommandMod() ++ { ++ return this.commandMod; ++ } ++ + private int getUsernameIndex(ICommand command, String[] args) throws CommandException + { + if (command == null) diff --git a/src/main/java/org/bukkit/command/CommandWrapper.java b/src/main/java/org/bukkit/command/CommandWrapper.java new file mode 100644 index 0000000..85de442 --- /dev/null +++ b/src/main/java/org/bukkit/command/CommandWrapper.java @@ -0,0 +1,116 @@ +package org.bukkit.command; + +import com.google.common.collect.ImmutableList; +import net.minecraft.advancements.FunctionManager; +import net.minecraft.command.ICommand; +import net.minecraft.command.ICommandSender; +import net.minecraft.entity.Entity; +import net.minecraft.network.rcon.RConConsoleSource; +import net.minecraft.server.MinecraftServer; +import net.minecraft.tileentity.CommandBlockBaseLogic; +import net.minecraft.util.math.BlockPos; +import org.bukkit.craftbukkit.command.CraftBlockCommandSender; +import org.bukkit.craftbukkit.command.CraftFunctionCommandSender; +import org.bukkit.craftbukkit.command.CraftRemoteConsoleCommandSender; +import org.magmafoundation.magma.api.ServerAPI; + +import javax.annotation.Nullable; +import java.util.List; + +/** + * CommandWrapper + * + * @author Hexeption admin@hexeption.co.uk + * @since 23/08/2019 - 11:22 am + */ +public class CommandWrapper implements ICommand { + + private final CommandSender sender; + private final String name; + private final Command command; + + public CommandWrapper(CommandSender sender, String name, Command command) { + this.sender = sender; + this.name = name; + this.command = command; + } + + @Nullable + public static CommandWrapper toNMSCommand(ICommandSender sender, String name) { + Command command; + CommandSender bukkitSender; + if ((command = ServerAPI.getCBServer().getCommandMap().getCommand(name)) != null && (bukkitSender = toBukkitSender(sender)) != null) { + return new CommandWrapper(bukkitSender, name, command); + } + return null; + } + + @Nullable + public static CommandSender toBukkitSender(ICommandSender sender) { + if (sender instanceof MinecraftServer) { + return MinecraftServer.getServerInstance().console; + } + if (sender instanceof RConConsoleSource) { + return new CraftRemoteConsoleCommandSender((RConConsoleSource) sender); + } + if (sender instanceof CommandBlockBaseLogic) { + return new CraftBlockCommandSender(sender); + } + if (sender instanceof FunctionManager.CustomFunctionListener) { + return new CraftFunctionCommandSender(sender); + } + if (sender instanceof Entity) { + return ((Entity) sender).getBukkitEntity(); + } + return null; + } + + @Override + public String getName() { + return this.command.getName(); + } + + @Override + public String getUsage(ICommandSender sender) { + return this.command.getDescription(); + } + + @Override + public List getAliases() { + return this.command.getAliases(); + } + + @Override + public void execute(MinecraftServer server, ICommandSender iCommandSender, String[] args) throws CommandException { + try { + this.command.execute(sender, name, args); + } catch (Exception e) { + throw new CommandException(e.getMessage()); + } + } + + @Override + public boolean checkPermission(MinecraftServer server, ICommandSender iCommandSender) { + return this.command.testPermission(sender); + } + + @Override + public List getTabCompletions(MinecraftServer server, ICommandSender iCommandSender, String[] args, @Nullable BlockPos targetPos) { + try { + return this.command.tabComplete(sender, name, args); + } catch (Exception e) { + e.printStackTrace(); + return ImmutableList.of(); + } + } + + @Override + public boolean isUsernameIndex(String[] args, int index) { + return false; + } + + @Override + public int compareTo(ICommand o) { + return 0; + } +} diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java index 1945e39..d5b8f3d 100644 --- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java +++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java @@ -1,16 +1,9 @@ package org.bukkit.craftbukkit.event; -import java.net.InetAddress; -import java.util.ArrayList; -import java.util.EnumMap; -import java.util.List; -import java.util.Map; -import javax.annotation.Nullable; - import com.google.common.base.Function; import com.google.common.base.Functions; - import net.minecraft.block.state.IBlockState; +import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityAreaEffectCloud; import net.minecraft.entity.EntityLiving; @@ -24,19 +17,16 @@ import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.entity.projectile.EntityArrow; import net.minecraft.entity.projectile.EntityPotion; +import net.minecraft.init.Enchantments; import net.minecraft.init.Items; import net.minecraft.inventory.Container; import net.minecraft.inventory.InventoryCrafting; - import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; +import net.minecraft.item.ItemSword; import net.minecraft.network.play.client.CPacketCloseWindow; import net.minecraft.network.play.server.SPacketSetSlot; -import net.minecraft.util.DamageSource; -import net.minecraft.util.EntityDamageSource; -import net.minecraft.util.EntityDamageSourceIndirect; -import net.minecraft.util.EnumFacing; -import net.minecraft.util.EnumHand; +import net.minecraft.util.*; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.RayTraceResult; import net.minecraft.util.text.ITextComponent; @@ -45,6 +35,7 @@ import net.minecraft.world.Explosion; import net.minecraft.world.World; import net.minecraft.world.WorldServer; +import net.minecraftforge.common.util.FakePlayer; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.Server; @@ -65,20 +56,7 @@ import org.bukkit.craftbukkit.inventory.CraftMetaBook; import org.bukkit.craftbukkit.util.CraftDamageSource; import org.bukkit.craftbukkit.util.CraftMagicNumbers; -import org.bukkit.entity.AreaEffectCloud; -import org.bukkit.entity.Arrow; -import org.bukkit.entity.Creeper; -import org.bukkit.entity.EntityType; -import org.bukkit.entity.ExperienceOrb; -import org.bukkit.entity.Firework; -import org.bukkit.entity.LightningStrike; -import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Pig; -import org.bukkit.entity.PigZombie; -import org.bukkit.entity.Player; -import org.bukkit.entity.Projectile; -import org.bukkit.entity.ThrownExpBottle; -import org.bukkit.entity.ThrownPotion; +import org.bukkit.entity.*; import org.bukkit.event.Cancellable; import org.bukkit.event.Event; import org.bukkit.event.block.*; @@ -93,12 +71,17 @@ import org.bukkit.event.inventory.PrepareItemCraftEvent; import org.bukkit.event.player.*; import org.bukkit.event.server.ServerListPingEvent; +import org.bukkit.event.vehicle.VehicleCreateEvent; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.meta.BookMeta; -import org.bukkit.entity.AbstractHorse; -import org.bukkit.entity.Vehicle; -import org.bukkit.event.vehicle.VehicleCreateEvent; + +import javax.annotation.Nullable; +import java.net.InetAddress; +import java.util.ArrayList; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; public class CraftEventFactory { public static final DamageSource MELTING = CraftDamageSource.copyOf(DamageSource.ON_FIRE); @@ -1114,4 +1097,28 @@ public static boolean handleBlockFormEvent(World world, BlockPos pos, IBlockStat return !event.isCancelled(); } + + // Cauldron start + public static BlockBreakEvent callBlockBreakEvent(net.minecraft.world.World world, BlockPos pos, IBlockState iBlockState, net.minecraft.entity.player.EntityPlayerMP player) { + org.bukkit.block.Block bukkitBlock = world.getWorld().getBlockAt(pos.getX(), pos.getY(), pos.getZ()); + org.bukkit.event.block.BlockBreakEvent blockBreakEvent = new org.bukkit.event.block.BlockBreakEvent(bukkitBlock, ((EntityPlayerMP) player).getBukkitEntity()); + EntityPlayerMP playermp = (EntityPlayerMP) player; + net.minecraft.block.Block block = iBlockState.getBlock(); + if (!(playermp instanceof FakePlayer)) { + boolean isSwordNoBreak = playermp.interactionManager.getGameType().isCreative() && !playermp.getHeldItemMainhand().isEmpty() && playermp.getHeldItemMainhand().getItem() instanceof ItemSword; + if (!isSwordNoBreak) { + int exp = 0; + if (!(block == null || !player.canHarvestBlock(block.getDefaultState()) || block.canSilkHarvest(world, pos, block.getBlockState().getBaseState(), player) && EnchantmentHelper.getEnchantmentLevel(Enchantments.SILK_TOUCH, player.getHeldItemMainhand()) > 0)) { + int bonusLevel = EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, player.getHeldItemMainhand()); + exp = block.getExpDrop(iBlockState, world, pos, bonusLevel); + } + blockBreakEvent.setExpToDrop(exp); + } else { + blockBreakEvent.setCancelled(true); + } + } + + world.getServer().getPluginManager().callEvent(blockBreakEvent); + return blockBreakEvent; + } }