Skip to main content

Server, Tasks, and Utilities

Commands and server context​

Call under mc.serverResult
run(command)Executes a Minecraft command in the script's command context. When a result is captured, returns [result, messages, error]. Returns null if there is no captured result.
current_dimension()ID of the dimension used by the current script context.
in_dimension(dimension, callback)Evaluates the callback in the specified dimension and returns its result.
tick_time()The server tick counter.
day_time([value])Reads the day clock. With a value, sets it and returns the previous time.
schedule(delay, callback, ...args)Runs a callback after the given number of server ticks.
system_info([key])Lists available information keys or fetches one value.
get_mob_counts([category])Returns mob counts and caps for the current dimension, or null if spawn data is unavailable.
print(message), print(targets, message)Sends a message to the current command source or the chosen player or player array.

Additional functions include statistic, logger, format, display_title, sound, particle, particle_line, particle_box, draw_shape, create_marker, remove_all_markers, item_display_name, and save.

NBT and app data​

Call under mc.serverUse
nbt(value), encode_nbt(value[, force])Obtains an NBT handle. Use encode_nbt to convert a JavaScript object to a compound.
parse_nbt(nbtOrSnbt)Converts NBT to script values, including plain objects and arrays.
tag_matches(source, pattern[, matchLists])Tests an NBT pattern against a source.
load_app_data(), store_app_data(nbt)Reads or writes the app's saved NBT.
nbt_storage([key][, nbt])Lists command storage keys, reads a key, or sets its compound tag.
list_files, read_file, write_file, delete_fileOperate on the script's file storage.

For example, save these functions in a global app to record how often you call countRun across reloads.

function storedRuns() {
const data = mc.server.load_app_data();
return data === null ? 0 : mc.server.parse_nbt(data).runs;
}

function countRun() {
const runs = storedRuns() + 1;
mc.server.store_app_data(mc.server.encode_nbt({runs}));
return runs;
}

Schedule or run a task​

Use mc.server.schedule when you want to run code later on the server. Background tasks use a separate API. Use mc.tasks.task_dock for work that must return to the server thread.

NamespaceCallUse
mc.utiltask(callback, ...args)Starts a background task and returns a task handle.
mc.utiltask_thread(queue, callback, ...args)Submits a task to the selected queue.
mc.utiltask_completed(task), task_value(task)Checks completion or reads the available task value.
mc.taskstask_dock(callback)Runs the function on the server thread and returns its result.
mc.taskstask_join(task)Waits for a task from a background thread. Calls from the server thread are rejected.

Other utilities include mc.util.type(value), mc.util.system_variable_get(key[, initialValue]), and mc.util.system_variable_set(key, value). System variables live in memory and can be shared by apps. For diagnostic output, use console.log(...), console.info(...), console.warn(...), or console.error(...).