Files
netease-modsdk-wiki/docs/mcdocs/1-ModAPI/接口/实体/行为.md
2025-03-17 13:24:39 +08:00

1681 lines
45 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
sidebarDepth: 1
---
# 行为
## AddEntityAroundEntityMotion
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.actorMotionCompServer.ActorMotionComponentServer
- 描述
给实体(不含玩家)添加对实体环绕运动器
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| eID | str | 要环绕的某个实体的ID |
| angularVelocity | float | 圆周运动的角速度(弧度/秒) |
| axis | tuple(float,float,float) | 圆周运动的轴,决定了在哪个平面上做圆周运动,默认为(0, 1, 0) |
| lockDir | bool | 是否在运动器生效时锁定实体的朝向不锁定则实体的朝向会随着运动而改变默认为False。 |
| stopRad | float | 停止该运动器所需要的弧度当stopRad为0时该运动器会一直运行默认为0 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| int | 运动器ID添加失败时返回-1 |
- 备注
- 该接口不屏蔽生物本身的AI运动以及重力作用当有AI运动发生时最终的表现结果可能与预期有差异。
- 环绕运动器可叠加多个,且可与速度运动器互相叠加。
- 由于引擎中在加载的区块以外的实体时会停止一切活动建议将实体的运动范围控制在玩家位置±100内。
- 示例
```python
import mod.server.extraServerApi as serverApi
motionComp = serverApi.GetEngineCompFactory().CreateActorMotion(entityId)
axis=(-1, 1, 1)
mID = motionComp.AddEntityAroundEntityMotion(eID, 1.0, axis, lockDir=False, stopRad=0)
```
## AddEntityAroundPointMotion
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.actorMotionCompServer.ActorMotionComponentServer
- 描述
给实体(不含玩家)添加对点环绕运动器
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| center | tuple(float,float,float) | 要环绕的圆心点坐标 |
| angularVelocity | float | 圆周运动的角速度(弧度/秒) |
| axis | tuple(float,float,float) | 圆周运动的轴,决定了在哪个平面上做圆周运动,默认为(0, 1, 0) |
| lockDir | bool | 是否在运动器生效时锁定实体的朝向不锁定则实体的朝向会随着运动而改变默认为False。 |
| stopRad | float | 停止该运动器所需要的弧度当stopRad为0时该运动器会一直运行默认为0 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| int | 运动器ID添加失败时返回-1 |
- 备注
- 该接口不屏蔽生物本身的AI运动以及重力作用当有AI运动发生时最终的表现结果可能与预期有差异。
- 环绕运动器可叠加多个,且可与速度运动器互相叠加。
- 由于引擎中在加载的区块以外的实体时会停止一切活动建议将实体的运动范围控制在玩家位置±100内。
- 示例
```python
import mod.server.extraServerApi as serverApi
motionComp = serverApi.GetEngineCompFactory().CreateActorMotion(entityId)
center = (0, 8, 0)
axis=(-1, 1, 1)
mID = motionComp.AddEntityAroundPointMotion(center, 1.0, axis, lockDir=False, stopRad=0)
```
## AddEntityTrackMotion
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.actorMotionCompServer.ActorMotionComponentServer
- 描述
给实体(不含玩家)添加轨迹运动器
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| targetPos | tuple(float,float,float) | 轨迹终点 |
| duraTime | float | 到达终点所需要的时间 |
| startPos | tuple(float,float,float) | 轨迹起点默认为None表示以调用[StartEntityMotion](#StartEntityMotion)的位置作为起点。 |
| relativeCoord | bool | 是否使用相对坐标设置起点和终点默认为False。 |
| isLoop | bool | 是否循环若设为True则实体会在起点和终点之间往复运动。 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| int | 运动器ID添加失败时返回-1 |
- 备注
- 该接口不屏蔽生物本身的AI运动并且生物在空中时会受到跌落伤害当有AI运动发生时最终的表现结果可能与预期有差异建议将生物设置为NPC。
- 轨迹运动器不可叠加,仅能添加一个。
- 由于引擎中在加载的区块以外的实体时会停止一切活动建议将运动范围控制在玩家位置±100内。
- 示例
```python
import mod.server.extraServerApi as serverApi
motionComp = serverApi.GetEngineCompFactory().CreateActorMotion(entityId)
target = (5, 0, 0)
mID = motionComp.AddEntityTrackMotion(target, 3.0, startPos=None, relativeCoord=True, isLoop=False)
```
## AddEntityVelocityMotion
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.actorMotionCompServer.ActorMotionComponentServer
- 描述
给实体(不含玩家)添加速度运动器
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| velocity | tuple(float,float,float) | 速度,包含大小、方向 |
| accelerate | tuple(float,float,float) | 加速度包含大小、方向默认为None表示没有加速度 |
| useVelocityDir | bool | 是否使用当前速度的方向作为此刻实体的朝向默认为True |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| int | 运动器ID添加失败时返回-1 |
- 备注
- 该接口不屏蔽生物本身的AI运动以及重力作用当有AI运动发生时最终的表现结果可能与预期有差异。
- 速度运动器可叠加多个,且可与环绕运动器互相叠加。
- 由于引擎中在加载的区块以外的实体时会停止一切活动建议将实体的运动范围控制在玩家位置±100内。
- 示例
```python
import mod.server.extraServerApi as serverApi
motionComp = serverApi.GetEngineCompFactory().CreateActorMotion(entityId)
velocity = (0, 0, 1)
accelerate = (0, 0, -1)
mID = motionComp.AddEntityVelocityMotion(velocity, accelerate, useVelocityDir=True)
```
## GetAttackTarget
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.actionCompServer.ActionCompServer
- 描述
获取仇恨目标
- 参数
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| str | 返回仇恨目标的实体id。如果传入的实体id所对应的实体没有仇恨目标则返回-1。如果传入的实体id所对应的实体不存在则返回None。 |
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateAction(entityId)
comp.GetAttackTarget()
```
## GetBlockControlAi
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.controlAiCompServer.ControlAiCompServer
- 描述
获取生物原生AI是否被屏蔽
- 参数
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | AI是否保留。False为AI被屏蔽。 |
- 备注
- 屏蔽AI后的生物无法行动不受重力且不会被推动。但是可以受到伤害也可以被玩家交互例如马被骑或村民被交易
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateControlAi(entityId)
comp.GetBlockControlAi()
```
## GetCustomGoalCls
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.extraServerApi
- 描述
用于获取服务器自定义行为节点的基类。实现新的行为节点时,需要继承该接口返回的类
- 参数
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| type(CustomGoal) | 服务端自定义行为节点类 |
- 示例
```python
import mod.server.extraServerApi as serverApi
CustomGoal = serverApi.GetCustomGoalCls()
class CustomGoalDemo(CustomGoal):
def __init__(self, entityId, argsJson):
CustomGoalCls.__init__(self, entityId, argsJson)
```
## GetEntityMotions
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.actorMotionCompServer.ActorMotionComponentServer
- 描述
获取实体(不含玩家)身上的所有运动器
- 参数
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| dict | 运动器集合key值代表运动器mIDvalue值代表运动器类型0轨迹运动器、1速度运动器、2环绕运动器 |
- 备注
- 运动器非人为停止后会被移除。
- 示例
```python
import mod.server.extraServerApi as serverApi
motionComp = serverApi.GetEngineCompFactory().CreateActorMotion(entityId)
motions = motionComp.GetEntityMotions()
# motions = {
# 0:1,
# 1:2
# }
```
## GetMotion
<span style="display:inline;color:#ff5555">服务端</span><span style="display:inline;color:#7575f9">客户端</span>
### 服务端接口
<span id="s0"></span>
method in mod.server.component.actorMotionCompServer.ActorMotionComponentServer
- 描述
获取生物(含玩家)的瞬时移动方向向量
- 参数
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| tuple(int,int,int) | 瞬时移动方向向量异常时返回None |
- 示例
```python
import mod.server.extraServerApi as serverApi
motionComp = serverApi.GetEngineCompFactory().CreateActorMotion(entityId)
motionComp.GetMotion()
```
### 客户端接口
<span id="c0"></span>
method in mod.client.component.actorMotionCompClient.ActorMotionComponentClient
- 描述
获取生物的瞬时移动方向向量
- 参数
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| tuple(int,int,int) | 瞬时移动方向向量异常时返回None |
- 示例
```python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateActorMotion(entityId)
motionComp.GetMotion()
```
## GetOwnerId
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.tameCompServer.TameComponentServer
- 描述
获取驯服生物的主人id
- 参数
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| str | 主人id不存在时返回None |
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateTame(entityId)
oid = comp.GetOwnerId()
```
## GetStepHeight
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.attrCompServer.AttrCompServer
- 描述
返回玩家前进非跳跃状态下能上的最大台阶高度
- 参数
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| float | 台阶高度 |
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateAttr(entityId)
print(comp.GetStepHeight())
```
## Hurt
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.hurtCompServer.HurtCompServer
- 描述
设置实体伤害
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| damage | int | 伤害值 |
| cause | str | 伤害来源详见Minecraft枚举值文档的[ActorDamageCause枚举](../../枚举值/ActorDamageCause.md) |
| attackerId | str | 伤害来源的实体id默认为None |
| childAttackerId | str | 伤害来源的子实体id默认为None比如玩家使用抛射物对实体造成伤害该值应为抛射物Id |
| knocked | bool | 实体是否被击退默认值为True |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 是否设置成功 |
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateHurt(playerId)
comp.Hurt(10, serverApi.GetMinecraftEnum().ActorDamageCause.EntityAttack, attackerId, None, False)
```
## ImmuneDamage
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.hurtCompServer.HurtCompServer
- 描述
设置实体是否免疫伤害(该属性存档)
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| immune | bool | 是否免疫伤害 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 是否设置成功 |
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateHurt(entityId)
comp.ImmuneDamage(True)
```
## IsEntityOnFire
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.attrCompServer.AttrCompServer
- 描述
获取实体是否着火
- 参数
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 是否着火 |
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateAttr(entityId)
isOnFire = comp.IsEntityOnFire()
```
## RemoveEntityMotion
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.actorMotionCompServer.ActorMotionComponentServer
- 描述
移除实体(不含玩家)身上的运动器
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| motionId | int | 要移除的某个运动器的ID |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 是否成功移除 |
- 示例
```python
import mod.server.extraServerApi as serverApi
motionComp = serverApi.GetEngineCompFactory().CreateActorMotion(entityId)
motionComp.RemoveEntityMotion(mID)
```
## ResetAttackTarget
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.actionCompServer.ActionCompServer
- 描述
清除仇恨目标
- 参数
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 设置结果 |
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateAction(entityId)
comp.ResetAttackTarget()
```
## ResetMotion
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.actorMotionCompServer.ActorMotionComponentServer
- 描述
重置生物(不含玩家)的瞬时移动方向向量
- 参数
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 设置是否成功 |
- 备注
- 该接口只能重置SetMotion所设置的瞬时移动方向向量无法影响由生物本身的AI所产生的运动。
- 示例
```python
import mod.server.extraServerApi as serverApi
motionComp = serverApi.GetEngineCompFactory().CreateActorMotion(entityId)
motionComp.ResetMotion()
```
## ResetStepHeight
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.attrCompServer.AttrCompServer
- 描述
恢复引擎默认玩家前进非跳跃状态下能上的最大台阶高度
- 参数
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 是否设置成功 |
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateAttr(entityId)
comp.ResetStepHeight()
```
## SetActorCollidable
<span style="display:inline;color:#7575f9">客户端</span>
method in mod.client.component.actorCollidableCompClient.ActorCollidableCompClient
- 描述
设置实体是否可碰撞
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| isCollidable | int | 0:不可碰撞 1:可碰撞 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | True表示设置成功 |
- 示例
```python
import mod.client.extraClientApi as clientApi
comp = clientApi.GetEngineCompFactory().CreateActorCollidable(entityId)
success = comp.SetActorCollidable(1)
```
## SetActorPushable
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.actorPushableCompServer.ActorPushableCompServer
- 描述
设置实体是否可推动
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| isPushable | int | 0:不可推动 1:可推动 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | True表示设置成功 |
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateActorPushable(entityId)
success = comp.SetActorPushable(1)
```
## SetAttackTarget
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.actionCompServer.ActionCompServer
- 描述
设置仇恨目标
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| targetId | str | 目标实体id |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 设置结果 |
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateAction(entityId)
comp.SetAttackTarget(targetId)
```
## SetBlockControlAi
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.controlAiCompServer.ControlAiCompServer
- 描述
设置屏蔽生物原生AI
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| isBlock | bool | 是否保留AIFalse为屏蔽 |
| freezeAnim | bool | 屏蔽AI时是否冻结动作默认为False仅当isBlock为False时生效。重进世界会恢复成初始动作 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 设置结果 |
- 备注
- 屏蔽AI后的生物无法行动不受重力且不会被推动但是可以受到伤害也可以被玩家交互例如马被骑或村民被交易
- 对玩家无效
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateControlAi(entityId)
comp.SetBlockControlAi(False, True)
```
## SetCanOtherPlayerRide
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.rideCompServer.RideCompServer
- 描述
设置其他玩家是否有权限骑乘True表示每个玩家都能骑乘False只有驯服者才能骑乘
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| tamedEntityId | str | 可骑乘生物id |
| canRide | bool | 是否控制 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 设置结果 |
- 示例
```python
# 驯服生物
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateRide(entityId)
comp.SetCanOtherPlayerRide(entityId,False)
```
## SetControl
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.rideCompServer.RideCompServer
- 描述
设置该生物无需装备鞍就可以控制行走跳跃
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| tamedEntityId | str | 可骑乘生物id |
| isControl | bool | 是否控制 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 设置结果 |
- 备注
- 该接口仅对已被驯服的可骑乘生物生效若使用SetEntityRide接口驯服生物需间隔一定帧数后再调用本接口。
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateRide(entityId)
comp.SetControl(entityId,True)
```
## SetEntityInteractFilter
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.interactCompServer.InteractComponentServer
- 描述
设置与生物可交互的条件
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| index | int | 交互列表下标 |
| interactFilter | str | 可交互的条件 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 设置结果 |
- 备注
- 该接口修改minecraft:interact->on_interact中的filters的定义
- 仅当生物存在minecraft:interact组件时才能调用该接口例如牛、羊驼、猪灵等
- 示例
```python
import mod.server.extraServerApi as serverApi
import json
comp = serverApi.GetEngineCompFactory().CreateInteract(entityId)
filterDict = {
"filters": {
"all_of": [
{ "test": "is_family", "subject" : "other", "value" : "player"},
{ "test": "has_equipment", "domain": "hand", "subject": "other", "value": "bucket:0"}
]
}
}
filterStr = json.dumps(filterDict)
comp.SetEntityInteractFilter(0, filterStr)
```
## SetEntityOnFire
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.attrCompServer.AttrCompServer
- 描述
设置实体着火
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| seconds | int | 着火时间(单位:秒) |
| burn_damage | int | 着火状态下每秒扣的血量,不传的话默认是1 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 是否设置成功 |
- 备注
- 在水中或者雨中不会生效着火时间受生物装备、生物的状态影响。burn_damage取值范围是0~1000,小于0将取0大于1000将取1000
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateAttr(entityId)
comp.SetEntityOnFire(1, 2)
```
## SetEntityRide
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.rideCompServer.RideCompServer
- 描述
驯服可骑乘生物
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| playerId | str | 玩家id |
| tamedEntityId | str | 要驯服的可骑乘生物id |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 设置结果 |
- 备注
- 驯服信息会被存盘
- 示例
```python
# 驯服生物
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateRide(entityId)
comp.SetEntityRide(playerId,entityId)
```
## SetEntityShareablesItems
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.shareableCompServer.ShareableComponentServer
- 描述
设置生物可分享/可拾取的物品列表
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| items | list(itemdict) | 可分享/可拾取的物品列表 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 设置结果 |
- 备注
- 该接口修改minecraft:shareables的items定义
- 仅当生物存在minecraft:shareables组件时才能调用该接口例如狐狸、尸壳、猪灵等。若admire为True,则生物还需要有minecraft:admire_item组件。若barter为True,则生物还需有minecraft:behavior.barter行为。
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateShareables(entityId)
shareableItems = []
shareableItems.append({
"item": "minecraft:golden_sword",
"auxValue": 0,
"priority": 1,
"pickupLimit": 1,
"barter": True,
"admire": True,
})
comp.SetEntityShareablesItems(shareableItems)
```
## SetEntityTamed
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.tameCompServer.TameComponentServer
- 描述
设置生物驯服,需要配合 entityEvent组件使用。该类驯服不包含骑乘功能。
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| playerId | str | 驯服玩家Id |
| tamedId | str | 被驯服的生物Id |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 设置结果 |
- 备注
- 驯服一个生物,需要按以下步骤:
1. 搭配修改生物json文件使其能够被驯服
- 添加minecraft:tameable组件
- 添加一个component group里面放驯服后的行为
- 添加一个event添加刚刚添加的component group并移除不需要的component group
2. 使用SetEntityTamed使目标生物被驯服
3. 使用TriggerCustomEvent触发驯服event
如果是对原版可驯服生物使用则无需按照第一步修改json而是找到已有的event在第三步中触发即可
- 如修改苦力怕可被驯服json做如下修改
```json
{
"format_version": "1.8.0",
"minecraft:entity": {
"description": {
"identifier": "minecraft:creeper",
"is_spawnable": true,
"is_summonable": true,
"is_experimental": false
},
"component_groups": {
...
//增加驯服状态
"netease:creeper_tame":{
"minecraft:is_tamed": {
},
//跟随主人
"minecraft:behavior.follow_owner": {
"priority": 4, //优先级要高于其他移动行为
"speed_multiplier": 1.0,
"start_distance": 10,
"stop_distance": 2
}
}
},
"components": {
...
//增加驯服组件,使用骨头驯服
"minecraft:tameable": {
"probability": 0.33,
"tameItems": "bone",
"tame_event": {
"event": "netease:on_tame",
"target": "self"
}
}
},
"events": {
...
//增加驯服事件
"netease:on_tame": {
//移除掉所有跟爆炸有关的逻辑
"remove": {
"component_groups": [
"minecraft:exploding",
"minecraft:charged_exploding",
"minecraft:forced_exploding",
"minecraft:forced_charged_exploding",
"minecraft:charged_creeper"
]
},
//添加netease:creeper_tame
"add": {
"component_groups": [
"netease:creeper_tame"
]
}
}
}
}
}
```
- 示例
```python
import mod.server.extraServerApi as serverApi
# 使tamedId被playerId驯服
tameComp = serverApi.GetEngineCompFactory().CreateTame(tamedId)
tameComp.SetEntityTamed(playerId,tamedId)
# 触发tamedId的netease:on_tame自定义event
envComp = serverApi.GetEngineCompFactory().CreateEntityEvent(tamedId)
envComp.TriggerCustomEvent(tamedId,'netease:on_tame')
```
## SetJumpPower
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.gravityCompServer.GravityComponentServer
- 描述
设置生物跳跃力度0.42表示正常水平
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| jumpPower | float | 跳跃力度正常是0.42 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 是否设置成功 |
- 备注
- 生物跳跃力度影响生物跳跃高度本接口调用时需要客户端加载完成如在AddServerPlayer中客户端还没加载完成要延后执行才能生效
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateGravity(entityId)
comp.SetJumpPower(0.84)
```
## SetMobKnockback
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.actionCompServer.ActionCompServer
- 描述
设置击退的初始速度,需要考虑阻力的影响
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| xd | float | x轴方向用來控制角度 |
| zd | float | z轴方向用來控制角度 |
| power | float | 用来控制水平方向的初速度 |
| height | float | 竖直方向的初速度 |
| heightCap | float | 向上速度阈值当实体本身已经有向上的速度时需要考虑这个值用来确保最终向上的速度不会超过heightCap |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| None | 无返回值 |
- 备注
- 在damageEvent事件里面使用该接口时需把damageEvent事件回调的knock参数设置为False
- 该接口会触发OnKnockBackServerEvent事件所以当需要在该事件中使用时请编写逻辑避免循环调用
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateAction(entityId)
comp.SetMobKnockback(0.1, 0.1, 1.0, 1.0, 1.0)
```
## SetMotion
<span style="display:inline;color:#ff5555">服务端</span><span style="display:inline;color:#7575f9">客户端</span>
### 服务端接口
<span id="s0"></span>
method in mod.server.component.actorMotionCompServer.ActorMotionComponentServer
- 描述
设置生物(不含玩家)的瞬时移动方向向量
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| motion | tuple(float,float,float) | 世界坐标系下的向量该方向为世界坐标系下的向量以x,z,y三个轴的正方向为正值可以通过当前生物的rot组件判断目前玩家面向的方向可在开发模式下打开F3观察数值变化。 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 设置是否成功 |
- 备注
- 在damageEvent事件里面使用该接口时需把damageEvent事件回调的knock参数设置为False
- 示例
```python
import mod.server.extraServerApi as serverApi
# 使生物向准星的方向突进一段距离
rotComp = serverApi.GetEngineCompFactory().CreateRot(entityId)
rot = rotComp.GetRot()
x, y, z = serverApi.GetDirFromRot(rot)
motionComp = serverApi.GetEngineCompFactory().CreateActorMotion(entityId)
motionComp.SetMotion((x * 5, y * 5, z * 5))
# rot 和 世界坐标系关系
# ^ x -90°
# |
# 180°/-180 ----------> z 0°
# | 90°
```
### 客户端接口
<span id="c0"></span>
method in mod.client.component.actorMotionCompClient.ActorMotionComponentClient
- 描述
设置瞬时的移动方向向量,用于本地玩家
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| motion | tuple(float,float,float) | 世界坐标系下的向量该方向为世界坐标系下的向量以x,z,y三个轴的正方向为正值可以通过当前玩家的rot组件判断目前玩家面向的方向可在开发模式下打开F3观察数值变化。 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 设置是否成功 |
- 备注
- 如果频繁快速修改本地玩家的瞬时移动向量可能会触发引擎服务端的反作弊机制例如掉落伤害需要频繁快速修改时最好搭配服务端SetMotion同步修改
- 示例
```python
import mod.client.extraClientApi as clientApi
# 使玩家向准星的方向突进一段距离
localPlayerId = clientApi.GetLocalPlayerId()
rotComp = clientApi.GetEngineCompFactory().CreateRot(localPlayerId)
rot = rotComp.GetRot()
x, y, z = clientApi.GetDirFromRot(rot)
motionComp = clientApi.GetEngineCompFactory().CreateActorMotion(localPlayerId)
motionComp.SetMotion((x * 5, y * 5, z * 5))
# rot 和 世界坐标系关系
# ^ x -90°
# |
# 180°/-180 ----------> z 0°
# | 90°
```
## SetMoveSetting
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.moveToCompServer.MoveToComponentServer
- 描述
寻路组件
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| pos | tuple(float,float,float) | 寻路目标位置 |
| speed | float | 移动速度指正常移动速度的倍率。如1.0表示正常速度2.0表示两倍速 |
| maxIteration | int | 寻路算法最大迭代次数 默认200 |
| callback | function | 寻路结束回调函数 |
- 返回值
- 备注
- 使用该接口时需要在生物中配置有寻路的json组件。配置寻路json组件后该接口会自动选择相应类型的寻路
目前支持的寻路json组件包括
- minecraft:navigation.walk
陆地寻路,与原版僵尸的寻路相同
- minecraft:navigation.generic
水陆寻路,支持陆地与水中,与原版溺尸的寻路相同
- minecraft:navigation.climb
陆地寻路,但是支持爬墙,与原版蜘蛛的寻路相同。这种寻路可能会被头顶方块阻挡,一直无法抵达目的地
- minecraft:navigation.fly
空中寻路,与原版鹦鹉的寻路相同
以上的寻路都需要搭配一些其他json组件例如movement使用具体可以参考NavigationMod的示例
上面没有提到的navigation类型暂不支持例如minecraft:navigation.float如原版恶魂minecraft:navigation.hover如原版蜜蜂
- 不同的生物拥有不同的默认最大跟随距离若要寻路的目标点距离大于此值引擎会拒绝寻路要修改该距离可以通过在entity的json中配置.
```json
{
"format_version": "1.8.0",
"minecraft:entity": {
"components": {
"minecraft:follow_range": {
"value": 48,
"max": 48
}
}
}
}
```
- 关于maxIteration参数
该参数会影响实际寻到路径的长度。若寻路算法迭代一定次数后,未寻到目标点,会返回局部最优解,即生物只会走到半路。
在无大型障碍物的情况下参数对应的参考寻路距离如下该参数默认值200最大值2000,请开发者根据实际情况选择。
| maxIteration | 与目标点直线距离 |
| ------------ | ---------------- |
| 200 | 13 |
| 500 | 20 |
| 1000 | 30 |
| 2000 | 43 |
- 关于callback函数
该函数需要接受两个参数第一个参数为寻路的entityId类型str第二个参数为寻路结果类型int
玩家获取到的位置比地面会高1.62格若以玩家位置为目标点需要先把y轴减去1.62否则callback会一直返回1
| 结果 | 说明 |
| ---- | ------------------------------------------------------------ |
| -3 | 寻路失败,大于跟随距离,或者生物周围没有可行走位置,或者对正在寻路的飞行系生物使用 |
| -2 | 寻路失败生物没有寻路组件指minecraft:navigation |
| -1 | 寻路失败,参数错误,或生物不存在 |
| 0 | 寻路完成。到达设定的目标点 |
| 1 | 寻路完成但未到达目标点可能由于maxIteration参数偏小 |
| 2 | 寻路中断。中途遇到障碍物被阻碍 |
| 3 | 寻路中断。被生物原版寻路行为覆盖或寻路未结束时重复调用moveTo组件。<br>若生物的移速太低真实速度小于0.3格每秒),也会被当成寻路被卡住,返回该错误码 |
- 对于各种类型寻路的生物,还需要满足以下初始条件:
1. 陆地寻路,水陆寻路与爬墙寻路:需要满足以下任一条件:
- 生物着地
- 生物正在骑乘,并且骑乘物着地
- 生物在液体中
- 生物是凋零
2. 飞行寻路:需要满足以下任一条件:
- 不在骑乘状态
- 在液体中
- 生物着地
- json配置中的can_path_from_air属性为true
- demo简介
聊天栏输入walk/generic/climb/fly会原地生成一个使用对应navigation json组件的生物然后跑到其他位置再输入go会将刚才生成的生物导航到玩家当前位置。
这4种示例生物的行为json可以在NavigationMod_behavior/entities目录查看。
4种示例生物的最大寻路距离都设置为了48格。
- 示例
```python
from mod_log import logger as logger
def myCallback(entityId, result):
if result in (-1,-2,-3):
logger.info('[error] [SetMoveSetting] failed')
elif result==0:
logger.info('[info] [SetMoveSetting] success')
elif result in (1,2,3):
logger.info('[warn] [SetMoveSetting] terminated')
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateMoveTo(entityId)
comp.SetMoveSetting((x,y,z),2.0,200,myCallback)
```
## SetPersistence
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.persistenceCompServer.PersistenceCompServer
- 描述
设置实体是否持久化。
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| isPersistent | bool | True为设置实体持久化False为设置实体不持久化。 |
- 返回值
- 备注
- 游戏中,实体默认持久化,若设置不持久化,则实体会在区块卸载和退出存档时被删除,不会存档。
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreatePersistence(entityId)
comp.SetPersistence(True)
```
## SetRidePos
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.rideCompServer.RideCompServer
- 描述
设置生物骑乘位置
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| tamedEntityId | str | 可骑乘生物id |
| pos | tuple(float,float,float) | 骑乘时挂接点 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 设置结果 |
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateRide(entityId)
comp.SetRidePos(entityId,(1,1,1))
```
## SetRiderRideEntity
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.rideCompServer.RideCompServer
- 描述
设置实体骑乘生物(或者船与矿车)
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| riderId | str | 骑乘生物id |
| riddenEntityId | str | 被骑乘生物id。要求被骑乘生物的定义中具有minecraft:rideable组件且组件中family_types含有可骑乘者的类型声明 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 设置结果 |
- 备注
- 通常需要配合SetEntityRide、SetControl一起使用需要被骑乘生物json中骑乘组件支持骑乘者的生物类型
当被控制的entity有多个位置时且开发者想要添加多个玩家时第一个被添加的玩家会被引擎默认设置为控制者
- 示例
```python
# 骑上坐骑
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateRide(entityId)
comp.SetRiderRideEntity(playerId,rideEntityId)
```
## SetStepHeight
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.attrCompServer.AttrCompServer
- 描述
设置玩家前进非跳跃状态下能上的最大台阶高度, 默认值为0.56251的话表示能上一个台阶
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| stepHeight | float | 最大高度需要大于0 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 是否设置成功 |
- 备注
- 为了避免因浮点数误差导致错误设置的时候通常会增加1/16个方块大小即0.0625。所以此处我们设置2.0625。游戏中默认值是0.5625,即半格高度。
- 只对玩家生效,无法修改其它实体该属性
- 修改后不影响跳跃逻辑及跳跃高度,并不会因此而跳到更高,因此在某些特定情况下,你可以走上方块但跳不上去。
- 示例
```python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateAttr(entityId)
#如果前面放置有两格高的方块,玩家按前进能直接上去,无须跳跃
comp.SetStepHeight(2.0625)
```
## StartEntityMotion
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.actorMotionCompServer.ActorMotionComponentServer
- 描述
启动实体(不含玩家)身上的某个运动器
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| motionId | int | 要启动的某个运动器的ID |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 是否成功启动 |
- 示例
```python
import mod.server.extraServerApi as serverApi
motionComp = serverApi.GetEngineCompFactory().CreateActorMotion(entityId)
motionComp.StartEntityMotion(mID)
```
## StopEntityMotion
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.actorMotionCompServer.ActorMotionComponentServer
- 描述
停止实体(不含玩家)身上的某个运动器
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| motionId | int | 要停止的某个运动器的ID |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 是否成功停止 |
- 备注
- 调用该接口不会触发事件[EntityMotionStopServerEvent](../../事件/实体.md#EntityMotionStopServerEvent)。
- 示例
```python
import mod.server.extraServerApi as serverApi
motionComp = serverApi.GetEngineCompFactory().CreateActorMotion(entityId)
motionComp.StopEntityMotion(mID)
```
## TriggerCustomEvent
<span style="display:inline;color:#ff5555">服务端</span>
method in mod.server.component.entityEventCompServer.EntityEventComponentServer
- 描述
触发生物自定义事件
- 参数
| 参数名 | <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- | :--- |
| entityId | str | 生物Id |
| eventName | str | 事件名称 |
- 返回值
| <div style="width: 4em">数据类型</div> | 说明 |
| :--- | :--- |
| bool | 设置结果 |
- 备注
- 触发苦力怕爆炸
在苦力怕的entity json文件中`events`字段下增加如下事件,然后在mod中运行示例代码
```json
"netease:custom_exploading":{
"sequence": [
{
"filters": {
"test": "has_component",
"operator": "!=",
"value": "minecraft:is_charged"
},
"add": {
"component_groups": [
"minecraft:forced_exploding"
]
}
},
{
"filters": {
"test": "has_component",
"value": "minecraft:is_charged"
},
"add": {
"component_groups": [
"minecraft:forced_charged_exploding"
]
}
}
]
}
```
- 触发事件所添加或移除的component_groups会在下一帧才真正生效。
- 示例
```python
import mod.server.extraServerApi as serverApi
#触发entity自定义event
eventName = "netease:custom_exploading"
comp = serverApi.GetEngineCompFactory().CreateEntityEvent(entityId)
comp.TriggerCustomEvent(entityId,eventName)
```