Regenerate trees

Discussion in 'Plugin Development' started by MrBakbuki, Jan 19, 2021.

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

    MrBakbuki

    Hi all,
    I want to create a plugin that restores a tree after a player breaks it, and I can't figure out how to do this.
    Any help appreciated! Keep in mind I'm pretty new to Java, so please make it simple.
    Thanks for reading.
     
  2. Offline

    Kars

    There are several ways to do this of varying levels of complexity.

    The first and simplest way is to regenerate any log-block when a player breaks it. You can achieve this using BlockBreakEvent. If the broken block is a type of desired log, you schedule a task 1 tick in the future that replaces the broken block with a log. This will make any placed log-block unable to be removed, however. But it is by far the simplest way.
    PHP:
    BukkitScheduler scheduler getServer().getScheduler();
            
    scheduler.scheduleSyncDelayedTask(this, new Runnable() {
                @
    Override
                
    public void run() {
                    
    // Place the log back
                
    }
            }, 
    20L); // 20 ticks (1 second) in the future
    The other more complex way is to determine wether or not any broken log-block belongs to a tree and place it back either after it is broken, or after the complete tree is broken. Placing it back immediately would be simpler, however. I think the more complex option is out of your league here.
    You would have to keep track of broken log blocks while preventing memory leaks. It will get a little complex.
     
  3. Offline

    MrBakbuki

    @Kars Thanks for responding.
    However, this is not what I'm looking for. Is there a way to make a tree regenerate after the player breaks the full tree?
     
  4. Offline

    Kars

    Yes, but as you requested we "keep it simple" i assume that this would be out of reach for you at this point.

    The best way to do this in my opinion is this:
    • Catch BlockBreakEvent. If the broken block is a log, scan adjacent blocks (recursively) for logs/leaves and save their Locations into an object.
    • Scan only within a range to prevent entire dark oak forests from being saved.
    • Delete the saved tree after a while to prevent memory leaks and overflow.
    • On BlockBreakEvent, if the broken block is a log, loop through all saved trees. If the block belongs to a saved tree, check all Locations of the saved tree and if all logs are removed, restore all the blocks to their original state.
    • In addition to Location you will have to save if that block was a log or a leaf.
    Good luck.
     
Thread Status:
Not open for further replies.

Share This Page