slot is now an object

This commit is contained in:
əlemi 2022-01-06 19:07:01 +01:00
parent d3e7b6a8f3
commit 41c778729e
2 changed files with 31 additions and 9 deletions

View file

@ -39,6 +39,27 @@ class BlockPos:
( self.z - p.z ) ** 2 ( 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): class Dimension(Enum):
NETHER = -1 NETHER = -1
OVERWORLD = 0 OVERWORLD = 0

View file

@ -8,6 +8,7 @@ import pynbt
from typing import List, Tuple, Dict, Any, Union, Optional, Callable, Type as Class from typing import List, Tuple, Dict, Any, Union, Optional, Callable, Type as Class
from .definitions import Item
class Type(object): class Type(object):
pytype : Union[type, Callable] = lambda x : x 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 } return { k : t.read(buffer, ctx=ctx) for k, t in self.fields }
class SlotType(Type): 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 new_way = ctx._proto > 340
check_type = Boolean if new_way else Short check_type = Boolean if new_way else Short
if data: 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: if new_way:
VarInt.write(data["id"], buffer) VarInt.write(data.id, buffer)
Byte.write(data["count"], buffer) Byte.write(data.count, buffer)
if not new_way: if not new_way:
Short.write(data["damage"], buffer) Short.write(data.damage, buffer)
NBTTag.write(data["nbt"], buffer) # TODO handle None ? NBTTag.write(data.nbt, buffer) # TODO handle None maybe?
else: else:
check_type.write(False if new_way else -1, buffer) check_type.write(False if new_way else -1, buffer)
def read(self, buffer:io.BytesIO, ctx:object=None) -> Any: def read(self, buffer:io.BytesIO, ctx:object=None) -> Any:
slot = {} slot : Dict[Any, Any] = {}
new_way = ctx._proto > 340 new_way = ctx._proto > 340
check_type = Boolean if new_way else Short check_type = Boolean if new_way else Short
val = check_type.read(buffer) val = check_type.read(buffer)
@ -335,7 +336,7 @@ class SlotType(Type):
if not new_way: if not new_way:
slot["damage"] = Short.read(buffer) slot["damage"] = Short.read(buffer)
slot["nbt"] = NBTTag.read(buffer) slot["nbt"] = NBTTag.read(buffer)
return slot return Item(**slot)
Slot = SlotType() Slot = SlotType()