Fixed npe, renamed some util methods

This commit is contained in:
Robin Kupper
2011-06-21 22:36:52 +02:00
parent 88048c15d8
commit 85b4fad65d
4 changed files with 23 additions and 25 deletions

View File

@@ -1,7 +1,7 @@
package de.diddiz.LogBlock;
import static de.diddiz.util.BukkitUtils.compressInventory;
import static de.diddiz.util.BukkitUtils.getEntityName;
import static de.diddiz.util.BukkitUtils.entityName;
import static de.diddiz.util.BukkitUtils.rawData;
import java.sql.Connection;
import java.sql.ResultSet;
@@ -176,7 +176,7 @@ public class Consumer extends TimerTask
weapon = ((Player)killer).getItemInHand().getTypeId();
lastAttackedEntity.put(killer.getEntityId(), victim.getEntityId());
lastAttackTime.put(killer.getEntityId(), System.currentTimeMillis());
queueKill(victim.getWorld(), getEntityName(killer), getEntityName(victim), weapon);
queueKill(victim.getWorld(), entityName(killer), entityName(victim), weapon);
}
/**

View File

@@ -1,6 +1,6 @@
package de.diddiz.LogBlock;
import static de.diddiz.util.BukkitUtils.getMaterialName;
import static de.diddiz.util.BukkitUtils.materialName;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
@@ -34,26 +34,26 @@ public class HistoryFormatter
final int itemType = rs.getInt("itemtype");
final int itemAmount = rs.getInt("itemamount");
if (itemType == 0 || itemAmount == 0)
msg.append("looked inside " + getMaterialName(type));
msg.append("looked inside " + materialName(type));
else if (itemAmount < 0)
msg.append("took " + itemAmount * -1 + "x " + getMaterialName(itemType));
msg.append("took " + itemAmount * -1 + "x " + materialName(itemType));
else
msg.append("put in " + itemAmount + "x " + getMaterialName(itemType));
msg.append("put in " + itemAmount + "x " + materialName(itemType));
} else if (type == 69)
msg.append("swiched " + getMaterialName(type));
msg.append("swiched " + materialName(type));
else if (type == 77)
msg.append("pressed " + getMaterialName(type));
msg.append("pressed " + materialName(type));
} else if (type == 0)
msg.append("destroyed " + getMaterialName(replaced));
msg.append("destroyed " + materialName(replaced));
else if (replaced == 0)
msg.append("created " + getMaterialName(type));
msg.append("created " + materialName(type));
else
msg.append("replaced " + getMaterialName(replaced) + " with " + getMaterialName(type));
msg.append("replaced " + materialName(replaced) + " with " + materialName(type));
if (coords)
msg.append(" at " + rs.getInt("x") + ":" + rs.getInt("y") + ":" + rs.getInt("z"));
return msg.toString();
} else if (sum == SummarizationMode.TYPES)
return fillWithSpaces(rs.getInt("created")) + fillWithSpaces(rs.getInt("destroyed")) + getMaterialName(rs.getInt("type"));
return fillWithSpaces(rs.getInt("created")) + fillWithSpaces(rs.getInt("destroyed")) + materialName(rs.getInt("type"));
else
return fillWithSpaces(rs.getInt("created")) + fillWithSpaces(rs.getInt("destroyed")) + rs.getString("playername");
}

View File

@@ -2,8 +2,8 @@ package de.diddiz.LogBlock;
import static de.diddiz.util.BukkitUtils.friendlyWorldname;
import static de.diddiz.util.BukkitUtils.getBlockEquivalents;
import static de.diddiz.util.BukkitUtils.getMaterialName;
import static de.diddiz.util.BukkitUtils.getSenderName;
import static de.diddiz.util.BukkitUtils.materialName;
import static de.diddiz.util.BukkitUtils.senderName;
import static de.diddiz.util.Utils.isInt;
import static de.diddiz.util.Utils.parseTimeSpec;
import java.util.ArrayList;
@@ -90,7 +90,7 @@ public class QueryParams implements Cloneable
final StringBuilder title = new StringBuilder();
if (!types.isEmpty()) {
for (int i = 0; i < types.size(); i++)
title.append(getMaterialName(types.get(i)) + ", ");
title.append(materialName(types.get(i)) + ", ");
title.deleteCharAt(title.length() - 2);
} else
title.append("Block ");
@@ -204,7 +204,7 @@ public class QueryParams implements Cloneable
if (args == null || args.size() == 0)
throw new IllegalArgumentException("No parameters specified.");
final Player player = sender instanceof Player ? (Player)sender : null;
final Session session = prepareToolQuery ? null : logblock.getSession(getSenderName(sender));
final Session session = prepareToolQuery ? null : logblock.getSession(senderName(sender));
if (player != null && world == null)
world = player.getWorld();
for (int i = 0; i < args.size(); i++) {

View File

@@ -110,7 +110,7 @@ public class BukkitUtils
return blockEquivalents;
}
public static String getEntityName(Entity entity) {
public static String entityName(Entity entity) {
if (entity instanceof Player)
return ((Player)entity).getName();
if (entity instanceof TNTPrimed)
@@ -118,14 +118,12 @@ public class BukkitUtils
return entity.getClass().getSimpleName().substring(5);
}
public static String getMaterialName(int type) {
public static String materialName(int type) {
final Material mat = Material.getMaterial(type);
if (mat != null)
return mat.toString().replace('_', ' ').toLowerCase();
return String.valueOf(type);
return mat != null ? mat.toString().replace('_', ' ').toLowerCase() : String.valueOf(type);
}
public static String getSenderName(CommandSender sender) {
public static String senderName(CommandSender sender) {
if (sender instanceof Player)
return ((Player)sender).getName();
if (sender instanceof ConsoleCommandSender)
@@ -136,21 +134,21 @@ public class BukkitUtils
public static void giveTool(Player player, int type) {
final Inventory inv = player.getInventory();
if (inv.contains(type))
player.sendMessage(ChatColor.RED + "You have alredy a " + getMaterialName(type));
player.sendMessage(ChatColor.RED + "You have alredy a " + materialName(type));
else {
final int free = inv.firstEmpty();
if (free >= 0) {
if (player.getItemInHand() != null && player.getItemInHand().getTypeId() != 0)
inv.setItem(free, player.getItemInHand());
player.setItemInHand(new ItemStack(type, 1));
player.sendMessage(ChatColor.GREEN + "Here's your " + getMaterialName(type));
player.sendMessage(ChatColor.GREEN + "Here's your " + materialName(type));
} else
player.sendMessage(ChatColor.RED + "You have no empty slot in your inventory");
}
}
public static byte rawData(ItemStack item) {
return item.getData() != null ? item.getData().getData() : 0;
return item.getType() != null ? (item.getData() != null ? item.getData().getData() : 0) : 0;
}
public static int saveSpawnHeight(Location loc) {