类 World实验性

表示一个世界。包含了世界的各种状态,即一系列维度以及 Minecraft 的环境。

A class that wraps the state of a world - a set of dimensions and the environment of Minecraft.

属性

afterEvents: WorldAfterEvents

Contains a set of events that are applicable to the entirety of the world. Event callbacks are called in a deferred manner. Event callbacks are executed in read-write mode.

beforeEvents: WorldBeforeEvents

Contains a set of events that are applicable to the entirety of the world. Event callbacks are called immediately. Event callbacks are executed in read-only mode.

import { world, DimensionLocation } from "@minecraft/server";

function customCommand(targetLocation: DimensionLocation) {
const chatCallback = world.beforeEvents.chatSend.subscribe((eventData) => {
if (eventData.message.includes("cancel")) {
// Cancel event if the message contains "cancel"
eventData.cancel = true;
} else {
const args = eventData.message.split(" ");

if (args.length > 0) {
switch (args[0].toLowerCase()) {
case "echo":
// Send a modified version of chat message
world.sendMessage(`Echo '${eventData.message.substring(4).trim()}'`);
break;
case "help":
world.sendMessage(`Available commands: echo <message>`);
break;
}
}
}
});
}
gameRules: GameRules

The game rules that apply to the world.

isHardcore: boolean
scoreboard: Scoreboard

全局的、唯一的记分板对象。

Returns the general global scoreboard that applies to the world.

structureManager: StructureManager

Returns the manager for Structure related APIs.

