API

Interfaccia

Moduli che il giocatore compila ed elementi a schermo: barre del boss e una barra laterale. Tutto funziona con qualsiasi client, che abbia Crafting installato oppure no.

Moduli

Un modulo ha un titolo, un testo, dei campi e dei pulsanti. Lo dichiari in un file dentro ui/ e lo mostri dove ti serve (un comando, un evento, quando si usa un oggetto):

ui/shop.craftCraftScript
import { give_rubies } from "/lib/rewards"

const SHOP = form("Ruby shop", {
    body: "Rubies cost 10 emeralds each.",
    fields: [
        { slider: "amount", label: "Rubies", min: 1, max: 16, default: 1 },
        { toggle: "gift", label: "Wrap as a gift" },
        { choice: "pouch", label: "Pouch", options: ["leather", "silk"] },
        { text: "note", label: "Note" }
    ],
    buttons: [{ id: "buy", label: "Buy" }, "cancel"]
})

fun open_shop(p: Player) {
    p.show_form(SHOP, fun(r) {
        if r.button != "buy" { return }                  // closed with Esc or cancelled
        let n = int(r.number("amount") ?? 1)
        if p.inventory.count("minecraft:emerald") < n * 10 { p.send("&cNot enough emeralds"); return }
        give_rubies(p, n)
        if r.toggle("gift") { p.send("&dWrapped in {r.choice("pouch")}: {r.text("note")}") }
    })
}
commands/shop.craftCraftScript
import { open_shop } from "/ui/shop"

command shop describe "Buy rubies" {
    if player == null { return "Players only" }
    open_shop(player)
}
Campo Si scrive Si legge con
Testo { text: "note", label: "Nota", default: "" } r.text("note")
Interruttore { toggle: "gift", label: "È un regalo", default: false } r.toggle("gift")
Cursore { slider: "amount", label: "Quantità", min: 1, max: 64, step: 1, default: 8 } r.number("amount")
Scelta { choice: "color", label: "Colore", options: ["red", "blue"], default: "red" } r.choice("color")
  • La chiave di ogni campo usa lettere minuscole, cifre e _. label è ciò che vede il giocatore; se manca, si usa la chiave.
  • buttons è un elenco di { id, label } o solo di id. Un modulo senza pulsanti riceve un pulsante “OK”.
  • Nella risposta, r.button è l’id del pulsante premuto, o null se il giocatore ha chiuso il modulo (Esc). r.values è una mappa con tutti i campi.
  • La risposta arriva nella funzione che passi a show_form; il resto del server continua a funzionare mentre il giocatore decide.

In Kotlin

ui/ShopForm.ktKotlin
package com.example.rubies.ui

object ShopForm {
    private val form = Form("Ruby shop").apply {
        body = "Rubies cost 10 emeralds each."
        slider("amount", "Rubies", 1.0, 16.0, default = 1.0)
        toggle("gift", "Wrap as a gift")
        choice("pouch", "Pouch", listOf("leather", "silk"))
        text("note", "Note")
        button("buy", "Buy")
        button("cancel", "Cancel")
    }

    fun show(player: Player) = player.showForm(form) { r ->
        if (r.button != "buy") return@showForm                 // closed with Esc or cancelled
        val n = (r.number("amount") ?: 1.0).toInt()
        if (player.inventory.count(Identifier.parse("minecraft:emerald")) < n * 10) { player.send("&cNot enough emeralds"); return@showForm }
        player.give(ItemStack.of(Items.RUBY, n))
        if (r.toggle("gift") == true) player.send("&dWrapped in ${r.choice("pouch")}: ${r.text("note")}")
    }
}

Form(title) con body, text(key, label, default, maxLength), toggle(key, label, default), slider(key, label, min, max, step, default), choice(key, label, options, default) e button(id, label). player.showForm(form) { r -> … } riceve una FormResponse con button, values e text, toggle, number, choice.

Barre del boss

Una barra in cima allo schermo, con titolo, colore, avanzamento e stile. Usala per un conto alla rovescia, un evento del server o la salute di qualcosa:

events/countdown.craftCraftScript
const TIMER = server.boss_bar("rubies:countdown", "&cCorsa ai rubini", "red", "segments_10")
let left = 60

on join { TIMER.show(player) }

every 1 second {
    left = max(0, left - 1)
    TIMER.progress = left / 60
    TIMER.title = "&cCorsa ai rubini: {left}s"
    if left == 0 { TIMER.remove() }
}
Membro Cosa fa
server.boss_bar(id, title?, color?, style?) Crea la barra o restituisce quella esistente con quell’id. Un id per barra: ricaricare la mod non la duplica.
bar.progress Da 0 a 1.
bar.title Il testo, con colori &.
bar.color pink, blue, red, green, yellow, purple, white.
bar.style solid, segments_6, segments_10, segments_12, segments_20.
bar.show(player), bar.hide(player) Chi la vede. bar.players li elenca.
bar.remove() La rimuove per tutti e libera l’id.

In Kotlin: Crafting.server?.bossBar("rubies:countdown", "Time", BarColor.RED, BarStyle.SEGMENTS_10), con le stesse proprietà in camelCase.

Barra laterale

Un elenco di massimo 15 righe sul lato destro dello schermo, diverso per ogni giocatore. Richiamala per aggiornarla:

events/welcome.craftCraftScript
import { visits } from "/lib/storage"

on join {
    player.set_sidebar("&6Rubini", [
        "&fVisite: &e{visits(player)}",
        "&fRubini: &c{player.inventory.count("rubies:ruby")}"
    ])
}
  • player.set_sidebar(title, lines) la mostra o la aggiorna; la vede solo quel giocatore. Colori con &.
  • player.clear_sidebar() la rimuove.
  • In Kotlin: player.setSidebar(title, lines) e player.clearSidebar().

Tasti di una mod

Una mod può aggiungere tasti propri. Si dichiarano con key e si reagisce con on key, che viene eseguito sul server per il giocatore che l'ha premuto. e.key è l'id con il namespace della mod (dashmod:dash).

content/keys.craftCraftScript
key "dash" { label: "Dash", default: "R" }
key "heal" { label: "Second wind", default: "G" }

on key(e) {
    let p = e.player
    if e.key == "dashmod:dash" && !p.has_cooldown("minecraft:feather") {
        let d = p.direction
        p.velocity = vector(d.x * 1.6, 0.35, d.z * 1.6)
        p.set_cooldown("minecraft:feather", 2)
    }
    if e.key == "dashmod:heal" { p.heal(6) }
}

I tasti compaiono in Opzioni > Controlli > Assegnazione tasti, in una sezione con il nome della mod. I giocatori possono cambiarli e la loro scelta viene conservata. default è il nome di un tasto come "R", "G", "F6" o "LEFT_ALT".

I giocatori devono avere Crafting installato nel loro gioco per avere i tasti. In Kotlin: ContentBuilder.key("dash", label = "Dash", default = "R") e Crafting.events.on<PlayerKeyEvent> { e -> … }.

Buono a sapersi

  • Moduli, barre e barra laterale sono visibili a qualsiasi client di Minecraft Java 26.3, con o senza Crafting. Una mod per server può usarli senza che nessuno installi nulla.
  • Titoli, barra delle azioni e messaggi in chat sono trattati in Il mondo e i giocatori.
  • Le schermate personalizzate con un layout tuo (menu, inventari di macchinari) non esistono ancora; vedi la roadmap.