From d7324f6b139d77c6d88004cc484173e376770ffa Mon Sep 17 00:00:00 2001 From: wizjany Date: Fri, 2 Aug 2013 21:33:11 -0400 Subject: [PATCH] Made a checkMaxBrushRadius method for centralized use. --- .../worldedit/MaxBrushRadiusException.java | 10 ++++ .../java/com/sk89q/worldedit/WorldEdit.java | 14 ++++++ .../worldedit/commands/BrushCommands.java | 48 ++++--------------- .../worldedit/commands/ToolUtilCommands.java | 6 +-- 4 files changed, 35 insertions(+), 43 deletions(-) create mode 100644 src/main/java/com/sk89q/worldedit/MaxBrushRadiusException.java diff --git a/src/main/java/com/sk89q/worldedit/MaxBrushRadiusException.java b/src/main/java/com/sk89q/worldedit/MaxBrushRadiusException.java new file mode 100644 index 000000000..8f58bded0 --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/MaxBrushRadiusException.java @@ -0,0 +1,10 @@ +package com.sk89q.worldedit; + +/** + * + * Thrown when a maximum radius for a brush is reached. + * + */ +public class MaxBrushRadiusException extends MaxRadiusException { + +} diff --git a/src/main/java/com/sk89q/worldedit/WorldEdit.java b/src/main/java/com/sk89q/worldedit/WorldEdit.java index 96cc65339..1b814ddaa 100644 --- a/src/main/java/com/sk89q/worldedit/WorldEdit.java +++ b/src/main/java/com/sk89q/worldedit/WorldEdit.java @@ -917,6 +917,18 @@ public class WorldEdit { } } + /** + * Checks to see if the specified brush radius is within bounds. + * + * @param radius + * @throws MaxBrushRadiusException + */ + public void checkMaxBrushRadius(double radius) throws MaxBrushRadiusException { + if (config.maxBrushRadius > 0 && radius > config.maxBrushRadius) { + throw new MaxBrushRadiusException(); + } + } + /** * Get a file relative to the defined working directory. If the specified * path is absolute, then the working directory is not used. @@ -1445,6 +1457,8 @@ public class WorldEdit { } catch (MaxChangedBlocksException e) { player.printError("Max blocks changed in an operation reached (" + e.getBlockLimit() + ")."); + } catch (MaxBrushRadiusException e) { + player.printError("Maximum allowed brush radius/height: " + config.maxBrushRadius); } catch (MaxRadiusException e) { player.printError("Maximum radius: " + config.maxRadius); } catch (UnknownDirectionException e) { diff --git a/src/main/java/com/sk89q/worldedit/commands/BrushCommands.java b/src/main/java/com/sk89q/worldedit/commands/BrushCommands.java index 1aa2c10f0..a835440bb 100644 --- a/src/main/java/com/sk89q/worldedit/commands/BrushCommands.java +++ b/src/main/java/com/sk89q/worldedit/commands/BrushCommands.java @@ -77,11 +77,7 @@ public class BrushCommands { LocalConfiguration config = we.getConfiguration(); double radius = args.argsLength() > 1 ? args.getDouble(1) : 2; - if (radius > config.maxBrushRadius) { - player.printError("Maximum allowed brush radius: " - + config.maxBrushRadius); - return; - } + we.checkMaxBrushRadius(radius); BrushTool tool = session.getBrushTool(player.getItemInHand()); Pattern fill = we.getBlockPattern(player, args.getString(0)); @@ -116,18 +112,10 @@ public class BrushCommands { LocalConfiguration config = we.getConfiguration(); double radius = args.argsLength() > 1 ? args.getDouble(1) : 2; - if (radius > config.maxBrushRadius) { - player.printError("Maximum allowed brush radius: " - + config.maxBrushRadius); - return; - } + we.checkMaxBrushRadius(radius); int height = args.argsLength() > 2 ? args.getInteger(2) : 1; - if (height > config.maxBrushRadius) { - player.printError("Maximum allowed brush radius/height: " - + config.maxBrushRadius); - return; - } + we.checkMaxBrushRadius(height); BrushTool tool = session.getBrushTool(player.getItemInHand()); Pattern fill = we.getBlockPattern(player, args.getString(0)); @@ -170,13 +158,9 @@ public class BrushCommands { Vector size = clipboard.getSize(); - if (size.getBlockX() > config.maxBrushRadius - || size.getBlockY() > config.maxBrushRadius - || size.getBlockZ() > config.maxBrushRadius) { - player.printError("Maximum allowed brush radius/height: " - + config.maxBrushRadius); - return; - } + we.checkMaxBrushRadius(size.getBlockX()); + we.checkMaxBrushRadius(size.getBlockY()); + we.checkMaxBrushRadius(size.getBlockZ()); BrushTool tool = session.getBrushTool(player.getItemInHand()); tool.setBrush(new ClipboardBrush(clipboard, args.hasFlag('a')), "worldedit.brush.clipboard"); @@ -202,11 +186,7 @@ public class BrushCommands { LocalConfiguration config = we.getConfiguration(); double radius = args.argsLength() > 0 ? args.getDouble(0) : 2; - if (radius > config.maxBrushRadius) { - player.printError("Maximum allowed brush radius: " - + config.maxBrushRadius); - return; - } + we.checkMaxBrushRadius(radius); int iterations = args.argsLength() > 1 ? args.getInteger(1) : 4; @@ -232,11 +212,7 @@ public class BrushCommands { LocalConfiguration config = we.getConfiguration(); double radius = args.argsLength() > 1 ? args.getDouble(1) : 5; - if (radius > config.maxBrushRadius) { - player.printError("Maximum allowed brush radius: " - + config.maxBrushRadius); - return; - } + we.checkMaxBrushRadius(radius); BrushTool tool = session.getBrushTool(player.getItemInHand()); Pattern fill = new SingleBlockPattern(new BaseBlock(0)); @@ -268,11 +244,7 @@ public class BrushCommands { LocalConfiguration config = we.getConfiguration(); double radius = args.argsLength() > 0 ? args.getDouble(0) : 5; - if (radius > config.maxBrushRadius) { - player.printError("Maximum allowed brush radius: " - + config.maxBrushRadius); - return; - } + we.checkMaxBrushRadius(radius); BrushTool tool = session.getBrushTool(player.getItemInHand()); tool.setSize(radius); @@ -303,7 +275,7 @@ public class BrushCommands { // hmmmm not horribly worried about this because -1 is still rather efficient, // the problem arises when butcherMaxRadius is some really high number but not infinite // - original idea taken from https://github.com/sk89q/worldedit/pull/198#issuecomment-6463108 - if (player.hasPermission("worldedit. butcher")) { + if (player.hasPermission("worldedit.butcher")) { maxRadius = Math.max(config.maxBrushRadius, config.butcherMaxRadius); } if (radius > maxRadius) { diff --git a/src/main/java/com/sk89q/worldedit/commands/ToolUtilCommands.java b/src/main/java/com/sk89q/worldedit/commands/ToolUtilCommands.java index 6981add2b..a1c9946f8 100644 --- a/src/main/java/com/sk89q/worldedit/commands/ToolUtilCommands.java +++ b/src/main/java/com/sk89q/worldedit/commands/ToolUtilCommands.java @@ -152,11 +152,7 @@ public class ToolUtilCommands { LocalConfiguration config = we.getConfiguration(); int radius = args.getInteger(0); - if (radius > config.maxBrushRadius) { - player.printError("Maximum allowed brush radius: " - + config.maxBrushRadius); - return; - } + we.checkMaxBrushRadius(radius); session.getBrushTool(player.getItemInHand()).setSize(radius); player.print("Brush size set.");