只读
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 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.
要获取的维度的标识符。
The name of the dimension. For example, "overworld", "nether" or "the_end".
与 dimensionId
关联的维度对象。
The requested dimension
动态属性的标识符。
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可选,指定播放音乐使用的附加参数。
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.
将要广播的一段消息。
这段消息可能是一段字符串,或者符合 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
.
世界修改
Location of the spawn point. Note that this is assumed to be within the overworld dimension.
动态属性的标识符。
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);
}
表示一个世界。包含了世界的各种状态,即一系列维度以及 Minecraft 的环境。
A class that wraps the state of a world - a set of dimensions and the environment of Minecraft.