Skip to content

Commit

Permalink
Define common types for entity coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
SmylerMC committed Aug 3, 2024
1 parent 70501f7 commit f234aac
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions litemapy/minecraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
from typing import Any, Optional, Union


EntityPosition = tuple[float, float, float]
EntityRotation = tuple[float, float]
EntityMotion = tuple[float, float, float]

BlockPosition = tuple[int, int, int]


class BlockState:
"""
Represents an in-game block.
Expand Down Expand Up @@ -157,9 +164,9 @@ class Entity:
"""

_data: Compound
_position: tuple[float, float, float]
_rotation: tuple[float, float]
_motion: tuple[float, float, float]
_position: EntityPosition
_rotation: EntityRotation
_motion: EntityMotion

# TODO Needs unit tests

Expand Down Expand Up @@ -259,38 +266,38 @@ def id(self, id: str) -> None:
self._data['id'] = String(self._id)

@property
def position(self) -> tuple[float, float, float]:
def position(self) -> EntityPosition:
"""
The position of the entity.
"""
return self._position

@position.setter
def position(self, position: tuple[float, float, float]) -> None:
def position(self, position: EntityPosition) -> None:
self._position = position
self._data['Pos'] = List[Double]([Double(coord) for coord in self._position])

@property
def rotation(self) -> tuple[float, float]:
def rotation(self) -> EntityRotation:
"""
The rotation of the entity.
"""
return self._rotation

@rotation.setter
def rotation(self, rotation: tuple[float, float]) -> None:
def rotation(self, rotation: EntityRotation) -> None:
self._rotation = rotation
self._data['Rotation'] = List[Double]([Double(coord) for coord in self._rotation])

@property
def motion(self) -> tuple[float, float, float]:
def motion(self) -> EntityMotion:
"""
The velocity vector of the entity.
"""
return self._motion

@motion.setter
def motion(self, motion: tuple[float, float, float]) -> None:
def motion(self, motion: EntityMotion) -> None:
self._motion = motion
self._data['Motion'] = List[Double]([Double(coord) for coord in self._motion])

Expand All @@ -306,7 +313,7 @@ class TileEntity:
The ID can be inferred by looking up the :class:`BlockState` as the same position in the :class:`Region`.
"""
_data: Compound
_position: tuple[int, int, int]
_position: BlockPosition

def __init__(self, nbt: Compound) -> None:
# TODO Not documented because it only exposes NBT
Expand Down Expand Up @@ -369,14 +376,14 @@ def data(self, data: Compound):
self._position = (position[0], position[1], position[2])

@property
def position(self) -> tuple[int, int, int]:
def position(self) -> BlockPosition:
"""
The tile entity's position within the :class:`Region`/
"""
return self._position

@position.setter
def position(self, position: tuple[int, int, int]):
def position(self, position: BlockPosition):
self._position = position
for coord, index in [('x', 0), ('y', 1), ('z', 2)]:
self._data[coord] = Int(self._position[index])
Expand Down

0 comments on commit f234aac

Please sign in to comment.