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 <weswolf@aol.com>
This commit is contained in:
Bukkit/Spigot 2012-10-31 04:16:50 -05:00
parent 80c98cb5d4
commit cc865ea15c
3 changed files with 14 additions and 11 deletions

View file

@ -6,7 +6,6 @@ import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -47,10 +46,9 @@ public class SpawnpointCommand extends VanillaCommand {
if (args.length == 4) { if (args.length == 4) {
if (world != null) { if (world != null) {
int pos = 1; int pos = 1;
int maxPos = 30000000; int x = getInteger(sender, args[pos++], MIN_COORD, MAX_COORD);
int x = getInteger(sender, args[pos++], -maxPos, maxPos);
int y = getInteger(sender, args[pos++], 0, world.getMaxHeight()); 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); player.setBedSpawnLocation(new Location(world, x, y, z), true);
sender.sendMessage("Set " + player.getDisplayName() + "'s spawnpoint to " + x + ", " + y + ", " + z); sender.sendMessage("Set " + player.getDisplayName() + "'s spawnpoint to " + x + ", " + y + ", " + z);

View file

@ -14,6 +14,7 @@ import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
public class TeleportCommand extends VanillaCommand { public class TeleportCommand extends VanillaCommand {
public TeleportCommand() { public TeleportCommand() {
super("tp"); super("tp");
this.description = "Teleports the given player to another player or location"; 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 y = getCoordinate(sender,player.getLocation().getY(), args[args.length - 2], 0, 0);
double z = getCoordinate(sender, player.getLocation().getZ(), args[args.length - 1]); 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!"); sender.sendMessage("Please provide a valid location!");
return true; return true;
} }
@ -73,7 +74,7 @@ public class TeleportCommand extends VanillaCommand {
} }
private double getCoordinate(CommandSender sender, double current, String input) { 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) { 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); if (relative) input = input.substring(1);
double testResult = getDouble(sender, input); double testResult = getDouble(sender, input);
if (testResult == -30000001) { if (testResult == MIN_COORD_MINUS_ONE) {
return -30000001; return MIN_COORD_MINUS_ONE;
} }
result += testResult; result += testResult;
@ -94,11 +95,11 @@ public class TeleportCommand extends VanillaCommand {
} }
if (min != 0 || max != 0) { if (min != 0 || max != 0) {
if (result < min) { if (result < min) {
result = -30000001; result = MIN_COORD_MINUS_ONE;
} }
if (result > max) { if (result > max) {
result = -30000001; result = MIN_COORD_MINUS_ONE;
} }
} }

View file

@ -6,6 +6,10 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
public abstract class VanillaCommand extends Command { 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) { protected VanillaCommand(String name) {
super(name); super(name);
} }
@ -42,7 +46,7 @@ public abstract class VanillaCommand extends Command {
try { try {
return Double.parseDouble(input); return Double.parseDouble(input);
} catch (NumberFormatException ex) { } catch (NumberFormatException ex) {
return -30000001; return MIN_COORD_MINUS_ONE;
} }
} }