实验性只读 实验性after只读 实验性beforeContains 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;
}
}
}
});
}
只读 实验性game只读 实验性is只读 实验性scoreboard只读 实验性structureReturns the manager for Structure related APIs.
实验性The message identifier.
The message.
A method that is internal-only, used for broadcasting specific messages between client and server.
无法在只读模式下调用此函数,详见 WorldBeforeEvents。
实验性实验性动态属性的标识符。
The property identifier.
返回动态属性 identifier 的值。属性的值尚未设定时,返回 undefined。
Returns the value for the property, or undefined if the property has not been set.
若并未注册以 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);
}
实验性可选options: EntityQueryOptions可选的参数,用作于筛选指定条件的玩家。
注意,不能使用接口中的 type、location、maxDistance、minDistance 或 volume 属性。
Additional options that can be used to filter the set of players returned.
A player array.
实验性声音项目的标识符,要求声音项目的类别为音乐(category: music)。
可选musicOptions: MusicOptions可选,指定播放音乐使用的附加参数。
停止正在播放的音乐,并开始向玩家播放指定音乐。播放类别不为音乐的声音项目不会有任何效果。
Plays a particular music track for all players.
无法在只读模式下调用此函数,详见 WorldBeforeEvents。
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);
}
实验性声音项目的标识符,要求声音项目的类别为音乐(category: music)。
Identifier of the music track to play.
可选musicOptions: MusicOptions可选,指定播放音乐使用的附加参数。
Additional options for the music track.
将音乐添加到播放列表。如果没有任何正在播放的音乐,将会开始播放音乐。播放列表中的音乐将会按照添加顺序播放(需要更多测试)。
Queues an additional music track for players. If a track is not playing, a music track will play.
无法在只读模式下调用此函数,详见 WorldBeforeEvents。
实验性将要广播的一段消息。
这段消息可能是一段字符串,或者符合 RawMessage 接口的对象,或是这两种类型的组合。
The message to be displayed.
该方法在 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.
实验性动态属性的标识符。
The property identifier.
可选value: string | number | boolean | Vector3要设定的值,值的类型必须与动态属性注册的类型相同。
Data value of the property to set.
若并未注册以 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);
}
实验性停止客户端中正在播放的所有音乐曲目(需要更多测试)。
Stops any music tracks from playing.
无法在只读模式下调用此函数,详见 WorldBeforeEvents。
表示一个世界。包含了世界的各种状态,即一系列维度以及 Minecraft 的环境。
A class that wraps the state of a world - a set of dimensions and the environment of Minecraft.