Check if a sapling can grow at a certain location

Discussion in 'Plugin Development' started by vildaberper, Mar 6, 2012.

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

    vildaberper

    I want to check if a sapling can grow at a certain location. Is this possible?
     
  2. Don't think this is possible. I've dug into the source and found out an interact event with bonemeal with a sapling will call .grow. This method checks and creates the tree...

    You can check the source code to see how Bukkit does it and implement something like that.
     
  3. Offline

    luke100

    well you could make a BlockPlace event i which you check if the block is a sapling and if it is sourrounded by air in a specific radius...but this will only work if a player sets a sapling
     
  4. Offline

    vildaberper

    Wow, how did I not think of that? xD
    Thanks!

    Thats the problem, saplings can grow trees at some places when threres not only air around them.
     
  5. vildaberper
    If you're going to add your own check, look up the wiki on saplings to see the minimum requirements of space and light for a sapling to grow.
     
  6. Offline

    Father Of Time

    You guys do know that bukkit now has two grow events right?

    org.bukkit.event.block.BlockGrowEvent
    org.bukkit.event.world.StructureGrowEvent

    Rather than determining if the tree can fit on placement I would attempt it on growth; I say this because on my server we have a system that auto plants fallen saplings, what this results in is 3 or 4 saplings getting planted under a specific tree. What we wanted was for the first tree to attempt to grow grows naturally, but any further trees will be hindered by the main tree and stay saplings.

    I like this behavior for several reasons:
    1) because in a natural Forest this is normal behavior, and smaller shrubs are kept from growing because taller tree canapes block sunlight.
    2) as soon as a player cuts the tree down one of the other planted saplings on the ground will sprout up, making the tree downtime minimal.

    The StructureGrowEvent returns a collection of blocks that are part of the growth, simply cycle through that list of blocks and if any aren't air cancel the event, but if every block it's attempting to grow into is air then allow it.

    I hope my rambling has made some sense, good luck with your project!
     
  7. Offline

    vildaberper

    Im not sure what you mean, but this is my current approach:

    None of those events are ever fired. Perhaps they have yet to implemented in 1.2.3.

    This is the source of my plugin: PasteBin

    If the event would fire correctly, this would work perfectly.
    The only debug message I get it "canPlace ...".
     
  8. Offline

    bleachisback

    He wants to check if a sapling CAN grow, not if it DOES grow. Like the space requirements and such.
     
  9. Offline

    vildaberper

    One way would be checking if a tree can grow by generating the tree at the sapling, and cancel the event if it worked (generateTree returns false if it can't grow).
    But as I said, the event isn't firing at all.

    Edit: I could do the check "the hard way", but I prefer using minecraft code, not copying it.

    Allright, ran out of pacience.

    Heres how I check if a tree can grow:

    Code:
    public boolean canGrow(Block block){
        if(!canGrowOn(block.getRelative(BlockFace.DOWN).getType()) || !canGrowIn(block.getType()))
            return false;
        for(int x = block.getX() - 4; x <= block.getX() + 4; x++)
            for(int y = block.getY() - 1; y <= block.getY() + 1; y++)
                for(int z = block.getZ() - 4; z <= block.getZ() + 4; z++)
                    if(block.getWorld().getBlockAt(x, y, z).getType().equals(Material.SAPLING))
                        return false;
        for(int x = block.getX() - 2; x <= block.getX() + 2; x++)
            for(int y = block.getY() + 1; y <= block.getY() + 5; y++)
                for(int z = block.getZ() -2; z <= block.getZ() + 2; z++)
                    if(!canGrowIn(block.getWorld().getBlockAt(x, y, z).getType()))
                        return false;
        return true;
    }
     
    public boolean canGrowIn(Material material){
        return material.equals(Material.LEAVES) || material.equals(Material.AIR) || material.equals(Material.SNOW) || material.equals(Material.LONG_GRASS) || material.equals(Material.VINE);
    }
     
    public boolean canGrowOn(Material material){
        return material.equals(Material.GRASS) || material.equals(Material.DIRT);
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  10. Offline

    Father Of Time

    I have literally no idea what your point is...? I get this, which is why it would make more sense checking if it can from the grow event side and not the placing the sapling side.

    If you check from the sapling side you have to "assume" the size of the growth (trees are different sizes and they are randomized when they grow), so you would have to make a giant space check to make sure it fits all trees, which would trigger false results for the smaller trees.

    However, with my suggestion this isn't the case. When the event is triggered it provides a list of all block that will be changes, so we know EXACTLY what blocks we need to check, not just a guess... And regardless of which event you chose you can still terminate the growth of the tree...

    I mean this politely, but I think the issue is that you simply don't understand what I am saying...
     
  11. Offline

    bleachisback

    But if you force the sapling to grow through code, it doesn't fire an event I believe. It only fires if it grows naturally...
     
Thread Status:
Not open for further replies.

Share This Page