跳到主要内容

进阶:编写海洋杀手

目标

海洋杀手-玩家在水中击杀水中怪物时能够实现一击必杀的效果

功能分析

要实现此功能,我们应当监听实体受伤事件,并做以下检测:

  • 攻击者是否为玩家
  • 受伤实体是否在水中
  • 攻击者是否在水中

若满足条件,则将受伤实体直接击杀

相关接口

Details

开始编写

b/Scripts_Sample/index.py
def onEntityHurt(arg):
if arg.damageSource.damagingEntity:
if arg.damageSource.damagingEntity.typeId == 'minecraft:player':
# 攻击者是玩家
entity = arg.hurtEntity
player = arg.damageSource.damagingEntity
if player.isInWater and entity.isInWater:
# 同时在水中
entity.kill()
# 杀死实体

world.afterEvents.entityHurt.subscribe(onEntityHurt)
# 监听实体受伤事件