Quickstart

The Androidscraft JS API exposes two singleton classes World and Self, which has a range of methods that allows the android to interact with the world.

In order to make code reusable, you can export setting options via Self which you can set in the android’s settings UI.

image.png

Below is an example code for an auto wheat farmer bot, that would return itself to the chargepad when battery is low, harvest grown wheat, replant seeds, and store the harvested wheat and seeds in designated containers.

/* Wheat farming */
area = Self.exportBlockArea("area","Farm Area", null);
pos_container_seeds = Self.exportBlockPos("pos_container_seeds", "Seeds Container", null);
pos_container_wheat = Self.exportBlockPos("pos_container_wheat", "Wheat Container", null);
pos_chargepad = Self.exportBlockPos("pos_chargepad", "Chargepad", null)

grownBlockPositions = [];
emptyBlockPositions = [];

function start() {
    if(area == null || pos_container_seeds == null || pos_container_wheat == null || pos_chargepad == null)
        Self.shutdown(); // shutdown if not all positions are set

    Self.setPickupItem(true); // allow the android to pickup dropped items
}

async function tick() {
    // Battery check
    if(Self.getBatteryPercentage() <= 10) {
        // Go to chargepad
        await Self.navigateToAsync(pos_chargepad, 10000, 1);
        return;
    }

    // Check for wheat blocks in area
    let blockPositions = World.queryBlocksInAreaById(area, "minecraft:wheat");

    // Look for grown wheats
    if(grownBlockPositions.length == 0) {
        for(let pos of blockPositions) {
            if(World.blockCropIsMature(pos)) {
                grownBlockPositions.push(pos);
            }
        }
    }

    // Harvest grown wheats
    else {
        let target = grownBlockPositions.pop();
        await Self.navigateToAsync(target, 10000, 1);
        Self.destroyBlock(target);
        await sleep(20); // delay to pickup items
    }

    // Put in chests
    if(Self.hasItem("minecraft:wheat")) {
        await Self.navigateToAsync(pos_container_wheat, 10000, 1);
        Self.dumpInventoryToContainer(pos_container_wheat, "minecraft:wheat");
    }
    if(Self.hasItem("minecraft:wheat_seeds")) {
        await Self.navigateToAsync(pos_container_wheat, 10000, 1);
        Self.dumpInventoryToContainer(pos_container_seeds, "minecraft:wheat_seeds");
    }

    // Check for lying items on the ground
    var items = Self.getItemsInArea(area, null);
    if(items.length > 0) {
        for(let pos of items) {
            await Self.navigateToAsync(pos, 10000, 1);
        }
    }

    /// Replant empty fields

    // calculate required seeds
    if(emptyBlockPositions.length == 0) {
        emptyBlockPositions = World.queryBlocksInAreaById(area, "minecraft:air");
    } else {
        // take the seeds from the container
        await Self.navigateToAsync(pos_container_seeds, 10000, 1);
        Self.getItemFromContainer(pos_container_seeds, "minecraft:wheat_seeds", emptyBlockPositions.length);

        // plant the seeds
        for(let pos of emptyBlockPositions) {
            if(World.getBlockAt([pos[0], pos[1]-1, pos[2]]) == "minecraft:farmland") {
                await Self.navigateToAsync(pos, 10000, 1);
                Self.plantSeed([pos[0], pos[1]-1, pos[2]], "minecraft:wheat_seeds");
            }
        }

        emptyBlockPositions = [];
    }

}

Table of Contents

Types

BlockPos

Represents a position in the Minecraft world using x, y, and z coordinates.

type BlockPos = [
    number, // x coordinate    number, // y coordinate    number  // z coordinate]

BlockArea

Represents a rectangular area in the Minecraft world defined by two corner positions.

type BlockArea = [
    number, // x1 coordinate    number, // y1 coordinate    number, // z1 coordinate    number, // x2 coordinate    number, // y2 coordinate    number  // z2 coordinate]

Methods

Self.destroyBlock(_targetPos: BlockPos) : boolean

Breaks the block at the given position (server-side), dropping its items as if mined. Fails if the target is more than 5 blocks away, is air, or cannot be broken.