Casting to Chest

Discussion in 'Plugin Development' started by TheDuceCat, Nov 12, 2011.

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

    TheDuceCat

    In one part of my plugin, I have to determine whether a block someone clicks is a chest or not. Once I know it's a chest, I tried casting the block to a chest, but I keep getting an error. How can I do this?
     
  2. Offline

    DDoS

    Use this:

    PHP:
    if (block.getState() instanceof Chest) { //Verify that it's a chest

        
    Chest chest = (Chestblock.getState(); //Cast it
        //Now you've got a chest instance!

    }
     
    TheDuceCat likes this.
  3. Offline

    TheDuceCat

    Ah, thanks so much! What exactly is a BlockState?
     
  4. Offline

    md_5

    Its something bukkit uses internally, its the block a certain point in time. Every change you make to a block state will not happen unless you THESTATE.update()
     
  5. Offline

    TheDuceCat

    Do you know how to make it so the chests are combined. Right now, the inventories for the different parts of a large chest are separate, how can I find a way to "join" them into one.
     
  6. Offline

    md_5

    Try:
    block.getRelative(BlockFace.EAST);
    block.getRelative(BlockFace.WEST);
    block.getRelative(BlockFace.SOUTH);
    block.getRelative(BlockFace.NORTH);

    And see which one is a chest.
    Then:
    Code:
    ItemStack[] doublecontents = new ItemStack[chest1.getInventory().getContents().length + chest2.getInventory().getContents().length];
    System.arraycopy(chest1.getInventory().getContents(), 0, doublecontents, 0, chest1.getInventory().getContents().length);
    System.arraycopy(chest2.getInventory().getContents(), 0, doublecontents, chest1.getInventory().getContents().length, chest2.getInventory().getContents().length);
    doublecontents should have all the items.
     
  7. Offline

    TheDuceCat

    Looks good, only one problem. Say I have 10 redstone in the first half of the chest, and 10 diamond in the second half. If I use your code to get the contents and place it in another chest, how do I know if it will come out the right order (redstone first half, diamond second)?
     
  8. Offline

    md_5

    @TheDuceCat
    Problem. This will not be in order, ie spaces will be lost:
    Code:
                          Chest chest = (Chest) block.getState();
                            Chest chest2 = (Chest) block2.getState();
                            Inventory inv = chest.getInventory();
                            Inventory inv2 = chest2.getInventory();
                            Inventory[] contents;
                            contents[0] = inv;
                            contents[1] = inv2;
    Not sure if thats what you want, but it will keep both halves in one variable.
     
  9. Offline

    TheDuceCat

    I don't think there is any way around this, but has two double chests next to each other, then uses the right chest on one of them to get the inventory, and then go to the other double chest and put it on the left one, it will reverse the inventories.
     
  10. Offline

    md_5

    Meh, just keep track of each, there is no reason to join them unless you want to compare them.
    @TheDuceCat
     
  11. Offline

    TheDuceCat

    I don't want to physically "join" them, I just couldn't find a better word. I want to take the inventory from one double chest, and copy it to another one. However, if someone copies the left chest, which tags along the right chest, and then places the inventory on the right chest, the chest will be reversed.
     
  12. Offline

    bergerkiller

    People...please. General heads up, because I am tired of seeing that same code in all plugins.

    Stop using 'getstate instanceof <type>'
    The getState() function fires several background code to get the new object. For one, it is loading lighting information and tile entity information from the chunk. Getting this information takes some time, but it's not much.

    If you are continuously using getState() to compare something, it fires this same coding every.single.damn.time, which is completely useless and a waste of CPU power.

    Alternatives:
    Store the data and compare that
    Code:
    BlockState bs = block.getState();
    if (bs instanceof Sign) {
       //case
    } else if (bs instanceof Chest) {
       //cast
    } else if (bs instanceof Jukebox) {
       //cast
    }
    Compare the material type - also store here as it gets this from the chunk too
    Code:
    Material type = block.getType();
    if (type == Material.CHEST) {
        //cast getState
    } else if (type == Material.JUKEBOX) {
        //cast getstate
    }
    I know, it is insignificant, but the more of these plugins do this, the more impact it will have on the server performance. Some developers do this in onPlayerMove for various reasons, and this is just a bad way to do it.
     
    halley and tips48 like this.
  13. Offline

    md_5

    Yeah just get the block North, East, SOuth, West and find which one is a chest.
     
  14. Offline

    TheDuceCat

    I always check that the block clicked is the right one before I get the getState() and cast it.
     
  15. Great post! My views also :)
     
Thread Status:
Not open for further replies.

Share This Page