Skip to content

Commit

Permalink
Bug fixes and Minor Changes
Browse files Browse the repository at this point in the history
- Fixed bug in Data storage that caused chest-linkage files to be wiped on every launch/reload
- Fixed "bug" where both sides of a shops double chest were not added to the chest linkage map
- Removed Unused messages from Message enum and lang file
- Fixed Bug causing a Shops chest to be unlinked if the other half of a double chest was destroyed
- Changed potential bug in Utils#findShopSign, the sign is now found in the same direction order as the sign would find the chest but with each direction flipped instead of the whole list flipped(could grab a different data from shop->chest vs chest->shop)
  • Loading branch information
KillerOfPie committed Jan 9, 2022
1 parent 659bf19 commit f20fa38
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/shanerx/tradeshop/objects/Shop.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 32 additions & 14 deletions src/main/java/org/shanerx/tradeshop/objects/ShopChest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -194,6 +211,7 @@ public void loadFromName() {
} catch (IllegalWorldException e) {
shopSign = new ShopLocation(e.getLoc().getLocation(chest.getWorld()));
}

owner = UUID.fromString(chestData.get("Owner"));
}
}
Expand Down
34 changes: 15 additions & 19 deletions src/main/java/org/shanerx/tradeshop/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@
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;
import org.shanerx.tradeshop.utils.config.Setting;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
Expand Down Expand Up @@ -290,30 +290,26 @@ public Sign findShopSign(Block chest) {
if (potentialLocation != null && ShopType.isShop(potentialLocation.getLocation().getBlock()))
return (Sign) potentialLocation.getLocation().getBlock().getState();

ArrayList<BlockFace> faces = PLUGIN.getListManager().getDirections();
Collections.reverse(faces);
ArrayList<BlockFace> flatFaces = new ArrayList<>(Arrays.asList(BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST));
boolean isDouble = false;
BlockFace doubleSide = null;
ArrayList<BlockFace> 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();
}
}
}
}
}
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/org/shanerx/tradeshop/utils/config/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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"),
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/org/shanerx/tradeshop/utils/data/DataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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));
Expand Down
27 changes: 2 additions & 25 deletions src/main/resources/Lang/en-us.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,30 +164,17 @@ 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:"
existing-shop:
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"
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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!"
Expand Down

0 comments on commit f20fa38

Please sign in to comment.