API

CraftScript reference

Every keyword, operator, function and member available.

Keywords

Keyword Use
let / var / const Declare variables (const can't be reassigned).
fun Named functions and lambdas fun(x) { }.
class, extends, this, super Classes and inheritance.
enum Enumerations.
import, from, as Modules.
if, else, while, for … in, break, continue, return Control flow.
and, or, not (or &&, ||, !) Logic.
true, false, null Literals.
mod, item, block, recipe Content declarations.
ore, tree, patch, structure, biome, dimension, terrain, biome_map, carver World generation declarations. See World generation and Generation by code.
on … priority Event handler.
command … requires … alias … describe Commands.
every, after + ticks/seconds/minutes Timers.

Comments: // line, # line, /* block */. Statements are separated by line breaks (or ;).

Operators

Operator Meaning
+ - * / % Arithmetic. + concatenates if one side is a string; it also joins lists.
== != < <= > >= Comparison. An id and a string are equal if they represent the same id.
= += -= *= /= Assignment.
?? Default value if the left side is null.
?. Safe access: returns null instead of failing.
a..b Inclusive range of numbers.
x[i] List index (negative counts from the end), map key, string character.

Falsy values: null, false, 0, "", empty list or map.

Built-in functions

Function Description
print(...) Writes to the server log.
log(...) Same as print, with the mod name.
str(x) Converts to a string.
num(x) Converts to a number (or null).
int(x) Integer part.
round, floor, ceil, abs, sqrt Math.
min(...), max(...) Minimum/maximum of numbers.
sin, cos, atan2, pi() Trigonometry, in radians.
len(x) Length of a string, list or map.
type(x) Name of the type.
is(x, Class) Checks a class or enum.
random(), random(n), random(a, b) Decimal 0–1, integer 0–n, integer a–b.
chance(p) true with probability p (0–1).
pick(list) Random element.
now() Current milliseconds.
id("ns:path") Creates an identifier.
item(id, count?, name?) Creates an ItemStack.
color(text) Converts & into color codes.
broadcast(msg) Message to all players.
run(command) Runs a game command as the console. Escape hatch: the command format belongs to the game and can change.
player(name) Online player or null.
players() List of players.
world(id?) World (the overworld by default).
mod_loaded(id) Is another mod loaded?
config(name, values?, synced?) Declares or reads a mod settings file.
listen(channel, fun(data, player)) Receives data on a mod channel.
send(player, channel, data) Sends data from the server to a player.
send_to_server(channel, data) Sends data from the client to the server.
server The server.
tick Current tick.
game_version, namespace Minecraft version and the mod namespace.

String members

length, upper(), lower(), trim(), contains(s), starts_with(s), ends_with(s), split(sep), replace(a, b), repeat(n), substring(from, to?), is_empty.

List members

size, is_empty, first, last, add(x)/push(x), remove(x), remove_at(i), contains(x), index_of(x), join(sep), reverse(), sort(), clear(), map(f), filter(f), each(f), any(f), all(f), sum().

Map members

keys(), values(), has(k), get(k, default?), remove(k), size; plus direct access with map.key or map["key"].

Game objects

Names use snake_case (max_health) and are the same members as in Kotlin (maxHealth). Enums are used as strings (player.game_mode = "creative"). Details for each one in The world and players.

Type Properties Methods
Player name, uuid, health, max_health, food_level, game_mode, inventory, held_item, is_op, is_sleeping, has_crafting_client, location, world, custom_name, is_alive, data send, action_bar, title, give, set_held_item, play_sound, teleport, heal, damage, has_permission, kick, set_rotation, add_tag, has_tag, set_cooldown, has_cooldown
World id, players, time, is_raining, data block_at, set_block (with optional states), spawn, drop_item, explode, strike_lightning, spawn_particle, play_sound, light_at, container_at, set_sign_text, set_chunk_loaded, entities, living_near, shoot, summon
Block type, x, y, z, world, location, is_solid, data set_type, relative
Entity uuid, type, world, location, custom_name, is_alive, data, direction, vehicle teleport, remove, set_rotation, add_tag, has_tag
LivingEntity everything in Entity, health, max_health damage, heal, set_ai, add_effect, remove_effect, has_effect, effects, add_modifier, remove_modifier, attribute, set_attribute, equipment, set_equipment
Inventory size get, set, add, count, clear
Location world, x, y, z, yaw, pitch, block_x, block_y, block_z add, distance_to
data (store) keys; data["key"] reads and writes get, set, get_int, get_number, get_string, get_boolean, remove, clear
ItemStack item, count, name, lore, data, enchantments with_count
Server players, worlds, motd, tps, is_dedicated player, world, broadcast, stop, run_command

World generation objects

What the functions of a world generation declaration receive while chunks are made. Explained on the Generation by code page.

Object Properties Methods
world (structure build) x, y, z, seed, min_y, max_y block, state, is_air, is_in, can_stay, set (with optional states), inside, surface, top, flow, structures, place_file
plan (structure plan) chunk_x, chunk_z, seed, min_y height, biome, column, file_size
site (structure build) x, y, z, radius, min_y, max_y, data, seed
world (terrain) seed, min_y, height biome
world (biome_map) seed tagged
ctx (carver) seed, start_x, start_z, chunk_x, chunk_z, min_y, max_y carve
at (biome surface) x, y, z, depth_above, depth_below, surface_depth, steep noise

Command argument types

player, int, number, bool, string (one word), text (the rest of the line), id. Add ? to make it optional.

Limits

  • 2,000,000 steps per invocation (event, command or timer).
  • 200 nested calls (recursion).
  • Ranges of up to 1,000,000 elements.