From 87799d2d7e1092f0a4742e762272a772c0a4dbbc Mon Sep 17 00:00:00 2001 From: Ghost_chu <2908803755@qq.com> Date: Wed, 5 Jun 2019 16:50:48 +0800 Subject: [PATCH] Revert codes for displayitem Former-commit-id: 5e106e89eb2b7fe2cdb4faa204349c715882ac9f [formerly bce2442a85c57bd0b45446aa9726bc968188ebdc] Former-commit-id: 3d4f9412b79f9244d42bace719d40cfa2d74ea03 --- .../quickshop/Shop/ContainerShop.java | 87 ++------ .../maxgamer/quickshop/Shop/DisplayItem.java | 170 +++++++++++---- .../quickshop/Shop/RealDisplayItem.java | 200 ------------------ 3 files changed, 152 insertions(+), 305 deletions(-) delete mode 100644 src/main/java/org/maxgamer/quickshop/Shop/RealDisplayItem.java diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java b/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java index 7f7c21e16..1db7b2468 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java @@ -79,7 +79,7 @@ public class ContainerShop implements Shop { this.plugin = (QuickShop) Bukkit.getPluginManager().getPlugin("QuickShop"); this.item.setAmount(1); if (plugin.isDisplay()) { - this.displayItem = new RealDisplayItem(this, this.item); + this.displayItem = new DisplayItem(this, this.item); } this.shopType = ShopType.SELLING; } @@ -103,7 +103,7 @@ public class ContainerShop implements Shop { this.plugin = (QuickShop) Bukkit.getPluginManager().getPlugin("QuickShop"); this.item.setAmount(1); if (plugin.isDisplay()) { - this.displayItem = new RealDisplayItem(this, this.item); + this.displayItem = new DisplayItem(this, this.item); } this.shopType = type; this.unlimited = unlimited; @@ -682,97 +682,45 @@ public class ContainerShop implements Shop { checkDisplay(); return Util.canBeShop(this.getLocation().getBlock()); } - - /** - * Check the display status, respawn, modify display location if need. - */ public void checkDisplay() { - if (!plugin.isDisplay()) + if (plugin.isDisplay() == false) return; if (getLocation().getWorld() == null) return; // not loaded - // if (!Util.isLoaded(getLocation())) - // return; - boolean trans; - try { - trans = Util.isTransparent(getLocation().clone().add(0.5, 1.2, 0.5).getBlock().getType()); - } catch (Throwable t) { - trans = false; - } - Material mat; - try { - mat = displayItem.getDisplayLocation().getBlock().getType(); - } catch (NullPointerException ex) { - mat = Material.AIR; - } - - if (this.getDisplayItem() != null) { - if (!trans) { - this.getDisplayItem().remove(); - Util.debugLog("Item was removed cause detected in untrans block: " + this.getLocation()); - return; - } - if (!Util.isAir(mat)) { - this.getDisplayItem().remove(); - Util.debugLog("Item was removed cause detected in not a air block: " + this.getLocation()); - return; - } - } - - if (trans && Util.isAir(mat) && (this.getDisplayItem() == null)) { - this.displayItem = new RealDisplayItem(this, this.getItem()); - this.getDisplayItem().removeDupe(); + boolean trans = Util.isTransparent(getLocation().clone().add(0.5, 1.2, 0.5).getBlock().getType()); + if (trans && this.getDisplayItem() == null) { + this.displayItem = new DisplayItem(this, this.getItem()); this.getDisplayItem().spawn(); - return; } - //Execute display check if (this.getDisplayItem() != null) { - // Already checked above - // if (!trans) { // We have a display item in a block... delete it - // this.getDisplayItem().remove(); - // this.displayItem = null; - // return; - // } + if (!trans) { // We have a display item in a block... delete it + this.getDisplayItem().remove(); + this.displayItem = null; + return; + } DisplayItem disItem = this.getDisplayItem(); Location dispLoc = disItem.getDisplayLocation(); - - // if(!Util.isAir(dispLoc.getBlock().getType())){ - // disItem.remove(); - // return; - // } + if (dispLoc.getBlock() != null && dispLoc.getBlock().getType() == Material.WATER) { + disItem.remove(); + return; + } if (disItem.getItem() == null) { disItem.removeDupe(); disItem.spawn(); - Util.debugLog("Item was respawned cause we not detected the item object: " + this.getLocation()); return; } - // if (disItem.getItem().isDead() || !disItem.getItem().isValid()) { - // disItem.remove(); - // disItem.removeDupe(); - // disItem.spawn(); - // return; - // } Item item = disItem.getItem(); - if (item.getTicksLived() > 5000 || !item.isValid() || item.isDead()) { - disItem.removeDupe(); disItem.respawn(); - Util.debugLog("Item was removeddupes and respawned cause AUTO-REGEN: " + this.getLocation()); - } else if (checkDisplayMoved()) { + disItem.removeDupe(); + } else if (item.getLocation().distanceSquared(dispLoc) > 1) { item.teleport(dispLoc, TeleportCause.PLUGIN); } } } - /** - * Check the display item is or not moved. - * @return Moved - */ public boolean checkDisplayMoved() { // don't check if the plugin doesn't know about the object - - if (!Util.isLoaded(getLocation())) - return false; if (this.getDisplayItem() == null) { return false; } @@ -790,6 +738,7 @@ public class ContainerShop implements Shop { if (!item.getLocation().getWorld().isChunkLoaded(item.getLocation().getChunk())) { return false; } + return this.getDisplayItem().getDisplayLocation().distanceSquared(item.getLocation()) > 0.2; } diff --git a/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java b/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java index 4dcbce495..fb5344a4b 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java @@ -1,67 +1,112 @@ package org.maxgamer.quickshop.Shop; +import java.util.ArrayList; import java.util.List; +import java.util.logging.Level; +import org.bukkit.Bukkit; +import org.bukkit.Chunk; import org.bukkit.Location; +import org.bukkit.entity.Entity; import org.bukkit.entity.Item; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.util.Vector; +import org.maxgamer.quickshop.QuickShop; +//import org.maxgamer.quickshop.Util.NMS; -public interface DisplayItem { - /** - * Spawn the displayItem - */ - public abstract void spawn(); +/** + * @author Netherfoam A display item, that spawns a block above the chest and + * cannot be interacted with. + */ +public class DisplayItem { + private Shop shop; + private ItemStack iStack; + private Item item; + static QuickShop plugin = QuickShop.instance; + // private Location displayLoc; /** - * Respawn the displayItem - */ - public abstract void respawn(); - - /** - * Protect the target Item and mark is QS's shop item. + * ZZ + * Creates a new display item. * - * @param item The item you want protect. + * @param shop The shop (See Shop) + * @param iStack The item stack to clone properties of the display item from. */ - public abstract void safeGuard(Item item); + public DisplayItem(Shop shop, ItemStack iStack) { + this.shop = shop; + this.iStack = iStack.clone(); + + // this.displayLoc = shop.getLocation().clone().add(0.5, 1.2, 0.5); + } /** - * Remove the dupe items. - * @return Had dupes removed. + * Spawns the dummy item on top of the shop. */ - public abstract boolean removeDupe(); + public void spawn() { + if (shop.getLocation().getWorld() == null) + return; + Location dispLoc = this.getDisplayLocation(); + //Call Event for QSAPI + + ShopDisplayItemSpawnEvent shopDisplayItemSpawnEvent = new ShopDisplayItemSpawnEvent(shop, iStack); + Bukkit.getPluginManager().callEvent(shopDisplayItemSpawnEvent); + if (shopDisplayItemSpawnEvent.isCancelled()) { + return; + } + this.item = shop.getLocation().getWorld().dropItem(dispLoc, this.iStack); + this.item.setVelocity(new Vector(0, 0.1, 0)); + try { + this.safeGuard(this.item); + ShopDisplayItemSpawnedEvent shopDisplayItemSpawnedEvent = new ShopDisplayItemSpawnedEvent(shop, this.item); + Bukkit.getPluginManager().callEvent(shopDisplayItemSpawnedEvent); + // NMS.safeGuard + } catch (Exception e) { + e.printStackTrace(); + plugin.getLogger().log(Level.WARNING, + "QuickShop version mismatch! This version of QuickShop is incompatible with this version of bukkit! Try update?"); + } + } /** - * Remove the DisplayItem + * Spawns the new display item. Does not remove duplicate items. */ - public abstract void remove(); + public void respawn() { + remove(); + spawn(); + } /** - * Get the displayLocation - * @return DisplayItem's location, not promise target location have the display item. + * Set item is QuickShop's DisplayItem and prevent them. + * + * @param item */ - public abstract Location getDisplayLocation(); + public void safeGuard(Item item) { + item.setPickupDelay(Integer.MAX_VALUE); + ItemMeta iMeta = item.getItemStack().getItemMeta(); - /** - * Get the displayItem's item, maybe return null - * @return The displayItem's item, maybe null when it not exist. - */ - public abstract Item getItem(); - - /** - * Check the display is or not moved. - * @param shop shop - */ - public abstract boolean checkDisplayMoved(Shop shop); + if (plugin.getConfig().getBoolean("shop.display-item-use-name")) { + item.setCustomName("QuickShop"); + iMeta.setDisplayName("QuickShop"); + } + item.setPortalCooldown(Integer.MAX_VALUE); + item.setSilent(true); + item.setInvulnerable(true); + java.util.List lore = new ArrayList(); + for (int i = 0; i < 21; i++) { + lore.add("QuickShop DisplayItem"); //Create 20 lines lore to make sure no stupid plugin accident remove mark. + } + iMeta.setLore(lore); + item.getItemStack().setItemMeta(iMeta); + } /** * Check the ItemStack is or not a DisplayItem - * * @param itemStack The ItemStack you want to check. * @return The check result. */ + public static boolean checkShopItem(ItemStack itemStack) { - if (itemStack == null) - return false; if (!itemStack.hasItemMeta()) { return false; } @@ -79,4 +124,57 @@ public interface DisplayItem { return false; } -} + /** + * Removes all items floating ontop of the chest that aren't the display + * item. + */ + public boolean removeDupe() { + if (shop.getLocation().getWorld() == null) + return false; + Location displayLoc = shop.getLocation().getBlock().getRelative(0, 1, 0).getLocation(); + boolean removed = false; + Chunk c = displayLoc.getChunk(); + for (Entity e : c.getEntities()) { + if (!(e instanceof Item)) + continue; + if (this.item != null && e.getEntityId() == this.item.getEntityId()) + continue; + Location eLoc = e.getLocation().getBlock().getLocation(); + if (eLoc.equals(displayLoc) || eLoc.equals(shop.getLocation())) { + ItemStack near = ((Item) e).getItemStack(); + // if its the same its a dupe + if (this.shop.matches(near)) { + e.remove(); + removed = true; + } + } + } + return removed; + + } + + /** + * Removes the display item. + */ + public void remove() { + if (this.item == null) + return; + this.item.remove(); + this.item = null; + } + + /** + * @return Returns the exact location of the display item. (1 above shop + * block, in the center) + */ + public Location getDisplayLocation() { + return this.shop.getLocation().clone().add(0.5, 1.2, 0.5); + } + + /** + * Returns the reference to this shops item. Do not modify. + */ + public Item getItem() { + return this.item; + } +} \ No newline at end of file diff --git a/src/main/java/org/maxgamer/quickshop/Shop/RealDisplayItem.java b/src/main/java/org/maxgamer/quickshop/Shop/RealDisplayItem.java deleted file mode 100644 index 66d4353b4..000000000 --- a/src/main/java/org/maxgamer/quickshop/Shop/RealDisplayItem.java +++ /dev/null @@ -1,200 +0,0 @@ -package org.maxgamer.quickshop.Shop; - -import java.util.List; -import java.util.logging.Level; - -import org.bukkit.Bukkit; -import org.bukkit.Chunk; -import org.bukkit.Location; -import org.bukkit.entity.Entity; -import org.bukkit.entity.Item; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; -import org.bukkit.util.Vector; -import org.maxgamer.quickshop.QuickShop; -import org.maxgamer.quickshop.Util.Util; -//import org.maxgamer.quickshop.Util.NMS; - -/** - * @author Netherfoam A display item, that spawns a block above the chest and - * cannot be interacted with. - */ -public class RealDisplayItem implements DisplayItem { - private Shop shop; - private ItemStack iStack; - private Item item; - static QuickShop plugin = QuickShop.instance; - // private Location displayLoc; - - /** - * ZZ - * Creates a new display item. - * - * @param shop The shop (See Shop) - * @param iStack The item stack to clone properties of the display item from. - */ - public RealDisplayItem(Shop shop, ItemStack iStack) { - this.shop = shop; - this.iStack = iStack.clone(); - - // this.displayLoc = shop.getLocation().clone().add(0.5, 1.2, 0.5); - } - - /** - * Spawns the dummy item on top of the shop. - */ - @Override - public void spawn() { - if (shop.getLocation().getWorld() == null) - return; - Location dispLoc = this.getDisplayLocation(); - //Call Event for QSAPI - - ShopDisplayItemSpawnEvent shopDisplayItemSpawnEvent = new ShopDisplayItemSpawnEvent(shop, iStack); - Bukkit.getPluginManager().callEvent(shopDisplayItemSpawnEvent); - if (shopDisplayItemSpawnEvent.isCancelled()) { - return; - } - if (!Util.isAir(dispLoc.getBlock().getType())) - return; - - this.item = shop.getLocation().getWorld().dropItem(dispLoc, this.iStack); - this.item.setVelocity(new Vector(0, 0.1, 0)); - try { - this.safeGuard(this.item); - ShopDisplayItemSpawnedEvent shopDisplayItemSpawnedEvent = new ShopDisplayItemSpawnedEvent(shop, this.item); - Bukkit.getPluginManager().callEvent(shopDisplayItemSpawnedEvent); - // NMS.safeGuard - } catch (Exception e) { - e.printStackTrace(); - plugin.getLogger().log(Level.WARNING, - "QuickShop version mismatch! This version of QuickShop is incompatible with this version of bukkit! Try update?"); - } - } - - /** - * Spawns the new display item. Does not remove duplicate items. - */ - @Override - public void respawn() { - remove(); - spawn(); - } - - /** - * Set item is QuickShop's DisplayItem and prevent them. - * - * @param item The item - */ - @Override - public void safeGuard(Item item) { - item.setPickupDelay(Integer.MAX_VALUE); - item.setPortalCooldown(Integer.MAX_VALUE); - item.setSilent(true); - item.setInvulnerable(true); - item.setItemStack(safeGuardItemStack(item.getItemStack())); - } - - private ItemStack safeGuardItemStack(ItemStack iStack) { - iStack = iStack.clone(); - if (plugin.getConfig().getBoolean("shop.display-item-use-name")) { - ItemMeta iMeta = iStack.getItemMeta(); - if (plugin.getConfig().getBoolean("shop.display-item-use-name")) { - iMeta.setDisplayName("QuickShop"); - - } - List lore = iMeta.getLore(); - for (int i = 0; i < 6; i++) { - lore.add("QuickShop DisplayItem"); //Create 5 lines lore to make sure no stupid plugin accident remove mark. - } - iMeta.setLore(lore); - iStack.setItemMeta(iMeta); - } - return iStack; - } - - /** - * Removes all items floating ontop of the chest that aren't the display - * item. - */ - @Override - public boolean removeDupe() { - if (shop.getLocation().getWorld() == null) { - return false; - } - Location displayLoc = shop.getLocation().getBlock().getRelative(0, 1, 0).getLocation(); - boolean removed = false; - // if (!Util.isLoaded(displayLoc)) - // return false; - Chunk c = displayLoc.getChunk(); - for (Entity e : c.getEntities()) { - if (this.item == null) - continue; - if (e == null) - continue; - if (!(e instanceof Item)) - continue; - if (e.getEntityId() == this.item.getEntityId()) //Not dupe, is our item - continue; - if (e.getUniqueId().equals(this.item.getUniqueId())) //Not dupe, is our item - continue; - - Item inChunkEntity = (Item) e; - ItemStack sgDisplayItemStack = safeGuardItemStack(this.iStack); - ItemStack sgEntityItemStack = safeGuardItemStack(inChunkEntity.getItemStack()); - - if (plugin.getItemMatcher().matches(sgDisplayItemStack, sgEntityItemStack)) { - Util.debugLog("The item matches display item but it not manage by QuickShop, removeing dupes: " + inChunkEntity - .getLocation()); - e.remove(); - removed = true; - } - - // Item - // Location eLoc = e.getLocation().getBlock().getLocation(); - // if (eLoc.equals(shop.getLocation()) || eLoc.equals(displayLoc)) { - // ItemStack near = ((Item) e).getItemStack(); - // // if its the same its a dupe - // if (this.shop.matches(near)) { - // e.remove(); - // removed = true; - // } - // } - } - return removed; - - } - - /** - * Removes the display item. - */ - @Override - public void remove() { - if (this.item == null) - return; - this.item.remove(); - this.item = null; - } - - /** - * @return Returns the exact location of the display item. (1 above shop - * block, in the center) - */ - @Override - public Location getDisplayLocation() { - return this.shop.getLocation().clone().add(0.5, 1.2, 0.5); - } - - /** - * Returns the reference to this shops item. Do not modify. - */ - @Override - public Item getItem() { - return this.item; - } - - @Override - public boolean checkDisplayMoved(Shop shop) { - return ((ContainerShop) shop).checkDisplayMoved(); - } -} \ No newline at end of file