烧烤 动物养殖 分类 功能实现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

@@ -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