Search code examples
javapluginsminecraftbukkit

How would I detect cobblestone being generated with Bukkit?


Ok, so I'm making a Bukkit plugin, that should detect cobblestone generators, keep in mind, that I don't want to prevent players from making cobblestone generators, I just want to get the block from the event, so I can do other stuff with that cobblestone.

What I've tried so far:

  • BlockFromToEvent(Doesn't find cobblestone, only finds STATIONARY_LAVA to AIR)
  • BlockFormEvent(It only found snow forming on the ground)

What I want to do:

  • I want to detect blocks of cobblestone generated from water+lava combo
  • I want to get their location/position/coordinates

Can you please at least point me in the right direction? I've been pulling out my hair for almost 3 hours already.

Thanks to everyone for the help!

EDIT: Solution in the picture below, will retype if requested! solution


Solution

  • Good answer from FireBlast709 at Bukkit Forums

    This will cancel any cobblestone generation. Basically, if you want to manipulate the cobblestone, you change the cancel line (indicated) to whatever you need.


    @EventHandler
    public void onFromTo(BlockFromToEvent event){
        Material type = event.getBlock().getType();
        if (type == Material.WATER || type == Material.STATIONARY_WATER || type == Material.LAVA || type == Material.STATIONARY_LAVA){
            Block b = event.getToBlock();
            if (b.getType() == Material.AIR){
                if (generatesCobble(type, b)){
                    /* DO WHATEVER YOU NEED WITH THE COBBLE */
                    event.setCancelled(true);
                }
            }
        }
    }
    
    private final BlockFace[] faces = new BlockFace[]{
            BlockFace.SELF,
            BlockFace.UP,
            BlockFace.DOWN,
            BlockFace.NORTH,
            BlockFace.EAST,
            BlockFace.SOUTH,
            BlockFace.WEST
        };
    
    public boolean generatesCobble(Material type, Block b){
        Material mirrorID1 = (type == Material.WATER || type == Material.STATIONARY_WATER ? Material.LAVA : Material.WATER);
        Material mirrorID2 = (type == Material.WATER || type == Material.STATIONARY_WATER ? Material.STATIONARY_LAVA : Material.STATIONARY_WATER);
        for (BlockFace face : faces){
            Block r = b.getRelative(face, 1);
            if (r.getType() == mirrorID1 || r.getType() == mirrorID2){
                return true;
            }
        }
        return false;
    }