Skip to content

Examples

Dimitri edited this page Aug 28, 2026 · 4 revisions

Complete, worked PotScript programs. See the Standard Library page for the builtins used here.

Ore-sorting timer with a heartbeat over wifi

sethost("timer")

fn hms(ticks) {
    let secs = floor(ticks / 20)
    return str(floor(secs / 60)) + "m" + str(secs % 60) + "s"
}

let pulses = num(load("pulses") or "0")
while true {
    rs_set("up", 15)
    sleep(4)
    rs_set("up", 0)
    pulses = pulses + 1
    store("pulses", pulses)
    broadcast(["pulse", pulses, hms(uptime())])
    sleep(196)      # 10s total cycle
}

Monitor that collects heartbeats

sethost("monitor")
let counts = []
let names = []

while true {
    let msg = recv(600)             # 30s timeout
    if msg == nil {
        print("[quiet] " + str(len(names)) + " known hosts")
        continue
    }
    let from = msg[0]
    let at = find(names, from)
    if at < 0 {
        push(names, from)
        push(counts, 1)
        print("new host: " + from)
    } else {
        counts[at] = counts[at] + 1
    }
    print(from + " -> " + str(msg[1]))
}

Interactive console app

print("pot shell. commands: light, pos, who, set <key> <value>, get <key>, quit")
while true {
    print("? ")
    let line = trim(read())
    let parts = split(line, " ")
    let cmd = lower(parts[0])

    if cmd == "quit" {
        print("bye")
        break
    } else if cmd == "light" {
        print("light: " + str(light()) + "  weather: " + weather())
    } else if cmd == "pos" {
        print(join(pos(), ", "))
    } else if cmd == "who" {
        let here = players(32)
        if len(here) == 0 { print("nobody nearby") } else { print(join(here, ", ")) }
    } else if cmd == "set" and len(parts) > 2 {
        remove(parts, 0)                 # drop "set"
        let key = remove(parts, 0)       # take the key, the rest is the value
        store(key, join(parts, " "))
        print("stored " + key)
    } else if cmd == "get" and len(parts) > 1 {
        print(load(parts[1]) or "(unset)")
    } else {
        print("unknown command")
    }
}

Redstone-triggered alarm

sethost("alarm")
let armed = true
while true {
    if armed and rs_get("down") > 0 {
        say("intruder detected!", 32)
        let i = 0
        while i < 6 {
            beep(24)
            rs_set("up", 15)
            sleep(5)
            rs_set("up", 0)
            beep(0)
            sleep(5)
            i = i + 1
        }
        broadcast(["alarm", pos()])
        sleep(100)
    }
    sleep(2)
}

Mob alarm

Watches for hostile mobs, shows the nearest one on a sign, and fires a redstone pulse (e.g. into a lamp or dispenser). Place a sign on the pot's north side and wire the top side to a lamp.

let hostiles = ["minecraft:zombie", "minecraft:skeleton", "minecraft:creeper",
                "minecraft:spider", "minecraft:witch", "minecraft:enderman"]

sign_write("north", ["mob alarm", "all clear"], "lime")

while true {
    let found = nil
    let near = entities(12)
    let i = 0
    while i < len(near) {
        # each entry is [type, name, distance], nearest first
        if find(hostiles, near[i][0]) >= 0 {
            found = near[i]
            break
        }
        i = i + 1
    }

    if found != nil {
        sign_write("north", ["! ALARM !", found[1], str(found[2]) + " blocks"], "red")
        rs_pulse("up", 15, 10)
        beep(20)
    } else {
        sign_write("north", ["mob alarm", "all clear"], "lime")
    }
    sleep(20)
}

Sign clock

Keeps a sign updated with the in-game day and time, plus the real-world time. Place a sign on the pot's north side.

let side = "north"

fn pad(n) {
    if n < 10 { return "0" + str(n) }
    return str(n)
}

while true {
    # in-game clock: 0 ticks = 06:00, 24000 ticks per day
    let t = daytime() % 24000
    let hour = floor(t / 1000 + 6) % 24
    let minute = floor((t % 1000) * 60 / 1000)

    # real-world clock (UTC), from unix seconds
    let now = realtime()
    let rh = floor(now / 3600) % 24
    let rm = floor(now / 60) % 60

    sign_write(side, [
        "day " + str(day()),
        pad(hour) + ":" + pad(minute),
        "real (UTC)",
        pad(rh) + ":" + pad(rm)
    ], "cyan")
    sleep(20)
}

See also: Errors & Gotchas · Wiki Home

Clone this wiki locally