From a18546d698738acd0f8b75198ad62598b1e7011f Mon Sep 17 00:00:00 2001 From: sk89q Date: Sun, 1 May 2011 17:06:40 -0700 Subject: [PATCH] Added permission checks for tools on /use/, so now if you switch world or lose your permissions, you lose your tools. --- .../com/sk89q/worldedit/LocalSession.java | 2 +- .../java/com/sk89q/worldedit/WorldEdit.java | 24 ++++++++++++------- .../worldedit/commands/BrushCommands.java | 14 +++++------ .../sk89q/worldedit/tools/AreaPickaxe.java | 4 ++++ .../sk89q/worldedit/tools/BlockDataCyler.java | 4 ++++ .../sk89q/worldedit/tools/BlockReplacer.java | 4 ++++ .../com/sk89q/worldedit/tools/BrushTool.java | 24 ++++++++++++++++++- .../com/sk89q/worldedit/tools/QueryTool.java | 4 ++++ .../worldedit/tools/RecursivePickaxe.java | 4 ++++ .../sk89q/worldedit/tools/SinglePickaxe.java | 4 ++++ .../java/com/sk89q/worldedit/tools/Tool.java | 12 ++++++++++ .../sk89q/worldedit/tools/TreePlanter.java | 4 ++++ 12 files changed, 87 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/LocalSession.java b/src/main/java/com/sk89q/worldedit/LocalSession.java index 8d04e1b33..27de2e9cc 100644 --- a/src/main/java/com/sk89q/worldedit/LocalSession.java +++ b/src/main/java/com/sk89q/worldedit/LocalSession.java @@ -455,7 +455,7 @@ public class LocalSession { Tool tool = getTool(item); if (tool == null || !(tool instanceof BrushTool)) { - tool = new BrushTool(); + tool = new BrushTool("worldedit.brush.sphere"); setTool(item, tool); } diff --git a/src/main/java/com/sk89q/worldedit/WorldEdit.java b/src/main/java/com/sk89q/worldedit/WorldEdit.java index 237848dc5..55d14fe45 100644 --- a/src/main/java/com/sk89q/worldedit/WorldEdit.java +++ b/src/main/java/com/sk89q/worldedit/WorldEdit.java @@ -834,8 +834,10 @@ public class WorldEdit { Tool tool = session.getTool(player.getItemInHand()); if (tool != null && tool instanceof TraceTool) { - ((TraceTool)tool).act(server, config, player, session); - return true; + if (tool.canUse(player)) { + ((TraceTool)tool).act(server, config, player, session); + return true; + } } return false; @@ -866,8 +868,10 @@ public class WorldEdit { Tool tool = session.getTool(player.getItemInHand()); if (tool != null && tool instanceof BlockTool) { - ((BlockTool)tool).actPrimary(server, config, player, session, clicked); - return true; + if (tool.canUse(player)) { + ((BlockTool)tool).actPrimary(server, config, player, session, clicked); + return true; + } } return false; @@ -901,16 +905,20 @@ public class WorldEdit { } } else if (player.isHoldingPickAxe() && session.hasSuperPickAxe()) { if (session.getSuperPickaxe() != null) { - return session.getSuperPickaxe().actPrimary(server, config, - player, session, clicked); + if (session.getSuperPickaxe().canUse(player)) { + return session.getSuperPickaxe().actPrimary(server, config, + player, session, clicked); + } } } Tool tool = session.getTool(player.getItemInHand()); if (tool != null && tool instanceof DoubleActionBlockTool) { - ((DoubleActionBlockTool)tool).actSecondary(server, config, player, session, clicked); - return true; + if (tool.canUse(player)) { + ((DoubleActionBlockTool)tool).actSecondary(server, config, player, session, clicked); + return true; + } } return false; diff --git a/src/main/java/com/sk89q/worldedit/commands/BrushCommands.java b/src/main/java/com/sk89q/worldedit/commands/BrushCommands.java index 981352ac7..eb9b5557a 100644 --- a/src/main/java/com/sk89q/worldedit/commands/BrushCommands.java +++ b/src/main/java/com/sk89q/worldedit/commands/BrushCommands.java @@ -77,9 +77,9 @@ public class BrushCommands { tool.setSize(radius); if (args.hasFlag('h')) { - tool.setBrush(new HollowSphereBrush()); + tool.setBrush(new HollowSphereBrush(), "worldedit.brush.sphere"); } else { - tool.setBrush(new SphereBrush()); + tool.setBrush(new SphereBrush(), "worldedit.brush.sphere"); } player.print(String.format("Sphere brush shape equipped (%d).", @@ -121,9 +121,9 @@ public class BrushCommands { tool.setSize(radius); if (args.hasFlag('h')) { - tool.setBrush(new HollowCylinderBrush(height)); + tool.setBrush(new HollowCylinderBrush(height), "worldedit.brush.cylinder"); } else { - tool.setBrush(new CylinderBrush(height)); + tool.setBrush(new CylinderBrush(height), "worldedit.brush.cylinder"); } player.print(String.format("Cylinder brush shape equipped (%d by %d).", @@ -163,7 +163,7 @@ public class BrushCommands { } BrushTool tool = session.getBrushTool(player.getItemInHand()); - tool.setBrush(new ClipboardBrush(clipboard, args.hasFlag('a'))); + tool.setBrush(new ClipboardBrush(clipboard, args.hasFlag('a')), "worldedit.brush.clipboard"); player.print("Clipboard brush shape equipped."); } @@ -193,7 +193,7 @@ public class BrushCommands { BrushTool tool = session.getBrushTool(player.getItemInHand()); tool.setSize(radius); - tool.setBrush(new SmoothBrush(iterations)); + tool.setBrush(new SmoothBrush(iterations), "worldedit.brush.smooth"); player.print(String.format("Smooth brush equipped (%d x %dx).", radius, iterations)); @@ -225,7 +225,7 @@ public class BrushCommands { tool.setFill(fill); tool.setSize(radius); tool.setMask(new BlockTypeMask(BlockID.FIRE)); - tool.setBrush(new SphereBrush()); + tool.setBrush(new SphereBrush(), "worldedit.brush.ex"); player.print(String.format("Extinguisher equipped (%d).", radius)); diff --git a/src/main/java/com/sk89q/worldedit/tools/AreaPickaxe.java b/src/main/java/com/sk89q/worldedit/tools/AreaPickaxe.java index fcbea9804..d31910556 100644 --- a/src/main/java/com/sk89q/worldedit/tools/AreaPickaxe.java +++ b/src/main/java/com/sk89q/worldedit/tools/AreaPickaxe.java @@ -36,6 +36,10 @@ public class AreaPickaxe implements BlockTool { this.range = range; } + public boolean canUse(LocalPlayer player) { + return player.hasPermission("worldedit.superpickaxe.area"); + } + public boolean actPrimary(ServerInterface server, LocalConfiguration config, LocalPlayer player, LocalSession session, WorldVector clicked) { LocalWorld world = clicked.getWorld(); diff --git a/src/main/java/com/sk89q/worldedit/tools/BlockDataCyler.java b/src/main/java/com/sk89q/worldedit/tools/BlockDataCyler.java index b69060581..bf7c42a9a 100644 --- a/src/main/java/com/sk89q/worldedit/tools/BlockDataCyler.java +++ b/src/main/java/com/sk89q/worldedit/tools/BlockDataCyler.java @@ -29,6 +29,10 @@ import com.sk89q.worldedit.blocks.BlockID; */ public class BlockDataCyler implements DoubleActionBlockTool { + public boolean canUse(LocalPlayer player) { + return player.hasPermission("worldedit.tool.data-cycler"); + } + private boolean handleCycle(ServerInterface server, LocalConfiguration config, LocalPlayer player, LocalSession session, WorldVector clicked, boolean forward) { diff --git a/src/main/java/com/sk89q/worldedit/tools/BlockReplacer.java b/src/main/java/com/sk89q/worldedit/tools/BlockReplacer.java index 1092d040e..08bf0eb46 100644 --- a/src/main/java/com/sk89q/worldedit/tools/BlockReplacer.java +++ b/src/main/java/com/sk89q/worldedit/tools/BlockReplacer.java @@ -36,6 +36,10 @@ public class BlockReplacer implements DoubleActionBlockTool { this.targetBlock = targetBlock; } + public boolean canUse(LocalPlayer player) { + return player.hasPermission("worldedit.tool.replacer"); + } + public boolean actPrimary(ServerInterface server, LocalConfiguration config, LocalPlayer player, LocalSession session, WorldVector clicked) { diff --git a/src/main/java/com/sk89q/worldedit/tools/BrushTool.java b/src/main/java/com/sk89q/worldedit/tools/BrushTool.java index cec0e233c..e78f101c5 100644 --- a/src/main/java/com/sk89q/worldedit/tools/BrushTool.java +++ b/src/main/java/com/sk89q/worldedit/tools/BrushTool.java @@ -39,6 +39,27 @@ public class BrushTool implements TraceTool { private Brush brush = new SphereBrush(); private Pattern material = new SingleBlockPattern(new BaseBlock(BlockID.COBBLESTONE)); private int size = 1; + private String permission; + + /** + * Construct the tool. + * + * @param permission + */ + public BrushTool(String permission) { + this.permission = permission; + } + + /** + * Checks to see if the player can still be using this tool (considering + * permissions and such). + * + * @param player + * @return + */ + public boolean canUse(LocalPlayer player) { + return player.hasPermission(permission); + } /** * Get the filter. @@ -63,8 +84,9 @@ public class BrushTool implements TraceTool { * * @param brush */ - public void setBrush(Brush brush) { + public void setBrush(Brush brush, String perm) { this.brush = brush; + this.permission = perm; } /** diff --git a/src/main/java/com/sk89q/worldedit/tools/QueryTool.java b/src/main/java/com/sk89q/worldedit/tools/QueryTool.java index 8303e6a9e..fbd57651a 100644 --- a/src/main/java/com/sk89q/worldedit/tools/QueryTool.java +++ b/src/main/java/com/sk89q/worldedit/tools/QueryTool.java @@ -28,6 +28,10 @@ import com.sk89q.worldedit.blocks.*; * @author sk89q */ public class QueryTool implements BlockTool { + + public boolean canUse(LocalPlayer player) { + return player.hasPermission("worldedit.tool.info"); + } public boolean actPrimary(ServerInterface server, LocalConfiguration config, LocalPlayer player, LocalSession session, WorldVector clicked) { diff --git a/src/main/java/com/sk89q/worldedit/tools/RecursivePickaxe.java b/src/main/java/com/sk89q/worldedit/tools/RecursivePickaxe.java index a5ac0b6a0..145c80d2b 100644 --- a/src/main/java/com/sk89q/worldedit/tools/RecursivePickaxe.java +++ b/src/main/java/com/sk89q/worldedit/tools/RecursivePickaxe.java @@ -39,6 +39,10 @@ public class RecursivePickaxe implements BlockTool { this.range = range; } + public boolean canUse(LocalPlayer player) { + return player.hasPermission("worldedit.superpickaxe.recursive"); + } + public boolean actPrimary(ServerInterface server, LocalConfiguration config, LocalPlayer player, LocalSession session, WorldVector clicked) { LocalWorld world = clicked.getWorld(); diff --git a/src/main/java/com/sk89q/worldedit/tools/SinglePickaxe.java b/src/main/java/com/sk89q/worldedit/tools/SinglePickaxe.java index d58e3bbd9..e1138b1b6 100644 --- a/src/main/java/com/sk89q/worldedit/tools/SinglePickaxe.java +++ b/src/main/java/com/sk89q/worldedit/tools/SinglePickaxe.java @@ -29,6 +29,10 @@ import com.sk89q.worldedit.blocks.BlockID; */ public class SinglePickaxe implements BlockTool { + public boolean canUse(LocalPlayer player) { + return player.hasPermission("worldedit.superpickaxe"); + } + public boolean actPrimary(ServerInterface server, LocalConfiguration config, LocalPlayer player, LocalSession session, WorldVector clicked) { LocalWorld world = clicked.getWorld(); diff --git a/src/main/java/com/sk89q/worldedit/tools/Tool.java b/src/main/java/com/sk89q/worldedit/tools/Tool.java index 5d4e02071..1cad8ddd6 100644 --- a/src/main/java/com/sk89q/worldedit/tools/Tool.java +++ b/src/main/java/com/sk89q/worldedit/tools/Tool.java @@ -19,6 +19,8 @@ package com.sk89q.worldedit.tools; +import com.sk89q.worldedit.LocalPlayer; + /** * Represents a tool. This interface alone defines nothing. A tool also * has to implement BlockTool or TraceTool. @@ -26,4 +28,14 @@ package com.sk89q.worldedit.tools; * @author sk89q */ public abstract interface Tool { + + /** + * Checks to see if the player can still be using this tool (considering + * permissions and such). + * + * @param player + * @return + */ + public boolean canUse(LocalPlayer player); + } diff --git a/src/main/java/com/sk89q/worldedit/tools/TreePlanter.java b/src/main/java/com/sk89q/worldedit/tools/TreePlanter.java index 2728f20cb..613c68f0f 100644 --- a/src/main/java/com/sk89q/worldedit/tools/TreePlanter.java +++ b/src/main/java/com/sk89q/worldedit/tools/TreePlanter.java @@ -34,6 +34,10 @@ public class TreePlanter implements BlockTool { this.gen = gen; } + public boolean canUse(LocalPlayer player) { + return player.hasPermission("worldedit.tool.tree"); + } + public boolean actPrimary(ServerInterface server, LocalConfiguration config, LocalPlayer player, LocalSession session, WorldVector clicked) {