Writed casually BlockUtils.js



  • var BlockPos = Java.type("net.minecraft.util.BlockPos");
    var Block = Java.type("net.minecraft.block.Block");
    
    /**
     * @returns {Block?} block
     * @param {BlockPos} blockPos 
     */
    function getBlock(blockPos) {
        return mc.theWorld.getBlockState(blockPos).getBlock();
    }
    
    /**
     * @returns {Map<BlockPos, Block>} blocks
     * @param {Number} radius 
     */
    function searchBlocks(radius) {
        var blocks = new Map(); radius |= 0;
        for (var x = -radius; x <= radius; x++) {
            for (var z = -radius; z <= radius; z++) {
                for (var y = radius; z >= -radius; y--) {
                    var blockPos = new BlockPos(~~mc.thePlayer.posX + x, ~~mc.thePlayer.posY + y, ~~mc.thePlayer.posZ + z);
                    var block = getBlock(blockPos);
                    block && blocks.set(blockPos, block);
                }
            }
        }
        return blocks;
    }
    
    function searchBlocks2(radius) {
        return new Map(BlockPos.getAllInBox(new BlockPos(mc.thePlayer.posX - radius, mc.thePlayer.posY - radius, mc.thePlayer.posZ - radius), 
            new BlockPos(mc.thePlayer.posX + radius, mc.thePlayer.posY + radius, mc.thePlayer.posZ + radius))
            .map(blockPos => [blockPos, getBlock(blockPos)]));
    }
    
    

  • Moderator

    Pretty Code. We need more people like you.



  • @Temm thanks. but I don't think this script can run on LB. It's just a demo
    (may replace ?. with . then function 1&2 can work



  • Some explanation of code

    Following code in JavaScript equals number > 0 ? Math.floor(number) : Math.ceil(number)

    number |= 0
    ~~number
    

    But according to my test, ~~x is much faster than Math.floor(x) for random positive double value. So you can use bit operation instead of Math.floor(x) and parseInt(x)

    if (block != null) blocks.set(blockPos, block); can be simplified to if (block) blocks.set(blockPos, block); or just block && blocks.set(blockPos, block);
    Because null in JavaScript is considered as false value, and && operator will not do things after it if things before it is a false value.


Log in to reply