Get the contents of a chest from right-clicking a adjacent block

Discussion in 'Plugin Development' started by Wbjpen, Sep 18, 2013.

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

    Wbjpen

    This is what I need to do: When you right-click a block (Cauldron) it finds the chest that is right next to it and gets the itemstack(s) in the first 4 slots of the chest. I'd then add some 'If statements' that do different things depending what is in the chest.
     
  2. Offline

    Nateb1121

    This is a horridly inefficient way to do it, but:

    Code:
            public void onPlayerInteract(PlayerInteractEvent evt) {
                //Scan through.
           
                int x = evt.getClickedBlock().getX() - 1;
                int y = evt.getClickedBlock().getY();
                int z = evt.getClickedBlock().getZ();
           
                if(evt.getClickedBlock().getWorld().getBlockAt(x, y, z).getType() == Material.CHEST) {
                    Chest c = (Chest)evt.getClickedBlock().getWorld().getBlockAt(x, y ,z);
                    Inventory inv = c.getInventory();
                    //Stuff Here.
               
                    return;
                }
           
                int x2 = evt.getClickedBlock().getX() + 1;
                int y2 = evt.getClickedBlock().getY();
           
                if(evt.getClickedBlock().getWorld().getBlockAt(x2, y2, z).getType() == Material.CHEST) {
                    Chest c = (Chest)evt.getClickedBlock().getWorld().getBlockAt(x2, y2 ,z);
                    Inventory inv = c.getInventory();
                    //Stuff Here.
               
                    return;
                }
           
                int x3 = evt.getClickedBlock().getX();
                int y3 = evt.getClickedBlock().getY() - 1;
           
                if(evt.getClickedBlock().getWorld().getBlockAt(x3, y3, z).getType() == Material.CHEST) {
                    Chest c = (Chest)evt.getClickedBlock().getWorld().getBlockAt(x3, y3 ,z);
                    Inventory inv = c.getInventory();
                    //Stuff Here.
               
                    return;
                }
                   
                int x4 = evt.getClickedBlock().getX();
                int y4 = evt.getClickedBlock().getY()+1;
           
                if(evt.getClickedBlock().getWorld().getBlockAt(x4, y4, z).getType() == Material.CHEST) {
                    Chest c = (Chest)evt.getClickedBlock().getWorld().getBlockAt(x4, y4 ,z);
                    Inventory inv = c.getInventory();
                    //Stuff Here.
               
                    return;
                }
           
            }

    What you really want to do is make this some sort of loop, so it's not taking up so much memory with the multiple variables. As well as computation time. Might want to just save an instance of the getClickedBlock()... But, if I spoon fed you good code, you wouldn't learn ;)
     
Thread Status:
Not open for further replies.

Share This Page