From 85b4fad65d8becf6b13f04ee614f6a43fb13a884 Mon Sep 17 00:00:00 2001 From: Robin Kupper Date: Tue, 21 Jun 2011 22:36:52 +0200 Subject: [PATCH] Fixed npe, renamed some util methods --- src/de/diddiz/LogBlock/Consumer.java | 4 ++-- src/de/diddiz/LogBlock/HistoryFormatter.java | 20 ++++++++++---------- src/de/diddiz/LogBlock/QueryParams.java | 8 ++++---- src/de/diddiz/util/BukkitUtils.java | 16 +++++++--------- 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/de/diddiz/LogBlock/Consumer.java b/src/de/diddiz/LogBlock/Consumer.java index 50f10a0..82b09db 100644 --- a/src/de/diddiz/LogBlock/Consumer.java +++ b/src/de/diddiz/LogBlock/Consumer.java @@ -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); } /** diff --git a/src/de/diddiz/LogBlock/HistoryFormatter.java b/src/de/diddiz/LogBlock/HistoryFormatter.java index c7fe700..7b4b39b 100644 --- a/src/de/diddiz/LogBlock/HistoryFormatter.java +++ b/src/de/diddiz/LogBlock/HistoryFormatter.java @@ -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"); } diff --git a/src/de/diddiz/LogBlock/QueryParams.java b/src/de/diddiz/LogBlock/QueryParams.java index eb29b93..a808032 100644 --- a/src/de/diddiz/LogBlock/QueryParams.java +++ b/src/de/diddiz/LogBlock/QueryParams.java @@ -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++) { diff --git a/src/de/diddiz/util/BukkitUtils.java b/src/de/diddiz/util/BukkitUtils.java index 184b194..ef44cc6 100644 --- a/src/de/diddiz/util/BukkitUtils.java +++ b/src/de/diddiz/util/BukkitUtils.java @@ -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) {