Leaves Decay

Discussion in 'Spigot Plugin Development' started by emanon, Jan 28, 2017.

  1. im attemping to set a leaves block as a block that shouldn't be able to decay

    here is my code


    //1
    @EventHandler(priority=EventPriority.MONITOR)
    public static void LeavesDecayEvent(org.bukkit.event.block.LeavesDecayEvent event){


    ((Leaves) event.getBlock().getState().getData()).setDecayable(false);
    event.setCancelled(true);

    }


    //2
    @EventHandler(priority=EventPriority.MONITOR)
    public static void LeavesDecayEvent(org.bukkit.event.block.LeavesDecayEvent event){


    Block b = event.getBlock();
    b.getState().setData(
    new Leaves(
    b.getType(),
    ((Leaves) b.getState()).getSpecies(),
    false
    ));
    event.setCancelled(true);
    }


    on the first attemp the code stops the event but it doesn't set the not-decayable flag

    on the second attemp i get an exception:
    java.lang.ClassCastException: org.bukkit.craftbukkit.v1_11_R1.block.CraftBlockState cannot be cast to org.bukkit.material.Leaves

    what's the mistake?
     
  2. How about
    Code (Java):
    @EventHandler
    public void onLeaveDecay(LeavesDecayEvent e) {
        e.setCancelled(true);
    }
    ?
     
  3. thats a way to deny the event, but it will loop. i want to stop the event from being called again and again for the same block.
     
  4. Not possible if you dont modify spigot itself
     
  5. i solved it with deprecated metods, so i guess must be there a way to do it with new methods too


    @EventHandler(priority=EventPriority.NORMAL)
    public static void LeavesDecayEvent(org.bukkit.event.block.LeavesDecayEvent event){

    byte d = event.getBlock().getData();
    event.getBlock().setData((byte) (d%4));
    event.setCancelled(true);

    }
     
    • Like Like x 1
  6. While searching the internet for the same thing I found your post but also a way to do this with the new methods, so yes you're right there is a way and you actually were quite close:
    You only made a small mistake: Leaves is a Subinterface of MaterialData not of BlockState. Thats why you get a Exception.
    To solve this change ((Leaves) b.getState()).getSpecies() to ((Leaves) b.getState.getData()).getSpecies().

    I hope I could help although my answer is a bit late...
     
    • Like Like x 1