173 lines
5.1 KiB
Python
173 lines
5.1 KiB
Python
# coding=utf-8
|
|
import copy
|
|
|
|
from ToolsConfig import *
|
|
|
|
|
|
def IsItemDict(itemDict):
|
|
"""
|
|
是否为有效的物品字典
|
|
:param itemDict:
|
|
:return:
|
|
"""
|
|
if itemDict and (
|
|
not isinstance(itemDict, dict) or itemDict.get('newItemName') is None or
|
|
itemDict.get('newAuxValue') is None or itemDict['newItemName'] == 'minecraft:air'
|
|
):
|
|
return False
|
|
return True
|
|
|
|
|
|
def GetItemUserData(itemDict, *args):
|
|
"""
|
|
获取物品的userData,若不含有相应的键,则创建
|
|
:param itemDict: 物品字典
|
|
:param args: 键值key
|
|
:return:
|
|
"""
|
|
itemDict['userData'] = itemDict.get('userData', {})
|
|
if itemDict.get('userData') is None:
|
|
itemDict['userData'] = {}
|
|
ver = itemDict['userData']
|
|
for key in args:
|
|
ver[key] = ver.get(key, {})
|
|
ver = ver[key]
|
|
return ver
|
|
|
|
|
|
def CreateItemDict(itemName, aux=0, count=1):
|
|
"""
|
|
快速生成最基本的物品字典
|
|
"""
|
|
itemInfo = GetItemBaseInfoSafely({'newItemName': itemName, 'newAuxValue': aux})
|
|
return {
|
|
'newItemName': itemName, 'newAuxValue': aux, 'count': count, 'durability': itemInfo.get('maxDurability', 0)
|
|
}
|
|
|
|
|
|
def GetItemBaseInfoSafely(itemDict):
|
|
"""
|
|
安全获取物品基本信息,网易接口对于不存在的物品ID_AUX获取为None
|
|
:param itemDict:
|
|
:return:
|
|
"""
|
|
default = None
|
|
try:
|
|
if itemDict:
|
|
default = clientCompFactory.CreateItem(LevelId).GetItemBasicInfo(
|
|
itemDict['newItemName'], itemDict['newAuxValue'],
|
|
(itemDict.get('enchantData', []) + itemDict.get('modEnchantData', [])).__len__() > 0
|
|
) or serverCompFactory.CreateItem(LevelId).GetItemBasicInfo(
|
|
itemDict['newItemName'], itemDict['newAuxValue'],
|
|
(itemDict.get('enchantData', []) + itemDict.get('modEnchantData', [])).__len__() > 0
|
|
)
|
|
except:
|
|
pass
|
|
default = default if default else {
|
|
'foodNutrition': 0, 'maxDurability': 0, 'armorDefense': 0, 'tierDict': None,
|
|
'itemType': '', 'fuelDuration': 0.0, 'foodSaturation': 0.0,
|
|
'armorKnockbackResistance': 0.0, 'customItemType': '', 'itemTierLevel': -1,
|
|
'maxStackSize': 64, 'weaponDamage': 0, 'itemName': '',
|
|
'itemCategory': 'construction', 'id_aux': 0, 'armorToughness': 0
|
|
}
|
|
return default
|
|
|
|
|
|
def DeclareItem(itemDict):
|
|
# type:(Dict) -> Tuple[Dict,Dict]
|
|
"""
|
|
减少物品一个,如果个数为0则会返回空dict
|
|
:return:减少后的物品字典,独立出来的物品
|
|
"""
|
|
if not itemDict:
|
|
return itemDict, {}
|
|
itemDict['count'] -= 1
|
|
newItem = copy.deepcopy(itemDict)
|
|
newItem['count'] = 1
|
|
if itemDict['count'] > 0:
|
|
return itemDict, newItem
|
|
else:
|
|
return {}, newItem
|
|
|
|
|
|
def DeclareItemAnyNum(itemDict, num):
|
|
# type:(Dict,int) -> Tuple[Dict,Dict]
|
|
"""
|
|
减少物品多个,如果个数为0则会返回空dict
|
|
:return:减少后的物品字典,独立出来的物品
|
|
"""
|
|
if not isinstance(itemDict, dict): return {}, {}
|
|
if not IsItemDict(itemDict): return {}, {}
|
|
if not itemDict: return {}, itemDict
|
|
if itemDict['count'] < num: return {}, itemDict
|
|
itemDict['count'] -= num
|
|
newItem = copy.deepcopy(itemDict)
|
|
newItem['count'] = num
|
|
if itemDict['count'] > 0:
|
|
return itemDict, newItem
|
|
else:
|
|
return {}, newItem
|
|
|
|
|
|
def StackItems(item1, item2):
|
|
# type:(Dict,Dict) -> Dict
|
|
"""
|
|
使item1与item2堆叠在一起,返回多余的物品
|
|
"""
|
|
if not item1 or not item2: return {}
|
|
if not IsSameItemAndCanStack(item1, item2): return {}
|
|
itemInfo = GetItemBaseInfoSafely(item1)
|
|
maxCount = itemInfo['maxStackSize']
|
|
count1 = item1['count']
|
|
count2 = item2['count']
|
|
t_count = count1 + count2 - maxCount
|
|
t_item = {}
|
|
if t_count > 0:
|
|
t_item = copy.deepcopy(item1)
|
|
t_item['count'] = t_count
|
|
item1['count'] = maxCount
|
|
else:
|
|
item1['count'] += item2['count']
|
|
return t_item
|
|
|
|
|
|
def IsSameItemAndCanStack(item1, item2):
|
|
# type:(Dict,Dict) -> bool
|
|
"""
|
|
判断item1是否能与item2堆叠在一起
|
|
"""
|
|
if not item1 or not item2:
|
|
return True
|
|
if IsSameItem(item1, item2):
|
|
itemInfo = GetItemBaseInfoSafely(item1)
|
|
return item1['count'] + item2['count'] <= itemInfo['maxStackSize']
|
|
return False
|
|
|
|
|
|
def IsSameItem(item1, item2, args=('newItemName', 'newAuxValue', 'durability')):
|
|
"""
|
|
判断item1是否与item2相同.目前判断物品ID,AUX,耐久度
|
|
"""
|
|
if item1 and item2:
|
|
same = True
|
|
for key in args: same = same and item1.get(key, 0) == item2.get(key, 0)
|
|
return same
|
|
else:
|
|
return False
|
|
|
|
|
|
def GetItemDurabilityPercent(itemDict, itemInfo=None):
|
|
"""
|
|
获取耐久值百分比与总值
|
|
:param itemDict:物品信息字典
|
|
:param itemInfo:物品基础信息字典
|
|
:return:
|
|
"""
|
|
if itemInfo is None:
|
|
itemInfo = GetItemBaseInfoSafely(itemDict)
|
|
return (
|
|
(itemDict.get('durability', 0.0) / float(itemInfo['maxDurability']))
|
|
if itemInfo['maxDurability'] != 0 else 0.0,
|
|
1.0 if itemInfo['maxDurability'] != 0 else 0.0
|
|
)
|