From 2464e7287f82d6137abd7fda2607290c31074c85 Mon Sep 17 00:00:00 2001 From: Brokkonaut Date: Tue, 7 Jul 2020 05:08:50 +0200 Subject: [PATCH] Upgrade items to keep them compatible --- .../Listeners/Player/PlayerInteract.java | 4 +- .../PreShopCreation/ItemChecker.java | 2 +- .../com/Acrobot/ChestShop/Utils/uBlock.java | 51 +++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java index d61c759df..166b4a5b9 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java @@ -137,7 +137,7 @@ private static void showShopInfo(Player player, Sign sign) { if (Properties.SHOW_SHOP_INFORMATION_ON_SHIFT_CLICK) { if (!ChestShopSign.isAdminShop(sign)) { - Container chest = uBlock.findConnectedChest(sign); + Container chest = uBlock.findConnectedChest(sign, true); if (chest != null) { // do not allow shulker boxes inside of shulker boxes if (chest instanceof ShulkerBox && BlockUtil.isShulkerBox(item.getType())) { @@ -179,7 +179,7 @@ private static PreTransactionEvent preparePreTransactionEvent(Sign sign, Player Action buy = Properties.REVERSE_BUTTONS ? LEFT_CLICK_BLOCK : RIGHT_CLICK_BLOCK; double price = (action == buy ? PriceUtil.getBuyPrice(prices) : PriceUtil.getSellPrice(prices)); - Container chest = uBlock.findConnectedChest(sign); + Container chest = uBlock.findConnectedChest(sign, true); Inventory ownerInventory = (ChestShopSign.isAdminShop(sign) ? new AdminInventory() : chest != null ? chest.getInventory() : null); ItemStack item = MaterialUtil.getItem(material); diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/ItemChecker.java b/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/ItemChecker.java index c210c0212..a2dd4bb39 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/ItemChecker.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/ItemChecker.java @@ -28,7 +28,7 @@ public static void onPreShopCreation(PreShopCreationEvent event) { boolean foundItem = false; if (Properties.ALLOW_AUTO_ITEM_FILL && itemCode.equals(AUTOFILL_CODE) && uBlock.findConnectedChest(event.getSign()) != null) { - for (ItemStack stack : uBlock.findConnectedChest(event.getSign()).getInventory().getContents()) { + for (ItemStack stack : uBlock.findConnectedChest(event.getSign(), true).getInventory().getContents()) { if (!MaterialUtil.isEmpty(stack)) { item = stack; itemCode = MaterialUtil.getSignName(stack); diff --git a/src/main/java/com/Acrobot/ChestShop/Utils/uBlock.java b/src/main/java/com/Acrobot/ChestShop/Utils/uBlock.java index 82f9964d2..72bf61fb3 100644 --- a/src/main/java/com/Acrobot/ChestShop/Utils/uBlock.java +++ b/src/main/java/com/Acrobot/ChestShop/Utils/uBlock.java @@ -10,7 +10,12 @@ import org.bukkit.block.Sign; import org.bukkit.block.data.BlockData; import org.bukkit.block.data.type.WallSign; +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; /** * @author Acrobot @@ -73,6 +78,10 @@ public static Block getConnectedChest(Block chestBlock) { } public static Container findConnectedChest(Sign sign) { + return findConnectedChest(sign, false); + } + + public static Container findConnectedChest(Sign sign, boolean upgradeItems) { Block block = sign.getBlock(); BlockFace signFace = null; BlockData data = sign.getBlockData(); @@ -81,6 +90,9 @@ public static Container findConnectedChest(Sign sign) { signFace = signData.getFacing().getOppositeFace(); Block faceBlock = block.getRelative(signFace); if (BlockUtil.isChest(faceBlock)) { + if (upgradeItems) { + upgradeContainerItems(faceBlock); + } return (Container) faceBlock.getState(); } } @@ -88,6 +100,9 @@ public static Container findConnectedChest(Sign sign) { if (bf != signFace) { Block faceBlock = block.getRelative(bf); if (BlockUtil.isChest(faceBlock)) { + if (upgradeItems) { + upgradeContainerItems(faceBlock); + } return (Container) faceBlock.getState(); } } @@ -95,6 +110,42 @@ public static Container findConnectedChest(Sign sign) { return null; } + private static void upgradeContainerItems(Block chestBlock) { + BlockState blockState = chestBlock.getState(); + if (blockState instanceof Container) { + Container container = (Container) blockState; + Inventory inventory = container.getInventory(); + ItemStack[] contents = inventory.getContents(); + YamlConfiguration conf = null; + for (int i = 0; i < contents.length; i++) { + ItemStack stack = contents[i]; + if (stack != null && stack.hasItemMeta()) { + ItemMeta meta = stack.getItemMeta(); + if (meta.hasDisplayName() || meta.hasLocalizedName() || meta.hasLore()) { + if (conf == null) { + conf = new YamlConfiguration(); + } + conf.set("item" + i, stack); + } + } + } + if (conf != null) { + try { + conf.loadFromString(conf.saveToString()); + } catch (InvalidConfigurationException e) { + throw new RuntimeException("should be impossible", e); + } + for (int i = 0; i < contents.length; i++) { + ItemStack stack = conf.getItemStack("item" + i); + if (stack != null) { + contents[i] = stack; + } + } + inventory.setContents(contents); + } + } + } + private static Sign findAnyNearbyShopSign(Block block, Block ignoredSign) { for (BlockFace bf : SHOP_FACES) { Block faceBlock = block.getRelative(bf);