Empty chest inventory?

Discussion in 'Plugin Development' started by Nitnelave, Jun 6, 2011.

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

    Nitnelave

    Hello everyone!
    I've got a problem I can't seem to be able to solve : I check for a chest around a dispenser with scanNeighborsExtended (returns an array of chests), and for each of the ones I find, I check for an adjacent chest (double chest) with scanForNeighborChest. The problem is, I tested it, and it gives me an empty chest inventory for the adjacent one, even though it detects that there is one (and yes, there is one, that is not empty). So, here's the relevant part of the code, hope you can help me on that :
    Code:
    public static Block scanForNeighborChest(World world, int x, int y, int z)
        {
        if ((world.getBlockAt(x - 1, y, z)).getType().equals(Material.CHEST)) {
        return world.getBlockAt(x - 1, y, z);
        }
        if ((world.getBlockAt(x + 1, y, z)).getType().equals(Material.CHEST)) {
        return world.getBlockAt(x + 1, y, z);
        }
        if ((world.getBlockAt(x, y, z - 1)).getType().equals(Material.CHEST)) {
        return world.getBlockAt(x, y, z - 1);
        }
        if ((world.getBlockAt(x, y, z + 1)).getType().equals(Material.CHEST)) {
        return world.getBlockAt(x, y, z + 1);
        }
        return null;
        }
    
        public static Block scanForNeighborChest(Block block)
        {
        return scanForNeighborChest(block.getWorld(), block.getX(), block.getY(), block.getZ());
        }
    
        private ContainerBlock[] scanNeighborsExtended(World world, Location location) {
            ContainerBlock[] chest =  new ContainerBlock[12];
            int list_length = 0;
            int x = (int) location.getX();
            int y = (int) location.getY();
            int z = (int) location.getZ();
            if (world.getBlockAt(x - 1, y, z).getType().equals(Material.CHEST)) {
                chest[list_length] = (ContainerBlock)world.getBlockAt(x - 1, y, z).getState();
                list_length++;
                Block tmp = scanForNeighborChest(world, x, y, z);
                if(tmp!=null){
                    chest[list_length] = (ContainerBlock)world.getBlockAt(tmp.getLocation()).getState();
                    list_length++;
                }
            }
            if (world.getBlockAt(x + 1, y, z).getType().equals(Material.CHEST)) {
                chest[list_length] = (ContainerBlock)world.getBlockAt(x + 1, y, z).getState();
                list_length++;
                Block tmp = scanForNeighborChest(world, x, y, z);
                if(tmp!=null){
                    chest[list_length] = (ContainerBlock)world.getBlockAt((int)tmp.getLocation().getX(), (int)tmp.getLocation().getY(), (int)tmp.getLocation().getZ()).getState();
                    list_length++;
                }
            }
            if (world.getBlockAt(x, y + 1, z).getType().equals(Material.CHEST)) {
                chest[list_length] = (ContainerBlock)world.getBlockAt(x, y + 1, z).getState();
                list_length++;
                Block tmp = scanForNeighborChest(world, x, y, z);
                if(tmp!=null){
                    chest[list_length] = (ContainerBlock)world.getBlockAt((int)tmp.getLocation().getX(), (int)tmp.getLocation().getY(), (int)tmp.getLocation().getZ()).getState();
                    list_length++;
                }
            }
            if (world.getBlockAt(x, y - 1, z).getType().equals(Material.CHEST)) {
                chest[list_length] = (ContainerBlock)world.getBlockAt(x, y - 1, z).getState();
                list_length++;
                Block tmp = scanForNeighborChest(world, x, y, z);
                if(tmp!=null){
                    chest[list_length] = (ContainerBlock)world.getBlockAt((int)tmp.getLocation().getX(), (int)tmp.getLocation().getY(), (int)tmp.getLocation().getZ()).getState();
                    list_length++;
                }
            }
            if (world.getBlockAt(x, y, z - 1).getType().equals(Material.CHEST)) {
                chest[list_length] = (ContainerBlock)world.getBlockAt(x, y, z - 1).getState();
                list_length++;
                Block tmp = scanForNeighborChest(world, x, y, z);
                if(tmp!=null){
                    chest[list_length] = (ContainerBlock)world.getBlockAt((int)tmp.getLocation().getX(), (int)tmp.getLocation().getY(), (int)tmp.getLocation().getZ()).getState();
                    list_length++;
                }
            }
            if (world.getBlockAt(x, y, z + 1).getType().equals(Material.CHEST)) {
                chest[list_length] = (ContainerBlock)world.getBlockAt(x, y, z + 1).getState();
                list_length++;
                Block tmp = scanForNeighborChest(world, x, y, z);
                if(tmp!=null){
                    chest[list_length] = (ContainerBlock)world.getBlockAt((int)tmp.getLocation().getX(), (int)tmp.getLocation().getY(), (int)tmp.getLocation().getZ()).getState();
                    list_length++;
                }
            }
            return chest;
        }
    
    chest[0] is a fine, non-empty chest (next to the dispenser, detected by the first battery of tests)
    chest[1] is an empty chest, event though it shouldn't be empty (next to chest[0], detected with scanForNeighborChest()).
     
  2. Offline

    Nitnelave

    ALERT! NOOB ERROR!
    Each time I was scanning for chest next to the original block... which would throw me back the same chest! I just had to change this :
    Code:
    chest[list_length] = (ContainerBlock)world.getBlockAt(x - 1, y, z).getState();             list_length++;             Block tmp = scanForNeighborChest(world, x, y, z);
    into this :
    Code:
    chest[list_length] = (ContainerBlock)world.getBlockAt(x - 1, y, z).getState();             list_length++;             Block tmp = scanForNeighborChest(world, x - 1, y, z);
    for each of the checks.
     
Thread Status:
Not open for further replies.

Share This Page