Solved How to break a whole tree in 1 hit.

Discussion in 'Plugin Development' started by hubeb, May 13, 2013.

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

    hubeb

    Hello i would like to know how to break all trees in one block break including jungle and large oak trees.
    I have code that would break trees but only in a straight line up. (this code works)
    Code:
    Location bLoc = event.getBlock().getLocation();
            double y = bLoc.getBlockY();
            double x = bLoc.getBlockX();
            double z = bLoc.getBlockZ();
            World currentWorld = event.getPlayer().getWorld();
            boolean logsLeft = true;
            while(logsLeft == true){
                y++;
                Location blockAbove = new Location(currentWorld,x,y,z);
                Material blockAboveType = blockAbove.getBlock().getType();
                blockAbove.getBlock().getData();
               
                if(blockAboveType == Material.LOG){
                    currentWorld.playEffect(blockAbove, Effect.SMOKE,1);
                    blockAbove.getBlock().setType(Material.AIR);
                    logsLeft = true;
    }else{
    logsLeft = false;
    }
    Thanks any help would be greatly appreciated :D

    * Using tree assist or any other plugin wont really work, because it interferes with my plugin in what im trying to do with it

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

    ZeusAllMighty11

    It's really hard to predict a tree algorithm, as it can now (in the new updates) be even more random - it can grow in diagonals, it can even be up to... 5 blocks into the leaves of the tree, not even connected to the log
     
  3. Offline

    hubeb

    TheGreenGamerHD
    Is it then possible to block the growth of large oak trees?
     
  4. Offline

    sirrus86

    I made an ability in my plugin S86 Powers that does this.

    Code:
    private List<Block> bList = new ArrayList<Block>();
     
    @EventHandler
    public void chop(BlockBreakEvent event) {
        Block block = event.getBlock();
        if (block.getType() == Material.LOG) {
            bList.add(block);
            breakLogs.runTaskTimer(plugin, 0L, 1L);
        }
    }
     
    private BukkitRunnable breakLogs = new BukkitRunnable() {
        @Override
        public void run() {
            if (!bList.isEmpty()) {
                for (int i = 0; i < bList.size(); i ++) {
                    Block block = bList.get(i);
                    if (block.getType() == Material.LOG) {
                        for (ItemStack item : block.getDrops()) {
                            block.getWorld().dropItemNaturally(block.getLocation(), item);
                        }
                    block.setType(Material.AIR);
                }
                for (BlockFace face : BlockFace.values()) {
                    if (block.getRelative(face).getType() == Material.LOG) {
                        bList.add(block.getRelative(face));
                    }
                }
                bList.remove(block);
            }
            else {
                this.cancel();
            }
        }
    }
    Might take some tweaking, but should work. Also, this will cause some decent lag in jungles.
     
  5. Offline

    Tirelessly

    Winner:
    Code:
    public void breakTree(Block tree){
       if(tree.getType()!=Material.LOG && tree.getType()!= Material.LEAVES) return;
       tree.breakNaturally();
       for(BlockFace face: BlockFace.values())
          breakTree(tree.getRelative(face));
     
    }
    
     
  6. Offline

    hubeb

    how would i go about using that im my code, like how to i get the block of the tree to put as an argument to call the method with?
     
  7. Offline

    ZeusAllMighty11

    event.getBlock()...
     
  8. Offline

    sirrus86

    Ah yeah, my code was to allow delay between breaks for effect, whereas yours would be instant... touche :p
     
  9. Offline

    hubeb

    How would I go about adjusting the radius, at the moment it can destroy an entire forest.
     
  10. Offline

    Garris0n

    If every tree in the forest was touching...
     
  11. Offline

    hubeb

    I know every tree was touching. I want it to only cut down I tree not all of them
     
  12. Offline

    Garris0n

    Could continue to check if the blocks were in a valid radius of the original tree block or something...
     
  13. The code also removes leaves, and dense forests almost always have their leaves touching ;)
    You could remove that.


    And now imagine someone built their house out of logs, all gone in one punch ;)
     
  14. Offline

    caelum19

    You could, save all the tree shapes as they were generated and load those shapes when players load chunks, then when someone breaks a tree, it breaks all the blocks in this shape.

    By shape I mean 3diemensional boolean array

    EDIT: Better still you could use the seed of the world to find where and what shape trees are.
     
  15. Offline

    hubeb

    I'll fiddle with it when I get off work
     
  16. Offline

    Tirelessly

    Store the original block's location and use the .distance/.distanceSquared method
     
  17. Offline

    hubeb

    Thanks I will prob post a working code snip if I get that far
     
  18. Offline

    Caltinor

    your edit would work for spawned trees but if you are harvesting your own orchard then you would need your first part and that might be too much.

    hubeb a simple way would be to select a bubble around the tree (ie 2-4 block radius) break everything log and leaves naturally within that radius at heights that are normal for trees. you may not get all the leaves, but they'll die off.
     
  19. Offline

    hubeb

    Caltinor Tirelessly TheGreenGamerHD sirrus86
    Thank you all for the help, i finally got it to work the way i wanted with the help of Tirelessly's code (with major tweaking) I now have it setup so that if it breaks another tree (ex an entire forest) it will get the bottom block of that tree and plant a sapling there 1 tick later.

    Again Thanks for all the help!
     
  20. Offline

    rfsantos1996

    I know this is an old thread but I want to suggest people to use HashSet or checking the List for duplicates, it made my tree to go down in 1-2 seconds instead of 20 (note that I made it 2 tick delay instead of 1)
     
Thread Status:
Not open for further replies.

Share This Page