From 41c778729efde1868ae62318efb1853756312040 Mon Sep 17 00:00:00 2001 From: alemidev Date: Thu, 6 Jan 2022 19:07:01 +0100 Subject: [PATCH] slot is now an object --- aiocraft/mc/definitions.py | 21 +++++++++++++++++++++ aiocraft/mc/types.py | 19 ++++++++++--------- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/aiocraft/mc/definitions.py b/aiocraft/mc/definitions.py index 8fecd36..efdb3d0 100644 --- a/aiocraft/mc/definitions.py +++ b/aiocraft/mc/definitions.py @@ -39,6 +39,27 @@ class BlockPos: ( self.z - p.z ) ** 2 ) +class Item: + id : int + count : int + nbt : dict + damage : int # This got removed past 1.12.2 + + def __init__(self, item:Item = None): + if item: + self.id = item.id + self.count = item.count + self.nbt = item.nbt + self.damage = item.damage + + def __getitem__(self, key:str): # backwards compatibility + return getattr(self, key) + + @property + def durability(self) -> int: + # TODO make a map of durability for each item and subtract damage? + return self.damage + class Dimension(Enum): NETHER = -1 OVERWORLD = 0 diff --git a/aiocraft/mc/types.py b/aiocraft/mc/types.py index 38e822f..4170ab3 100644 --- a/aiocraft/mc/types.py +++ b/aiocraft/mc/types.py @@ -8,6 +8,7 @@ import pynbt from typing import List, Tuple, Dict, Any, Union, Optional, Callable, Type as Class +from .definitions import Item class Type(object): pytype : Union[type, Callable] = lambda x : x @@ -305,24 +306,24 @@ class StructType(Type): # TODO sub objects return { k : t.read(buffer, ctx=ctx) for k, t in self.fields } class SlotType(Type): - pytype : type = dict + pytype : type = Item - def write(self, data:Any, buffer:io.BytesIO, ctx:object=None): + def write(self, data:Item, buffer:io.BytesIO, ctx:object=None): new_way = ctx._proto > 340 check_type = Boolean if new_way else Short if data: - check_type.write(True if new_way else data["id"], buffer) + check_type.write(True if new_way else data.id, buffer) if new_way: - VarInt.write(data["id"], buffer) - Byte.write(data["count"], buffer) + VarInt.write(data.id, buffer) + Byte.write(data.count, buffer) if not new_way: - Short.write(data["damage"], buffer) - NBTTag.write(data["nbt"], buffer) # TODO handle None ? + Short.write(data.damage, buffer) + NBTTag.write(data.nbt, buffer) # TODO handle None maybe? else: check_type.write(False if new_way else -1, buffer) def read(self, buffer:io.BytesIO, ctx:object=None) -> Any: - slot = {} + slot : Dict[Any, Any] = {} new_way = ctx._proto > 340 check_type = Boolean if new_way else Short val = check_type.read(buffer) @@ -335,7 +336,7 @@ class SlotType(Type): if not new_way: slot["damage"] = Short.read(buffer) slot["nbt"] = NBTTag.read(buffer) - return slot + return Item(**slot) Slot = SlotType()