2023-08-24 17:40:23 +03:00
|
|
|
|
|
|
|
from typing import Any
|
2023-08-25 23:16:51 +03:00
|
|
|
from bta_proxy.datainputstream import AsyncDataInputStream, SyncDataInputStream
|
2023-08-24 17:40:23 +03:00
|
|
|
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
|
2023-08-25 21:49:10 +03:00
|
|
|
async def read_from(cls, dis: AsyncDataInputStream) -> list[DataItem]:
|
2023-08-24 17:40:23 +03:00
|
|
|
items = []
|
2023-08-25 21:49:10 +03:00
|
|
|
while (data := await dis.read()) != 0x7F:
|
2023-08-26 01:12:19 +03:00
|
|
|
print(f"========= EntityData.read_from {data=} ({(data & 0xE0) >> 5})")
|
2023-08-24 17:40:23 +03:00
|
|
|
item_type = DataItemType((data & 0xE0) >> 5)
|
|
|
|
item_id: int = data & 0x1F
|
|
|
|
match item_type:
|
|
|
|
case DataItemType.BYTE:
|
2023-08-25 21:49:10 +03:00
|
|
|
items.append(DataItem(item_type, item_id, await dis.read()))
|
2023-08-24 17:40:23 +03:00
|
|
|
case DataItemType.SHORT:
|
2023-08-25 21:49:10 +03:00
|
|
|
items.append(DataItem(item_type, item_id, await dis.read_short()))
|
2023-08-24 17:40:23 +03:00
|
|
|
case DataItemType.FLOAT:
|
2023-08-25 21:49:10 +03:00
|
|
|
items.append(DataItem(item_type, item_id, await dis.read_float()))
|
2023-08-24 17:40:23 +03:00
|
|
|
case DataItemType.STRING:
|
2023-08-25 21:49:10 +03:00
|
|
|
items.append(DataItem(item_type, item_id, await dis.read_string()))
|
2023-08-24 17:40:23 +03:00
|
|
|
case DataItemType.ITEMSTACK:
|
2023-08-25 21:49:10 +03:00
|
|
|
items.append(DataItem(item_type, item_id, await ItemStack.read_from(dis)))
|
2023-08-24 17:40:23 +03:00
|
|
|
case DataItemType.CHUNK_COORDINATES:
|
2023-08-25 21:49:10 +03:00
|
|
|
x = await dis.read_float()
|
|
|
|
y = await dis.read_float()
|
|
|
|
z = await dis.read_float()
|
2023-08-24 17:40:23 +03:00
|
|
|
items.append(DataItem(item_type, item_id, (x, y, z)))
|
|
|
|
return items
|
|
|
|
|
2023-08-25 23:16:51 +03:00
|
|
|
@classmethod
|
|
|
|
def read_from_sync(cls, dis: SyncDataInputStream) -> list[DataItem]:
|
|
|
|
items = []
|
|
|
|
while (data := dis.read()) != 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()))
|
|
|
|
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_sync(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
|
|
|
|
|