chore!: add serialize/deserialize to Item

This commit is contained in:
əlemi 2022-07-02 23:59:11 +02:00
parent 14da27e52e
commit 5cff6bc208
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E

View file

@ -118,8 +118,8 @@ class BlockPos:
class Item: class Item:
id : int id : int
count : int count : int
nbt : dict nbt : dict # TODO
damage : int # This got removed past 1.12.2 damage : Optional[int] # This got removed past 1.12.2
def __init__(self, item:'Item' = None, id:int=0, count:int=1, nbt:dict=None, damage:int=0): def __init__(self, item:'Item' = None, id:int=0, count:int=1, nbt:dict=None, damage:int=0):
self.id = id self.id = id
@ -132,7 +132,7 @@ class Item:
self.nbt = item.nbt self.nbt = item.nbt
self.damage = item.damage self.damage = item.damage
def as_dict(self) -> dict: def serialize(self) -> dict:
return { return {
'id': self.id, 'id': self.id,
'count': self.count, 'count': self.count,
@ -140,13 +140,22 @@ class Item:
'damage': self.damage, 'damage': self.damage,
} }
@classmethod
def deserialize(cls, data:dict) -> 'Item':
return cls(
id=data["id"],
count=data["count"],
nbt=data["nbt"],
damage=data["damage"]
)
def __getitem__(self, key:str): # backwards compatibility def __getitem__(self, key:str): # backwards compatibility
return getattr(self, key) return getattr(self, key)
@property @property
def durability(self) -> int: def durability(self) -> int:
# TODO make a map of durability for each item and subtract damage? # TODO make a map of durability for each item and subtract damage?
return self.damage return self.damage or -1
class Enchantment: class Enchantment:
eid : int eid : int