diff --git a/src/main/java/org/shanerx/tradeshop/listeners/ShopProtectionListener.java b/src/main/java/org/shanerx/tradeshop/listeners/ShopProtectionListener.java index 2e13dacd..0bdd2ec6 100644 --- a/src/main/java/org/shanerx/tradeshop/listeners/ShopProtectionListener.java +++ b/src/main/java/org/shanerx/tradeshop/listeners/ShopProtectionListener.java @@ -52,6 +52,7 @@ import org.shanerx.tradeshop.framework.events.PlayerShopInventoryOpenEvent; import org.shanerx.tradeshop.objects.Shop; import org.shanerx.tradeshop.objects.ShopChest; +import org.shanerx.tradeshop.objects.ShopLocation; import org.shanerx.tradeshop.utils.Utils; import org.shanerx.tradeshop.utils.config.Message; import org.shanerx.tradeshop.utils.config.Setting; @@ -236,7 +237,10 @@ public void onBlockBreak(BlockBreakEvent event) { if (sc != null) sc.resetName(); - shop.removeStorage(); + if (shop.getInventoryLocationAsSL().equals(new ShopLocation(block.getLocation()))) + shop.removeStorage(); + else + plugin.getDataStorage().removeChestLinkage(new ShopLocation(block.getLocation())); if (shop.getShopSign() == null) { shop.remove(); diff --git a/src/main/java/org/shanerx/tradeshop/objects/Shop.java b/src/main/java/org/shanerx/tradeshop/objects/Shop.java index 120afcf7..fd1c64ce 100644 --- a/src/main/java/org/shanerx/tradeshop/objects/Shop.java +++ b/src/main/java/org/shanerx/tradeshop/objects/Shop.java @@ -723,6 +723,7 @@ public void fixAfterLoad() { if (!getShopType().isITrade() && chestLoc != null) { chestLoc.stringToWorld(); cost.removeIf(item -> item.getItemStack().getType().toString().endsWith("SHULKER_BOX") && getInventoryLocation().getBlock().getType().toString().endsWith("SHULKER_BOX")); + utils.PLUGIN.getDataStorage().addChestLinkage(chestLoc, shopLoc); } /* TODO Fix this after 2.4 diff --git a/src/main/java/org/shanerx/tradeshop/objects/ShopChest.java b/src/main/java/org/shanerx/tradeshop/objects/ShopChest.java index 8d9ba151..c8164b0a 100644 --- a/src/main/java/org/shanerx/tradeshop/objects/ShopChest.java +++ b/src/main/java/org/shanerx/tradeshop/objects/ShopChest.java @@ -120,24 +120,40 @@ public static void resetOldName(Block checking) { } if (((Nameable) stateLeft).getCustomName().contains("$ ^Sign:l_")) { ((Nameable) stateLeft).setCustomName(((Nameable) stateLeft).getCustomName().split("\\$ \\^")[0]); - stateLeft.update(); - } + stateLeft.update(); + } - } else if (((Nameable) bs).getCustomName().contains("$ ^Sign:l_")) { - ((Nameable) bs).setCustomName(((Nameable) bs).getCustomName().split("\\$ \\^")[0]); - bs.update(); - } - } - } + } else if (((Nameable) bs).getCustomName().contains("$ ^Sign:l_")) { + ((Nameable) bs).setCustomName(((Nameable) bs).getCustomName().split("\\$ \\^")[0]); + bs.update(); + } + } + } } - public static DoubleChest getDoubleChest(Block chest) { - try { + public static Block getOtherHalfOfDoubleChest(Block check) { + Block otherChest = null; + if (check.getState() instanceof Chest) { + Chest chest = (Chest) check.getState(); + Location chestLoc = chest.getInventory().getLocation(), otherChestLoc = chest.getInventory().getLocation(); + if (chestLoc.getX() - Math.floor(chestLoc.getX()) > 0) { + otherChestLoc.setX(check.getX() == Math.floor(chestLoc.getX()) ? Math.ceil(chestLoc.getX()) : Math.floor(chestLoc.getX())); + } else if (chestLoc.getZ() - Math.floor(chestLoc.getZ()) > 0) { + otherChestLoc.setZ(check.getX() == Math.floor(chestLoc.getZ()) ? Math.ceil(chestLoc.getZ()) : Math.floor(chestLoc.getZ())); + } + otherChest = otherChestLoc.getBlock(); + } + + return otherChest; + } + + public static DoubleChest getDoubleChest(Block chest) { + try { return (DoubleChest) ((Chest) chest.getState()).getInventory().getHolder(); - } catch (ClassCastException | NullPointerException ex) { - return null; - } - } + } catch (ClassCastException | NullPointerException ex) { + return null; + } + } public static boolean isDoubleChest(Block chest) { return getDoubleChest(chest) != null; @@ -153,6 +169,7 @@ private void getBlock() { Container left = ((Container) dbl.getLeftSide()), right = ((Container) dbl.getRightSide()); chest = left.getPersistentDataContainer().has(plugin.getSignKey(), PersistentDataType.STRING) ? left.getBlock() : right.getBlock(); + } else chest = block; } catch (NullPointerException npe) { @@ -194,6 +211,7 @@ public void loadFromName() { } catch (IllegalWorldException e) { shopSign = new ShopLocation(e.getLoc().getLocation(chest.getWorld())); } + owner = UUID.fromString(chestData.get("Owner")); } } diff --git a/src/main/java/org/shanerx/tradeshop/utils/Utils.java b/src/main/java/org/shanerx/tradeshop/utils/Utils.java index a1989565..ae30bbfb 100644 --- a/src/main/java/org/shanerx/tradeshop/utils/Utils.java +++ b/src/main/java/org/shanerx/tradeshop/utils/Utils.java @@ -46,6 +46,7 @@ import org.shanerx.tradeshop.objects.Debug; import org.shanerx.tradeshop.objects.IllegalItemList; import org.shanerx.tradeshop.objects.Shop; +import org.shanerx.tradeshop.objects.ShopChest; import org.shanerx.tradeshop.objects.ShopItemStack; import org.shanerx.tradeshop.objects.ShopLocation; import org.shanerx.tradeshop.utils.config.Message; @@ -53,7 +54,6 @@ import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.UUID; @@ -290,30 +290,26 @@ public Sign findShopSign(Block chest) { if (potentialLocation != null && ShopType.isShop(potentialLocation.getLocation().getBlock())) return (Sign) potentialLocation.getLocation().getBlock().getState(); - ArrayList faces = PLUGIN.getListManager().getDirections(); - Collections.reverse(faces); - ArrayList flatFaces = new ArrayList<>(Arrays.asList(BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST)); - boolean isDouble = false; - BlockFace doubleSide = null; + ArrayList faces = PLUGIN.getListManager().getDirections(), + flatFaces = new ArrayList<>(Arrays.asList(BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST)); for (BlockFace face : faces) { + face = face.getOppositeFace(); // Check in the opposite direction that a sign would check Block relative = chest.getRelative(face); if (ShopType.isShop(relative)) { + if (ShopChest.isDoubleChest(chest)) { + PLUGIN.getDataStorage().addChestLinkage(new ShopLocation(ShopChest.getOtherHalfOfDoubleChest(chest).getLocation()), new ShopLocation(relative.getLocation())); + } return (Sign) relative.getState(); } else if (flatFaces.contains(face) && (chest.getType().equals(Material.CHEST) || chest.getType().equals(Material.TRAPPED_CHEST))) { - if (relative.getType().equals(chest.getType())) { - isDouble = true; - doubleSide = face; - } - } - } - - if (isDouble) { - chest = chest.getRelative(doubleSide); - for (BlockFace face : faces) { - Block relative = chest.getRelative(face); - if (ShopType.isShop(relative)) { - return (Sign) relative.getState(); + if (relative.getType().equals(chest.getType()) && ShopChest.isDoubleChest(chest)) { + for (BlockFace face2 : faces) { + Block relative2 = chest.getRelative(face).getRelative(face2.getOppositeFace()); + if (ShopType.isShop(relative2)) { + PLUGIN.getDataStorage().addChestLinkage(new ShopLocation(chest.getLocation()), new ShopLocation(relative2.getLocation())); + return (Sign) relative2.getState(); + } + } } } } diff --git a/src/main/java/org/shanerx/tradeshop/utils/config/Message.java b/src/main/java/org/shanerx/tradeshop/utils/config/Message.java index 2edbff92..59b2f60c 100644 --- a/src/main/java/org/shanerx/tradeshop/utils/config/Message.java +++ b/src/main/java/org/shanerx/tradeshop/utils/config/Message.java @@ -39,29 +39,22 @@ public enum Message { MESSAGE_VERSION(MessageSection.NONE, "message-version"), LANGUAGE(MessageSection.NONE, "language"), - AMOUNT_NOT_NUM(MessageSection.UNUSED, "amount-not-num"), - BUY_FAILED_SIGN(MessageSection.UNUSED, "buy-failed-sign"), CHANGE_CLOSED(MessageSection.NONE, "change-closed"), CHANGE_OPEN(MessageSection.NONE, "change-open"), - CONFIRM_TRADE(MessageSection.UNUSED, "confirm-trade"), EMPTY_TS_ON_SETUP(MessageSection.NONE, "empty-ts-on-setup"), EXISTING_SHOP(MessageSection.NONE, "existing-shop"), FEATURE_DISABLED(MessageSection.NONE, "feature-disabled"), - FULL_AMOUNT(MessageSection.UNUSED, "full-amount"), HELD_EMPTY(MessageSection.NONE, "held-empty"), ILLEGAL_ITEM(MessageSection.NONE, "illegal-item"), NO_SHULKER_COST(MessageSection.NONE, "no-shulker-cost"), INSUFFICIENT_ITEMS(MessageSection.NONE, "insufficient-items"), SHOP_INSUFFICIENT_ITEMS(MessageSection.NONE, "shop-insufficient-items"), INVALID_ARGUMENTS(MessageSection.NONE, "invalid-arguments"), - INVALID_SIGN(MessageSection.UNUSED, "invalid-sign"), - INVALID_SUBCOMMAND(MessageSection.UNUSED, "invalid-subcommand"), ITEM_ADDED(MessageSection.NONE, "item-added"), ITEM_NOT_REMOVED(MessageSection.NONE, "item-not-removed"), ITEM_REMOVED(MessageSection.NONE, "item-removed"), MISSING_CHEST(MessageSection.NONE, "missing-chest"), MISSING_ITEM(MessageSection.NONE, "missing-item"), - MISSING_SHOP(MessageSection.UNUSED, "missing-shop"), MULTI_AMOUNT(MessageSection.NONE, "multi-amount"), MULTI_UPDATE(MessageSection.NONE, "multi-update"), NO_CHEST(MessageSection.NONE, "no-chest"), @@ -83,7 +76,6 @@ public enum Message { SHOP_CLOSED(MessageSection.NONE, "shop-closed"), SHOP_EMPTY(MessageSection.NONE, "shop-empty"), SHOP_FULL(MessageSection.NONE, "shop-full"), - SHOP_FULL_AMOUNT(MessageSection.UNUSED, "shop-full-amount"), SHOP_ITEM_LIST(MessageSection.NONE, "shop-item-list"), SHOP_TYPE_SWITCHED(MessageSection.NONE, "shop-type-switched"), SUCCESSFUL_SETUP(MessageSection.NONE, "successful-setup"), diff --git a/src/main/java/org/shanerx/tradeshop/utils/data/DataStorage.java b/src/main/java/org/shanerx/tradeshop/utils/data/DataStorage.java index 3260881b..4dbdd093 100644 --- a/src/main/java/org/shanerx/tradeshop/utils/data/DataStorage.java +++ b/src/main/java/org/shanerx/tradeshop/utils/data/DataStorage.java @@ -216,7 +216,7 @@ public void removeChestLinkage(ShopLocation chestLocation) { } } - public void addChestLinkage(ShopLocation chestLocation, ShopLocation shopLocation) { + public void addLinkage(ShopLocation chestLocation, ShopLocation shopLocation) { if (chestLinkage.containsKey(chestLocation.getWorld())) { if (chestLinkage.get(chestLocation.getWorld()).containsKey(chestLocation.serialize())) chestLinkage.get(chestLocation.getWorld()).replace(chestLocation.serialize(), shopLocation.serialize()); @@ -225,9 +225,27 @@ public void addChestLinkage(ShopLocation chestLocation, ShopLocation shopLocatio } else { chestLinkage.put(chestLocation.getWorld(), Collections.singletonMap(chestLocation.serialize(), shopLocation.serialize())); } + } + + public void addChestLinkage(ShopLocation chestLocation, ShopLocation shopLocation) { + loadChestLinkage(chestLocation.getWorld()); + + if (ShopChest.isDoubleChest(chestLocation.getLocation().getBlock())) { + ShopLocation otherSideLocation = new ShopLocation(ShopChest.getOtherHalfOfDoubleChest(chestLocation.getLocation().getBlock()).getLocation()); + addLinkage(otherSideLocation, shopLocation); + } + + addLinkage(chestLocation, shopLocation); + saveChestLinkages(); } + + public boolean hasChestLinkage(ShopLocation chestLocation) { + loadChestLinkage(chestLocation.getWorld()); + return chestLinkage.containsKey(chestLocation.getWorld()) && chestLinkage.get(chestLocation.getWorld()).containsKey(chestLocation.serialize()); + } + public void saveChestLinkages() { for (World world : chestLinkage.keySet()) { new JsonConfiguration(world).saveChestLinkage(chestLinkage.get(world)); diff --git a/src/main/resources/Lang/en-us.yml b/src/main/resources/Lang/en-us.yml index 3ea8484a..15e5fd0a 100644 --- a/src/main/resources/Lang/en-us.yml +++ b/src/main/resources/Lang/en-us.yml @@ -164,20 +164,10 @@ message: default: "en-us" pre-comment: "This currently does not change anything in the message files, but is used in metrics so that we can see what languages we should try to provide built in support for in the future.\n Please use codes as listed at: https://www.andiamo.co.uk/resources/iso-language-codes/" post-comment: "\n" - amount-not-num: - default: "&cYou should have an amount before each item." - pre-comment: "\\Unused\\" - post-comment: - buy-failed-sign: - default: "&cThis shop sign does not seem to be formatted correctly, please notify the owner." - pre-comment: "\\Unused\\" change-closed: default: "&cThe shop is now &l&bCLOSED&r&a." change-open: default: "&aThe shop is now &l&bOPEN&r&a." - confirm-trade: - default: "&eTrade &6{AMOUNT1} {ITEM1} &efor &6{AMOUNT2} {ITEM2} &e?" - pre-comment: "\\Unused\\" empty-ts-on-setup: default: "&cTradeShop empty, please remember to fill it!" pre-comment: "Text to display when a player places a TradeSign above an empty chest:" @@ -185,9 +175,6 @@ message: default: "&cThis storage or sign is already linked to a shop." feature-disabled: default: "&cThis feature has been disabled on this server!" - full-amount: - default: "&cYou must have &e{AMOUNT} &cof a single type of &e{ITEM}&c!" - pre-comment: "\\Unused\\" held-empty: default: "&eYou are currently holding nothing." pre-comment: "Text to display when the player is not holding anything" @@ -206,12 +193,6 @@ message: invalid-arguments: default: "&eTry &6/tradeshop help &eto display help!" pre-comment: "Text to display when invalid arguments are submitted through the \"/tradeshop\" command:" - invalid-sign: - default: "&cInvalid sign format!" - pre-comment: "\\Unused\\" - invalid-subcommand: - default: "&cInvalid sub-command. Cannot display usage." - pre-comment: "\\Unused\\" item-added: default: "&aItem successfully added to shop." item-not-removed: @@ -223,9 +204,6 @@ message: missing-item: default: "&cYour sign is missing an item for trade." pre-comment: "Text to display when a shop sign failed creation due to missing an item" - missing-shop: - default: "&cThere is not currently a shop here, please tell the owner or come back later!" - pre-comment: "\\Unused\\" multi-amount: default: "&aYour trade multiplier is %amount%." multi-update: @@ -274,9 +252,8 @@ message: pre-comment: "Text to display when a player tries to buy form a shop in which they are a user" setup-help: default: "\n&2Setting up a TradeShop is easy! Just make sure to follow these steps:\n \nStep 1: &ePlace down a chest.\n&2Step 2: &ePlace a sign on top of or around the chest.\n&2Step 3: &eWrite the following on the sign\n&6 [%header%]\n&6&o-- Leave Blank --\n&6&o-- Leave Blank --\n&6&o-- Leave Blank --\n&2Step 4: &eUse the addCost and addProduct commands to add items to your shop" - pre-comment: "Text to display on \"/tradeshop setup\":" - post-comment: - shop-closed: + pre-comment: "Text to display on \"/tradeshop setup\":" + shop-closed: default: "&cThis shop is currently closed." shop-empty: default: "&cThis TradeShop is currently missing items to complete the trade!"