方块事件迁移指南
help
开始前须知
本页面要求您熟悉基础JavaScript知识,并了解现代方块事件的工作原理。
在将方块的JSON事件升级为自定义组件时遇到困难?别担心!本页面将帮助您理解如何用Script API实现那些已弃用的JSON事件响应。
添加药水效果
自定义组件
js
onStepOn({ entity }) {
entity?.addEffect("regeneration", 30, {
amplifier: 10,
showParticles: false
});
}造成伤害(实体)
js
import { EntityDamageCause } from "@minecraft/server";自定义组件
js
onStepOn({ entity }) {
entity?.applyDamage(2, {
cause: EntityDamageCause.drowning
});
}造成伤害(物品)
js
import { EquipmentSlot, GameMode } from "@minecraft/server";自定义组件
js
onPlayerInteract({ player }) {
// 获取主手物品栏
if (!player) return;
const equippable = player.getComponent("minecraft:equippable");
if (!equippable) return;
const mainhand = equippable.getEquipmentSlot(EquipmentSlot.Mainhand);
if (!mainhand.hasItem()) return;
// 非创造模式时减少耐久度
if (player.getGameMode() === GameMode.creative) return;
const itemStack = mainhand.getItem(); // 获取物品组件
const durability = itemStack.getComponent("minecraft:durability");
if (!durability) return;
// 计算耐久附魔影响
const enchantable = itemStack.getComponent("minecraft:enchantable");
const unbreakingLevel = enchantable?.getEnchantment("unbreaking")?.level;
const damageChance = durability.getDamageChance(unbreakingLevel) / 100;
if (Math.random() > damageChance) return; // 根据耐久附魔等级随机跳过伤害
// 对物品造成伤害
const shouldBreak = durability.damage === durability.maxDurability;
if (shouldBreak) {
mainhand.setItem(undefined); // 移除物品
player.playSound("random.break"); // 播放破坏音效
} else {
durability.damage++; // 增加耐久损伤值
mainhand.setItem(itemStack); // 更新主手物品
}
}减少堆叠数量
js
import { EquipmentSlot, GameMode } from "@minecraft/server";自定义组件
js
onPlayerInteract({ player }) {
if (!player) return;
const equippable = player.getComponent("minecraft:equippable");
if (!equippable) return;
const mainhand = equippable.getEquipmentSlot(EquipmentSlot.Mainhand);
if (!mainhand.hasItem()) return;
if (player.getGameMode() !== GameMode.creative) {
if (mainhand.amount > 1) {
mainhand.amount--; // 减少一个物品
} else {
mainhand.setItem(undefined); // 移除整个物品堆
}
}
}方块消失
自定义组件
js
onStepOn({ block }) {
block.setType("minecraft:air");
}实体死亡
自定义组件
js
onStepOn({ entity }) {
entity?.kill();
}播放粒子效果
自定义组件
js
onStepOn({ dimension, block }) {
dimension.spawnParticle("minecraft:campfire_smoke_particle", block.center());
}播放音效
自定义组件
js
onStepOn({ dimension, block }) {
dimension.playSound("dig.stone", block.center());
}移除药水效果
自定义组件
js
onStepOn({ entity }) {
entity?.removeEffect("regeneration");
}执行命令
自定义组件
js
onStepOn({ dimension }) {
dimension.runCommand("say 你好!");
dimension.runCommand("say 欢迎来到我的世界!");
}设置方块类型
自定义组件
js
onStepOn({ block }) {
block.setType("minecraft:grass_block");
}js
import { BlockPermutation } from "@minecraft/server";自定义组件
js
onStepOn({ block }) {
block.setPermutation(BlockPermutation.resolve("minecraft:campfire", {
"minecraft:cardinal_direction": "east",
"extinguished": true
}));
}在指定位置设置方块
自定义组件
js
onStepOn({ dimension, block }) {
const offset = { x: -1, y: 1, z: 5 };
const locationWithOffset = {
x: block.location.x + offset.x,
y: block.location.y + offset.y,
z: block.location.z + offset.z
};
dimension.setBlockType(locationWithOffset, "minecraft:grass_block");
}js
import { BlockPermutation } from "@minecraft/server";自定义组件
js
onStepOn({ dimension, block }) {
const offset = { x: -1, y: 1, z: 5 };
const locationWithOffset = {
x: block.location.x + offset.x,
y: block.location.y + offset.y,
z: block.location.z + offset.z
};
dimension.setBlockPermutation(locationWithOffset, BlockPermutation.resolve("minecraft:campfire", {
"minecraft:cardinal_direction": "east",
"extinguished": true
}));
}设置方块状态
自定义组件
js
onStepOn({ block }) {
block.setPermutation(
block.permutation.withState("wiki:integer_example", 5)
);
}js
import { BlockPermutation } from "@minecraft/server";自定义组件
js
onStepOn({ block }) {
const states = {
...block.permutation.getAllStates(),
"wiki:boolean_example": false,
"wiki:integer_example": 5,
"wiki:string_example": "blue"
};
block.setPermutation(
BlockPermutation.resolve(block.typeId, states)
);
}生成战利品
js
import { ItemStack } from "@minecraft/server";自定义组件
js
onStepOn({ dimension, block }) {
dimension.spawnItem(new ItemStack("minecraft:stick", 3), block.center());
}自定义组件
js
onStepOn({ dimension, block }) {
const { x, y, z } = block.center();
dimension.runCommand(`loot spawn ${x} ${y} ${z} loot "entities/ghast"`);
}传送实体
自定义组件
js
onStepOn({ entity }) {
entity?.teleport({ x: 100, y: 20, z: 786 });
}物品转换
js
import { EquipmentSlot, ItemStack } from "@minecraft/server";自定义组件
js
onPlayerInteract({ player }) {
const equippable = player?.getComponent("minecraft:equippable");
if (!equippable) return;
const mainhand = equippable.getEquipmentSlot(EquipmentSlot.Mainhand);
if (!mainhand.hasItem() || mainhand.typeId !== "minecraft:bowl") return;
mainhand.setItem(new ItemStack("minecraft:suspicious_stew"));
}贡献者
编辑 方块事件迁移指南本页面上的文本和图像内容根据 知识共享署名 4.0 国际许可协议
本页中的代码示例根据 MIT 许可证
