From 8d627c04436e28727a3e848201fea0ef629deb3e Mon Sep 17 00:00:00 2001 From: j502647092 Date: Sat, 24 Oct 2015 23:01:25 +0800 Subject: [PATCH] fix all bug and complete config save... Signed-off-by: j502647092 --- .../cn/citycraft/ProtectItem/ProtectItem.java | 12 ++--- .../ProtectItem/listen/ClickItemListen.java | 8 ++- .../listen/InteractItemListen.java | 16 +++++- .../ProtectItem/listen/ItemHeldListen.java | 8 ++- .../ProtectItem/utils/ItemManager.java | 50 ++++++++++++++----- src/main/resources/messages.yml | 10 ++-- 6 files changed, 78 insertions(+), 26 deletions(-) diff --git a/src/main/java/cn/citycraft/ProtectItem/ProtectItem.java b/src/main/java/cn/citycraft/ProtectItem/ProtectItem.java index 8ec7c94..2a4c9cb 100644 --- a/src/main/java/cn/citycraft/ProtectItem/ProtectItem.java +++ b/src/main/java/cn/citycraft/ProtectItem/ProtectItem.java @@ -21,21 +21,21 @@ public class ProtectItem extends JavaPlugin { public FileConfig config; public String pluginname; + public ItemManager getItemManager() { + return itemManager; + } + public boolean isCantAction(final Player p, final ItemStack i, final ActionType action) { - if (itemManager.canAction(i, action) && !itemManager.hasActionPerm(p, i, action)) { + if (!itemManager.canAction(i, action) && !itemManager.hasActionPerm(p, i, action)) { final String message = msgcfg.getMessage("Message." + action.toString()); if (message != null && !message.isEmpty()) { - p.sendMessage(pluginname + " " + message); + p.sendMessage(String.format(pluginname + " " + message, itemManager.getItemName(i))); } return true; } return false; } - public ItemManager getItemManager() { - return itemManager; - } - @Override public void onEnable() { itemManager = new ItemManager(this); diff --git a/src/main/java/cn/citycraft/ProtectItem/listen/ClickItemListen.java b/src/main/java/cn/citycraft/ProtectItem/listen/ClickItemListen.java index 0048035..e3031d0 100644 --- a/src/main/java/cn/citycraft/ProtectItem/listen/ClickItemListen.java +++ b/src/main/java/cn/citycraft/ProtectItem/listen/ClickItemListen.java @@ -21,8 +21,14 @@ public class ClickItemListen implements Listener { public void onClickItem(final InventoryClickEvent e) { final Player p = (Player) e.getWhoClicked(); final ItemStack i = e.getCurrentItem(); + final int solt = e.getSlot(); if (i != null && i.getType() != Material.AIR) { - e.setCancelled(plugin.isCantAction(p, i, ActionType.Click)); + if (plugin.isCantAction(p, i, ActionType.Click)) { + try { + e.getInventory().setItem(solt, new ItemStack(Material.AIR)); + } catch (final Exception e2) { + } + } } } } diff --git a/src/main/java/cn/citycraft/ProtectItem/listen/InteractItemListen.java b/src/main/java/cn/citycraft/ProtectItem/listen/InteractItemListen.java index 2e1fbc5..c3b347b 100644 --- a/src/main/java/cn/citycraft/ProtectItem/listen/InteractItemListen.java +++ b/src/main/java/cn/citycraft/ProtectItem/listen/InteractItemListen.java @@ -1,5 +1,8 @@ package cn.citycraft.ProtectItem.listen; +import java.util.Collection; + +import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -20,6 +23,17 @@ public class InteractItemListen implements Listener { public void onPickupItem(final PlayerInteractEvent e) { final Player p = e.getPlayer(); final ItemStack i = p.getItemInHand(); - e.setCancelled(plugin.isCantAction(p, i, ActionType.Interact)); + if (plugin.isCantAction(p, i, ActionType.Interact)) { + e.setCancelled(true); + return; + } + final Block b = e.getClickedBlock(); + final Collection items = b.getDrops(); + for (final ItemStack itemStack : items) { + if (plugin.isCantAction(p, itemStack, ActionType.Interact)) { + e.setCancelled(true); + return; + } + } } } diff --git a/src/main/java/cn/citycraft/ProtectItem/listen/ItemHeldListen.java b/src/main/java/cn/citycraft/ProtectItem/listen/ItemHeldListen.java index 6719a3e..610a860 100644 --- a/src/main/java/cn/citycraft/ProtectItem/listen/ItemHeldListen.java +++ b/src/main/java/cn/citycraft/ProtectItem/listen/ItemHeldListen.java @@ -1,5 +1,6 @@ package cn.citycraft.ProtectItem.listen; +import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -23,6 +24,11 @@ public class ItemHeldListen implements Listener { final Inventory inv = p.getInventory(); final int slot = e.getNewSlot(); final ItemStack i = inv.getItem(slot); - e.setCancelled(plugin.isCantAction(p, i, ActionType.Held)); + if (plugin.isCantAction(p, i, ActionType.Held)) { + try { + inv.setItem(slot, new ItemStack(Material.AIR)); + } catch (final Exception e2) { + } + } } } diff --git a/src/main/java/cn/citycraft/ProtectItem/utils/ItemManager.java b/src/main/java/cn/citycraft/ProtectItem/utils/ItemManager.java index 9c41225..2fd7aed 100644 --- a/src/main/java/cn/citycraft/ProtectItem/utils/ItemManager.java +++ b/src/main/java/cn/citycraft/ProtectItem/utils/ItemManager.java @@ -1,8 +1,11 @@ package cn.citycraft.ProtectItem.utils; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; +import java.util.Map.Entry; +import java.util.Set; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -13,42 +16,50 @@ import cn.citycraft.ProtectItem.ProtectItem; public class ItemManager { FileConfig itemconfig; - HashMap> itemlist; + HashMap> itemlist = new HashMap>(); ProtectItem plugin; - @SuppressWarnings("unchecked") public ItemManager(final ProtectItem main) { plugin = main; itemconfig = new FileConfig(plugin, "banitems.yml"); - try { - itemlist = (HashMap>) itemconfig.get("banitems"); - if (itemlist == null) { - itemlist = new HashMap>(); + final Set items = itemconfig.getConfigurationSection("banitems").getKeys(false); + if (items != null) { + for (final String banitem : items) { + final List actlist = new ArrayList(); + for (final String action : itemconfig.getStringList("banitems." + banitem)) { + try { + final ActionType act = Enum.valueOf(ActionType.class, action); + actlist.add(act); + } catch (final Exception e) { + } + } + itemlist.put(banitem, actlist); } - } catch (final Exception e) { + } else { itemlist = new HashMap>(); } } public void banItem(final ItemStack item) { - banItem(item, new ActionType[] {}); + banItem(item, ActionType.values()); } public void banItem(final ItemStack item, final ActionType... action) { final String itemname = getItemName(item); if (itemlist.containsKey(itemname)) { final List actlist = itemlist.get(itemname); + final List addactlist = new ArrayList(); for (final ActionType actionType : actlist) { if (!actlist.contains(action)) { - actlist.add(actionType); + addactlist.add(actionType); } } + actlist.addAll(addactlist); itemlist.put(itemname, actlist); } else { itemlist.put(itemname, Arrays.asList(action)); } - itemconfig.set("banitems", itemlist); - itemconfig.save(); + this.saveConfig(); } public boolean canAction(final ItemStack i, final ActionType action) { @@ -61,7 +72,7 @@ public class ItemManager { return true; } final Boolean result = actlist.contains(action); - return result; + return result ? false : true; } public String getItemName(final ItemStack i) { @@ -79,9 +90,23 @@ public class ItemManager { return p.hasPermission("pi" + itemname + ".*") ? true : p.hasPermission(perm); } + public void saveConfig() { + final HashMap> banlist = new HashMap>(); + for (final Entry> banitem : itemlist.entrySet()) { + final List actlist = new ArrayList(); + for (final ActionType act : banitem.getValue()) { + actlist.add(act.toString()); + } + banlist.put(banitem.getKey(), actlist); + } + itemconfig.set("banitems", banlist); + itemconfig.save(); + } + public void unBanItem(final ItemStack item) { final String itemname = getItemName(item); itemlist.remove(itemname); + this.saveConfig(); } public void unBanItem(final ItemStack item, final ActionType... action) { @@ -91,6 +116,7 @@ public class ItemManager { actlist.removeAll(Arrays.asList(action)); itemlist.put(itemname, actlist); } + this.saveConfig(); } } diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml index 8621a93..65d02c1 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages.yml @@ -3,8 +3,8 @@ Version: 1.0 Message: Reload: '&a配置文件已重新载入!' #方块安全提示 - Drop: '&c当前世界禁止丢弃该物品!' - Pickup: '&c当前世界禁止拾取该物品!' - Interact: '&c当前世界禁止使用该物品!' - Held: '&c当前世界禁止使用该物品!' - Click: '&c当前世界禁止点击该物品!' \ No newline at end of file + Drop: '&c当前世界禁止丢弃 %s 物品!' +# Pickup: '&c当前世界禁止拾取 %s 物品!' + Interact: '&c当前世界禁止与 %s 物品交互!' + Held: '&c当前世界禁止持有 %s 物品 &4已清理!' + Click: '&c当前世界禁止点击 %s 物品 &4已清理!' \ No newline at end of file