1
0
mirror of https://e.coding.net/circlecloud/ProtectItem.git synced 2024-12-27 20:48:51 +00:00

fix all bug and complete config save...

Signed-off-by: j502647092 <jtb1@163.com>
This commit is contained in:
j502647092 2015-10-24 23:01:25 +08:00
parent 83c808d362
commit 8d627c0443
6 changed files with 78 additions and 26 deletions

View File

@ -21,21 +21,21 @@ public class ProtectItem extends JavaPlugin {
public FileConfig config; public FileConfig config;
public String pluginname; public String pluginname;
public ItemManager getItemManager() {
return itemManager;
}
public boolean isCantAction(final Player p, final ItemStack i, final ActionType action) { 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()); final String message = msgcfg.getMessage("Message." + action.toString());
if (message != null && !message.isEmpty()) { if (message != null && !message.isEmpty()) {
p.sendMessage(pluginname + " " + message); p.sendMessage(String.format(pluginname + " " + message, itemManager.getItemName(i)));
} }
return true; return true;
} }
return false; return false;
} }
public ItemManager getItemManager() {
return itemManager;
}
@Override @Override
public void onEnable() { public void onEnable() {
itemManager = new ItemManager(this); itemManager = new ItemManager(this);

View File

@ -21,8 +21,14 @@ public class ClickItemListen implements Listener {
public void onClickItem(final InventoryClickEvent e) { public void onClickItem(final InventoryClickEvent e) {
final Player p = (Player) e.getWhoClicked(); final Player p = (Player) e.getWhoClicked();
final ItemStack i = e.getCurrentItem(); final ItemStack i = e.getCurrentItem();
final int solt = e.getSlot();
if (i != null && i.getType() != Material.AIR) { 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) {
}
}
} }
} }
} }

View File

@ -1,5 +1,8 @@
package cn.citycraft.ProtectItem.listen; package cn.citycraft.ProtectItem.listen;
import java.util.Collection;
import org.bukkit.block.Block;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -20,6 +23,17 @@ public class InteractItemListen implements Listener {
public void onPickupItem(final PlayerInteractEvent e) { public void onPickupItem(final PlayerInteractEvent e) {
final Player p = e.getPlayer(); final Player p = e.getPlayer();
final ItemStack i = p.getItemInHand(); 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<ItemStack> items = b.getDrops();
for (final ItemStack itemStack : items) {
if (plugin.isCantAction(p, itemStack, ActionType.Interact)) {
e.setCancelled(true);
return;
}
}
} }
} }

View File

@ -1,5 +1,6 @@
package cn.citycraft.ProtectItem.listen; package cn.citycraft.ProtectItem.listen;
import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -23,6 +24,11 @@ public class ItemHeldListen implements Listener {
final Inventory inv = p.getInventory(); final Inventory inv = p.getInventory();
final int slot = e.getNewSlot(); final int slot = e.getNewSlot();
final ItemStack i = inv.getItem(slot); 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) {
}
}
} }
} }

View File

