Am I missing something when modifying chests?

Discussion in 'Plugin Development' started by bowlerguy66, Jul 31, 2018.

Thread Status:
Not open for further replies.
  1. Offline

    bowlerguy66

    Hello! I'm working on a sign shop system but for some reason after the following code gets executed the item is not being removed from the chest at all. I'm using Bukkit.broadcastMessage to show that everything is correct, but it doesn't get removed from the chest.

    Main method (open)

    Code:
        public void performTransaction(Player buyer) {
    
            Sign sign = getSignObject();   
            org.bukkit.material.Sign signMaterial = (org.bukkit.material.Sign) this.signLocation.getBlock().getState().getData();
           
            Block attachedBlock = sign.getBlock().getRelative(signMaterial.getAttachedFace());
                   
            if(attachedBlock.getType() != Material.CHEST) {
                return;
            }
           
            Chest chest = (Chest) attachedBlock.getState();
            String line3 = sign.getLine(3);
    
            if(line3.length() < 2) {
                return;
            }
           
            PlayerAccount buyerAccount = new PlayerAccount(plugin, buyer);
           
            if(new String(line3.charAt(0) + "").equalsIgnoreCase("B")) {
    
                double price = LineTranslation.getPriceFromSignLine(line3);
               
                if(buyerAccount.getBalance() < price) {
                    buyer.sendMessage(Messages.SHOP_BALANCE_TOO_LOW);
                    return;
                }
               
                ItemStack item = LineTranslation.getItemFromSignLine(sign.getLine(2)); // Get Item from sign line translates the string from the in-game sign into an ItemStack
                Inventory chestInventory = chest.getInventory();
                           
                if(!Utils.chestContainsItem(chestInventory, item)) {
                    buyer.sendMessage(Messages.SHOP_CHEST_DOESNT_CONTAIN_ITEM);
                    return;
                }
               
                int firstAvailableSlot = Utils.getSlotOfFirstAvailable(chestInventory, item); // This gets the integer of the slot where there is an available itemstack to be removed
                ItemStack itemInChest = chestInventory.getItem(firstAvailableSlot);
           
                int amountDifference = itemInChest.getAmount() - item.getAmount();
           
                Bukkit.broadcastMessage("First available slot: " + firstAvailableSlot);
                Bukkit.broadcastMessage("Amountdifference: " + amountDifference);
               
                if(amountDifference != 0) {
                    Bukkit.broadcastMessage("Amount difference isn't 0");
                    chestInventory.setItem(firstAvailableSlot, new ItemStack(itemInChest.getType(), amountDifference));
                } else {
                    Bukkit.broadcastMessage("Amount difference is 0");
                    chestInventory.clear(firstAvailableSlot);
                }
    
                Bukkit.broadcastMessage("Complete. Updating chest");
               
                buyer.openInventory(chestInventory);           
                chest.update();
               
                Bukkit.broadcastMessage("Updated chest");
           
                buyerAccount.withdraw(price);
                buyerAccount.saveToConfig();
    
                buyer.getInventory().addItem(item);
                return;
               
            } else if(new String(line3.charAt(0) + "").equalsIgnoreCase("S")) {
           
               
               
            } else if(new String(line3.charAt(0) + "").equalsIgnoreCase("T")) {
           
           
               
            }
           
        }
    

    Utils.getSlotOfFirstAvailable() (open)

    Code:
        public static int getSlotOfFirstAvailable(Inventory inv, ItemStack item) {
    
            for(int slot = 0; slot < inv.getSize(); slot++) {
               
                ItemStack target = inv.getItem(slot);
               
                if(target == null || target.getType() == Material.AIR) {
                    continue;
                }
    
                if(target.getType() != item.getType()) {
                    continue;
                }
               
                if(target.getAmount() < item.getAmount()) {
                    continue;
                }
               
                return slot;
               
            }
           
            return -1;
           
        }
    

    Utils.chestContainsItem (open)

    Code:
        public static boolean chestContainsItem(Inventory inv, ItemStack item) {
           
            for(int slot = 0; slot < inv.getSize(); slot++) {
               
                ItemStack target = inv.getItem(slot);
               
                if(target == null || target.getType() == Material.AIR) {
                    continue;
                }
               
                if(target.getType() != item.getType()) {
                    continue;
                }
               
                if(target.getAmount() < item.getAmount()) {
                    continue;
                }
               
                return true;
               
            }
           
            return false;
           
        }
    
     
  2. Offline

    bowlerguy66

  3. Offline

    bowlerguy66

    SOLUTION:
    It turns out that it won't work out correctly if you try to update the block from the chest object itself. What you have to do is update the block.getState() for it to correctly update.
     
Thread Status:
Not open for further replies.

Share This Page