Skip to content
This repository has been archived by the owner on May 8, 2023. It is now read-only.

Commit

Permalink
Chunk.get_tile_entity
Browse files Browse the repository at this point in the history
  • Loading branch information
matcool committed Oct 7, 2020
1 parent e6eb5e9 commit 8edbc12
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion anvil/chunk.py
@@ -1,4 +1,4 @@
from typing import Union, Tuple, Generator
from typing import Union, Tuple, Generator, Optional
from nbt import nbt
from .block import Block, OldBlock
from .region import Region
Expand Down Expand Up @@ -45,6 +45,8 @@ class Chunk:
Version of the chunk NBT structure
data: :class:`nbt.TAG_Compound`
Raw NBT data of the chunk
tile_entities: :class:`nbt.TAG_Compound`
``self.data['TileEntities']`` as an attribute for easier use
"""
__slots__ = ('version', 'data', 'x', 'z')

Expand All @@ -53,6 +55,7 @@ def __init__(self, nbt_data: nbt.NBTFile):
self.data = nbt_data['Level']
self.x = self.data['xPos'].value
self.z = self.data['zPos'].value
self.tile_entities = self.data['TileEntities']

def get_section(self, y: int) -> nbt.TAG_Compound:
"""
Expand Down Expand Up @@ -339,6 +342,17 @@ def stream_chunk(self, index: int=0, section: Union[int, nbt.TAG_Compound]=None)
for block in self.stream_blocks(section=section):
yield block

def get_tile_entity(self, x: int, y: int, z: int) -> Optional[nbt.TAG_Compound]:
"""
Returns the tile entity at given coordinates, or ``None`` if there isn't a tile entity
To iterate through all tile entities in the chunk, use :class:`Chunk.tile_entities`
"""
for tile_entity in self.tile_entities:
t_x, t_y, t_z = [tile_entity[k].value for k in 'xyz']
if x == t_x and y == t_y and z == t_z:
return tile_entity

@classmethod
def from_region(cls, region: Union[str, Region], chunkX: int, chunkZ: int):
"""
Expand Down

0 comments on commit 8edbc12

Please sign in to comment.