Validate Server method input. Addresses BUKKIT-3687

CraftServer methods that implement the Server interface will throw an
IllegalArgumentException if a method cannot operate on a null input
and given a null pointer.

This causes methods to fail early and identify that a plugin is
responsible for passing in an invalid argument. This will only
change the exception thrown, if there originally was a thrown
exception. This helps with hunting down legitimate problems
with CraftBukkit.
This commit is contained in:
Edmond Poon 2013-03-18 23:32:10 -06:00 committed by Nate Mortensen
parent abee2151ea
commit e639690e45

View file

@ -320,6 +320,8 @@ public final class CraftServer implements Server {
} }
public Player getPlayer(final String name) { public Player getPlayer(final String name) {
Validate.notNull(name, "Name cannot be null");
Player[] players = getOnlinePlayers(); Player[] players = getOnlinePlayers();
Player found = null; Player found = null;
@ -339,6 +341,8 @@ public final class CraftServer implements Server {
} }
public Player getPlayerExact(String name) { public Player getPlayerExact(String name) {
Validate.notNull(name, "Name cannot be null");
String lname = name.toLowerCase(); String lname = name.toLowerCase();
for (Player player : getOnlinePlayers()) { for (Player player : getOnlinePlayers()) {
@ -359,6 +363,8 @@ public final class CraftServer implements Server {
} }
public List<Player> matchPlayer(String partialName) { public List<Player> matchPlayer(String partialName) {
Validate.notNull(partialName, "PartialName cannot be null");
List<Player> matchedPlayers = new ArrayList<Player>(); List<Player> matchedPlayers = new ArrayList<Player>();
for (Player iterPlayer : this.getOnlinePlayers()) { for (Player iterPlayer : this.getOnlinePlayers()) {
@ -511,6 +517,9 @@ public final class CraftServer implements Server {
} }
public boolean dispatchCommand(CommandSender sender, String commandLine) { public boolean dispatchCommand(CommandSender sender, String commandLine) {
Validate.notNull(sender, "Sender cannot be null");
Validate.notNull(commandLine, "CommandLine cannot be null");
if (commandMap.dispatch(sender, commandLine)) { if (commandMap.dispatch(sender, commandLine)) {
return true; return true;
} }
@ -665,9 +674,7 @@ public final class CraftServer implements Server {
} }
public World createWorld(WorldCreator creator) { public World createWorld(WorldCreator creator) {
if (creator == null) { Validate.notNull(creator, "Creator may not be null");
throw new IllegalArgumentException("Creator may not be null");
}
String name = creator.name(); String name = creator.name();
ChunkGenerator generator = creator.generator(); ChunkGenerator generator = creator.generator();
@ -808,6 +815,8 @@ public final class CraftServer implements Server {
} }
public World getWorld(String name) { public World getWorld(String name) {
Validate.notNull(name, "Name cannot be null");
return worlds.get(name.toLowerCase()); return worlds.get(name.toLowerCase());
} }
@ -852,6 +861,8 @@ public final class CraftServer implements Server {
} }
public void configureDbConfig(ServerConfig config) { public void configureDbConfig(ServerConfig config) {
Validate.notNull(config, "Config cannot be null");
DataSourceConfig ds = new DataSourceConfig(); DataSourceConfig ds = new DataSourceConfig();
ds.setDriver(configuration.getString("database.driver")); ds.setDriver(configuration.getString("database.driver"));
ds.setUrl(configuration.getString("database.url")); ds.setUrl(configuration.getString("database.url"));
@ -888,6 +899,8 @@ public final class CraftServer implements Server {
} }
public List<Recipe> getRecipesFor(ItemStack result) { public List<Recipe> getRecipesFor(ItemStack result) {
Validate.notNull(result, "Result cannot be null");
List<Recipe> results = new ArrayList<Recipe>(); List<Recipe> results = new ArrayList<Recipe>();
Iterator<Recipe> iter = recipeIterator(); Iterator<Recipe> iter = recipeIterator();
while (iter.hasNext()) { while (iter.hasNext()) {
@ -1015,6 +1028,8 @@ public final class CraftServer implements Server {
} }
public CraftMapView createMap(World world) { public CraftMapView createMap(World world) {
Validate.notNull(world, "World cannot be null");
net.minecraft.server.ItemStack stack = new net.minecraft.server.ItemStack(Item.MAP, 1, -1); net.minecraft.server.ItemStack stack = new net.minecraft.server.ItemStack(Item.MAP, 1, -1);
WorldMap worldmap = Item.MAP.getSavedMap(stack, ((CraftWorld) world).getHandle()); WorldMap worldmap = Item.MAP.getSavedMap(stack, ((CraftWorld) world).getHandle());
return worldmap.mapView; return worldmap.mapView;
@ -1044,6 +1059,8 @@ public final class CraftServer implements Server {
} }
public OfflinePlayer getOfflinePlayer(String name, boolean search) { public OfflinePlayer getOfflinePlayer(String name, boolean search) {
Validate.notNull(name, "Name cannot be null");
OfflinePlayer result = getPlayerExact(name); OfflinePlayer result = getPlayerExact(name);
String lname = name.toLowerCase(); String lname = name.toLowerCase();
@ -1078,6 +1095,8 @@ public final class CraftServer implements Server {
} }
public void banIP(String address) { public void banIP(String address) {
Validate.notNull(address, "Address cannot be null.");
BanEntry entry = new BanEntry(address); BanEntry entry = new BanEntry(address);
playerList.getIPBans().add(entry); playerList.getIPBans().add(entry);
playerList.getIPBans().save(); playerList.getIPBans().save();
@ -1135,9 +1154,7 @@ public final class CraftServer implements Server {
} }
public void setDefaultGameMode(GameMode mode) { public void setDefaultGameMode(GameMode mode) {
if (mode == null) { Validate.notNull(mode, "Mode cannot be null");
throw new IllegalArgumentException("Mode cannot be null");
}
for (World world : getWorlds()) { for (World world : getWorlds()) {
((CraftWorld) world).getHandle().worldData.setGameType(EnumGamemode.a(mode.getValue())); ((CraftWorld) world).getHandle().worldData.setGameType(EnumGamemode.a(mode.getValue()));