@ -1,8 +1,11 @@
package cn.citycraft.ProtectItem.utils; package cn.citycraft.ProtectItem.utils;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -13,42 +16,50 @@ import cn.citycraft.ProtectItem.ProtectItem;
public class ItemManager { public class ItemManager {
FileConfig itemconfig; FileConfig itemconfig;
HashMap<String, List<ActionType>> itemlist; HashMap<String, List<ActionType>> itemlist = new HashMap<String, List<ActionType>>();
ProtectItem plugin; ProtectItem plugin;
@SuppressWarnings("unchecked")
public ItemManager(final ProtectItem main) { public ItemManager(final ProtectItem main) {
plugin = main; plugin = main;
itemconfig = new FileConfig(plugin, "banitems.yml"); itemconfig = new FileConfig(plugin, "banitems.yml");
try { final Set<String> items = itemconfig.getConfigurationSection("banitems").getKeys(false);
itemlist = (HashMap<String, List<ActionType>>) itemconfig.get("banitems"); if (items != null) {
if (itemlist == null) { for (final String banitem : items) {
itemlist = new HashMap<String, List<ActionType>>(); final List<ActionType> actlist = new ArrayList<ActionType>();
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<String, List<ActionType>>(); itemlist = new HashMap<String, List<ActionType>>();
} }
} }
public void banItem(final ItemStack item) { public void banItem(final ItemStack item) {
banItem(item, new ActionType[] {}); banItem(item, ActionType.values());
} }
public void banItem(final ItemStack item, final ActionType... action) { public void banItem(final ItemStack item, final ActionType... action) {
final String itemname = getItemName(item); final String itemname = getItemName(item);
if (itemlist.containsKey(itemname)) { if (itemlist.containsKey(itemname)) {
final List<ActionType> actlist = itemlist.get(itemname); final List<ActionType> actlist = itemlist.get(itemname);
final List<ActionType> addactlist = new ArrayList<ActionType>();
for (final ActionType actionType : actlist) { for (final ActionType actionType : actlist) {
if (!actlist.contains(action)) { if (!actlist.contains(action)) {
actlist.add(actionType); addactlist.add(actionType);
} }
} }
actlist.addAll(addactlist);
itemlist.put(itemname, actlist); itemlist.put(itemname, actlist);
} else { } else {
itemlist.put(itemname, Arrays.asList(action)); itemlist.put(itemname, Arrays.asList(action));
} }
itemconfig.set("banitems", itemlist); this.saveConfig();
itemconfig.save();
} }
public boolean canAction(final ItemStack i, final ActionType action) { public boolean canAction(final ItemStack i, final ActionType action) {
@ -61,7 +72,7 @@ public class ItemManager {
return true; return true;
} }
final Boolean result = actlist.contains(action); final Boolean result = actlist.contains(action);
return result; return result ? false : true;
} }
public String getItemName(final ItemStack i) { public String getItemName(final ItemStack i) {
@ -79,9 +90,23 @@ public class ItemManager {
return p.hasPermission("pi" + itemname + ".*") ? true : p.hasPermission(perm); return p.hasPermission("pi" + itemname + ".*") ? true : p.hasPermission(perm);
} }
public void saveConfig() {
final HashMap<String, List<String>> banlist = new HashMap<String, List<String>>();
for (final Entry<String, List<ActionType>> banitem : itemlist.entrySet()) {
final List<String> actlist = new ArrayList<String>();
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) { public void unBanItem(final ItemStack item) {
final String itemname = getItemName(item); final String itemname = getItemName(item);
itemlist.remove(itemname); itemlist.remove(itemname);
this.saveConfig();
} }
public void unBanItem(final ItemStack item, final ActionType... action) { public void unBanItem(final ItemStack item, final ActionType... action) {
@ -91,6 +116,7 @@ public class ItemManager {
actlist.removeAll(Arrays.asList(action)); actlist.removeAll(Arrays.asList(action));
itemlist.put(itemname, actlist); itemlist.put(itemname, actlist);
} }
this.saveConfig();
} }
} }

View File

@ -3,8 +3,8 @@ Version: 1.0
Message: Message:
Reload: '&a配置文件已重新载入' Reload: '&a配置文件已重新载入'
#方块安全提示 #方块安全提示
Drop: '&c当前世界禁止丢弃物品!' Drop: '&c当前世界禁止丢弃 %s 物品!'
Pickup: '&c当前世界禁止拾取该物品!' # Pickup: '&c当前世界禁止拾取 %s 物品!'
Interact: '&c当前世界禁止使用该物品!' Interact: '&c当前世界禁止与 %s 物品交互!'
Held: '&c当前世界禁止使用该物品!' Held: '&c当前世界禁止持有 %s 物品 &4已清理!'
Click: '&c当前世界禁止点击该物品!' Click: '&c当前世界禁止点击 %s 物品 &4已清理!'