From 02938bc6261fb961ec3a0c51d8a332786082b1cf Mon Sep 17 00:00:00 2001 From: j502647092 Date: Sun, 14 Jun 2015 15:55:22 +0800 Subject: [PATCH] Performance optimization... Signed-off-by: j502647092 --- .../listeners/InventoryListener.java | 354 ++++++++---------- .../citycraft/RealBackpacks/util/RBUtil.java | 83 ++-- 2 files changed, 200 insertions(+), 237 deletions(-) diff --git a/src/cn/citycraft/RealBackpacks/listeners/InventoryListener.java b/src/cn/citycraft/RealBackpacks/listeners/InventoryListener.java index aeddf08..b18eefa 100644 --- a/src/cn/citycraft/RealBackpacks/listeners/InventoryListener.java +++ b/src/cn/citycraft/RealBackpacks/listeners/InventoryListener.java @@ -28,199 +28,17 @@ public class InventoryListener implements Listener { this.plugin = plugin; } - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void onInventoryClose(final InventoryCloseEvent e) { - final String name = e.getPlayer().getName(); - final Inventory inv = e.getView().getTopInventory(); - final List invString = Serialization.toString(inv); - final String backpack = plugin.playerData.get(name); - plugin.playerData.remove(name); - final String adminBackpack = plugin.adminFullView.get(name); - plugin.adminFullView.remove(name); - if (backpack != null) { - plugin.getServer().getScheduler() - .runTaskAsynchronously(plugin, new Runnable() { - @Override - public void run() { - if (plugin.isUsingMysql()) { - try { - MysqlFunctions.addBackpackData(name, - backpack, invString); - } catch (final SQLException e) { - e.printStackTrace(); - } - } else { - PlayerConfig.getInstance(plugin, name).set(backpack + ".Inventory", invString); - PlayerConfig.save(); - } - } - }); - } else if (adminBackpack != null) { - plugin.getServer().getScheduler() - .runTaskAsynchronously(plugin, new Runnable() { - @Override - public void run() { - final String[] split = adminBackpack.split(":"); - if (plugin.isUsingMysql()) { - try { - MysqlFunctions.addBackpackData(split[0], - split[1], invString); - } catch (final SQLException e) { - e.printStackTrace(); - } - } else { - PlayerConfig.getInstance(plugin, split[1]). - set(split[0] + ".Inventory", invString); - PlayerConfig.save(); - } - } - }); - } else if (plugin.adminRestrictedView.contains(name)) { - plugin.adminRestrictedView.remove(name); - } - } - - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) - public void onInventoryClick(final InventoryClickEvent e) { - if (e.getWhoClicked() instanceof Player) { - - final Player p = (Player) e.getWhoClicked(); - final String name = p.getName(); - - if (plugin.adminRestrictedView.contains(name)) { - e.setCancelled(true); - return; - } - - final Inventory otherInv = e.getView().getTopInventory(); - final ItemStack curItem = e.getCurrentItem(); - boolean otherInvPresent = false; - - if (otherInv != null) - otherInvPresent = true; - - if (curItem != null && curItem.hasItemMeta() - && curItem.getItemMeta().hasDisplayName()) { - for (final String backpack : plugin.backpacks) { - if (curItem.isSimilar(plugin.backpackItems.get(backpack))) { - plugin.slowedPlayers.remove(name); - p.setWalkSpeed(0.2F); - break; - } - } - } - - if (!e.isCancelled() && curItem != null && otherInvPresent) { - - if (plugin.playerData.containsKey(name)) { - - final String backpack = plugin.playerData.get(name); - final List key = plugin.backpackData.get(backpack); - final ItemStack cursor = e.getCursor(); - boolean go = true; - - if (key.get(16) != null - && key.get(16).equalsIgnoreCase("true")) { - for (final String whitelist : plugin.backpackWhitelist - .get(backpack)) { - if (whitelist == null) { - continue; - } - String potentialBackpack = RBUtil - .stringToBackpack(whitelist); - if (potentialBackpack != null - && plugin.backpackItems - .containsKey(potentialBackpack)) { - if (curItem.isSimilar(plugin.backpackItems - .get(potentialBackpack)) - || cursor - .isSimilar(plugin.backpackItems - .get(potentialBackpack))) { - go = false; - break; - } - } else { - if (RBUtil.itemsAreEqual(curItem, whitelist) - || RBUtil.itemsAreEqual(cursor, - whitelist)) { - go = false; - break; - } - } - } - if (go) { - e.setCancelled(true); - p.sendMessage(ChatColor.RED + "当前物品不能放入背包..."); - return; - } - } - - for (final String blacklist : plugin.backpackBlacklist - .get(backpack)) { - if (blacklist == null) { - continue; - } - String potentialBackpack = RBUtil - .stringToBackpack(blacklist); - if (potentialBackpack != null - && plugin.backpackItems - .containsKey(potentialBackpack)) { - if (isSimilar(curItem, - plugin.backpackItems.get(potentialBackpack))) { - e.setCancelled(true); - p.sendMessage(ChatColor.RED + "当前物品不能放入背包..."); - return; - } - } else { - if (RBUtil.itemsAreEqual(curItem, blacklist)) { - e.setCancelled(true); - p.sendMessage(ChatColor.RED + "当前物品不能放入背包..."); - return; - } - } - } - } - - } - - /* - * Dupes for (String backpack : plugin.backpackItems.keySet()) { if - * (p.getInventory().contains(plugin.backpackItems.get(backpack))) { - * plugin.getServer().getScheduler().runTaskLater(plugin, new - * Runnable() { - * - * @Override public void run() { Location loc = p.getLocation(); - * World world = p.getWorld(); for (ItemStack item : - * p.getInventory().getContents()) { if (item != null && - * item.hasItemMeta() && item.getAmount() > 1) { for (String - * backpack : plugin.backpacks) { String unstackable = - * plugin.backpackData.get(backpack).get(18); if (unstackable == - * null || unstackable.equalsIgnoreCase("false")) { continue; } if - * (item.isSimilar(plugin.backpackItems.get(backpack))) { while - * (item.getAmount() > 1) { item.setAmount(item.getAmount() - 1); if - * (p.getInventory().firstEmpty() != -1) { - * p.getInventory().setItem(p.getInventory().firstEmpty(), item); - * p.updateInventory(); } else { world.dropItemNaturally(loc, - * plugin.backpackItems.get(backpack)); } } } } } } } }, 2L); break; - * } } - */ - - } - } - @SuppressWarnings("deprecation") public boolean isSimilar(ItemStack origin, ItemStack compare) { - if (compare == null) { + if (compare == null) return false; - } - if (compare == origin) { + if (compare == origin) return true; - } return compare.getTypeId() == origin.getTypeId() && compare.getDurability() == origin.getDurability() && compare.hasItemMeta() == origin.hasItemMeta() - && (compare.hasItemMeta() ? isSimilarMeta( - compare.getItemMeta(), origin.getItemMeta()) : true); + && (compare.hasItemMeta() ? isSimilarMeta(compare.getItemMeta(), + origin.getItemMeta()) : true); } public boolean isSimilarMeta(ItemMeta origin, ItemMeta compare) { @@ -240,11 +58,171 @@ public class InventoryListener implements Listener { return false; } if (origin.hasLore() && compare.hasLore()) { - if (!(origin.getLore().containsAll(compare.getLore()) || compare - .getLore().containsAll(origin.getLore()))) + if (!(compare.getLore().containsAll(origin.getLore()))) return false; } return true; } + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + public void onInventoryClick(final InventoryClickEvent e) { + if (e.getWhoClicked() instanceof Player) { + + final Player p = (Player) e.getWhoClicked(); + final String name = p.getName(); + + if (plugin.adminRestrictedView.contains(name)) { + e.setCancelled(true); + return; + } + + final Inventory otherInv = e.getView().getTopInventory(); + final ItemStack curItem = e.getCurrentItem(); + boolean otherInvPresent = false; + + if (otherInv != null) { + otherInvPresent = true; + } + + if (curItem != null && curItem.hasItemMeta() && curItem.getItemMeta().hasDisplayName()) { + for (final String backpack : plugin.backpacks) { + if (curItem.isSimilar(plugin.backpackItems.get(backpack))) { + plugin.slowedPlayers.remove(name); + p.setWalkSpeed(0.2F); + break; + } + } + } + + if (!e.isCancelled() && curItem != null && otherInvPresent) { + + if (plugin.playerData.containsKey(name)) { + + final String backpack = plugin.playerData.get(name); + final List key = plugin.backpackData.get(backpack); + final ItemStack cursor = e.getCursor(); + boolean go = true; + + if (key.get(16) != null && key.get(16).equalsIgnoreCase("true")) { + for (final String whitelist : plugin.backpackWhitelist.get(backpack)) { + if (whitelist == null) { + continue; + } + String potentialBackpack = RBUtil.stringToBackpack(whitelist); + if (potentialBackpack != null + && plugin.backpackItems.containsKey(potentialBackpack)) { + if (curItem.isSimilar(plugin.backpackItems.get(potentialBackpack)) + || cursor.isSimilar(plugin.backpackItems + .get(potentialBackpack))) { + go = false; + break; + } + } else { + if (RBUtil.itemsAreEqual(curItem, whitelist) + || RBUtil.itemsAreEqual(cursor, whitelist)) { + go = false; + break; + } + } + } + if (go) { + e.setCancelled(true); + p.sendMessage(ChatColor.RED + "当前物品不能放入背包..."); + return; + } + } + + for (final String blacklist : plugin.backpackBlacklist.get(backpack)) { + if (blacklist == null) { + continue; + } + String potentialBackpack = RBUtil.stringToBackpack(blacklist); + if (potentialBackpack != null + && plugin.backpackItems.containsKey(potentialBackpack)) { + if (isSimilar(curItem, plugin.backpackItems.get(potentialBackpack))) { + e.setCancelled(true); + p.sendMessage(ChatColor.RED + "当前物品不能放入背包..."); + return; + } + } else { + if (RBUtil.itemsAreEqual(curItem, blacklist)) { + e.setCancelled(true); + p.sendMessage(ChatColor.RED + "当前物品不能放入背包..."); + return; + } + } + } + } + + } + + /* + * Dupes for (String backpack : plugin.backpackItems.keySet()) { if + * (p.getInventory().contains(plugin.backpackItems.get(backpack))) { + * plugin.getServer().getScheduler().runTaskLater(plugin, new Runnable() { + * + * @Override public void run() { Location loc = p.getLocation(); World world = + * p.getWorld(); for (ItemStack item : p.getInventory().getContents()) { if (item != + * null && item.hasItemMeta() && item.getAmount() > 1) { for (String backpack : + * plugin.backpacks) { String unstackable = plugin.backpackData.get(backpack).get(18); + * if (unstackable == null || unstackable.equalsIgnoreCase("false")) { continue; } if + * (item.isSimilar(plugin.backpackItems.get(backpack))) { while (item.getAmount() > 1) { + * item.setAmount(item.getAmount() - 1); if (p.getInventory().firstEmpty() != -1) { + * p.getInventory().setItem(p.getInventory().firstEmpty(), item); p.updateInventory(); } + * else { world.dropItemNaturally(loc, plugin.backpackItems.get(backpack)); } } } } } } + * } }, 2L); break; } } + */ + + } + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onInventoryClose(final InventoryCloseEvent e) { + final String name = e.getPlayer().getName(); + final Inventory inv = e.getView().getTopInventory(); + final List invString = Serialization.toString(inv); + final String backpack = plugin.playerData.get(name); + plugin.playerData.remove(name); + final String adminBackpack = plugin.adminFullView.get(name); + plugin.adminFullView.remove(name); + if (backpack != null) { + plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() { + @Override + public void run() { + if (plugin.isUsingMysql()) { + try { + MysqlFunctions.addBackpackData(name, backpack, invString); + } catch (final SQLException e) { + e.printStackTrace(); + } + } else { + PlayerConfig.getInstance(plugin, name).set(backpack + ".Inventory", + invString); + PlayerConfig.save(); + } + } + }); + } else if (adminBackpack != null) { + plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() { + @Override + public void run() { + final String[] split = adminBackpack.split(":"); + if (plugin.isUsingMysql()) { + try { + MysqlFunctions.addBackpackData(split[0], split[1], invString); + } catch (final SQLException e) { + e.printStackTrace(); + } + } else { + PlayerConfig.getInstance(plugin, split[1]).set(split[0] + ".Inventory", + invString); + PlayerConfig.save(); + } + } + }); + } else if (plugin.adminRestrictedView.contains(name)) { + plugin.adminRestrictedView.remove(name); + } + } + } diff --git a/src/cn/citycraft/RealBackpacks/util/RBUtil.java b/src/cn/citycraft/RealBackpacks/util/RBUtil.java index a1e64b3..69b5baa 100644 --- a/src/cn/citycraft/RealBackpacks/util/RBUtil.java +++ b/src/cn/citycraft/RealBackpacks/util/RBUtil.java @@ -14,10 +14,6 @@ public class RBUtil { private static RealBackpacks plugin; - public static void setRBUtil(final RealBackpacks plugin) { - RBUtil.plugin = plugin; - } - public static void destroyContents(final String name, final String backpack) { plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() { @Override @@ -56,15 +52,6 @@ public class RBUtil { return i; } - public static String stringToBackpack(String s) { - for (final String b : plugin.backpacks) { - if (b.equalsIgnoreCase(s)) { - return b; - } - } - return null; - } - @SuppressWarnings("deprecation") public static ItemStack getItemstackFromString(final String s) { ItemStack item = null; @@ -72,23 +59,32 @@ public class RBUtil { if (split.length == 1) { item = new ItemStack(Material.getMaterial(Integer.parseInt(split[0])), 1); } else { - if (split[1].equalsIgnoreCase("enchant") || split[1].equalsIgnoreCase("lore") || split[1].equalsIgnoreCase("all")) { + if (split[1].equalsIgnoreCase("enchant") || split[1].equalsIgnoreCase("lore") + || split[1].equalsIgnoreCase("all")) { item = new ItemStack(Material.getMaterial(Integer.parseInt(split[0]))); } else { - item = new ItemStack(Material.getMaterial(Integer.parseInt(split[0])), 1, (byte) Integer.parseInt(split[1])); + item = new ItemStack(Material.getMaterial(Integer.parseInt(split[0])), 1, + (byte) Integer.parseInt(split[1])); } } return item; } + public static boolean hasLore(final ItemStack item) { + if (item.getItemMeta() != null) { + if (item.getItemMeta().hasDisplayName() || item.getItemMeta().hasLore()) + return true; + } + return false; + } + public static boolean isEnchanted(final String s) { final String[] split = s.split(":"); int i = 0; if (split.length != 1) { for (i = 1; i < split.length; i++) { - if (split[i].equalsIgnoreCase("enchant") || split[i].equalsIgnoreCase("all")) { + if (split[i].equalsIgnoreCase("enchant") || split[i].equalsIgnoreCase("all")) return true; - } } } return false; @@ -99,18 +95,8 @@ public class RBUtil { int i = 0; if (split.length != 1) { for (i = 1; i < split.length; i++) { - if (split[i].equalsIgnoreCase("lore") || split[i].equalsIgnoreCase("all")) { + if (split[i].equalsIgnoreCase("lore") || split[i].equalsIgnoreCase("all")) return true; - } - } - } - return false; - } - - public static boolean hasLore(final ItemStack item) { - if (item.getItemMeta() != null) { - if (item.getItemMeta().hasDisplayName() || item.getItemMeta().hasLore()) { - return true; } } return false; @@ -121,30 +107,29 @@ public class RBUtil { final boolean enchant = item.getEnchantments().size() >= 1; final boolean isLored = isLored(s); final boolean isEnchanted = isEnchanted(s); - if (!isLored && !isEnchanted && item.isSimilar(getItemstackFromString(s))) { + if (!isLored && !isEnchanted && item.isSimilar(getItemstackFromString(s))) return true; - } else if (item.getType() == getItemstackFromString(s).getType()) { - if (enchant && !lore) { - if (isEnchanted && !isLored) { - return true; - } else { - return false; - } - } else if (enchant && lore) { - if (isEnchanted && isLored) { - return true; - } else { - return false; - } - } else if (!enchant && lore) { - if (isLored && !isEnchanted) { - return true; - } else { - return false; - } - } + else if (item.getType() == getItemstackFromString(s).getType()) { + if (enchant && !lore) + return isEnchanted && !isLored; + else if (enchant && lore) + return isEnchanted && isLored; + else if (!enchant && lore) + return isLored && !isEnchanted; } return false; } + public static void setRBUtil(final RealBackpacks plugin) { + RBUtil.plugin = plugin; + } + + public static String stringToBackpack(String s) { + for (final String b : plugin.backpacks) { + if (b.equalsIgnoreCase(s)) + return b; + } + return null; + } + }