49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
|
|
||
|
from typing import Any
|
||
|
from bta_proxy.datainputstream import DataInputStream
|
||
|
from enum import Enum
|
||
|
from dataclasses import dataclass
|
||
|
|
||
|
from bta_proxy.itemstack import ItemStack
|
||
|
|
||
|
class DataItemType(Enum):
|
||
|
BYTE = 0
|
||
|
SHORT = 1
|
||
|
INTEGER = 2
|
||
|
FLOAT = 3
|
||
|
STRING = 4
|
||
|
ITEMSTACK = 5
|
||
|
CHUNK_COORDINATES = 6
|
||
|
|
||
|
@dataclass
|
||
|
class DataItem:
|
||
|
type: DataItemType
|
||
|
id: int
|
||
|
value: Any
|
||
|
|
||
|
class EntityData:
|
||
|
@classmethod
|
||
|
def read_from(cls, dis: DataInputStream) -> list[DataItem]:
|
||
|
items = []
|
||
|
while (data := dis.read_byte()) != 0x7F:
|
||
|
item_type = DataItemType((data & 0xE0) >> 5)
|
||
|
item_id: int = data & 0x1F
|
||
|
match item_type:
|
||
|
case DataItemType.BYTE:
|
||
|
items.append(DataItem(item_type, item_id, dis.read_byte()))
|
||
|
case DataItemType.SHORT:
|
||
|
items.append(DataItem(item_type, item_id, dis.read_short()))
|
||
|
case DataItemType.FLOAT:
|
||
|
items.append(DataItem(item_type, item_id, dis.read_float()))
|
||
|
case DataItemType.STRING:
|
||
|
items.append(DataItem(item_type, item_id, dis.read_string()))
|
||
|
case DataItemType.ITEMSTACK:
|
||
|
items.append(DataItem(item_type, item_id, ItemStack.read_from(dis)))
|
||
|
case DataItemType.CHUNK_COORDINATES:
|
||
|
x = dis.read_float()
|
||
|
y = dis.read_float()
|
||
|
z = dis.read_float()
|
||
|
items.append(DataItem(item_type, item_id, (x, y, z)))
|
||
|
return items
|
||
|
|