胡萝卜牛肉汤 翻炒存储 测试成功

This commit is contained in:
wenhe committed 2026-07-22 16:03:10 +08:00
1 parent 87991d5dcf
commit 82a72e5e2a
1 file changed
+39 -61
@@ -14,8 +14,6 @@
serverApi.RegisterSystem("rtdd", "CookingService",
"Script_NeteaseModousIY5aSF.ceshi.service.CookingService.CookingService")
"""
import os
import json
import time
import mod.server.extraServerApi as serverApi
@@ -26,6 +24,9 @@ comp = serverApi.GetEngineCompFactory()
from ...BlueFoxTools.SpecialFunciton.BlueFoxSystem import BlueFoxServerSystem
from ...BlueFoxTools.ToolsConfig import LevelId
# 方块实体数据操作组件(和老系统 Function.py 用的同一个 LevelId,操作同一份数据)
blockEntityDataComp = comp.CreateBlockEntityData(LevelId)
# 从配方数据文件导入所有配方
from ..RecipeData import RECIPES
@@ -106,8 +107,11 @@ class CookingService(BlueFoxServerSystem):
# 从内存或方块数据加载翻炒进度
stirInfo = self.stirData.get(key, {})
if stirInfo:
self.stirData[key] = stirInfo
if not stirInfo:
# 内存里没有,尝试从方块实体数据加载(服务器重启后恢复数据)
stirInfo = self.loadStirData(playerId, pos, dim, recipe)
if stirInfo:
self.stirData[key] = stirInfo
if recipe.get("needFinished") is True and not stirInfo.get("finished"):
return
@@ -273,76 +277,50 @@ class CookingService(BlueFoxServerSystem):
except:
pass
# ==================== 翻炒数据持久化 ====================
def _getDataFile(self):
"""获取持久化数据文件路径"""
# 获取模组脚本目录
scriptDir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
return os.path.join(scriptDir, "rtdd_cooking_data.json")
def _loadFileData(self):
"""从文件加载所有翻炒数据"""
filePath = self._getDataFile()
if os.path.exists(filePath):
try:
with open(filePath, "r") as f:
return json.load(f)
except:
pass
return {}
def _saveFileData(self, allData):
"""保存所有翻炒数据到文件"""
filePath = self._getDataFile()
try:
with open(filePath, "w") as f:
json.dump(allData, f)
except:
pass
def _makeKey(self, pos, dim):
"""把方块坐标拼成一个字符串 key,如 '0_100_64_200'"""
return "%s_%s_%s_%s" % (dim, pos[0], pos[1], pos[2])
# ==================== 翻炒数据持久化(存方块里,不存JSON文件) ====================
def loadStirData(self, playerId, pos, dim, recipe):
"""
从方块的实体数据里读取翻炒进度。
注意:GetBlockEntityData 返回的是 BlockEntityData 对象,不是普通 dict,
必须用方括号 [] 取值,不能用 .get()。
"""
try:
key = self._makeKey(pos, dim)
allData = self._loadFileData()
blockData = allData.get(key, {})
if blockData and "rtdd_sc" in blockData:
return {
"count": blockData.get("rtdd_sc", 0),
"lastTime": blockData.get("rtdd_slt", 0.0),
"finished": blockData.get("rtdd_sf", False),
"dropIfDone": recipe.get("dropIfDone", []),
"dropIfNot": recipe.get("dropIfNot", []),
}
blockData = blockEntityDataComp.GetBlockEntityData(dim, pos)
stirCount = blockData["rtdd_sc"]
return {
"count": stirCount,
"lastTime": blockData["rtdd_slt"],
"finished": blockData["rtdd_sf"],
"dropIfDone": recipe.get("dropIfDone", []),
"dropIfNot": recipe.get("dropIfNot", []),
}
except:
pass
return {}
return {}
def saveStirData(self, playerId, pos, dim, data):
"""
把翻炒进度写入方块的实体数据。
用方括号 [] 直接赋值,网易API会自动保存。
"""
try:
key = self._makeKey(pos, dim)
allData = self._loadFileData()
allData[key] = {
"rtdd_sc": data["count"],
"rtdd_slt": data["lastTime"],
"rtdd_sf": data["finished"],
}
self._saveFileData(allData)
blockData = blockEntityDataComp.GetBlockEntityData(dim, pos)
blockData["rtdd_sc"] = data["count"]
blockData["rtdd_slt"] = data["lastTime"]
blockData["rtdd_sf"] = data["finished"]
except:
pass
def clearStirData(self, key, playerId, pos, dim):
"""
清除方块上的翻炒数据(方块被替换或拆掉时调用)。
"""
self.stirData.pop(key, None)
try:
dataKey = self._makeKey(pos, dim)
allData = self._loadFileData()
if dataKey in allData:
del allData[dataKey]
self._saveFileData(allData)
blockData = blockEntityDataComp.GetBlockEntityData(dim, pos)
blockData["rtdd_sc"] = 0
blockData["rtdd_slt"] = 0.0
blockData["rtdd_sf"] = False
except:
pass