Delogger selecting leaves on a tree

Discussion in 'Plugin Development' started by AdvsNoob, Feb 26, 2012.

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

    AdvsNoob

    I am attentping to select the leaves on the tree and natually destroythem (Chances for apples and such to fall) but i am having trouble selecting all of them.

    This is my block listener.
    Note:Things are jumbled around and there are messages printing to the player for debug reasons)

    Code:
    package me.advsnoob.Delogger;
     
    import java.util.logging.Logger;
     
    import org.bukkit.Effect;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.block.BlockFace;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockListener;
    import org.bukkit.inventory.ItemStack;
     
    public class MyBlockListener extends BlockListener{
        public static Delogger plugin;
     
        public MyBlockListener(Delogger instance){
            plugin = instance;
        }
     
        public void onBlockBreak(BlockBreakEvent event){
            Material block = event.getBlock().getType();
            Player player = event.getPlayer();
            if(block == Material.LOG && player.getItemInHand().getType().name().toLowerCase().contains("axe")){
                Location blockLocation = event.getBlock().getLocation();
                    double y = blockLocation.getBlockY();
                    double x = blockLocation.getBlockX();
                    double z = blockLocation.getBlockZ();
                World currentWorld = event.getPlayer().getWorld();
             
                boolean logLeft = true;
             
                while(logLeft == true){
                    y++;
                    Location blockAbove = new Location(currentWorld, x, y, z);
                    Material blockAboveType = blockAbove.getBlock().getType();
                    Byte blockAboveData = blockAbove.getBlock().getData();
                    if(blockAboveType == Material.LOG || blockAboveType == Material.LEAVES){
                        if(blockAboveType == Material.LOG)
                        {
                        player.sendMessage("Log");
                        Material Leaves = block.LEAVES;
                        Block East = blockAbove.getBlock().getFace(BlockFace.EAST);
                        Block East2 = blockLocation.getBlock().getFace(BlockFace.EAST);
                        Block West = blockLocation.getBlock().getFace(BlockFace.WEST);
                        Block West2 = blockLocation.getBlock().getFace(BlockFace.WEST);
                        Block North = blockLocation.getBlock().getFace(BlockFace.NORTH);
                        Block North2 = blockLocation.getBlock().getFace(BlockFace.NORTH);
                        Block South = blockLocation.getBlock().getFace(BlockFace.SOUTH);
                        Block South2 = blockLocation.getBlock().getFace(BlockFace.SOUTH);
                        East.getData();
                        East2.getData();
                        West.getData();
                        West2.getData();
                        North.getData();
                        North2.getData();
                        South.getData();
                        South2.getData();
                        if(East.getState().getType() == Leaves || East2.getState().getType() == Leaves){
                            East.breakNaturally();
                            East2.breakNaturally();
                            player.sendMessage("East");
                        }
                        if(West.getState().getType() == Leaves || West2.getState().getType() == Leaves){
                            West.breakNaturally();
                            West2.breakNaturally();
                            player.sendMessage("West");
                        }
                        if(North.getState().getType() == Leaves || North2.getState().getType() == Leaves){
                            North.breakNaturally();
                            North2.breakNaturally();
                            player.sendMessage("North");
                        }
                        if(South.getState().getType() == Leaves || South2.getState().getType() == Leaves){
                            South.breakNaturally();
                            South2.breakNaturally();
                            player.sendMessage("South");
                        }
                        ItemStack droppedItem = new ItemStack(blockAboveType, 1, blockAboveData);
                        currentWorld.playEffect(blockAbove, Effect.SMOKE, 1);
                        blockAbove.getBlock().setType(Material.AIR);
                        currentWorld.dropItem(blockAbove, droppedItem);
                        }
                        else if(blockAboveType == Material.LEAVES){
                            blockAbove.getBlock().breakNaturally();
                        }
                        logLeft = true;
                    } else {
                        logLeft = false;
                    }
                }
            }
        }
     
    }
    

    Anyone have any ideas :]

    Any help? :]

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

    dead4y

    you want to destroy all wood block in the tree?
    May be use PlayerInteractEvent and check when player broke block of id 17(wood), check is under and above ( or only above) have again log and destroy it.For the leaves i think there is leaf decay event but not sure.If it has leaf decay event use it to make leaves drop whatever you want
     
  3. Offline

    AdvsNoob

    I already have the log part made its just destroying the leaves instantly like the logs are. I am having trouble selecting them to destroy them. The event you are talking about i believe is only cancel-able and i cant force it to happen Immediately. (So i believe)
     
  4. Offline

    dead4y

    or may be your server is crashing because you brake it and the event is again checking...
    For what you want to use this code?
     
  5. Offline

    AdvsNoob

    My servers not crashing.. Haha, i just want to make it so when you cut down a tree it drops all the logs (Like the timber mod) but instead of having random floating leaves they will be destroyed.
     
  6. Offline

    dead4y

    hmm well why u not using playerbreak event?
     
  7. Offline

    Njol

    Because 1) theres no PlayerBreakEvent and 2) he's already using the correct event
    I suggest recursing over all wood blocks of the tree, and destroying all leaf blocks within a radius of 2 around the block.
     
  8. I recommend destroying the blocks at an 5 radius of the log, because leaf beclay cleans up the forther away leaves while the log is there
     
  9. Offline

    AdvsNoob

    That's what i am trying to do XD How would i accomplish this?
     
  10. use an recursif function to add the blocks to an set/list that are in radius
     
  11. Offline

    AdvsNoob

    Hm i kinda get what your saying but can you provide an example for me please?
     
Thread Status:
Not open for further replies.

Share This Page