Skip to content

Commit

Permalink
Upgrade items to keep them compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
Brokkonaut committed Jul 7, 2020
1 parent e3e2db1 commit 2464e72
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
Expand Up @@ -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())) {
Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -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);
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/com/Acrobot/ChestShop/Utils/uBlock.java
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -81,20 +90,62 @@ 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();
}
}
for (BlockFace bf : SHOP_FACES) {
if (bf != signFace) {
Block faceBlock = block.getRelative(bf);
if (BlockUtil.isChest(faceBlock)) {
if (upgradeItems) {
upgradeContainerItems(faceBlock);
}
return (Container) faceBlock.getState();
}
}
}
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);
Expand Down

0 comments on commit 2464e72

Please sign in to comment.