烧烤 动物养殖 分类 功能实现2

This commit is contained in:
wenhe committed 2026-09-01 22:47:08 +08:00
1 parent eac9409571
commit 9c99295db6
13 files changed
+201 -42

No files matched your search

@@ -11,7 +11,7 @@
BlockSwitchData.py = 通用工具方块切换(箭桶、挂肉钩、水槽、书柜等)
AnimalData.py = 动物养殖专属(鸡窝、饲料盆、饲料槽、粪便等)
字段说明(与 BlockSwitchData 基本一致,新增 giveCount/dropItem):
字段说明(与 BlockSwitchData 基本一致,新增 giveCount/dropItem/tip):
source: 被右键的方块ID(必填)
item: 玩家手里必须拿的物品,None=空手也行
target: 替换成什么方块,"minecraft:air"=消失
@@ -23,6 +23,9 @@
做成掉落物而不是直接塞背包,符合"取下/获得"的体验
type: "switch"=右键切换, "timer"=计时自动转换
time: 计时型转换需要多少秒(到时间自动替换方块)
tip: 转换成功时给玩家的提示(绿色§a开头)
startTip: 计时型规则启动计时时给玩家的提示(仅玩家主动触发时发)
doneTip: 计时链结束时给启动者的提示(只有整条链的最后一级会发)
================================================================================
"""
@@ -40,6 +43,7 @@ ANIMAL_SWITCHES = [
"consume": 1,
"type": "switch",
"switchCd": 0.15,
"tip": "§a鸡蛋已放入鸡窝 (1/5)",
"giveItem": None,
},
# 【放蛋2】鸡窝_2(1蛋) → 鸡窝_3(2蛋)
@@ -50,6 +54,7 @@ ANIMAL_SWITCHES = [
"consume": 1,
"type": "switch",
"switchCd": 0.15,
"tip": "§a鸡蛋已放入鸡窝 (2/5)",
"giveItem": None,
},
# 【放蛋3】鸡窝_3(2蛋) → 鸡窝_4(3蛋)
@@ -60,6 +65,7 @@ ANIMAL_SWITCHES = [
"consume": 1,
"type": "switch",
"switchCd": 0.15,
"tip": "§a鸡蛋已放入鸡窝 (3/5)",
"giveItem": None,
},
# 【放蛋4】鸡窝_4(3蛋) → 鸡窝_5(4蛋)
@@ -70,6 +76,7 @@ ANIMAL_SWITCHES = [
"consume": 1,
"type": "switch",
"switchCd": 0.15,
"tip": "§a鸡蛋已放入鸡窝 (4/5)",
"giveItem": None,
},
# 【放蛋5】鸡窝_5(4蛋) → 鸡窝_6(5蛋)
@@ -80,6 +87,7 @@ ANIMAL_SWITCHES = [
"consume": 1,
"type": "switch",
"switchCd": 0.15,
"tip": "§a鸡蛋已放入鸡窝 (5/5)",
"giveItem": None,
},
@@ -96,6 +104,7 @@ ANIMAL_SWITCHES = [
"consume": 0,
"type": "switch",
"dropItem": [{"name": "minecraft:egg", "count": 5}],
"tip": "§a取走了 5 个鸡蛋",
"giveItem": None,
},
# 【取4蛋】鸡窝_5(4蛋) → 鸡窝_1(0蛋),掉落4个鸡蛋
@@ -106,6 +115,7 @@ ANIMAL_SWITCHES = [
"consume": 0,
"type": "switch",
"dropItem": [{"name": "minecraft:egg", "count": 4}],
"tip": "§a取走了 4 个鸡蛋",
"giveItem": None,
},
# 【取3蛋】鸡窝_4(3蛋) → 鸡窝_1(0蛋),掉落3个鸡蛋
@@ -116,6 +126,7 @@ ANIMAL_SWITCHES = [
"consume": 0,
"type": "switch",
"dropItem": [{"name": "minecraft:egg", "count": 3}],
"tip": "§a取走了 3 个鸡蛋",
"giveItem": None,
},
# 【取2蛋】鸡窝_3(2蛋) → 鸡窝_1(0蛋),掉落2个鸡蛋
@@ -126,6 +137,7 @@ ANIMAL_SWITCHES = [
"consume": 0,
"type": "switch",
"dropItem": [{"name": "minecraft:egg", "count": 2}],
"tip": "§a取走了 2 个鸡蛋",
"giveItem": None,
},
# 【取1蛋】鸡窝_2(1蛋) → 鸡窝_1(0蛋),掉落1个鸡蛋
@@ -136,6 +148,7 @@ ANIMAL_SWITCHES = [
"consume": 0,
"type": "switch",
"dropItem": [{"name": "minecraft:egg", "count": 1}],
"tip": "§a取走了 1 个鸡蛋",
"giveItem": None,
},
@@ -150,6 +163,7 @@ ANIMAL_SWITCHES = [
"target": "rtdd:chickenmanger_1",
"consume": 16,
"type": "switch",
"tip": "§a鸡饲料已加满",
"giveItem": None,
},
# 【计时变空】鸡饲料盆_2(满) → 鸡饲料盆_1(空),计时60秒
@@ -161,6 +175,8 @@ ANIMAL_SWITCHES = [
"consume": 0,
"type": "timer",
"time": 60,
"startTip": "§e鸡饲料已加满,鸡正在进食…",
"doneTip": "§e鸡饲料已吃完",
"giveItem": None,
},
@@ -175,6 +191,7 @@ ANIMAL_SWITCHES = [
"target": "rtdd:manger_3",
"consume": 32,
"type": "switch",
"tip": "§a小麦饲料已加满",
"giveItem": None,
},
# 【计时减少1】饲料槽_2(满小麦) → 饲料槽_3(半量小麦),计时60秒
@@ -185,6 +202,7 @@ ANIMAL_SWITCHES = [
"consume": 0,
"type": "timer",
"time": 60,
"startTip": "§e小麦饲料已加满,动物正在进食…",
"giveItem": None,
},
# 【计时减少2】饲料槽_3(半量小麦) → 饲料槽_4(少量小麦),计时60秒
@@ -205,6 +223,7 @@ ANIMAL_SWITCHES = [
"consume": 0,
"type": "timer",
"time": 60,
"doneTip": "§e小麦饲料已吃完",
"giveItem": None,
},
@@ -221,6 +240,7 @@ ANIMAL_SWITCHES = [
"type": "switch",
"giveItem": "minecraft:bucket",
"giveCount": 1,
"tip": "§a已给饲料槽加水",
},
# ============================================================
@@ -234,6 +254,7 @@ ANIMAL_SWITCHES = [
"target": "rtdd:pigmanger_1",
"consume": 32,
"type": "switch",
"tip": "§a胡萝卜饲料已加满",
"giveItem": None,
},
# 【计时减少1】饲料槽_7(满胡萝卜) → 饲料槽_8(半量胡萝卜),计时60秒
@@ -244,6 +265,7 @@ ANIMAL_SWITCHES = [
"consume": 0,
"type": "timer",
"time": 60,
"startTip": "§e胡萝卜饲料已加满,动物正在进食…",
"giveItem": None,
},
# 【计时减少2】饲料槽_8(半量胡萝卜) → 饲料槽_1(空),计时60秒
@@ -254,6 +276,7 @@ ANIMAL_SWITCHES = [
"consume": 0,
"type": "timer",
"time": 60,
"doneTip": "§e胡萝卜饲料已吃完",
"giveItem": None,
},
@@ -269,6 +292,7 @@ ANIMAL_SWITCHES = [
"target": "rtdd:feces_2",
"consume": 0,
"type": "switch",
"tip": "§a清理了粪便",
"giveItem": None,
},
# 【清理2】粪便(半坨) → 粪便(仅痕迹)
@@ -278,6 +302,7 @@ ANIMAL_SWITCHES = [
"target": "rtdd:feces_1",
"consume": 0,
"type": "switch",
"tip": "§a清理了粪便",
"giveItem": None,
},
# 【清理3】粪便(仅痕迹) → 消失(空气)
@@ -287,6 +312,7 @@ ANIMAL_SWITCHES = [
"target": "minecraft:air",
"consume": 0,
"type": "switch",
"tip": "§a清理了粪便",
"giveItem": None,
},
]
@@ -35,6 +35,9 @@ comp = serverApi.GetEngineCompFactory() # 组件工厂
from ....BlueFoxTools.SpecialFunciton.BlueFoxSystem import BlueFoxServerSystem
from ....BlueFoxTools.ToolsConfig import LevelId
# ---- 方块实体数据组件(计时持久化:把开始时间戳+时长写进方块,重启后继续计时) ----
blockEntityDataComp = comp.CreateBlockEntityData(LevelId)
# ---- 只导入动物养殖自己的数据库 ----
from ..AnimalData import ANIMAL_SWITCHES
@@ -73,9 +76,6 @@ class AnimalService(BlueFoxServerSystem):
if sw.get("type") == "timer":
self.animalTimers[sw["source"]] = sw
# ---- 计时去重表:key=(dim,x,y,z) -> 上次启动计时的时间戳 ----
self._timerSchedule = {}
# ---- 右键冷却表:防止连点瞬间触发多次 ----
self.cooldown = {}
@@ -185,6 +185,10 @@ class AnimalService(BlueFoxServerSystem):
nextTimer = self.animalTimers[recipe["target"]]
self.startTimer(key, nextTimer, dim, playerId)
# 转换成功提示
if recipe.get("tip"):
comp.CreateMsg(playerId).NotifyOneMessage(playerId, recipe["tip"])
return
# ---- 逆向操作:取东西/清理 ----
@@ -198,43 +202,129 @@ class AnimalService(BlueFoxServerSystem):
if recipe.get("target"):
self.replaceBlock(pos, dim, recipe["target"], playerId)
# 转换成功提示
if recipe.get("tip"):
comp.CreateMsg(playerId).NotifyOneMessage(playerId, recipe["tip"])
# ============================================================================
# 计时型规则处理(AddTimer 一次性定时,不依赖方块实体tick)
# 计时型规则处理(方块实体 tick 持久化计时)
# ============================================================================
def startTimer(self, key, recipe, dim, playerId=None):
"""
为计时型规则启动一次性定时器。
1 秒内的重复调用会被去重(防止多次右键重复计时)。
到时间后由 _timerDone 检查方块状态并执行转换。
"""
now = time.time()
last = self._timerSchedule.get(key, 0)
if now - last < 1.0:
return
self._timerSchedule[key] = now
t = recipe.get("time", 60)
# 用系统自带 AddTimer 一次性定时
self.AddTimer(t, self._timerDone, key, recipe, dim)
def _timerDone(self, key, recipe, dim):
"""
定时器到点回调:
检查当前方块是否仍是计时源方块(防止玩家中途改变了方块),
是则替换为下一状态;若新状态仍是计时源则继续下一级计时。
启动计时:把 计时标识+开始时间戳+时长 写入方块实体数据。
- 已处于本规则计时中的方块不会重复重置时间(防止空手右键刷新计时)
- 到点判断由 FeatureDispatcher 转发的 ServerBlockEntityTickEvent 完成
- 时间戳存在方块上 → 服务器重启/玩家重进后继续计时
"""
pos = (key[1], key[2], key[3])
# 去重:如果该方块已经在本规则计时中,不重置(保持原剩余时间)
try:
data = blockEntityDataComp.GetBlockEntityData(dim, pos)
if data and data.get("rtdd_tid") == recipe["source"]:
return
except Exception:
pass
self._writeTimerData(dim, pos, recipe, playerId)
# 计时开始提示
if playerId and recipe.get("startTip"):
comp.CreateMsg(playerId).NotifyOneMessage(playerId, recipe["startTip"])
def handleBlockEntityTick(self, args):
"""FeatureDispatcher 转发:计时方块每 tick 检查是否到点"""
posDim = args.get("posDim")
if not posDim:
return
dim = posDim[3]
pos = (posDim[0], posDim[1], posDim[2])
self._tickProcess(dim, pos)
def _tickProcess(self, dim, pos):
"""
tick 主逻辑:读方块上记录的计时数据,到点就执行转换。
即便服务器重启过,只要 tick 恢复,时间戳照常生效,
比如烤制进行到 40 秒退出,回来时按原时间戳继续,只剩 20 秒。
"""
try:
data = blockEntityDataComp.GetBlockEntityData(dim, pos)
if not data:
return
tid = data.get("rtdd_tid")
if not tid:
return
recipe = self.animalTimers.get(tid)
if not recipe:
return
tstart = data.get("rtdd_tstart", 0)
tdur = data.get("rtdd_tdur", 0)
if time.time() - tstart >= tdur:
self._finishTimer(dim, pos, recipe, data)
except Exception:
pass
def _finishTimer(self, dim, pos, recipe, data):
"""
计时到点:确认方块仍是计时源后替换方块。
- 若新方块是下一级计时源 → 写入新计时数据继续
- 若整条链结束 → 清除计时数据,并给启动者发送完成提示
"""
try:
cur = comp.CreateBlockInfo(LevelId).GetBlockNew(pos, dim)
if not cur or cur.get("name") != recipe["source"]:
return # 方块已变,放弃本次转换
if cur and cur.get("name") != recipe["source"]:
# 方块已被玩家改变 → 清掉过期计时
self._clearTimerData(dim, pos)
return
# 替换方块
self.replaceBlock(pos, dim, recipe["target"], None)
# 若新方块仍是计时源 → 继续下一级计时(如 满→半→少→空)
nextRecipe = self.animalTimers.get(recipe["target"])
if nextRecipe:
self.startTimer(key, nextRecipe, dim)
# 下一级计时(换新的开始时间戳)
self._writeTimerData(dim, pos, nextRecipe, data.get("rtdd_tp"))
else:
# 整条链结束:清数据 + 完成提示
self._clearTimerData(dim, pos)
doneTip = recipe.get("doneTip")
if doneTip:
self._notifyIfOnline(data.get("rtdd_tp"), doneTip)
except Exception:
pass
# ---- 计时数据读写(存在方块实体数据里,重启不丢) ----
def _writeTimerData(self, dim, pos, recipe, playerId=None):
"""把计时信息写进方块实体数据"""
try:
blockData = blockEntityDataComp.GetBlockEntityData(dim, pos)
if blockData is None:
return
blockData["rtdd_tid"] = recipe["source"]
blockData["rtdd_tstart"] = time.time()
blockData["rtdd_tdur"] = recipe.get("time", 60)
blockData["rtdd_tp"] = playerId if playerId else ""
except Exception:
pass
def _clearTimerData(self, dim, pos):
"""清除方块上的计时数据"""
try:
blockData = blockEntityDataComp.GetBlockEntityData(dim, pos)
if blockData is None:
return
blockData["rtdd_tid"] = ""
blockData["rtdd_tstart"] = 0
blockData["rtdd_tdur"] = 0
blockData["rtdd_tp"] = ""
except Exception:
pass
def _notifyIfOnline(self, playerId, tip):
"""若启动者在线,发送完成提示(离线则跳过)"""
if not tip or not playerId:
return
try:
ids = comp.CreateGame(LevelId).GetAllPlayerIds()
if playerId in ids:
comp.CreateMsg(playerId).NotifyOneMessage(playerId, tip)
except Exception:
pass
@@ -40,6 +40,10 @@ comp = serverApi.GetEngineCompFactory() # 组件工厂
# ---- 继承 BlueFox 框架的系统基类 ----
from ...BlueFoxTools.SpecialFunciton.BlueFoxSystem import BlueFoxServerSystem
from ...BlueFoxTools.ToolsConfig import LevelId
# ---- 方块实体数据组件(计时持久化:记录开始时间戳,重启后继续计时) ----
blockEntityDataComp = comp.CreateBlockEntityData(LevelId)
class FeatureDispatcher(BlueFoxServerSystem):
@@ -95,10 +99,11 @@ class FeatureDispatcher(BlueFoxServerSystem):
def ListenEvent(self):
"""
注册三个事件:
1. ServerBlockUseEvent —— 玩家右键方块(所有功能的右键转换入口)
2. ServerItemUseOnEvent —— 手持物品右键方块表面(如放置生鸡肉)
3. ServerItemTryUseEvent —— 玩家尝试使用物品(拦截喝药水/吃食物等默认行为)
注册四个事件:
1. ServerBlockUseEvent —— 玩家右键方块(所有功能的右键转换入口)
2. ServerItemUseOnEvent —— 手持物品右键方块表面(如放置生鸡肉)
3. ServerItemTryUseEvent —— 玩家尝试使用物品(拦截喝药水/吃食物等默认行为)
4. ServerBlockEntityTickEvent —— 计时方块每tick触发(烤制/饲料变空等,持久化计时)
"""
self.ListenForEvent(serverApi.GetEngineNamespace(),
serverApi.GetEngineSystemName(),
@@ -109,6 +114,9 @@ class FeatureDispatcher(BlueFoxServerSystem):
self.ListenForEvent(serverApi.GetEngineNamespace(),
serverApi.GetEngineSystemName(),
"ServerItemTryUseEvent", self, self.OnItemTryUse)
self.ListenForEvent(serverApi.GetEngineNamespace(),
serverApi.GetEngineSystemName(),
"ServerBlockEntityTickEvent", self, self.OnBlockEntityTick)
def UnListenEvent(self):
self.UnListenForEvent(serverApi.GetEngineNamespace(),
@@ -120,6 +128,9 @@ class FeatureDispatcher(BlueFoxServerSystem):
self.UnListenForEvent(serverApi.GetEngineNamespace(),
serverApi.GetEngineSystemName(),
"ServerItemTryUseEvent", self, self.OnItemTryUse)
self.UnListenForEvent(serverApi.GetEngineNamespace(),
serverApi.GetEngineSystemName(),
"ServerBlockEntityTickEvent", self, self.OnBlockEntityTick)
# ============================================================================
# 主入口1:玩家右键方块
@@ -183,3 +194,35 @@ class FeatureDispatcher(BlueFoxServerSystem):
playerId = args.get("playerId")
if self._cancelItemUse.pop(playerId, False):
args['cancel'] = True
# ============================================================================
# 主入口4:计时方块 tick(持久化计时)
# ============================================================================
def OnBlockEntityTick(self, args):
"""
配置了 tick:true 的方块每帧触发。
用于"烤制/饲料变空"等计时转换的持久化:
- 转换开始时把 开始时间戳+时长 写进方块实体数据
- 服务器重启/玩家重进后,tick 恢复,读回时间戳继续计时,
到点即转换(例如烤鸡烤了40秒退出,回来只剩20秒)
按方块实体数据里的计时标识 rtdd_tid 路由到对应功能服务。
"""
posDim = args.get("posDim")
if not posDim:
return
dim = posDim[3]
pos = (posDim[0], posDim[1], posDim[2])
try:
data = blockEntityDataComp.GetBlockEntityData(dim, pos)
if not data:
return
tid = data.get("rtdd_tid") # 计时标识 = 计时源方块名
except Exception:
return
if not tid:
return
service = self.blockOwner.get(tid) # 计时源方块一定注册在路由表里
if not service:
return
service.handleBlockEntityTick(args)
@@ -48,7 +48,7 @@
"netease:block_entity": {
"client_tick": false,
"movable": false,
"tick": false
"tick": true
}
}
}
@@ -48,7 +48,7 @@
"netease:block_entity": {
"client_tick": false,
"movable": false,
"tick": false
"tick": true
}
}
}
@@ -48,7 +48,7 @@
"netease:block_entity": {
"client_tick": false,
"movable": false,
"tick": false
"tick": true
}
}
}
@@ -48,7 +48,7 @@
"netease:block_entity": {
"client_tick": false,
"movable": false,
"tick": false
"tick": true
}
}
}
@@ -48,7 +48,7 @@
"netease:block_entity": {
"client_tick": false,
"movable": false,
"tick": false
"tick": true
}
}
}
@@ -48,7 +48,7 @@
"netease:block_entity": {
"client_tick": false,
"movable": false,
"tick": false
"tick": true
}
}
}
@@ -48,7 +48,7 @@
"netease:block_entity": {
"client_tick": false,
"movable": false,
"tick": false
"tick": true
}
}
}
@@ -48,7 +48,7 @@
"netease:block_entity": {
"client_tick": false,
"movable": false,
"tick": false
"tick": true
}
}
}
@@ -48,7 +48,7 @@
"netease:block_entity": {
"client_tick": false,
"movable": false,
"tick": false
"tick": true
}
}
}
@@ -48,7 +48,7 @@
"netease:block_entity": {
"client_tick": false,
"movable": false,
"tick": false
"tick": true
}
}
}