1
0
mirror of https://e.coding.net/circlecloud/RealBackpacks.git synced 2024-12-04 03:49:07 +00:00

Performance optimization...

Signed-off-by: j502647092 <jtb1@163.com>
This commit is contained in:
j502647092 2015-06-14 15:55:22 +08:00
parent 80c5d63a84
commit 02938bc626
2 changed files with 200 additions and 237 deletions

View File

@ -28,199 +28,17 @@ public class InventoryListener implements Listener {
this.plugin = plugin; 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<String> 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<String> 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") @SuppressWarnings("deprecation")
public boolean isSimilar(ItemStack origin, ItemStack compare) { public boolean isSimilar(ItemStack origin, ItemStack compare) {
if (compare == null) { if (compare == null)
return false; return false;
} if (compare == origin)
if (compare == origin) {
return true; return true;
}
return compare.getTypeId() == origin.getTypeId() return compare.getTypeId() == origin.getTypeId()
&& compare.getDurability() == origin.getDurability() && compare.getDurability() == origin.getDurability()
&& compare.hasItemMeta() == origin.hasItemMeta() && compare.hasItemMeta() == origin.hasItemMeta()
&& (compare.hasItemMeta() ? isSimilarMeta( && (compare.hasItemMeta() ? isSimilarMeta(compare.getItemMeta(),
compare.getItemMeta(), origin.getItemMeta()) : true); origin.getItemMeta()) : true);
} }
public boolean isSimilarMeta(ItemMeta origin, ItemMeta compare) { public boolean isSimilarMeta(ItemMeta origin, ItemMeta compare) {
@ -240,11 +58,171 @@ public class InventoryListener implements Listener {
return false; return false;
} }
if (origin.hasLore() && compare.hasLore()) { if (origin.hasLore() && compare.hasLore()) {
if (!(origin.getLore().containsAll(compare.getLore()) || compare if (!(compare.getLore().containsAll(origin.getLore())))
.getLore().containsAll(origin.getLore())))
return false; return false;
} }
return true; 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<String> 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<String> 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);
}
}
} }

View File

@ -14,10 +14,6 @@ public class RBUtil {
private static RealBackpacks plugin; private static RealBackpacks plugin;
public static void setRBUtil(final RealBackpacks plugin) {
RBUtil.plugin = plugin;
}
public static void destroyContents(final String name, final String backpack) { public static void destroyContents(final String name, final String backpack) {
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() { plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override @Override
@ -56,15 +52,6 @@ public class RBUtil {
return i; return i;
} }
public static String stringToBackpack(String s) {
for (final String b : plugin.backpacks) {
if (b.equalsIgnoreCase(s)) {
return b;
}
}
return null;
}
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public static ItemStack getItemstackFromString(final String s) { public static ItemStack getItemstackFromString(final String s) {
ItemStack item = null; ItemStack item = null;
@ -72,23 +59,32 @@ public class RBUtil {
if (split.length == 1) { if (split.length == 1) {
item = new ItemStack(Material.getMaterial(Integer.parseInt(split[0])), 1); item = new ItemStack(Material.getMaterial(Integer.parseInt(split[0])), 1);
} else { } 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]))); item = new ItemStack(Material.getMaterial(Integer.parseInt(split[0])));
} else { } 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; 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) { public static boolean isEnchanted(final String s) {
final String[] split = s.split(":"); final String[] split = s.split(":");
int i = 0; int i = 0;
if (split.length != 1) { if (split.length != 1) {
for (i = 1; i < split.length; i++) { 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 true;
}
} }
} }
return false; return false;
@ -99,18 +95,8 @@ public class RBUtil {
int i = 0; int i = 0;
if (split.length != 1) { if (split.length != 1) {
for (i = 1; i < split.length; i++) { 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 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; return false;
@ -121,30 +107,29 @@ public class RBUtil {
final boolean enchant = item.getEnchantments().size() >= 1; final boolean enchant = item.getEnchantments().size() >= 1;
final boolean isLored = isLored(s); final boolean isLored = isLored(s);
final boolean isEnchanted = isEnchanted(s); final boolean isEnchanted = isEnchanted(s);
if (!isLored && !isEnchanted && item.isSimilar(getItemstackFromString(s))) { if (!isLored && !isEnchanted && item.isSimilar(getItemstackFromString(s)))
return true; return true;
} else if (item.getType() == getItemstackFromString(s).getType()) { else if (item.getType() == getItemstackFromString(s).getType()) {
if (enchant && !lore) { if (enchant && !lore)
if (isEnchanted && !isLored) { return isEnchanted && !isLored;
return true; else if (enchant && lore)
} else { return isEnchanted && isLored;
return false; else if (!enchant && lore)
} return isLored && !isEnchanted;
} else if (enchant && lore) {
if (isEnchanted && isLored) {
return true;
} else {
return false;
}
} else if (!enchant && lore) {
if (isLored && !isEnchanted) {
return true;
} else {
return false;
}
}
} }
return false; 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;
}
} }