第2章 script api_2(1 / 1)

entity.hasTag(\u0027\u0027) - Tag標簽檢測

var overworld \u003d world.getDimension(\"overworld\")

let entities \u003d overworld.getEntities();

for (let entity of entities) {

if (entity.hasTag(\u0027Tag name\u0027)) return entity.runCommand(\"say hello player\")

getComponent(\"\") - 獲取實體組件

格式 :

var overworld \u003d world.getDimension(\"overworld\")

let entities \u003d overworld.getEntities()

for (let entity of entities) {

entity.getComponent(\"health\")

}

子功能 通過“.”號使用

entity.getComponent(\"health\").setCurrent(20) - 設置血量

var health \u003d entity.getComponent(\"health\")

health.value - 返回血量上限值

health.current - 返回血量當前值

其餘屬性如 :

minecraft:movement等,可通過類似方法更改

請勿將除了血量以外的值更改為infinity,10e10~100e10000,NaN等

world.say() - 發送消息

world.say(\"hello world\")

var a \u003d 0

world.say(a)

removeTag - 移除標簽

var entities \u003d overworld.getEntities();

for (let entity of entities) {

var tags \u003d entity.getTags();

for (let tag of tags) {

entity.removeTag(tag)

來移除實體標簽

spawnEntity - 生成實體

格式:

var loc \u003d new Location(0,-50,0);

overworld.spawnEntity(\"minecraft:pig\",loc)

用於在已加載區塊生成實體

system.events.beforeWatchdogTerminate.subscribe((e) \u003d> {

e.cancel \u003d true;

});

取消Watchdog事件(重要)

Watchdog會在運行過快/多時踢出玩家

....................