Handle entity metadata

This commit is contained in:
2020-09-23 00:00:28 -06:00
parent 97f248b0c6
commit 43f4eb3517
4 changed files with 79 additions and 5 deletions
+40 -2
View File
@@ -4,7 +4,8 @@ import struct
from minecraft.networking.types.basic import (
Type, Byte, Short, Integer, Long, Float, Double,
ShortPrefixedByteArray, Boolean, VarInt, TrailingByteArray
ShortPrefixedByteArray, Boolean, VarInt, TrailingByteArray,
Position, String, UnsignedByte
)
from minecraft.networking.types.utility import Vector
@@ -39,6 +40,8 @@ class Nbt(Type):
@staticmethod
def read(file_object):
type_id = Byte.read(file_object)
if type_id == TAG_End:
return None
if type_id != TAG_Compound:
raise Exception("Invalid NBT header")
name = ShortPrefixedByteArray.read(file_object).decode('utf-8')
@@ -118,7 +121,8 @@ class Slot(Type):
present = Boolean.read(file_object)
item_id = VarInt.read(file_object) if present else None
item_count = Byte.read(file_object) if present else None
nbt = TrailingByteArray.read(file_object) if present else None
print('slot read', present, item_id, item_count)
nbt = Nbt.read(file_object) if present else None
return Slot(present, item_id, item_count, nbt)
@staticmethod
@@ -127,3 +131,37 @@ class Slot(Type):
VarInt.send(value.item_id, socket)
Byte.send(value.item_count, socket)
TrailingByteArray.send(value.nbt, socket)
class Entry(Type):
types = {
0: Byte,
1: VarInt,
2: Float,
3: String,
6: Slot,
7: Boolean,
9: Position,
}
def __init__(self, index, type, value):
self.index = index
self.type = type
self.value = value
def __str__(self):
return str(self.__dict__)
def __repr__(self):
return 'Entry(index={}, type={}, value={}'.format(
self.index, self.type, self.value)
@staticmethod
def read(file_object):
index = UnsignedByte.read(file_object)
if index == 0xff: return None
type = VarInt.read(file_object)
try:
value = Entry.types[type].read(file_object)
except KeyError:
return None
return Entry(index, type, value)