For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /server/classes/System.md.

Class: System

Beta

提供系统级事件和函数的类。

A class that provides system-level events and functions.

Properties

afterEvents

readonly afterEvents: SystemAfterEvents

Early Execution Beta

Remarks

返回系统级操作的后置事件集合。

Returns a collection of after-events for system-level operations.


beforeEvents

readonly beforeEvents: SystemBeforeEvents

Early Execution Beta

Remarks

返回系统级操作的前置事件集合。

Returns a collection of before-events for system-level operations.


currentTick

readonly currentTick: number

Early Execution Beta

Remarks

表示服务器当前的世界刻数。

Represents the current world tick of the server.


isEditorWorld

readonly isEditorWorld: boolean

Early Execution Beta

Remarks

如果这是一个当前已加载编辑器的世界,则返回 true,否则返回 false

Returns true if this is a world where the editor is currently loaded, returns false otherwise.


serverSystemInfo

readonly serverSystemInfo: SystemInfo

Early Execution Beta

Remarks

包含服务器的设备信息。

Contains the device information for the server.

Methods

clearJob()

clearJob(jobId): void

Early Execution Beta

Parameters

jobId

number

System.runJob 返回的作业 ID。

The job ID returned from System.runJob.

Returns

void

Remarks

取消通过 System.runJob 排队的作业的执行。

Cancels the execution of a job queued via System.runJob.


clearRun()

clearRun(runId): void

Early Execution Beta

Parameters

runId

number

Returns

void

Remarks

取消先前通过 System.run 安排的函数运行的执行。

Cancels the execution of a function run that was previously scheduled via System.run.


run()

run(callback): number

Early Execution Beta

Parameters

callback

() => void

在下一个游戏刻运行的函数回调。

Function callback to run at the next game tick.

Returns

number

一个不透明标识符,可与 clearRun 函数一起使用以取消此运行的执行。

An opaque identifier that can be used with the clearRun function to cancel the execution of this run.

Remarks

在下一个可用的未来时间运行指定的函数。这通常用于实现延迟行为和游戏循环。在事件处理程序的上下文中运行时,通常会在事件发生的同一刻结束时运行代码。在其他代码(system.run 回调)中运行时,该函数将在下一刻运行。但请注意,根据系统负载,不能保证在同一刻或下一刻运行。

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.

Example

trapTick.ts

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);
}

runInterval()

runInterval(callback, tickInterval?): number

Early Execution Beta

Parameters

callback

() => void

在此间隔发生时将运行的功能代码。

Functional code that will run when this interval occurs.

tickInterval?

number

回调被调用的间隔刻数。

An interval of every N ticks that the callback will be called upon.

Returns

number

一个不透明句柄,可与 clearRun 方法一起使用以停止此函数的间隔运行。

An opaque handle that can be used with the clearRun method to stop the run of this function on an interval.

Remarks

按间隔运行一组代码。

Runs a set of code on an interval.

Example

every30Seconds.ts

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);
}

runJob()

runJob(generator): number

Early Execution Beta

Parameters

generator

Generator<void, void, void>

要运行的生成器实例。

The instance of the generator to run.

Returns

number

一个不透明句柄,可与 System.clearJob 一起使用以停止此生成器的运行。

An opaque handle that can be used with System.clearJob to stop the run of this generator.

Remarks

将生成器排队直到完成。生成器将在每刻获得一个时间片,并将运行直到它让出或完成。

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.

Example

cubeGenerator.ts

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;
      }
    }
  }
}

runTimeout()

runTimeout(callback, tickDelay?): number

Early Execution Beta

Parameters

callback

() => void

此超时发生时将运行的功能代码。

Functional code that will run when this timeout occurs.

tickDelay?

number

调用间隔前的时间(以刻为单位)。

Amount of time, in ticks, before the interval will be called.

Returns

number

一个不透明句柄,可与 clearRun 方法一起使用以停止此函数的间隔运行。

An opaque handle that can be used with the clearRun method to stop the run of this function on an interval.

Remarks

在由 tickDelay 指定的未来时间运行一组代码。

Runs a set of code at a future time specified by tickDelay.


sendScriptEvent()

sendScriptEvent(id, message): void

Beta

Parameters

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.

Returns

void

Remarks

使用指定的消息 ID 和负载在脚本内触发事件。

Causes an event to fire within script with the specified message ID and payload.

Throws

This function can throw errors.

EngineError

InvalidArgumentError

NamespaceNameError


waitTicks()

waitTicks(ticks): Promise<void>

Early Execution Beta

Parameters

ticks

number

要等待的刻数。最小值为 1。

The amount of ticks to wait. Minimum value is 1.

Returns

Promise<void>

在指定刻数过去后解析的 Promise。

A promise that is resolved when the specified amount of ticks have occurred.

Remarks

waitTicks 返回一个在请求的刻数后解析的 Promise。

waitTicks returns a promise that resolves after the requested number of ticks.

Throws

This function can throw errors.

EngineError