From 37ebbd9e375dd3f6aed8870a3d7fd73f22137c6c Mon Sep 17 00:00:00 2001 From: boy0001 Date: Fri, 13 Mar 2015 15:57:51 +1100 Subject: [PATCH] Various fixes and improvements - Added support for PlotSquared - Added generic checks for other plugins (e.g. PlotMe) - Added checks for stand movement (so you can't move your stand outside your region) - Added checks for '*' nodes and operator in permission checking - other minor tweaks --- .gitignore | 6 +- .../plugins/ArmorStandTools/Commands.java | 5 +- .../plugins/ArmorStandTools/Config.java | 6 ++ .../plugins/ArmorStandTools/MainListener.java | 100 +++++++++++++----- .../ArmorStandTools/PlotSquaredHook.java | 40 +++++++ .../plugins/ArmorStandTools/Utils.java | 18 ++++ src/plugin.yml | 4 +- 7 files changed, 150 insertions(+), 29 deletions(-) create mode 100644 src/com/gmail/St3venAU/plugins/ArmorStandTools/PlotSquaredHook.java diff --git a/.gitignore b/.gitignore index be5de33..ad9c598 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,8 @@ .idea/ libs/ out/ -ArmorStand.iml \ No newline at end of file +ArmorStand.iml +/bin/ + +*.project +.classpath \ No newline at end of file diff --git a/src/com/gmail/St3venAU/plugins/ArmorStandTools/Commands.java b/src/com/gmail/St3venAU/plugins/ArmorStandTools/Commands.java index 75dd1fc..9e20593 100644 --- a/src/com/gmail/St3venAU/plugins/ArmorStandTools/Commands.java +++ b/src/com/gmail/St3venAU/plugins/ArmorStandTools/Commands.java @@ -23,6 +23,9 @@ class Commands implements CommandExecutor { return false; } Player p = (Player) sender; + if (!Utils.hasPermissionNode(p, "astools.command")) { + p.sendMessage(ChatColor.RED + "You don't have permission to use this command"); + } if(args.length == 0) { UUID uuid = p.getUniqueId(); if(plugin.savedInventories.containsKey(uuid)) { @@ -39,7 +42,7 @@ class Commands implements CommandExecutor { } } if(args[0].equalsIgnoreCase("reload")) { - if(p.hasPermission("astools.reload")) { + if(Utils.hasPermissionNode(p, "astools.reload")) { Config.reload(); p.sendMessage(ChatColor.GREEN + Config.conReload); return true; diff --git a/src/com/gmail/St3venAU/plugins/ArmorStandTools/Config.java b/src/com/gmail/St3venAU/plugins/ArmorStandTools/Config.java index 4dfab37..5c7b35b 100644 --- a/src/com/gmail/St3venAU/plugins/ArmorStandTools/Config.java +++ b/src/com/gmail/St3venAU/plugins/ArmorStandTools/Config.java @@ -100,6 +100,12 @@ class Config { invulnerable = config.getBoolean("invulnerable"); equipmentLock = config.getBoolean("equipmentLock"); plugin.carryingArmorStand.clear(); + + Plugin plotSquared = plugin.getServer().getPluginManager().getPlugin("PlotSquared"); + if (plotSquared != null && plotSquared.isEnabled()) { + new PlotSquaredHook(plugin); + } + Plugin worldGuard = plugin.getServer().getPluginManager().getPlugin("WorldGuard"); worldGuardPlugin = worldGuard == null || !(worldGuard instanceof WorldGuardPlugin) ? null : (WorldGuardPlugin) worldGuard; if(config.getBoolean("integrateWithWorldGuard")) { diff --git a/src/com/gmail/St3venAU/plugins/ArmorStandTools/MainListener.java b/src/com/gmail/St3venAU/plugins/ArmorStandTools/MainListener.java index a9c7a92..c0a9ad6 100644 --- a/src/com/gmail/St3venAU/plugins/ArmorStandTools/MainListener.java +++ b/src/com/gmail/St3venAU/plugins/ArmorStandTools/MainListener.java @@ -1,5 +1,6 @@ package com.gmail.St3venAU.plugins.ArmorStandTools; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; @@ -14,6 +15,7 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; +import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.SignChangeEvent; import org.bukkit.event.entity.EntityDamageByEntityEvent; @@ -53,16 +55,24 @@ public class MainListener implements Listener { public void onPlayerInteractAtEntity(PlayerInteractAtEntityEvent event) { if (event.getRightClicked() instanceof ArmorStand) { Player p = event.getPlayer(); - if(plugin.carryingArmorStand.containsKey(p.getUniqueId()) && playerHasPermission(p, plugin.carryingArmorStand.get(p.getUniqueId()).getLocation().getBlock(), null)) { - plugin.carryingArmorStand.remove(p.getUniqueId()); - Utils.actionBarMsg(p, Config.asDropped); - event.setCancelled(true); - return; + if(plugin.carryingArmorStand.containsKey(p.getUniqueId())) { + if (playerHasPermission(p, plugin.carryingArmorStand.get(p.getUniqueId()).getLocation().getBlock(), null)) { + plugin.carryingArmorStand.remove(p.getUniqueId()); + Utils.actionBarMsg(p, Config.asDropped); + event.setCancelled(true); + return; + } + else { + p.sendMessage(ChatColor.RED + Config.wgNoPerm); + } } ArmorStandTool tool = ArmorStandTool.get(p.getItemInHand()); if(tool == null) return; ArmorStand as = (ArmorStand) event.getRightClicked(); - if (!playerHasPermission(p, event.getRightClicked().getLocation().getBlock(), tool)) return; + if (!playerHasPermission(p, event.getRightClicked().getLocation().getBlock(), tool)) { + p.sendMessage(ChatColor.RED + Config.wgNoPerm); + return; + } double num = event.getClickedPosition().getY() - 0.05; if (num < 0) { num = 0; @@ -264,8 +274,12 @@ public class MainListener implements Listener { Utils.actionBarMsg(p, Config.asDropped); return; } - as.teleport(Utils.getLocationFacingPlayer(p)); - Utils.actionBarMsg(p, ChatColor.GREEN + Config.carrying); + Location loc = Utils.getLocationFacingPlayer(p); + Block block = loc.getBlock(); + if (playerHasPermission(p, block, null)) { + as.teleport(loc); + Utils.actionBarMsg(p, ChatColor.GREEN + Config.carrying); + } } } @@ -337,10 +351,16 @@ public class MainListener implements Listener { @EventHandler public void onPlayerInteract(PlayerInteractEvent event) { Player p = event.getPlayer(); - if(plugin.carryingArmorStand.containsKey(p.getUniqueId()) && playerHasPermission(p, plugin.carryingArmorStand.get(p.getUniqueId()).getLocation().getBlock(), null)) { - plugin.carryingArmorStand.remove(p.getUniqueId()); - Utils.actionBarMsg(p, Config.asDropped); - event.setCancelled(true); + if(plugin.carryingArmorStand.containsKey(p.getUniqueId())) { + boolean perm = playerHasPermission(p, plugin.carryingArmorStand.get(p.getUniqueId()).getLocation().getBlock(), null); + if (perm) { + plugin.carryingArmorStand.remove(p.getUniqueId()); + Utils.actionBarMsg(p, Config.asDropped); + event.setCancelled(true); + } + else { + p.sendMessage(ChatColor.RED + Config.wgNoPerm); + } return; } Action action = event.getAction(); @@ -351,8 +371,11 @@ public class MainListener implements Listener { Utils.cycleInventory(p); return; } - if(action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) { - if(!playerHasPermission(p, p.getLocation().getBlock(), tool)) return; + else if(action == Action.RIGHT_CLICK_BLOCK) { + if (!playerHasPermission(p, event.getClickedBlock(), tool)) { + p.sendMessage(ChatColor.RED + Config.wgNoPerm); + return; + } switch (tool) { case SUMMON: event.setCancelled(true); @@ -565,20 +588,49 @@ public class MainListener implements Listener { } return null; } - + + public boolean checkPermission(Player player, Block block) { + + // Check PlotSquared + Location loc = block.getLocation(); + if (PlotSquaredHook.api != null) { + if (PlotSquaredHook.isPlotWorld(loc)) { + boolean result = PlotSquaredHook.checkPermission(player, loc); + return result; + } + } + + // check WorldGuard + if(Config.worldGuardPlugin != null) { + boolean canBuild = Config.worldGuardPlugin.canBuild(player, block); + return canBuild; + } + + // Use standard permission checking (will support basically any plugin) + BlockBreakEvent mybreak = new BlockBreakEvent(block, player); + Bukkit.getServer().getPluginManager().callEvent(mybreak); + boolean hasperm; + if (mybreak.isCancelled()) { + hasperm = false; + } else { + hasperm = true; + } + BlockPlaceEvent place = new BlockPlaceEvent(block, block.getState(), block, null, player, true); + Bukkit.getServer().getPluginManager().callEvent(place); + if (place.isCancelled()) { + hasperm = false; + } + return hasperm; + } + boolean playerHasPermission(Player p, Block b, ArmorStandTool tool) { - if(!p.isOp() && (!p.hasPermission("astools.use") - || (ArmorStandTool.SAVE == tool && !p.hasPermission("astools.cmdblock")) - || (ArmorStandTool.CLONE == tool && !p.hasPermission("astools.clone")))) { + if(tool != null && !p.isOp() && (!Utils.hasPermissionNode(p, "astools.use") + || (ArmorStandTool.SAVE == tool && !Utils.hasPermissionNode(p, "astools.cmdblock")) + || (ArmorStandTool.CLONE == tool && !Utils.hasPermissionNode(p, "astools.clone")))) { p.sendMessage(ChatColor.RED + Config.noPerm); return false; } - if(Config.worldGuardPlugin == null) return true; - boolean canBuild = Config.worldGuardPlugin.canBuild(p, b); - if(!canBuild) { - p.sendMessage(ChatColor.RED + Config.wgNoPerm); - } - return canBuild; + return checkPermission(p, b); } void pickUpArmorStand(ArmorStand as, Player p, boolean newlySummoned) { diff --git a/src/com/gmail/St3venAU/plugins/ArmorStandTools/PlotSquaredHook.java b/src/com/gmail/St3venAU/plugins/ArmorStandTools/PlotSquaredHook.java new file mode 100644 index 0000000..b9b2442 --- /dev/null +++ b/src/com/gmail/St3venAU/plugins/ArmorStandTools/PlotSquaredHook.java @@ -0,0 +1,40 @@ +package com.gmail.St3venAU.plugins.ArmorStandTools; + +import java.util.UUID; + +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.entity.Player; + +import com.intellectualcrafters.plot.api.PlotAPI; +import com.intellectualcrafters.plot.object.Plot; +import com.intellectualcrafters.plot.object.PlotPlayer; +import com.intellectualcrafters.plot.util.Permissions; +import com.intellectualcrafters.plot.util.bukkit.BukkitUtil; + +public class PlotSquaredHook { + + public static PlotAPI api = null; + + public PlotSquaredHook(Main plugin) { + PlotSquaredHook.api = new PlotAPI(plugin); + } + + public static boolean isPlotWorld(Location loc) { + World world = loc.getWorld(); + return api.isPlotWorld(world); + } + + public static boolean checkPermission(Player player, Location loc) { + Plot plot = api.getPlot(loc); + if (plot == null) { + return Permissions.hasPermission(BukkitUtil.getPlayer(player), "plots.admin.build.road"); + } + PlotPlayer pp = BukkitUtil.getPlayer(player); + UUID uuid = pp.getUUID(); + if (plot.isAdded(uuid)) { + return true; + } + return Permissions.hasPermission(pp, "plots.admin.build.other"); + } +} diff --git a/src/com/gmail/St3venAU/plugins/ArmorStandTools/Utils.java b/src/com/gmail/St3venAU/plugins/ArmorStandTools/Utils.java index 21ac61a..fd933e4 100644 --- a/src/com/gmail/St3venAU/plugins/ArmorStandTools/Utils.java +++ b/src/com/gmail/St3venAU/plugins/ArmorStandTools/Utils.java @@ -24,6 +24,24 @@ class Utils { } return false; } + + public static boolean hasPermissionNode(Player player, String perm) { + if ((player == null) || player.isOp()) { + return true; + } + if (player.hasPermission(perm)) { + return true; + } + final String[] nodes = perm.split("\\."); + final StringBuilder n = new StringBuilder(); + for (int i = 0; i < (nodes.length - 1); i++) { + n.append(nodes[i] + (".")); + if (player.hasPermission(n + "*")) { + return true; + } + } + return false; + } static boolean hasItems(Player p) { for(ItemStack i : p.getInventory()) { diff --git a/src/plugin.yml b/src/plugin.yml index 726383e..1132278 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -8,6 +8,4 @@ commands: astools: description: Give yourself all of the armor stand tools (Warning; clears your inventory). aliases: ast - usage: Usage /astools or /astools reload - permission: astools.command - permission-message: You don't have permission to use this command \ No newline at end of file + usage: Usage /astools or /astools reload \ No newline at end of file