System Class
A class that provides system-level events and functions.
read-only afterEvents: SystemAfterEvents;
Returns a collection of after-events for system-level operations.
Type: SystemAfterEvents
read-only beforeEvents: SystemBeforeEvents;
Returns a collection of before-events for system-level operations.
Type: SystemBeforeEvents
Caution
This property is still in pre-release. Its signature may change or it may be removed in future releases.
read-only currentTick: number;
Represents the current world tick of the server.
Type: number
read-only serverSystemInfo: SystemInfo;
Contains the device information for the server.
Type: SystemInfo
clearJob(jobId: number): void
Cancels the execution of a job queued via @minecraft/server.System.runJob.
jobId: number
The job ID returned from @minecraft/server.System.runJob.
Notes:
clearRun(runId: number): void
Cancels the execution of a function run that was previously scheduled via @minecraft/server.System.run.
- runId: number
Notes:
run(callback: () => void): number
Runs a specified function at the next available future time. This is frequently used to implement delayed behaviors and game loops. When run within the context of an event handler, this will generally run the code at the end of the same tick where the event occurred. When run in other code (a system.run callout), this will run the function in the next tick. Note, however, that depending on load on the system, running in the same or next tick is not guaranteed.
callback: () => void
Function callback to run at the next game tick.
Returns number - An opaque identifier that can be used with the clearRun
function to cancel the execution of this run.
Notes:
import { world, system } from "@minecraft/server";
function trapTick() {
try {
// Minecraft runs at 20 ticks per second.
if (system.currentTick % 1200 === 0) {
world.sendMessage("Another minute passes...");
}
} catch (e) {
console.warn("Error: " + e);
}
system.run(trapTick);
}
(preview) Work with this sample on the MCTools.dev code sandbox.
runInterval(callback: () => void, tickInterval?: number): number
Runs a set of code on an interval.
callback: () => void
Functional code that will run when this interval occurs.
tickInterval?: number =
null
An interval of every N ticks that the callback will be called upon.
Returns number - An opaque handle that can be used with the clearRun method to stop the run of this function on an interval.
Notes:
import { world, system, DimensionLocation } from "@minecraft/server";
function every30Seconds(targetLocation: DimensionLocation) {
const intervalRunIdentifier = Math.floor(Math.random() * 10000);
system.runInterval(() => {
world.sendMessage("This is an interval run " + intervalRunIdentifier + " sending a message every 30 seconds.");
}, 600);
}
(preview) Work with this sample on the MCTools.dev code sandbox.
runJob(generator: Generator<void, void, void>): number
Queues a generator to run until completion. The generator will be given a time slice each tick, and will be run until it yields or completes.
generator: Generator<void, void, void>
The instance of the generator to run.
Returns number - An opaque handle that can be used with @minecraft/server.System.clearJob to stop the run of this generator.
Notes:
import { system, BlockPermutation, DimensionLocation } from "@minecraft/server";
function cubeGenerator(targetLocation: DimensionLocation) {
const blockPerm = BlockPermutation.resolve("minecraft:cobblestone");
system.runJob(blockPlacingGenerator(blockPerm, targetLocation, 15));
}
function* blockPlacingGenerator(blockPerm: BlockPermutation, startingLocation: DimensionLocation, size: number) {
for (let x = startingLocation.x; x < startingLocation.x + size; x++) {
for (let y = startingLocation.y; y < startingLocation.y + size; y++) {
for (let z = startingLocation.z; z < startingLocation.z + size; z++) {
const block = startingLocation.dimension.getBlock({ x: x, y: y, z: z });
if (block) {
block.setPermutation(blockPerm);
}
yield;
}
}
}
}
(preview) Work with this sample on the MCTools.dev code sandbox.
runTimeout(callback: () => void, tickDelay?: number): number
Runs a set of code at a future time specified by tickDelay.
callback: () => void
Functional code that will run when this timeout occurs.
tickDelay?: number =
null
Amount of time, in ticks, before the interval will be called.
Returns number - An opaque handle that can be used with the clearRun method to stop the run of this function on an interval.
Notes:
scriptEvent(id: string, message: string): void
Causes an event to fire within script with the specified message ID and payload.
id: string
Identifier of the message to send. This is custom and dependent on the kinds of behavior packs and content you may have installed within the world.
message: string
Data component of the message to send. This is custom and dependent on the kinds of behavior packs and content you may have installed within the world. Message may not exceed 2048 characters in length.
Caution
This function is still in pre-release. Its signature may change or it may be removed in future releases.
Notes:
- This function can't be called in read-only mode.
- This function can throw errors.
waitTicks(ticks: number): Promise<void>
waitTicks returns a promise that resolves after the requested number of ticks.
ticks: number
The amount of ticks to wait. Minimum value is 1.
Returns Promise<void> - A promise that is resolved when the specified amount of ticks have occurred.
Notes:
- This function can throw errors.