From e7613dd8795bc8bb0add5f1ff52df82c7d358054 Mon Sep 17 00:00:00 2001 From: Kenzie Togami Date: Tue, 14 May 2019 17:57:05 -0700 Subject: [PATCH] Stop using IAE to communicate parameter mis-use --- .../worldedit/command/GenerationCommands.java | 5 ++-- .../worldedit/command/RegionCommands.java | 16 +++++------ .../internal/command/CommandUtil.java | 28 +++++++++++++++++++ .../WorldEditExceptionConverter.java | 5 ---- 4 files changed, 37 insertions(+), 17 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/GenerationCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/GenerationCommands.java index 5f5229aeb..6c370a1a1 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/GenerationCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/GenerationCommands.java @@ -47,6 +47,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.sk89q.worldedit.command.util.Logging.LogMode.ALL; import static com.sk89q.worldedit.command.util.Logging.LogMode.PLACEMENT; import static com.sk89q.worldedit.command.util.Logging.LogMode.POSITION; +import static com.sk89q.worldedit.internal.command.CommandUtil.checkCommandArgument; /** * Commands for the generation of shapes and other objects. @@ -203,9 +204,7 @@ public class GenerationCommands { TreeType type, @Arg(desc = "The density of the forest, between 0 and 100", def = "5") double density) throws WorldEditException { - if (density < 0 || density > 100) { - throw new IllegalArgumentException("Density must be between 0 and 100"); - } + checkCommandArgument(0 <= density && density <= 100, "Density must be between 0 and 100"); density = density / 100; int affected = editSession.makeForest(session.getPlacementPosition(player), size, density, type); player.print(affected + " trees created."); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/RegionCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/RegionCommands.java index a8591e1de..ec3c09e4a 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/RegionCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/RegionCommands.java @@ -61,10 +61,10 @@ import org.enginehub.piston.annotation.param.Switch; import java.util.ArrayList; import java.util.List; -import static com.google.common.base.Preconditions.checkArgument; import static com.sk89q.worldedit.command.util.Logging.LogMode.ALL; import static com.sk89q.worldedit.command.util.Logging.LogMode.ORIENTATION_REGION; import static com.sk89q.worldedit.command.util.Logging.LogMode.REGION; +import static com.sk89q.worldedit.internal.command.CommandUtil.checkCommandArgument; import static com.sk89q.worldedit.regions.Regions.asFlatRegion; import static com.sk89q.worldedit.regions.Regions.maximumBlockY; import static com.sk89q.worldedit.regions.Regions.minimumBlockY; @@ -125,7 +125,7 @@ public class RegionCommands { player.printError("//line only works with cuboid selections"); return 0; } - checkArgument(thickness >= 0, "Thickness must be >= 0"); + checkCommandArgument(thickness >= 0, "Thickness must be >= 0"); CuboidRegion cuboidregion = (CuboidRegion) region; BlockVector3 pos1 = cuboidregion.getPos1(); @@ -155,7 +155,7 @@ public class RegionCommands { player.printError("//curve only works with convex polyhedral selections"); return 0; } - checkArgument(thickness >= 0, "Thickness must be >= 0"); + checkCommandArgument(thickness >= 0, "Thickness must be >= 0"); ConvexPolyhedralRegion cpregion = (ConvexPolyhedralRegion) region; List vectors = new ArrayList<>(cpregion.getVertices()); @@ -294,9 +294,7 @@ public class RegionCommands { boolean moveSelection, @Switch(name = 'a', desc = "Ignore air blocks") boolean ignoreAirBlocks) throws WorldEditException { - if (count < 1) { - throw new IllegalArgumentException("Count must be >= 1"); - } + checkCommandArgument(count >= 1, "Count must be >= 1"); int affected = editSession.moveRegion(region, direction, count, !ignoreAirBlocks, replace); @@ -433,7 +431,7 @@ public class RegionCommands { int thickness, @Arg(desc = "The pattern of blocks to replace the hollowed area with", def = "air") Pattern pattern) throws WorldEditException { - checkArgument(thickness >= 0, "Thickness must be >= 0"); + checkCommandArgument(thickness >= 0, "Thickness must be >= 0"); int affected = editSession.hollowOutRegion(region, thickness, pattern); player.print(affected + " block(s) have been changed."); @@ -451,7 +449,7 @@ public class RegionCommands { TreeType type, @Arg(desc = "The density of the forest", def = "5") double density) throws WorldEditException { - checkArgument(0 <= density && density <= 100, "Density must be in [0, 100]"); + checkCommandArgument(0 <= density && density <= 100, "Density must be in [0, 100]"); int affected = editSession.makeForest(region, density / 100, type); player.print(affected + " trees created."); return affected; @@ -466,7 +464,7 @@ public class RegionCommands { public int flora(Player player, EditSession editSession, @Selection Region region, @Arg(desc = "The density of the forest", def = "5") double density) throws WorldEditException { - checkArgument(0 <= density && density <= 100, "Density must be in [0, 100]"); + checkCommandArgument(0 <= density && density <= 100, "Density must be in [0, 100]"); density = density / 100; FloraGenerator generator = new FloraGenerator(editSession); GroundFunction ground = new GroundFunction(new ExistingBlockMask(editSession), generator); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/CommandUtil.java b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/CommandUtil.java index 395153e7e..e3032fa95 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/CommandUtil.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/CommandUtil.java @@ -19,10 +19,14 @@ package com.sk89q.worldedit.internal.command; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.sk89q.worldedit.extension.platform.PlatformCommandManager; import com.sk89q.worldedit.internal.util.Substring; +import com.sk89q.worldedit.util.formatting.text.Component; +import com.sk89q.worldedit.util.formatting.text.TextComponent; import org.enginehub.piston.Command; +import org.enginehub.piston.exception.CommandException; import org.enginehub.piston.part.SubCommandPart; import java.util.Comparator; @@ -92,6 +96,30 @@ public class CommandUtil { return Optional.of(builder.toString()); } + /** + * Require {@code condition} to be {@code true}, otherwise throw a {@link CommandException} + * with the given message. + * + * @param condition the condition to check + * @param message the message for failure + */ + public static void checkCommandArgument(boolean condition, String message) { + checkCommandArgument(condition, TextComponent.of(message)); + } + + /** + * Require {@code condition} to be {@code true}, otherwise throw a {@link CommandException} + * with the given message. + * + * @param condition the condition to check + * @param message the message for failure + */ + public static void checkCommandArgument(boolean condition, Component message) { + if (!condition) { + throw new CommandException(message, ImmutableList.of()); + } + } + private CommandUtil() { } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/exception/WorldEditExceptionConverter.java b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/exception/WorldEditExceptionConverter.java index b200e11b3..5270cab87 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/exception/WorldEditExceptionConverter.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/exception/WorldEditExceptionConverter.java @@ -165,11 +165,6 @@ public class WorldEditExceptionConverter extends ExceptionConverterHelper { throw newCommandException(e.getMessage(), e); } - @ExceptionMatch - public void convert(IllegalArgumentException e) throws CommandException { - throw newCommandException(e.getMessage(), e); - } - // Prevent investigation into UsageExceptions @ExceptionMatch public void convert(UsageException e) throws CommandException {