Paint

Discussion in 'CraftScripts' started by Scot, May 25, 2014.

  1. Scot

    Scot New Member

    This leaves a lot to be desired but it is good for quickly painting any block type on exposed surfaces. Cool for a quick diamond mine or a gravel road.

    This script currently only builds in one quadrant of x/z so it offsets the painting in that direction. It also doesn't do Q-Bert style slopes so well (I'm old). I would welcome any improvements that add a center-based paint or even a set of brushes.

    Code (Text):

    /*
    * Free
    */
    importPackage(Packages.com.sk89q.worldedit);
    importPackage(Packages.com.sk89q.worldedit.blocks);
    importPackage(Packages.com.sk89q.worldedit.tools);
    importPackage(Packages.com.sk89q.worldedit.tools.brushes);
    importPackage(Packages.com.sk89q.worldedit.patterns);
     
    var xOff = ['1', '-1', '1', '1', '-1', '1', '-1', '-1'];
    var yOff = ['1', '1', '-1', '1', '-1', '-1', '1', '-1'];
    var zOff = ['1', '1', '1', '-1', '1', '-1', '-1', '-1'];
     
    var bBlock = context.getBlock(argv[1]);
    var size = parseInt(argv[2]);
    var air = BlockID.AIR;
    var tool = context.getSession().getBrushTool(player.getItemInHand());
    var player = context.getPlayer();
    var session = context.remember();
     
    var matPat = new SingleBlockPattern(bBlock);
     
    tool.setSize(size);
    tool.setFill(matPat);
     
    var brush = new Brush({
        build : function(session,pos,bBlock,size) {
     
            var radius = size + 0.5;
            var invRadius = 1 / radius;
            var ceilRadius = Math.ceil(radius);
            var nextXn = 0;
           
            forX: for (var x = 0; x <= ceilRadius; ++x) {
                var xn = nextXn;
                nextXn = (x + 1) * invRadius;
                var nextYn = 0;
                forY: for (var y = 0; y <= ceilRadius; ++y) {
                    var yn = nextYn;
                    nextYn = (y + 1) * invRadius;
                    var nextZn = 0;
                    forZ: for (var z = 0; z <= ceilRadius; ++z) {
                        var zn = nextZn;
                        nextZn = (z + 1) * invRadius;
                       
                        var distanceSq = lengthSq(xn, yn, zn);
                        if (distanceSq > 1) {
                            if (z == 0) {
                                if (y == 0) {
                                    break forX;
                                }
                                break forY;
                            }
                            break forZ;
                        }
                       
                        for (var dirLoop = 0; dirLoop <= 7 ; dirLoop++)    {
                            var pt = pos.add(x, y, z);
                            var curBlock = session.getBlock(pt);
                           
                            for (var dirLoop = 0; dirLoop <= 7 ; dirLoop++)    {
                                var setPnt = pos.add(x, y, z);
     
                                if (curBlock.getType() != air) {
                                    session.setBlock(setPnt, bBlock);}
                            }
                        }
                    }
                }
            }
        }
    });
    if (argv.length < 2)  {
        CommandText();
    }
    else {
        tool.setBrush(brush,"worldedit.brush.paint");
        player.print("[Block: " + bBlock + " | Size: " + size + "]");
    }
     
    function CommandText()  {
       
        player.print(" ");
        player.print("paint Brush Script by scotlynhatt");
        player.printError("Usage: /cs paint <block> <size>");
        player.print(" ");
        player.print("block - solid block to paint with.");
        player.print("size - Brush radius size.");
        player.print(" ");
        player.print("Use '/cs paint 0' to create with default settings.");
        return true;
    }
     
    function lengthSq(x, y, z) {
        return (x * x) + (y * y) + (z * z);
    }