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>
<groupId>pw.yumc</groupId>
<artifactId>MenuProtect</artifactId>
<version>1.1</version>
<version>1.2</version>
<name>MenuProtect</name>
<build>
<finalName>${project.name}</finalName>

View File

@ -1,7 +1,6 @@
package pw.yumc.MenuProtect;
import org.bukkit.Bukkit;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import cn.citycraft.PluginHelper.config.FileConfig;
@ -9,18 +8,17 @@ import pw.yumc.MenuProtect.listen.ProtectListener;
import pw.yumc.MenuProtect.utils.MarkUtil;
public class MenuProtect extends JavaPlugin {
FileConfig config;
FileConfig config;
@Override
public void onEnable() {
final PluginManager pm = Bukkit.getPluginManager();
pm.registerEvents(new ProtectListener(this), this);
this.getLogger().info("菜单物品保护已开启!");
}
@Override
public void onEnable() {
Bukkit.getPluginManager().registerEvents(new ProtectListener(this), this);
this.getLogger().info("菜单物品保护已开启!");
}
@Override
public void onLoad() {
config = new FileConfig(this);
MarkUtil.marks = config.getStringList("MarkList");
}
@Override
public void onLoad() {
config = new FileConfig(this);
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.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
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.PlayerInventory;
@ -27,14 +22,14 @@ public class ProtectListener implements Listener {
this.plugin = plugin;
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@EventHandler(priority = EventPriority.MONITOR)
public void onInvClose(final InventoryCloseEvent e) {
if (e.getPlayer() instanceof Player) {
handlerPlayerInventory((Player) e.getPlayer());
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@EventHandler(priority = EventPriority.MONITOR)
public void onInvMove(final InventoryMoveItemEvent e) {
final ItemStack ci = e.getItem();
if (MarkUtil.hasMark(ci)) {
@ -42,48 +37,14 @@ public class ProtectListener implements Listener {
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onInvPickup(final InventoryPickupItemEvent e) {
final ItemStack ci = e.getItem().getItemStack();
if (MarkUtil.hasMark(ci)) {
e.setCancelled(true);
}
}
@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();
private ItemStack[] handlerItems(final ItemStack[] cis) {
for (int i = 0; i < cis.length; i++) {
final ItemStack itemStack = cis[i];
if (MarkUtil.hasMark(itemStack)) {
cis[i] = new ItemStack(Material.AIR);
}
} 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) {
@ -91,14 +52,8 @@ public class ProtectListener implements Listener {
@Override
public void run() {
final PlayerInventory inv = p.getInventory();
final ItemStack[] cis = inv.getArmorContents();
for (int i = 0; i < cis.length; i++) {
final ItemStack itemStack = cis[i];
if (MarkUtil.hasMark(itemStack)) {
cis[i] = new ItemStack(Material.AIR);
}
}
inv.setArmorContents(cis);
inv.setArmorContents(handlerItems(inv.getArmorContents()));
inv.setContents(handlerItems(inv.getContents()));
}
});
}