Replace Wood with Sapling

Discussion in 'Plugin Development' started by Mycrowut, Aug 4, 2013.

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

    Mycrowut

    Hello Everyone I am trying to create a tree replanting plugin. I am having some trouble trying to figure out how to plant the sapling on the location of the Broken Block. When I break the log, a sapling comes up (is on the ground) as if a player has dropped it on the ground.

    Could someone tell me how I could correctly do this?
    I am also checking if the block underneath the broken block is dirt. (Which works)

    Code:java
    1. @EventHandler
    2. public void onBlockBreak(BlockBreakEvent event) {
    3. Player player = event.getPlayer();
    4. if(event.getBlock().getTypeId() == 17){
    5. double yBlock = event.getBlock().getLocation().getBlockY();
    6. yBlock = (yBlock - 1);
    7. Location location = new Location(player.getWorld(), event.getBlock().getX(), yBlock, event.getBlock().getZ());
    8. Block block = location.getBlock();
    9.  
    10. Location saplingLocation = new Location(player.getWorld(), event.getBlock().getX(), event.getBlock().getY(), event.getBlock().getZ());
    11. Block saplingBlock = (Block) saplingLocation.getBlock().getLocation();
    12. if(block.getTypeId() == 3){
    13. if(event.getBlock().getData() == 0){
    14. saplingBlock.setType(Material.STONE);
    15. Bukkit.broadcastMessage("asd");
    16. }
    17. if(event.getBlock().getData() == 1){
    18. getServer().broadcastMessage("broke dark wood");
    19. }
    20. if(event.getBlock().getData() == 2){
    21. getServer().broadcastMessage("broke white? wood");
    22. }
    23. if(event.getBlock().getData() == 3){
    24. getServer().broadcastMessage("broke jungle? wood");
    25. }
    26. }
    27. }
    28. }


    Thank you,
    -Mycro

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  2. Offline

    Axe2760

    To set a block,
    Code:
    block.setType(material);
    Where block is the Block where you want it planted, and material is the Material of sapling. (I think its Material.SAPLING, but I'm too lazy to check).
     
Thread Status:
Not open for further replies.

Share This Page