From 5b6abc1c076c617e9bf85b50417153f189bb1960 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Tue, 20 Oct 2015 18:36:53 +0800 Subject: [PATCH] add protect holder and update version to 1.4.4... Signed-off-by: 502647092 --- pom.xml | 2 +- .../QuickShop/Listeners/PlayerListener.java | 6 +++ .../QuickShop/Listeners/ProtectListener.java | 52 +++++++++++++------ .../org/maxgamer/QuickShop/Util/MsgUtil.java | 6 --- 4 files changed, 42 insertions(+), 24 deletions(-) diff --git a/pom.xml b/pom.xml index ac25df6..659366b 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.maxgamer QuickShop - 1.4.3 + 1.4.4 ${project.name} diff --git a/src/main/java/org/maxgamer/QuickShop/Listeners/PlayerListener.java b/src/main/java/org/maxgamer/QuickShop/Listeners/PlayerListener.java index 3a4f554..226d3d2 100644 --- a/src/main/java/org/maxgamer/QuickShop/Listeners/PlayerListener.java +++ b/src/main/java/org/maxgamer/QuickShop/Listeners/PlayerListener.java @@ -17,6 +17,7 @@ import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerTeleportEvent; +import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.util.BlockIterator; import org.maxgamer.QuickShop.QuickShop; @@ -62,6 +63,11 @@ public class PlayerListener implements Listener { shop.onClick(); // Text menu MsgUtil.sendShopInfo(p, shop); + if (!plugin.getConfigManager().isEnableMagicLib() && !shop.getOwner().equalsIgnoreCase(p.getName()) && b.getType() == Material.WALL_SIGN) { + final Inventory in = Bukkit.createInventory(null, 9, plugin.getConfigManager().getGuiTitle()); + in.setItem(4, shop.getItem()); + p.openInventory(in); + } if (shop.isSelling()) { p.sendMessage(MsgUtil.p("how-many-buy")); } else { diff --git a/src/main/java/org/maxgamer/QuickShop/Listeners/ProtectListener.java b/src/main/java/org/maxgamer/QuickShop/Listeners/ProtectListener.java index 7684ca8..d32ec8a 100644 --- a/src/main/java/org/maxgamer/QuickShop/Listeners/ProtectListener.java +++ b/src/main/java/org/maxgamer/QuickShop/Listeners/ProtectListener.java @@ -2,11 +2,12 @@ package org.maxgamer.QuickShop.Listeners; import org.bukkit.Bukkit; import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockState; +import org.bukkit.block.DoubleChest; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; -import org.bukkit.event.entity.ItemDespawnEvent; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryMoveItemEvent; import org.bukkit.event.inventory.InventoryPickupItemEvent; @@ -14,8 +15,10 @@ import org.bukkit.event.inventory.InventoryType; import org.bukkit.event.player.PlayerItemHeldEvent; import org.bukkit.event.player.PlayerPickupItemEvent; import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; import org.maxgamer.QuickShop.QuickShop; +import org.maxgamer.QuickShop.Shop.Shop; import org.maxgamer.QuickShop.Util.MarkUtil; public class ProtectListener implements Listener { @@ -26,18 +29,41 @@ public class ProtectListener implements Listener { this.plugin = plugin; } + public Shop getShop(final Inventory inv) { + if (inv == null) { + return null; + } + InventoryHolder holder = inv.getHolder(); + if (holder instanceof DoubleChest) { + holder = ((DoubleChest) holder).getLeftSide(); + } + + if (holder instanceof BlockState) { + final Block block = ((BlockState) holder).getBlock(); + final Shop sp = plugin.getShopManager().getShop(block.getLocation()); + if (sp != null) { + return sp; + } + } + return null; + } + @EventHandler public void onInvMove(final InventoryMoveItemEvent e) { final ItemStack ci = e.getItem(); if (MarkUtil.hasMark(ci)) { e.setCancelled(true); - final ItemStack[] items = e.getSource().getContents(); - for (final ItemStack itemStack : items) { - if (MarkUtil.hasMark(itemStack)) { - itemStack.setType(Material.AIR); - } - } - e.getSource().setContents(items); + } + + final Inventory src = e.getSource(); + final Inventory me = e.getInitiator(); + final Inventory des = e.getDestination(); + final Shop srcshop = getShop(src); + final Shop meshop = getShop(me); + final Shop desshop = getShop(des); + + if ((srcshop != null && meshop == null) || (meshop != null && desshop == null) || (srcshop != null && desshop != null && srcshop.getOwner().equalsIgnoreCase(desshop.getOwner()))) { + e.setCancelled(true); } } @@ -75,14 +101,6 @@ public class ProtectListener implements Listener { } } - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onItemDespawn(final ItemDespawnEvent e) { - final ItemStack ci = e.getEntity().getItemStack(); - if (MarkUtil.hasMark(ci)) { - e.setCancelled(true); - } - } - @EventHandler public void onPlayerHandlerItem(final PlayerItemHeldEvent e) { final Player p = e.getPlayer(); diff --git a/src/main/java/org/maxgamer/QuickShop/Util/MsgUtil.java b/src/main/java/org/maxgamer/QuickShop/Util/MsgUtil.java index 592c8d9..38aa9cf 100644 --- a/src/main/java/org/maxgamer/QuickShop/Util/MsgUtil.java +++ b/src/main/java/org/maxgamer/QuickShop/Util/MsgUtil.java @@ -9,7 +9,6 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; -import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.maxgamer.QuickShop.QuickShop; import org.maxgamer.QuickShop.Shop.Shop; @@ -193,10 +192,5 @@ public class MsgUtil { p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.p("menu.this-shop-is-selling")); } p.sendMessage(ChatColor.DARK_PURPLE + "+---------------------------------------------------+"); - if (!plugin.getConfigManager().isEnableMagicLib() && !shop.getOwner().equalsIgnoreCase(p.getName())) { - final Inventory in = Bukkit.createInventory(null, 9, plugin.getConfigManager().getGuiTitle()); - in.setItem(4, items); - p.openInventory(in); - } } } \ No newline at end of file