Skip to content

自定义鱼竿 ​

鱼竿(Fishing Rod)是自定义物品的一种特殊类型。在支持所有自定义物品特性的基础上,鱼竿还具备原版钓鱼的完整功能,并可自定义鱼线长度、鱼线颜色以及鱼漂实体。

前置条件

请先阅读 自定义基础物品 了解通用注册流程(步骤 1-6)。format_version 使用 1.10。

概述 ​

自定义鱼竿在基础物品之上扩展了钓鱼功能。你可以:

  • 自定义鱼漂实体(使用任何已注册的实体作为鱼漂,支持动画和渲染控制器)
  • 自定义鱼线最大长度
  • 自定义鱼线颜色和透明度
  • 通过脚本动态修改鱼线属性

不配置 netease:fishing_hook 时,鱼竿的行为与原版鱼竿完全一致。

netease:fishing_hook 组件 ​

键类型默认值说明
hook_entitystring—鱼漂使用的实体 identifier。填入的实体除了无 AI 以外,支持动画、动画控制器等一切实体操控
line_maxint32鱼线最大长度(鱼漂与玩家的直线距离,单位:方块),超过后鱼线自动断开
line_colorarray(float)原版黑色鱼线颜色与透明度,格式 [R, G, B, A],取值 0.0~1.0

注意

  • hook_entity 指定的实体在作为鱼漂时不会有 AI,但动画、动画控制器和渲染控制器仍然生效
  • line_color 使用 0.0~1.0 浮点数(不是 0~255 整数),例如 [0.6, 0.5, 0.3, 1.0]

相关事件与接口 ​

名称类型端说明
PlayerStartFishingServerEvent事件服务端玩家抛出鱼竿时触发
PlayerFishingServerEvent事件服务端钓鱼成功收杆时触发
PlayerFishingAfterServerEvent事件服务端钓鱼成功收杆后触发(物品已生成)
GetPlayerFishItem接口服务端获取玩家当前使用的鱼竿物品字典
GetPlayerIsFishing接口客户端获取玩家是否正在钓鱼
GetPlayerFishHookEntity接口客户端获取玩家鱼漂的实体 ID
SetFishingLineColor接口客户端动态设置鱼线颜色
GetFishingLineColor接口客户端获取当前鱼线颜色
SetFishingLineMax接口服务端动态设置鱼线最大长度
GetFishingLineMax接口服务端获取当前鱼线最大长度

完整案例:苦力怕鱼漂鱼竿 ​

一把使用苦力怕作为鱼漂、绿色鱼线、50 格超长鱼线的自定义鱼竿。

行为包 ​

behavior_pack/netease_items_beh/creeper_rod.json:

json
{
  "format_version": "1.10",
  "minecraft:item": {
    "description": {
      "identifier": "mymod:creeper_rod",
      "category": "equipment",
      "custom_item_type": "fishing_rod"
    },
    "components": {
      "minecraft:max_stack_size": 1,
      "minecraft:max_damage": 384,
      "minecraft:hand_equipped": true,
      "netease:fishing_hook": {
        "hook_entity": "minecraft:creeper",
        "line_max": 50,
        "line_color": [0.2, 0.8, 0.2, 1.0]
      }
    }
  }
}

资源包 ​

resource_pack/netease_items_res/creeper_rod.json:

json
{
  "format_version": "1.10",
  "minecraft:item": {
    "description": {
      "identifier": "mymod:creeper_rod",
      "texture": "creeper_rod"
    }
  }
}

item_texture.json ​

json
{
  "resource_pack_name": "mymod",
  "texture_data": {
    "creeper_rod": {
      "textures": "textures/items/creeper_rod"
    }
  }
}

本地化 ​

item.mymod:creeper_rod.name=苦力怕鱼竿

服务端脚本:钓鱼奖励 ​

python
import server.extraServerApi as serverApi
ServerSystem = serverApi.GetServerSystemCls()

class FishingMod(ServerSystem):
    def __init__(self, namespace, systemName):
        super(FishingMod, self).__init__(namespace, systemName)
        self.ListenForEvent(
            serverApi.GetEngineNamespace(),
            serverApi.GetEngineSystemName(),
            'PlayerFishingServerEvent',
            self, self.OnFishing
        )

    def OnFishing(self, args):
        playerId = args.get('playerId')
        itemDict = args.get('itemDict')  # 钓上来的物品
        # 获取鱼竿信息
        comp = serverApi.GetEngineCompFactory().CreateItem(playerId)
        rodDict = comp.GetPlayerFishItem(playerId)
        if rodDict:
            rodName = rodDict.get('itemName', '')
            if rodName == 'mymod:creeper_rod':
                # 使用苦力怕鱼竿钓鱼时,额外给予经验
                expComp = serverApi.GetEngineCompFactory().CreateExp(playerId)
                expComp.AddPlayerExperience(10)

客户端脚本:动态变色鱼线 ​

python
import client.extraClientApi as clientApi
ClientSystem = clientApi.GetClientSystemCls()

class FishingClient(ClientSystem):
    def __init__(self, namespace, systemName):
        super(FishingClient, self).__init__(namespace, systemName)
        self.mTickCount = 0

    def Update(self):
        self.mTickCount += 1
        playerId = clientApi.GetLocalPlayerId()
        comp = clientApi.GetEngineCompFactory().CreateItem(playerId)
        if comp.GetPlayerIsFishing():
            # 每 20 tick 变换一次鱼线颜色
            t = (self.mTickCount % 60) / 60.0
            lineComp = clientApi.GetEngineCompFactory().CreateItem(playerId)
            lineComp.SetFishingLineColor(
                [t, 1.0 - t, 0.5, 1.0]
            )

常见问题 ​

鱼漂不显示自定义实体 ​

确认 hook_entity 指定的实体 identifier 正确,且该实体在资源包中有对应的客户端定义(模型、贴图、渲染控制器)。

鱼线颜色不对 ​

确认 line_color 使用 0.0~1.0 的浮点数,不是 0~255 的整数。[255, 0, 0, 255] 是错误的,应该写 [1.0, 0.0, 0.0, 1.0]。

鱼线太短/太长 ​

调整 line_max 值(默认 32,即 32 个方块的直线距离)。也可以在运行时通过 SetFishingLineMax 接口动态调整。

下一步 ​