配方 替换方块ceshi3

This commit is contained in:
wenhe committed 2026-07-21 15:00:50 +08:00
1 parent 2f79f08b97
commit 0354b1dd47
1 file changed
+32 -29
@@ -23,11 +23,11 @@ comp = serverApi.GetEngineCompFactory()
# 从配方数据文件导入所有配方
from ..RecipeData import RECIPES
def _getUserDataField(itemDict, *keys):
2
def getUserDataField(itemDict, *keys):
"""
读取物品 dict 里的 userData 嵌套字段(纯本地操作,不调API)。
例如 _getUserDataField(itemDict, 'rtdd', 'oil') → 返回 itemDict['userData']['rtdd']['oil']
例如 getUserDataField(itemDict, 'rtdd', 'oil') → 返回 itemDict['userData']['rtdd']['oil']
如果中间任何一层的 key 不存在,自动创建空 dict。
"""
itemDict['userData'] = itemDict.get('userData', {})
@@ -96,21 +96,21 @@ class CookingService(ServerSystem):
dim = comp.CreateDimension(playerId).GetEntityDimensionId()
key = (dim, x, y, z)
itemDict = self._getCarriedItem(playerId)
itemDict = self.getCarriedItem(playerId)
recipe = self._findRecipe(blockName, itemDict)
recipe = self.findRecipe(blockName, itemDict)
if not recipe:
return
if recipe.get("stirCount"):
self._handleStir(key, recipe, playerId, pos, dim, itemDict)
self.handleStir(key, recipe, playerId, pos, dim, itemDict)
return
self._executeRecipe(recipe, playerId, pos, dim, itemDict)
self.executeRecipe(recipe, playerId, pos, dim, itemDict)
# ==================== 配方匹配 ====================
def _findRecipe(self, blockName, itemDict):
def findRecipe(self, blockName, itemDict):
itemName = itemDict.get("newItemName") if itemDict else None
for recipe in RECIPES:
if recipe["source"] != blockName:
@@ -122,30 +122,30 @@ class CookingService(ServerSystem):
# ==================== 配方执行(普通替换) ====================
def _executeRecipe(self, recipe, playerId, pos, dim, itemDict):
def executeRecipe(self, recipe, playerId, pos, dim, itemDict):
# 1. 检查附加条件
if not self._checkConditions(recipe, playerId, pos, dim, itemDict):
if not self.checkConditions(recipe, playerId, pos, dim, itemDict):
return
# 2. 消耗物品
if recipe.get("consume", 1) > 0:
self._consumeItem(playerId, itemDict, recipe.get("consume", 1))
self.consumeItem(playerId, itemDict, recipe.get("consume", 1))
if recipe.get("needOil"):
self._removeOil(itemDict, playerId)
self.removeOil(itemDict, playerId)
# 3. 替换方块
if recipe.get("target"):
self._replaceBlock(pos, dim, recipe["target"], playerId)
self.replaceBlock(pos, dim, recipe["target"], playerId)
# 4. 给物品
if recipe.get("giveItem"):
self._giveItem(playerId, recipe["giveItem"])
self.giveItem(playerId, recipe["giveItem"])
# 5. 播放声音(需要配合 BlueFox 框架的客户端 PlaySound 事件,此处暂不实现)
# ==================== 翻炒处理 ====================
def _handleStir(self, key, recipe, playerId, pos, dim, itemDict):
def handleStir(self, key, recipe, playerId, pos, dim, itemDict):
now = time.time()
data = self.stirData.get(key) or {
"count": 0, "lastTime": 0.0, "finished": False,
@@ -172,12 +172,12 @@ class CookingService(ServerSystem):
if recipe.get("doneMsg"):
comp.CreateMsg(playerId).NotifyOneMessage(playerId, recipe["doneMsg"])
elif recipe.get("target"):
self._replaceBlock(pos, dim, recipe["target"], playerId)
self.replaceBlock(pos, dim, recipe["target"], playerId)
self.stirData.pop(key, None)
# ==================== 条件检查 ====================
def _checkConditions(self, recipe, playerId, pos, dim, itemDict):
def checkConditions(self, recipe, playerId, pos, dim, itemDict):
# 检查下面是否有指定方块
if recipe.get("below"):
belowPos = (pos[0], pos[1] - 1, pos[2])
@@ -191,20 +191,20 @@ class CookingService(ServerSystem):
# 检查物品是否有油标签
if recipe.get("needOil") and itemDict:
if not self._getItemOil(itemDict):
if not self.getItemOil(itemDict):
return False
return True
# ==================== 工具方法 ====================
def _getCarriedItem(self, playerId):
def getCarriedItem(self, playerId):
"""获取玩家主手拿的物品"""
itemComp = comp.CreateItem(playerId)
return itemComp.GetPlayerItem(
serverApi.GetMinecraftEnum().ItemPosType.CARRIED, 0)
def _consumeItem(self, playerId, itemDict, amount=1):
def consumeItem(self, playerId, itemDict, amount=1):
"""
消耗玩家手里的物品(减少数量)。
注意:SetEntityItem 接收的 key 是 itemName/auxValue/count,
@@ -222,29 +222,32 @@ class CookingService(ServerSystem):
"count": itemDict["count"],
"auxValue": itemDict.get("newAuxValue", 0)})
def _replaceBlock(self, pos, dim, newBlockName, playerId):
"""替换方块"""
def replaceBlock(self, pos, dim, newBlockName, playerId):
"""替换方块(对照 BlockBlock.py 标准写法:命名参数 + 带 aux)"""
comp.CreateBlockInfo(playerId).SetBlockNew(
pos, {"name": newBlockName}, 0, dim)
pos,
blockDict={"name": newBlockName, "aux": 0},
oldBlockHandling=0,
dimensionId=dim)
def _giveItem(self, playerId, itemName, count=1):
def giveItem(self, playerId, itemName, count=1):
"""给玩家背包里塞一个物品"""
comp.CreateItem(playerId).SpawnItemToPlayerInv(
{"itemName": itemName, "count": count, "auxValue": 0}, playerId)
# ==================== 油标签操作(纯本地 dict 操作,不调 API) ====================
def _getItemOil(self, itemDict):
def getItemOil(self, itemDict):
"""读取物品上的油标签"""
try:
return _getUserDataField(itemDict, 'rtdd').get('oil', False)
return getUserDataField(itemDict, 'rtdd').get('oil', False)
except:
return False
def _removeOil(self, itemDict, playerId):
def removeOil(self, itemDict, playerId):
"""去掉物品上的油标签,并同步到玩家手里"""
try:
_getUserDataField(itemDict, 'rtdd')['oil'] = False
getUserDataField(itemDict, 'rtdd')['oil'] = False
# 更新玩家手里的物品(userData 跟着 itemDict 一起写回去)
itemComp = comp.CreateItem(playerId)
itemComp.SetEntityItem(
@@ -279,6 +282,6 @@ class CookingService(ServerSystem):
dropItems = data.get("dropIfNot", [])
for itemName in dropItems:
self._giveItem(playerId, itemName)
self.giveItem(playerId, itemName)
self.stirData.pop(key, None)