API Reference¶
Block¶
-
class
anvil.
Block
(namespace, block_id=None, properties=None)¶ Represents a minecraft block.
-
__init__
(namespace, block_id=None, properties=None)¶
-
classmethod
from_name
(name, *args, **kwargs)¶ Creates a new Block from the format:
namespace:block_id
- Parameters
name (
str
) – Block in said format
:param : Will be passed on to the main constructor :param args: Will be passed on to the main constructor :param kwargs: Will be passed on to the main constructor
-
classmethod
from_numeric_id
(block_id, data=0)¶ Creates a new Block from the block_id:data fromat used pre-flattening (pre-1.13)
-
classmethod
from_palette
(tag)¶ Creates a new Block from the tag format on Section.Palette
- Parameters
tag (
TAG_Compound
) – Raw tag from a section’s palette
-
-
class
anvil.
OldBlock
(block_id, data=0)¶ Represents a pre 1.13 minecraft block, with a numeric id.
Region¶
-
class
anvil.
Region
(data)¶ Read-only region
-
chunk_data
(chunk_x, chunk_z)¶ Returns the NBT data for a chunk
-
chunk_location
(chunk_x, chunk_z)¶ Returns the chunk offset in the 4KiB sectors from the start of the file, and the length of the chunk in sectors of 4KiB
Will return
(0, 0)
if chunk hasn’t been generated yet
-
classmethod
from_file
(file)¶ Creates a new region with the data from reading the given file
-
get_chunk
(chunk_x, chunk_z)¶ Returns the chunk at given coordinates, same as doing
Chunk.from_region(region, chunk_x, chunk_z)
- Parameters
- Return type
-
Chunk¶
-
class
anvil.
Chunk
(nbt_data)¶ Represents a chunk from a
.mca
file.Note that this is read only.
-
data
¶ Raw NBT data of the chunk
- Type
nbt.TAG_Compound
-
tile_entities
¶ self.data['TileEntities']
as an attribute for easier use- Type
nbt.TAG_Compound
-
classmethod
from_region
(region, chunk_x, chunk_z)¶ Creates a new chunk from region and the chunk’s X and Z
- Parameters
region (
Union
[str
,Region
]) – Either aanvil.Region
or a region file name (liker.0.0.mca
)- Raises
anvil.ChunkNotFound – If a chunk is outside this region or hasn’t been generated yet
-
get_block
(x, y, z, section=None, force_new=False)¶ Returns the block in the given coordinates
- Parameters
x (int) – Block’s coordinates in the chunk
y (
int
) – Block’s coordinates in the chunkz (
int
) – Block’s coordinates in the chunksection (int) – Either a section NBT tag or an index. If no section is given, assume Y is global and use it for getting the section.
force_new (
bool
) – Always returns an instance of Block if True, otherwise returns type OldBlock for pre-1.13 versions. Defaults to False
- Raises
anvil.OutOfBoundCoordidnates – If X, Y or Z are not in the proper range
:rtype –
anvil.Block
:
- Return type
Union
[Block
,OldBlock
]
-
get_palette
(section)¶ Returns the block palette for given section
- Parameters
section (
Union
[int
,TAG_Compound
]) – Either a section NBT tag or an index- Return type
Tuple[
anvil.Block
]
-
get_section
(y)¶ Returns the section at given y index can also return nothing if section is missing, aka it’s empty
- Parameters
y (
int
) – Section Y index- Raises
anvil.OutOfBoundsCoordinates – If Y is not in range of 0 to 15
- Return type
TAG_Compound
-
get_tile_entity
(x, y, z)¶ Returns the tile entity at given coordinates, or
None
if there isn’t a tile entityTo iterate through all tile entities in the chunk, use
Chunk.tile_entities
- Return type
Optional
[TAG_Compound
]
-
for ... in
stream_blocks
(index=0, section=None, force_new=False)¶ Returns a generator for all the blocks in given section
- Parameters
index (
int
) –At what block to start from.
To get an index from (x, y, z), simply do:
y * 256 + z * 16 + x
section (
Union
[int
,TAG_Compound
,None
]) – Either a Y index or a section NBT tag.force_new (
bool
) – Always returns an instance of Block if True, otherwise returns type OldBlock for pre-1.13 versions. Defaults to False
- Raises
anvil.OutOfBoundCoordidnates – If section is not in the range of 0 to 15
- Yields
- Return type
Generator
[Block
,None
,None
]
-
Empty¶
Region¶
-
class
anvil.
EmptyRegion
(x, z)¶ Used for making own regions
-
chunks
¶ List of chunks in this region
- Type
List[
anvil.EmptyChunk
]
-
add_chunk
(chunk)¶ Adds given chunk to this region. Will overwrite if a chunk already exists in this location
- Parameters
chunk (
EmptyChunk
) –- Raises
anvil.OutOfBoundCoordidnates – If the chunk (x, z) is not inside this region
-
add_section
(section, x, z, replace=True)¶ Adds section to chunk at (x, z). Same as
EmptyChunk.add_section(section)
- Parameters
section (
EmptySection
) – Section to addx (int) – Chunk’s coordinate
z (
int
) – Chunk’s coordinatereplace (
bool
) – Whether to replace section if it already exists in the chunk
- Raises
anvil.OutOfBoundsCoordinates – If the chunk (x, z) is not inside this region
-
fill
(block, x1, y1, z1, x2, y2, z2, ignore_outside=False)¶ Fills in blocks from
(x1, y1, z1)
to(x2, y2, z2)
in a rectangle.- Parameters
- Raises
anvil.OutOfBoundsCoordinates – If any of the coordinates are outside the region
-
get_chunk
(x, z)¶ Returns the chunk at given chunk coordinates
- Parameters
- Raises
anvil.OutOfBoundCoordidnates – If the chunk (x, z) is not inside this region
:rtype –
anvil.EmptyChunk
:
- Return type
EmptyChunk
-
inside
(x, y, z, chunk=False)¶ Returns if the given coordinates are inside this region
-
save
(file=None)¶ Returns the region as bytes with the anvil file format structure, aka the final
.mca
file.
-
set_block
(block, x, y, z)¶ Sets block at given coordinates. New chunk is made if it doesn’t exist.
-
Chunk¶
-
class
anvil.
EmptyChunk
(x, z)¶ Used for making own chunks
-
sections
¶ List of all the sections in this chunk
- Type
List[
anvil.EmptySection
]
-
add_section
(section, replace=True)¶ Adds a section to the chunk
- Parameters
section (
EmptySection
) – Section to addreplace (
bool
) – Whether to replace section if one at same Y already exists
- Raises
anvil.EmptySectionAlreadyExists – If
replace
isFalse
and section with same Y already exists in this chunk
-
get_block
(x, y, z)¶ Gets the block at given coordinates
- Parameters
- Raises
anvil.OutOfBoundCoordidnates – If X, Y or Z are not in the proper range
- Returns
block – Returns
None
if the section is empty, meaning the block is most likely an air block.- Return type
anvil.Block
or None
-
save
()¶ Saves the chunk data to a
NBTFile
Notes
Does not contain most data a regular chunk would have, but minecraft stills accept it.
- Return type
NBTFile
-
Section¶
-
class
anvil.
EmptySection
(y)¶ Used for making own sections.
This is where the blocks are actually stored, in a 16³ sized array. To save up some space,
None
is used instead of the air block object, and will be replaced withself.air
when needed-
blockstates
(palette=None)¶ Returns a list of each block’s index in the palette.
This is used in the BlockStates tag of the section.
-
get_block
(x, y, z)¶ Gets the block at given coordinates.
-
staticmethod
inside
(x, y, z)¶ Check if X Y and Z are in range of 0-15
-
palette
()¶ Generates and returns a tuple of all the different blocks in the section The order can change as it uses sets, but should be fine when saving since it’s only called once.
- Return type
Tuple
[Block
]
-
save
()¶ Saves the section to a TAG_Compound and is used inside the chunk tag This is missing the SkyLight tag, but minecraft still accepts it anyway
- Return type
TAG_Compound
-
-
class
anvil.
RawSection
(y, blocks, palette)¶ Same as
EmptySection
but you manually set the palette and the blocks array (which instead ofBlock
, it’s indexes on the palette)-
blockstates
(palette=None)¶ Refer to
EmptySection.blockstates()
- Return type
-
save
()¶ Refer to
EmptySection.save()
- Return type
TAG_Compound
-
Errors¶
-
exception
anvil.errors.
ChunkNotFound
¶ Bases:
Exception
Exception used for when a chunk was not found
-
exception
anvil.errors.
EmptySectionAlreadyExists
¶ Bases:
Exception
Exception used for when trying to add an EmptySection to an EmptyChunk and the chunk already has a section with the same Y
-
exception
anvil.errors.
GZipChunkData
¶ Bases:
Exception
Exception used when trying to get chunk data compressed in gzip
-
exception
anvil.errors.
OutOfBoundsCoordinates
¶ Bases:
ValueError
Exception used for when coordinates are out of bounds