jungle bush script (actual Minecraft algorithm)

Discussion in 'CraftScripts' started by blockhead, Jul 10, 2012.

  1. blockhead

    blockhead New Member

    Although there is a tree tool, only oak, pine and birch trees are implemented (you can select other tree types but it will just plant oak trees), and since there is no way to grow jungle bushes, I created this script.

    Running this script will bind a new tool to the item you're holding that will create a jungle bush on top of whatever block you click. Note: you can only plant on grass and dirt. If you click any other type of block, nothing will happen, except for leaf and tall grass blocks. If you click a leaf or tall grass block, it will look for the first block underneath it that is not a leaf, tall grass or air block. If that block is a grass or dirt block it will plant it there, otherwise nothing happens.

    This is the exact same algorithm that Minecraft uses, except for two subtle differences. I took the jungle bush function from the decompiled Minecraft source and translated it to Javascript. The two differences are: being able to click on tall grass to plant on the block underneath, and there is a smaller set of blocks that can be overwritten by leaf blocks. When generating the bush's leaves, Minecraft will overwrite any non-opaque block. I decided to only have it overwrite air, saplings, water, leaves, webs, tall grass, dead bushes, flowers, mushrooms (the small ones), fire, snow, vines and lily pads.

    A neat little bonus is you can undo bushes created with the script, unlike trees created with the tree tool.

    Code (Text):

    var overwritable = {};
    overwritable[BlockID.AIR] = true;
    overwritable[BlockID.SAPLING] = true;
    overwritable[BlockID.WATER] = true;
    overwritable[BlockID.STATIONARY_WATER] = true;
    overwritable[BlockID.LEAVES] = true;
    overwritable[BlockID.WEB] = true;
    overwritable[BlockID.LONG_GRASS] = true;
    overwritable[BlockID.DEAD_BUSH] = true;
    overwritable[BlockID.YELLOW_FLOWER] = true;
    overwritable[BlockID.RED_FLOWER] = true;
    overwritable[BlockID.BROWN_MUSHROOM] = true;
    overwritable[BlockID.RED_MUSHROOM] = true;
    overwritable[BlockID.FIRE] = true;
    overwritable[BlockID.SNOW] = true;
    overwritable[BlockID.VINE] = true;
    overwritable[BlockID.LILY_PAD] = true;
    context.getSession().setTool(player.getItemInHand(),new BlockTool({
        canUse : function(player) {
            return player.hasPermission("worldedit.tool.tree");
        actPrimary : function(server,config,player,session,pos) {
            var es = session.createEditSession(player);
            try {
                var x = pos.getBlockX();
                var y = pos.getBlockY();
                var z = pos.getBlockZ();
                var j = es.getBlockType(new Vector(x,y,z));
                while((j == BlockID.AIR || j == BlockID.LEAVES || j == BlockID.LONG_GRASS) && y > 0)
                    j = es.getBlockType(new Vector(x,--y,z));
                if(j == BlockID.DIRT || j == BlockID.GRASS) {
                    var leaves = new BaseBlock(BlockID.LEAVES);
                    es.setBlock(new Vector(x,y,z),new BaseBlock(BlockID.LOG,3));
                    for(var k = y; k <= y + 2; k++) {
                        var l = k - y;
                        var i1 = 2 - l;
                        for(var j1 = x - i1; j1 <= x + i1; ++j1) {
                            var k1 = j1 - x;
                            for(var l1 = z - i1; l1 <= z + i1; ++l1) {
                                var i2 = l1 - z;
                                if(Math.abs(k1) != i1 || Math.abs(i2) != i1 || Math.random() >= 0.5) {
                                    var dest = new Vector(j1,k,l1);
                                    if(es.getBlockType(dest) in overwritable) es.setBlock(dest,leaves);
            } catch(e) {
                if(e instanceof MaxChangedBlocksException)
                    player.printError("Max. blocks changed reached.");
                else throw e;
            } finally {
            return true;
  2. BlueII

    BlueII New Member

    I've found a little mistake in your script:
    It makes just the normal log into the leafes not the junngle log...
  3. blockhead

    blockhead New Member

    Silly me. I always thought it was an oak log because of the oak leaves but I just checked it and it is indeed a jungle log.

    It's fixed now.
  4. BlueII

    BlueII New Member

    That's good to hear :)

    btw. in my opinion this is a very useful script!
  5. inHaze

    inHaze New Member

    Another amazing script blockhead, really like the fact they are randomly shaped. I had actually attempted pulling out some of the ore generation code from the decompiled source and converting it to a script. My attempt didn't go as well though, think I screwed something up while changing it over.