slot is now an object
This commit is contained in:
parent
d3e7b6a8f3
commit
41c778729e
2 changed files with 31 additions and 9 deletions
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
Loading…
Reference in a new issue