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