1
0
mirror of https://e.coding.net/circlecloud/MenuProtect.git synced 2024-11-24 11:18:46 +00:00

feat: 使用新的检测流程 去除多余事件

This commit is contained in:
502647092 2016-04-08 20:31:25 +08:00
parent ae49309f29
commit 185921d4b3
3 changed files with 22 additions and 69 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>pw.yumc</groupId> <groupId>pw.yumc</groupId>
<artifactId>MenuProtect</artifactId> <artifactId>MenuProtect</artifactId>
<version>1.1</version> <version>1.2</version>
<name>MenuProtect</name> <name>MenuProtect</name>
<build> <build>
<finalName>${project.name}</finalName> <finalName>${project.name}</finalName>

View File

@ -1,7 +1,6 @@
package pw.yumc.MenuProtect; package pw.yumc.MenuProtect;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import cn.citycraft.PluginHelper.config.FileConfig; import cn.citycraft.PluginHelper.config.FileConfig;
@ -9,18 +8,17 @@ import pw.yumc.MenuProtect.listen.ProtectListener;
import pw.yumc.MenuProtect.utils.MarkUtil; import pw.yumc.MenuProtect.utils.MarkUtil;
public class MenuProtect extends JavaPlugin { public class MenuProtect extends JavaPlugin {
FileConfig config; FileConfig config;
@Override @Override
public void onEnable() { public void onEnable() {
final PluginManager pm = Bukkit.getPluginManager(); Bukkit.getPluginManager().registerEvents(new ProtectListener(this), this);
pm.registerEvents(new ProtectListener(this), this); this.getLogger().info("菜单物品保护已开启!");
this.getLogger().info("菜单物品保护已开启!"); }
}
@Override @Override
public void onLoad() { public void onLoad() {
config = new FileConfig(this); config = new FileConfig(this);
MarkUtil.marks = config.getStringList("MarkList"); MarkUtil.marks = config.getStringList("MarkList");
} }
} }

View File

@ -6,13 +6,8 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryMoveItemEvent; import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.event.inventory.InventoryPickupItemEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.PlayerInventory;
@ -27,14 +22,14 @@ public class ProtectListener implements Listener {
this.plugin = plugin; this.plugin = plugin;
} }
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR)
public void onInvClose(final InventoryCloseEvent e) { public void onInvClose(final InventoryCloseEvent e) {
if (e.getPlayer() instanceof Player) { if (e.getPlayer() instanceof Player) {
handlerPlayerInventory((Player) e.getPlayer()); handlerPlayerInventory((Player) e.getPlayer());
} }
} }
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR)
public void onInvMove(final InventoryMoveItemEvent e) { public void onInvMove(final InventoryMoveItemEvent e) {
final ItemStack ci = e.getItem(); final ItemStack ci = e.getItem();
if (MarkUtil.hasMark(ci)) { if (MarkUtil.hasMark(ci)) {
@ -42,48 +37,14 @@ public class ProtectListener implements Listener {
} }
} }
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private ItemStack[] handlerItems(final ItemStack[] cis) {
public void onInvPickup(final InventoryPickupItemEvent e) { for (int i = 0; i < cis.length; i++) {
final ItemStack ci = e.getItem().getItemStack(); final ItemStack itemStack = cis[i];
if (MarkUtil.hasMark(ci)) { if (MarkUtil.hasMark(itemStack)) {
e.setCancelled(true); cis[i] = new ItemStack(Material.AIR);
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onItemClick(final InventoryClickEvent e) {
final Player p = (Player) e.getWhoClicked();
final ItemStack ci = e.getCurrentItem();
final Inventory inv = e.getInventory();
if (inv.getHolder() == null) {
return;
}
final int solt = e.getSlot();
try {
if (MarkUtil.hasMark(ci)) {
inv.setItem(solt, new ItemStack(Material.AIR));
p.closeInventory();
} }
} catch (final Exception ex) {
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerHandlerItem(final PlayerItemHeldEvent e) {
final PlayerInventory inv = e.getPlayer().getInventory();
final int newslot = e.getNewSlot();
final ItemStack newItem = inv.getItem(newslot);
if (MarkUtil.hasMark(newItem)) {
inv.setItem(newslot, new ItemStack(Material.AIR));
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerPickup(final PlayerPickupItemEvent e) {
final ItemStack ci = e.getItem().getItemStack();
if (MarkUtil.hasMark(ci)) {
e.setCancelled(true);
} }
return cis;
} }
private void handlerPlayerInventory(final Player p) { private void handlerPlayerInventory(final Player p) {
@ -91,14 +52,8 @@ public class ProtectListener implements Listener {
@Override @Override
public void run() { public void run() {
final PlayerInventory inv = p.getInventory(); final PlayerInventory inv = p.getInventory();
final ItemStack[] cis = inv.getArmorContents(); inv.setArmorContents(handlerItems(inv.getArmorContents()));
for (int i = 0; i < cis.length; i++) { inv.setContents(handlerItems(inv.getContents()));
final ItemStack itemStack = cis[i];
if (MarkUtil.hasMark(itemStack)) {
cis[i] = new ItemStack(Material.AIR);
}
}
inv.setArmorContents(cis);
} }
}); });
} }