方法

  • 实验性

    参数

    • id: string

      The message identifier.

    • value: string

      The message.

    返回 void

    A method that is internal-only, used for broadcasting specific messages between client and server.

    无法在只读模式下调用此函数,详见 WorldBeforeEvents。

  • 实验性

    返回 void

    Clears the set of dynamic properties declared for this behavior pack within the world.

  • 实验性

    返回 number

    自游戏开始以来流逝的时间,以刻为单位。

    获取自游戏开始以来流逝的时间(计算公式:day*24000+daytime)。 时间的流逝受到游戏规则 dodaylightcycle 的影响。

    Returns the absolute time since the start of the world.

  • 实验性

    返回 AimAssistRegistry

    The aim-assist presets and categories that can be used in the world.

    Required Experiments:

    • Camera Aim Assist
  • 实验性

    返回 Player[]

    返回包含了游戏中所有玩家的对象的数组。

    获取一个包含了游戏中所有玩家的对象的数组。

    Returns an array of all active players within the world.

    This function can throw errors.

  • 实验性

    返回 number

    The current day, determined by the world time divided by the number of ticks per day. New worlds start at day 0.

    Returns the current day.

  • 实验性

    返回 Vector3

    The default Overworld spawn location. By default, the Y coordinate is 32767, indicating a player's spawn height is not fixed and will be determined by surrounding blocks.

    Returns the default Overworld spawn location.

  • 实验性

    参数

    • dimensionId: string

      要获取的维度的标识符。

      The name of the dimension. For example, "overworld", "nether" or "the_end".

    返回 Dimension

    与 dimensionId 关联的维度对象。

    The requested dimension

    由 dimensionId 获取维度对象。

    Returns a dimension object.

    若 dimensionId 不与任何维度关联,抛出 "Dimension '<dimensionId>' is invalid"。

    Throws if the given dimension name is invalid

  • 实验性

    参数

    • identifier: string

      动态属性的标识符。

      The property identifier.

    返回 string | number | boolean | Vector3

    返回动态属性 identifier 的值。属性的值尚未设定时,返回 undefined。

    Returns the value for the property, or undefined if the property has not been set.

    获取由 identifier 指定的世界中已定义的动态属性的值。

    Returns a property value.

    若并未注册以 identifier 为标识符的动态属性,抛出 "Dynamic Property '<identifier>' is not defined" 。

    Throws if the given dynamic property identifier is not defined.

    import { world, DimensionLocation } from "@minecraft/server";

    function incrementDynamicProperty(
    log: (message: string, status?: number) => void,
    targetLocation: DimensionLocation
    ) {
    let number = world.getDynamicProperty("samplelibrary:number");

    log("Current value is: " + number);

    if (number === undefined) {
    number = 0;
    }

    if (typeof number !== "number") {
    log("Number is of an unexpected type.");
    return -1;
    }

    world.setDynamicProperty("samplelibrary:number", number + 1);
    }
    import { world, DimensionLocation } from "@minecraft/server";

    function incrementDynamicPropertyInJsonBlob(
    log: (message: string, status?: number) => void,
    targetLocation: DimensionLocation
    ) {
    let paintStr = world.getDynamicProperty("samplelibrary:longerjson");
    let paint: { color: string; intensity: number } | undefined = undefined;

    log("Current value is: " + paintStr);

    if (paintStr === undefined) {
    paint = {
    color: "purple",
    intensity: 0,
    };
    } else {
    if (typeof paintStr !== "string") {
    log("Paint is of an unexpected type.");
    return -1;
    }

    try {
    paint = JSON.parse(paintStr);
    } catch (e) {
    log("Error parsing serialized struct.");
    return -1;
    }
    }

    if (!paint) {
    log("Error parsing serialized struct.");
    return -1;
    }

    paint.intensity++;
    paintStr = JSON.stringify(paint); // be very careful to ensure your serialized JSON str cannot exceed limits
    world.setDynamicProperty("samplelibrary:longerjson", paintStr);
    }
  • 实验性

    返回 string[]

    A string array of active dynamic property identifiers.

    Gets a set of dynamic property identifiers that have been set in this world.

  • 实验性

    返回 number

    Gets the total byte count of dynamic properties. This could potentially be used for your own analytics to ensure you're not storing gigantic sets of dynamic properties.

  • 实验性

    参数

    • id: string

      The id of the entity.

    返回 Entity

    The requested entity object.

    Returns an entity based on the provided id.

    Throws if the given entity id is invalid.

  • 实验性

    返回 MoonPhase

    Returns the MoonPhase for the current time.

  • 实验性

    参数

    • 可选options: EntityQueryOptions

      可选的参数,用作于筛选指定条件的玩家。

      注意,不能使用接口中的 type、location、maxDistance、minDistance 或 volume 属性。

      Additional options that can be used to filter the set of players returned.

    返回 Player[]

    A player array.

    列出世界上的玩家,可使用 options 指定的实体查询选项对其进行筛选。

    Returns a set of players based on a set of conditions defined via the EntityQueryOptions set of filter criteria.

    若向 options 传入的对象含有 type 属性,抛出 "command.generic.invalidPlayerType"。

    若向 options 传入的对象含有 location、maxDistance、minDistance 或 volume 属性,抛出 "EntityQueryOptions property '<property>' is incompatible with function world.getPlayers"。

    Throws if the provided EntityQueryOptions are invalid.

  • 实验性

    返回 number

    当前一天中的时间,以刻为单位,为 0 至 24000 之间的整数。

    The time of day, in ticks, between 0 and 24000.

    返回当前一天中的时间。

    Returns the time of day.

  • 实验性

    参数

    • trackId: string

      声音项目的标识符,要求声音项目的类别为音乐(category: music)。

    • 可选musicOptions: MusicOptions

      可选,指定播放音乐使用的附加参数。

    返回 void

    停止正在播放的音乐,并开始向玩家播放指定音乐。播放类别不为音乐的声音项目不会有任何效果。

    Plays a particular music track for all players.

    无法在只读模式下调用此函数,详见 WorldBeforeEvents。

    This function can throw errors.

    import { world, MusicOptions, WorldSoundOptions, PlayerSoundOptions, DimensionLocation } from "@minecraft/server";

    function playMusicAndSound(targetLocation: DimensionLocation) {
    const players = world.getPlayers();

    const musicOptions: MusicOptions = {
    fade: 0.5,
    loop: true,
    volume: 1.0,
    };
    world.playMusic("music.menu", musicOptions);

    const worldSoundOptions: WorldSoundOptions = {
    pitch: 0.5,
    volume: 4.0,
    };
    world.playSound("ambient.weather.thunder", targetLocation, worldSoundOptions);

    const playerSoundOptions: PlayerSoundOptions = {
    pitch: 1.0,
    volume: 1.0,
    };

    players[0].playSound("bucket.fill_water", playerSoundOptions);
    }
  • 实验性

    参数

    • trackId: string

      声音项目的标识符,要求声音项目的类别为音乐(category: music)。

      Identifier of the music track to play.

    • 可选musicOptions: MusicOptions

      可选,指定播放音乐使用的附加参数。

      Additional options for the music track.

    返回 void

    将音乐添加到播放列表。如果没有任何正在播放的音乐,将会开始播放音乐。播放列表中的音乐将会按照添加顺序播放(需要更多测试)。

    Queues an additional music track for players. If a track is not playing, a music track will play.

    无法在只读模式下调用此函数,详见 WorldBeforeEvents。

    An error will be thrown if volume is less than 0.0. An error will be thrown if fade is less than 0.0.

  • 实验性

    参数

    • message: string | RawMessage | (string | RawMessage)[]

      将要广播的一段消息。 这段消息可能是一段字符串,或者符合 RawMessage 接口的对象,或是这两种类型的组合。

      The message to be displayed.

    返回 void

    向所有玩家广播一条消息。

    Sends a message to all players.

    该方法在 message 格式不正确时会抛出错误。例如 score 的 name 为空字符串时。

    This method can throw if the provided RawMessage is in an invalid format. For example, if an empty name string is provided to score.

  • 实验性

    参数

    • absoluteTime: number

      The world time, in ticks.

    返回 void

    Sets the world time.

    无法在只读模式下调用此函数,详见 WorldBeforeEvents。

  • 实验性

    参数

    • spawnLocation: Vector3

      Location of the spawn point. Note that this is assumed to be within the overworld dimension.

    返回 void

    Sets a default spawn location for all players.

    无法在只读模式下调用此函数,详见 WorldBeforeEvents。

    Throws if the provided spawn location is out of bounds.

    Error

    LocationOutOfWorldBoundariesError

  • 实验性

    参数

    • values: Record<string, string | number | boolean | Vector3>

      A Record of key value pairs of the dynamic properties to set.

    返回 void

    Sets multiple dynamic properties with specific values.

    This function can throw errors.

  • 实验性

    参数

    • identifier: string

      动态属性的标识符。

      The property identifier.

    • 可选value: string | number | boolean | Vector3

      要设定的值,值的类型必须与动态属性注册的类型相同。

      Data value of the property to set.

    返回 void

    为世界动态属性 identifier 设置一个值。

    Sets a specified property to a value.

    若并未注册以 identifier 为标识符的动态属性,抛出 "Dynamic Property '<identifier>' is not defined"。

    若动态属性的类型不符合值的类型,抛出 "Type mismatch for dynamic property '<identifier>'"。

    若动态属性的类型为字符串,且值在使用 UTF-8 编码后的字节长度大于动态属性所允许的最大长度,抛出 "Maximum string length exceeded (<length>/<maxLength>) for dynamic property '<identifier>'"。

    Throws if the given dynamic property identifier is not defined.

    import { world, DimensionLocation } from "@minecraft/server";

    function incrementDynamicProperty(
    log: (message: string, status?: number) => void,
    targetLocation: DimensionLocation
    ) {
    let number = world.getDynamicProperty("samplelibrary:number");

    log("Current value is: " + number);

    if (number === undefined) {
    number = 0;
    }

    if (typeof number !== "number") {
    log("Number is of an unexpected type.");
    return -1;
    }

    world.setDynamicProperty("samplelibrary:number", number + 1);
    }
    import { world, DimensionLocation } from "@minecraft/server";

    function incrementDynamicPropertyInJsonBlob(
    log: (message: string, status?: number) => void,
    targetLocation: DimensionLocation
    ) {
    let paintStr = world.getDynamicProperty("samplelibrary:longerjson");
    let paint: { color: string; intensity: number } | undefined = undefined;

    log("Current value is: " + paintStr);

    if (paintStr === undefined) {
    paint = {
    color: "purple",
    intensity: 0,
    };
    } else {
    if (typeof paintStr !== "string") {
    log("Paint is of an unexpected type.");
    return -1;
    }

    try {
    paint = JSON.parse(paintStr);
    } catch (e) {
    log("Error parsing serialized struct.");
    return -1;
    }
    }

    if (!paint) {
    log("Error parsing serialized struct.");
    return -1;
    }

    paint.intensity++;
    paintStr = JSON.stringify(paint); // be very careful to ensure your serialized JSON str cannot exceed limits
    world.setDynamicProperty("samplelibrary:longerjson", paintStr);
    }
  • 实验性

    参数

    • timeOfDay: number

      The time of day, in ticks, between 0 and 24000.

    返回 void

    Sets the time of day.

    无法在只读模式下调用此函数,详见 WorldBeforeEvents。

    Throws if the provided time of day is not within the valid range.

  • 实验性

    返回 void

    停止客户端中正在播放的所有音乐曲目(需要更多测试)。

    Stops any music tracks from playing.

    无法在只读模式下调用此函数,详见 WorldBeforeEvents。