Solved Check if clicked chest is double chest

Discussion in 'Spigot Plugin Development' started by GB_6, Apr 18, 2017.

  1. Title says it all, this is what I tried:

    Code (Text):
    if (e.getClickedBlock().getType() == Material.CHEST && e.getItem().equals(item)) {
                DoubleChest chest = null;
                if (e.getClickedBlock().getState() instanceof DoubleChest) {
                    chest = (DoubleChest) e.getClickedBlock().getState();
                    e.getPlayer().sendMessage("DC");
                }
                double earned = 0;
                if(chest.getInventory() == null) return;
                for (ItemStack i : chest.getInventory()) {
                    if (!(i == null)) {
                        e.getPlayer().sendMessage("CHEST" + i);
                        if (Main.plugin.getWorth().contains("worth." + i.getType().toString().toLowerCase())) {
                            double w = Main.plugin.getWorth().getDouble("worth." + i.getType().toString().toLowerCase());
                            earned += w * i.getAmount();
                            chest.getInventory().remove(i);
                        }
                    }

                }

                Main.plugin.getEco().depositPlayer(e.getPlayer(), earned);
                e.getPlayer().sendMessage(sellmessage.replace("{amount}", "" + earned));
            }
    But it doesnt send me the message "DC" so it doesnt think its an instance of DoubleChest, which it is...

    Solved somehow by changing every DoubleChest with Chest idk why....
     
  2. So now earned is just one even if i put in 4 dirt
     
  3. Wait, so I'm looking at your code, you may or may not have done this. Checking the chest's slots/space?
    Wait, did you solve it? I'm confused :(
     
  4. Double chests are a little odd, a double chest will pick up on instanceof double chest. But it is still comprised of 2 separate chest blocks.
    This means if you're dealing with something that might prevent opening them then you have to handle Both chests separately.




    Sent from my iPhone using Tapatalk
     
  5. How would i go about doing that?
     
  6. Since an adjacent chest can only be connected if it is north/south/east/west of the current chest.

    I would say create an array of blockfaces [] ={BlockFace.NORTH, BlockFace.SOUTH, etc}
    Then loop through those and check
    If (yourBlock.getRealitive(faceFromLoop).getType == Material.Chest)

    If you find one then handle it as you would the chest that was clicked on


    Sent from my iPhone using Tapatalk
     
    • Useful Useful x 1
  7. The problem is that DoubleChest isn't a BlockState, but an InventoryHolder. You'd probably need to somehow get it through the block's inventory I suppose.
    There can be multiple chests in that search, and not all of them are connected (you can have chests north and south of a chest which is connected either to east or west)
     
  8. I am pretty sure opening either block in a double chest points to the same inventory though. Thus if you need the other block you'll need to check the adjacent ones to find it


    Sent from my iPhone using Tapatalk
     
    • Useful Useful x 1
  9. I actually only need the inventory/the items inside it
     
  10. Then check for instance of chest should work!


    Sent from my iPhone using Tapatalk
     
  11. Choco

    Moderator

    I've always hated the way double chests were handled. If I'm not mistaken you can do the following to check if it is a double chest
    Code (Java):
    Block theChestBlock = // Get the block from somewhere
    BlockState chestState = theChestBlock.getState();
    if (state instanceof Chest) {
        Chest chest = (Chest) chestState;
        Inventory inventory = chest.getInventory();
        if (inventory instanceof DoubleChestInventory) {
            DoubleChest doubleChest = (DoubleChest) inventory.getHolder();
            // You have a double chest instance
        }
    }
    I'm not 100% certain if this will work, (in fact I'm rather doubtful). Though as I mentioned previously, I absolutely hate the DoubleChest API. I never managed to get it working in one of my plugins, and I ended up just checking if there were any chests within the vicinity of the original chest.
     
    • Useful Useful x 3
    • Like Like x 2
  12. Yes if figured it out. Thank you and @dNiym!!!
     
  13. Choco

    Moderator

    Keep in mind, if you call Chest#getInventory(), it only returns the portion of the inventory that the Chest state owns. i.e. if it is part of a DoubleChest, the Chest#getInventory() method will only return an Inventory with 27 slots rather than the whole 54. DoubleChest#getInventory() solves this issue, but eh...
     
    • Winner Winner x 2