应用持续效果
easy
scripting
本教程将展示如何为站在方块上的实体持续施加状态效果。
检测踩踏者
方块JSON
我们需要在代码中添加几个元素。首先创建一个状态值,当有实体站立时为true,否则为false:
minecraft:block > description
json
"states": {
"wiki:stood_on": [false, true]
}minecraft:block > components
json
"minecraft:custom_components": [
"wiki:treader_detection"
]自定义组件脚本
BP/scripts/treader_detection.js
js
import { BlockPermutation, GameMode, Player, world } from "@minecraft/server";
/** @type {import("@minecraft/server").BlockCustomComponent} */
const BlockTreaderDetectionComponent = {
onStepOn({ entity, block }) {
if (entity instanceof Player && entity.getGameMode() === GameMode.creative) return;
block.setPermutation(
BlockPermutation.resolve(block.typeId, {
"wiki:stood_on": true,
})
);
},
onStepOff({ entity, block }) {
if (entity instanceof Player && entity.getGameMode() === GameMode.creative) return;
block.setPermutation(
BlockPermutation.resolve(block.typeId, {
"wiki:stood_on": false,
})
);
},
};
world.beforeEvents.worldInitialize.subscribe(({ blockComponentRegistry }) => {
blockComponentRegistry.registerCustomComponent(
"wiki:treader_detection",
BlockTreaderDetectionComponent
);
});对踩踏者施加效果
方块JSON
我们需要让方块持续执行tick事件来每tick施加效果。通过permutations数组实现仅在被踩踏时应用自定义组件:
minecraft:block
json
"permutations": [
{
"condition": "q.block_state('wiki:stood_on')",
"components": {
"minecraft:custom_components": ["wiki:treader_detection", "wiki:wither_treaders"],
"minecraft:tick": {
"interval_range": [1, 1],
"looping": true
}
}
}
]自定义组件脚本
现在添加给予实体凋零效果的事件:
BP/scripts/wither_treaders.js
js
import { Entity, GameMode, Player, world } from "@minecraft/server";
/** @type {import("@minecraft/server").BlockCustomComponent} */
const BlockWitherTreadersComponent = {
onTick(event) {
const entities = event.dimension.getEntitiesAtBlockLocation(event.block.above().location);
entities.forEach((entity) => {
entity.addEffect("minecraft:wither", 2, { amplifier: 2 });
});
},
};
world.beforeEvents.worldInitialize.subscribe(({ blockComponentRegistry }) => {
blockComponentRegistry.registerCustomComponent(
"wiki:wither_treaders",
BlockWitherTreadersComponent
);
});完成!上述代码将在实体持续站立时触发指定的状态效果。
示例JSON
凋零方块示例
BP/blocks/wither_block.json
json
{
"format_version": "1.21.70",
"minecraft:block": {
"description": {
"identifier": "wiki:wither_block",
"states": {
"wiki:stood_on": [false, true]
}
},
"components": {
"minecraft:loot": "loot_tables/empty.json",
"minecraft:map_color": "#181818",
"minecraft:geometry": "geometry.wither_block",
"minecraft:material_instances": {
"*": {
"texture": "wiki:wither_block"
}
},
"minecraft:custom_components": ["wiki:treader_detection"]
},
"permutations": [
{
"condition": "q.block_state('wiki:stood_on')",
"components": {
"minecraft:custom_components": [
"wiki:treader_detection",
"wiki:wither_treaders"
],
"minecraft:tick": {
"interval_range": [1, 1],
"looping": true
}
}
}
]
}
}贡献者
编辑 应用持续效果本页面上的文本和图像内容根据 知识共享署名 4.0 国际许可协议
本页中的代码示例根据 MIT 许可证




