Stop using IAE to communicate parameter mis-use

This commit is contained in:
Kenzie Togami 2019-05-14 17:57:05 -07:00
parent 718c2e8306
commit e7613dd879
No known key found for this signature in database
GPG Key ID: 5D200B325E157A81
4 changed files with 37 additions and 17 deletions

View File

@ -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.ALL;
import static com.sk89q.worldedit.command.util.Logging.LogMode.PLACEMENT; 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.command.util.Logging.LogMode.POSITION;
import static com.sk89q.worldedit.internal.command.CommandUtil.checkCommandArgument;
/** /**
* Commands for the generation of shapes and other objects. * Commands for the generation of shapes and other objects.
@ -203,9 +204,7 @@ public class GenerationCommands {
TreeType type, TreeType type,
@Arg(desc = "The density of the forest, between 0 and 100", def = "5") @Arg(desc = "The density of the forest, between 0 and 100", def = "5")
double density) throws WorldEditException { double density) throws WorldEditException {
if (density < 0 || density > 100) { checkCommandArgument(0 <= density && density <= 100, "Density must be between 0 and 100");
throw new IllegalArgumentException("Density must be between 0 and 100");
}
density = density / 100; density = density / 100;
int affected = editSession.makeForest(session.getPlacementPosition(player), size, density, type); int affected = editSession.makeForest(session.getPlacementPosition(player), size, density, type);
player.print(affected + " trees created."); player.print(affected + " trees created.");

View File

@ -61,10 +61,10 @@ import org.enginehub.piston.annotation.param.Switch;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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.ALL;
import static com.sk89q.worldedit.command.util.Logging.LogMode.ORIENTATION_REGION; 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.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.asFlatRegion;
import static com.sk89q.worldedit.regions.Regions.maximumBlockY; import static com.sk89q.worldedit.regions.Regions.maximumBlockY;
import static com.sk89q.worldedit.regions.Regions.minimumBlockY; import static com.sk89q.worldedit.regions.Regions.minimumBlockY;
@ -125,7 +125,7 @@ public class RegionCommands {
player.printError("//line only works with cuboid selections"); player.printError("//line only works with cuboid selections");
return 0; return 0;
} }
checkArgument(thickness >= 0, "Thickness must be >= 0"); checkCommandArgument(thickness >= 0, "Thickness must be >= 0");
CuboidRegion cuboidregion = (CuboidRegion) region; CuboidRegion cuboidregion = (CuboidRegion) region;
BlockVector3 pos1 = cuboidregion.getPos1(); BlockVector3 pos1 = cuboidregion.getPos1();
@ -155,7 +155,7 @@ public class RegionCommands {
player.printError("//curve only works with convex polyhedral selections"); player.printError("//curve only works with convex polyhedral selections");
return 0; return 0;
} }
checkArgument(thickness >= 0, "Thickness must be >= 0"); checkCommandArgument(thickness >= 0, "Thickness must be >= 0");
ConvexPolyhedralRegion cpregion = (ConvexPolyhedralRegion) region; ConvexPolyhedralRegion cpregion = (ConvexPolyhedralRegion) region;
List<BlockVector3> vectors = new ArrayList<>(cpregion.getVertices()); List<BlockVector3> vectors = new ArrayList<>(cpregion.getVertices());
@ -294,9 +294,7 @@ public class RegionCommands {
boolean moveSelection, boolean moveSelection,
@Switch(name = 'a', desc = "Ignore air blocks") @Switch(name = 'a', desc = "Ignore air blocks")
boolean ignoreAirBlocks) throws WorldEditException { boolean ignoreAirBlocks) throws WorldEditException {
if (count < 1) { checkCommandArgument(count >= 1, "Count must be >= 1");
throw new IllegalArgumentException("Count must be >= 1");
}
int affected = editSession.moveRegion(region, direction, count, !ignoreAirBlocks, replace); int affected = editSession.moveRegion(region, direction, count, !ignoreAirBlocks, replace);
@ -433,7 +431,7 @@ public class RegionCommands {
int thickness, int thickness,
@Arg(desc = "The pattern of blocks to replace the hollowed area with", def = "air") @Arg(desc = "The pattern of blocks to replace the hollowed area with", def = "air")
Pattern pattern) throws WorldEditException { 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); int affected = editSession.hollowOutRegion(region, thickness, pattern);
player.print(affected + " block(s) have been changed."); player.print(affected + " block(s) have been changed.");
@ -451,7 +449,7 @@ public class RegionCommands {
TreeType type, TreeType type,
@Arg(desc = "The density of the forest", def = "5") @Arg(desc = "The density of the forest", def = "5")
double density) throws WorldEditException { 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); int affected = editSession.makeForest(region, density / 100, type);
player.print(affected + " trees created."); player.print(affected + " trees created.");
return affected; return affected;
@ -466,7 +464,7 @@ public class RegionCommands {
public int flora(Player player, EditSession editSession, @Selection Region region, public int flora(Player player, EditSession editSession, @Selection Region region,
@Arg(desc = "The density of the forest", def = "5") @Arg(desc = "The density of the forest", def = "5")
double density) throws WorldEditException { 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; density = density / 100;
FloraGenerator generator = new FloraGenerator(editSession); FloraGenerator generator = new FloraGenerator(editSession);
GroundFunction ground = new GroundFunction(new ExistingBlockMask(editSession), generator); GroundFunction ground = new GroundFunction(new ExistingBlockMask(editSession), generator);

View File

@ -19,10 +19,14 @@
package com.sk89q.worldedit.internal.command; package com.sk89q.worldedit.internal.command;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.sk89q.worldedit.extension.platform.PlatformCommandManager; import com.sk89q.worldedit.extension.platform.PlatformCommandManager;
import com.sk89q.worldedit.internal.util.Substring; 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.Command;
import org.enginehub.piston.exception.CommandException;
import org.enginehub.piston.part.SubCommandPart; import org.enginehub.piston.part.SubCommandPart;
import java.util.Comparator; import java.util.Comparator;
@ -92,6 +96,30 @@ public class CommandUtil {
return Optional.of(builder.toString()); 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() { private CommandUtil() {
} }
} }

View File

@ -165,11 +165,6 @@ public class WorldEditExceptionConverter extends ExceptionConverterHelper {
throw newCommandException(e.getMessage(), e); throw newCommandException(e.getMessage(), e);
} }
@ExceptionMatch
public void convert(IllegalArgumentException e) throws CommandException {
throw newCommandException(e.getMessage(), e);
}
// Prevent investigation into UsageExceptions // Prevent investigation into UsageExceptions
@ExceptionMatch @ExceptionMatch
public void convert(UsageException e) throws CommandException { public void convert(UsageException e) throws CommandException {