From cc865ea15c3fe20cb3af22db4f8ec4df63ed6a85 Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Wed, 31 Oct 2012 04:16:50 -0500 Subject: [PATCH] Replace 'Magic Numbers' in commands. These numbers are mirrored in vanilla code as the coordinate limits for a world. Replaced usages to a static final member for code readability. By: Wesley Wolfe --- .../bukkit/command/defaults/SpawnpointCommand.java | 6 ++---- .../bukkit/command/defaults/TeleportCommand.java | 13 +++++++------ .../org/bukkit/command/defaults/VanillaCommand.java | 6 +++++- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/paper-api/src/main/java/org/bukkit/command/defaults/SpawnpointCommand.java b/paper-api/src/main/java/org/bukkit/command/defaults/SpawnpointCommand.java index 50692157cd..02dcd4f7c0 100644 --- a/paper-api/src/main/java/org/bukkit/command/defaults/SpawnpointCommand.java +++ b/paper-api/src/main/java/org/bukkit/command/defaults/SpawnpointCommand.java @@ -6,7 +6,6 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.World; -import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -47,10 +46,9 @@ public class SpawnpointCommand extends VanillaCommand { if (args.length == 4) { if (world != null) { int pos = 1; - int maxPos = 30000000; - int x = getInteger(sender, args[pos++], -maxPos, maxPos); + int x = getInteger(sender, args[pos++], MIN_COORD, MAX_COORD); int y = getInteger(sender, args[pos++], 0, world.getMaxHeight()); - int z = getInteger(sender, args[pos], -maxPos, maxPos); + int z = getInteger(sender, args[pos], MIN_COORD, MAX_COORD); player.setBedSpawnLocation(new Location(world, x, y, z), true); sender.sendMessage("Set " + player.getDisplayName() + "'s spawnpoint to " + x + ", " + y + ", " + z); diff --git a/paper-api/src/main/java/org/bukkit/command/defaults/TeleportCommand.java b/paper-api/src/main/java/org/bukkit/command/defaults/TeleportCommand.java index cbb42abe34..2c2e11f0f0 100644 --- a/paper-api/src/main/java/org/bukkit/command/defaults/TeleportCommand.java +++ b/paper-api/src/main/java/org/bukkit/command/defaults/TeleportCommand.java @@ -14,6 +14,7 @@ import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import com.google.common.collect.ImmutableList; public class TeleportCommand extends VanillaCommand { + public TeleportCommand() { super("tp"); this.description = "Teleports the given player to another player or location"; @@ -60,7 +61,7 @@ public class TeleportCommand extends VanillaCommand { double y = getCoordinate(sender,player.getLocation().getY(), args[args.length - 2], 0, 0); double z = getCoordinate(sender, player.getLocation().getZ(), args[args.length - 1]); - if (x == -30000001 || y == -30000001 || z == -30000001) { + if (x == MIN_COORD_MINUS_ONE || y == MIN_COORD_MINUS_ONE || z == MIN_COORD_MINUS_ONE) { sender.sendMessage("Please provide a valid location!"); return true; } @@ -73,7 +74,7 @@ public class TeleportCommand extends VanillaCommand { } private double getCoordinate(CommandSender sender, double current, String input) { - return getCoordinate(sender, current, input, -30000000, 30000000); + return getCoordinate(sender, current, input, MIN_COORD, MAX_COORD); } private double getCoordinate(CommandSender sender, double current, String input, int min, int max) { @@ -85,8 +86,8 @@ public class TeleportCommand extends VanillaCommand { if (relative) input = input.substring(1); double testResult = getDouble(sender, input); - if (testResult == -30000001) { - return -30000001; + if (testResult == MIN_COORD_MINUS_ONE) { + return MIN_COORD_MINUS_ONE; } result += testResult; @@ -94,11 +95,11 @@ public class TeleportCommand extends VanillaCommand { } if (min != 0 || max != 0) { if (result < min) { - result = -30000001; + result = MIN_COORD_MINUS_ONE; } if (result > max) { - result = -30000001; + result = MIN_COORD_MINUS_ONE; } } diff --git a/paper-api/src/main/java/org/bukkit/command/defaults/VanillaCommand.java b/paper-api/src/main/java/org/bukkit/command/defaults/VanillaCommand.java index d762e64fcb..7b893bd926 100644 --- a/paper-api/src/main/java/org/bukkit/command/defaults/VanillaCommand.java +++ b/paper-api/src/main/java/org/bukkit/command/defaults/VanillaCommand.java @@ -6,6 +6,10 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandSender; public abstract class VanillaCommand extends Command { + static final int MAX_COORD = 30000000; + static final int MIN_COORD_MINUS_ONE = -30000001; + static final int MIN_COORD = -30000000; + protected VanillaCommand(String name) { super(name); } @@ -42,7 +46,7 @@ public abstract class VanillaCommand extends Command { try { return Double.parseDouble(input); } catch (NumberFormatException ex) { - return -30000001; + return MIN_COORD_MINUS_ONE; } }