From 0a99def55d18384f681cc031e9ce71ca7ca46c46 Mon Sep 17 00:00:00 2001 From: Ghost_chu <2908803755@qq.com> Date: Sun, 9 Jun 2019 22:16:14 +0800 Subject: [PATCH] Fix removeDupe will remove wrong dupe shop's display bug Former-commit-id: 8ede807d661209cbcc8ccce09c752dd5bc6c86cb [formerly 16a46c70be7880eb8b137d08984e11eb9913adc3] Former-commit-id: f45e841f0db174a5bb96577cd99135328bfde420 --- .../quickshop/Shop/ArmorStandDisplayItem.java | 12 ++++--- .../maxgamer/quickshop/Shop/DisplayItem.java | 31 +++++++++++++++++-- .../quickshop/Shop/RealDisplayItem.java | 12 ++++--- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ArmorStandDisplayItem.java b/src/main/java/org/maxgamer/quickshop/Shop/ArmorStandDisplayItem.java index 060acbacb..ccd62239d 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ArmorStandDisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ArmorStandDisplayItem.java @@ -84,7 +84,7 @@ public class ArmorStandDisplayItem implements DisplayItem { } ArmorStand armorStand = (ArmorStand) entity; //Set item protect in the armorstand's hand - this.guardedIstack = DisplayItem.createGuardItemStack(this.originalItemStack); + this.guardedIstack = DisplayItem.createGuardItemStack(this.originalItemStack, this.shop); armorStand.setItemInHand(guardedIstack); Util.debugLog("Successfully safeGuard ArmorStand: " + armorStand.getLocation().toString()); } @@ -221,10 +221,12 @@ public class ArmorStandDisplayItem implements DisplayItem { ArmorStand eArmorStand = (ArmorStand) entity; if (plugin.getItemMatcher().matches(eArmorStand.getItemInHand(), this.guardedIstack)) { if (!eArmorStand.getUniqueId().equals(this.armorStand.getUniqueId())) { - Util.debugLog("Removing dupes ArmorEntity " + eArmorStand.getUniqueId().toString() + " at " + eArmorStand - .getLocation().toString()); - entity.remove(); - removed = true; + if (DisplayItem.checkIsTargetShopDisplay(eArmorStand.getItemInHand(), this.shop)) { + Util.debugLog("Removing dupes ArmorEntity " + eArmorStand.getUniqueId().toString() + " at " + eArmorStand + .getLocation().toString()); + entity.remove(); + removed = true; + } } } } diff --git a/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java b/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java index 69a605c59..7b9df5671 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java @@ -97,7 +97,7 @@ public interface DisplayItem { * @param itemStack Old itemStack * @return New itemStack with protect flag. */ - public static ItemStack createGuardItemStack(ItemStack itemStack) { + public static ItemStack createGuardItemStack(ItemStack itemStack, Shop shop) { itemStack = itemStack.clone(); ItemMeta iMeta = itemStack.getItemMeta(); if (QuickShop.instance.getConfig().getBoolean("shop.display-item-use-name")) { @@ -105,7 +105,8 @@ public interface DisplayItem { } 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. + lore.add("QuickShop DisplayItem#" + shop + .hashCode()); //Create 20 lines lore to make sure no stupid plugin accident remove mark. } iMeta.setLore(lore); itemStack.setItemMeta(iMeta); @@ -139,6 +140,32 @@ public interface DisplayItem { return false; } + /** + * Check the itemStack is target shop's display + * + * @param itemStack Target ItemStack + * @param shop Target shop + * @return Is target shop's display + */ + public static boolean checkIsTargetShopDisplay(ItemStack itemStack, Shop shop) { + if (itemStack == null) + return false; + itemStack = itemStack.clone(); + if (!itemStack.hasItemMeta()) + return false; + ItemMeta iMeta = itemStack.getItemMeta(); + if (!iMeta.hasLore()) + return false; + List lores = iMeta.getLore(); + for (String lore : lores) { + if (lore.toLowerCase().contains("quickshop displayitem")) { + if (lore.equals("quickshop displayitem#" + shop.hashCode())) + return true; + } + } + return false; + } + /** * Get plugin now is using which one DisplayType * @return Using displayType. diff --git a/src/main/java/org/maxgamer/quickshop/Shop/RealDisplayItem.java b/src/main/java/org/maxgamer/quickshop/Shop/RealDisplayItem.java index 940f65e03..3b5b5194c 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/RealDisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/RealDisplayItem.java @@ -78,7 +78,7 @@ public class RealDisplayItem implements DisplayItem { } Item item = (Item) entity; //Set item protect in the armorstand's hand - this.guardedIstack = DisplayItem.createGuardItemStack(this.originalItemStack); + this.guardedIstack = DisplayItem.createGuardItemStack(this.originalItemStack, this.shop); item.setItemStack(this.guardedIstack); Util.debugLog("Successfully safeGuard Item: " + item.getLocation().toString()); } @@ -110,10 +110,12 @@ public class RealDisplayItem implements DisplayItem { continue; if (plugin.getItemMatcher().matches(eItem.getItemStack(), this.guardedIstack)) { if (!eItem.getUniqueId().equals(this.item.getUniqueId())) { - Util.debugLog("Removing a duped ItemEntity " + eItem.getUniqueId().toString() + " at " + eItem - .getLocation().toString()); - entity.remove(); - removed = true; + if (DisplayItem.checkIsTargetShopDisplay(eItem.getItemStack(), this.shop)) { + Util.debugLog("Removing a duped ItemEntity " + eItem.getUniqueId().toString() + " at " + eItem + .getLocation().toString()); + entity.remove(); + removed = true; + } } } }