[SOLVED] Cannot set block

Discussion in 'Spigot Plugin Development' started by KyleMan, May 23, 2015.

  1. I am making a plugin in which when you break a tree, if the block under it is dirt, it places a sappling. Now, this has not been working for me for anything, looked up tutorials and other help and cannot figure out what is wrong.

    Code (Text):
     
    //Checks for breaking a WOOD_BLOCK, LEAF_BLOCK
        @EventHandler
        public void onBlockBreak(BlockBreakEvent event){
            Player player = event.getPlayer();
            Block block = event.getBlock();
            World w = player.getWorld();
            Location location = block.getLocation();
            Location locUnder = event.getBlock().getLocation();
            locUnder.setY(block.getY() - 1);
            boolean c = check(block.getLocation(), w);
           
            int x = block.getX();
            int y = block.getY();
            int z = block.getZ();
           
            Block b = w.getBlockAt(x,y,z);
           
            if(block.getType() == Material.LOG && c == true){
                block.breakNaturally();
                 
                if((locUnder.getBlock().getType() == Material.DIRT) || (locUnder.getBlock().getType() == Material.GRASS)){
                b.setType(Material.STONE);
                player.sendMessage("Block under is dirt!");
                }
     
    Now I know that the IF is working because as you can see the player.sendMessage works ingame but the block does not change. Please help. Thanks
     
  2. Try having a physics cancel event

    Code (Text):
    @EventHandler
    public void onPhysics(BlockPhysicsEvent event) {
    If (event.getChangedType() == Material.SAPLING) {
    event.setCancelled(false);
    }
    }
    Sorry for any typos, phones and code don't mix :/
     
  3. Maybe you could cancel the block break event. I'm not sure about this, but it might be changing the block into stone, but because the event isn't cancelled the stone block breaks.
     
  4. Omg thank you so much! How stupid am I? Thanks for pointing that out! It works fine now