gdpc.editor¶
Provides the Editor class, which provides high-level functions to interact with the
Minecraft world through the GDMC HTTP interface.
- class Editor¶
Provides high-level functions to interact with the Minecraft world through the GDMC HTTP interface.
Stores various settings, resources, buffers and caches related to world interaction, and a transform that defines a local coordinate system.
-
__init__(transformLike: TransformLike | None =
None, dimension: str | None =None, buffering: bool =False, bufferLimit: int =1024, caching: bool =False, cacheLimit: int =8192, multithreading: bool =False, multithreadingWorkers: int =1, retries: int =4, timeout: float | None =None, host: str ='http://localhost:9000')¶ Constructs an Editor instance with the specified transform and settings.
All settings specified here can be accessed and modified through properties with the same name. For more information on each setting, see the documentation for the corresponding property.
- __del__() None¶
Cleans up this Editor instance.
Flushes the block buffer and waits for any remaining buffer flush futures.
- property transform : Transform¶
This editor’s local coordinate transform.
Many of
Editor’s methods have apositionparameter. In most cases, the passed position vector is interpreted as relative to the coordinate system defined by this property. For example,editor.placeBlock(pos, block)would placeblockateditor.transform * pos. Changing this property allows you to change the “point of view” of the editor.For a more comprehensive overview of GDPC’s transformation system, see here.
- property dimension : str | None¶
The Minecraft dimension this editor interacts with.
Can be set to any string, though specifying an invalid dimension may cause other
Editormethods to raise exceptions. See the documentation of the GDMC HTTP interface mod for the full list of options. The following options are certainly supported:"overworld""the_nether""the_end""nether""end"
Changing the dimension will flush the block buffer and invalidate all caches.
Note that
transformis NOT reset or modified when the dimension is changed! In particular, the transform’s translation (if any) is NOT adjusted for the nether’s 8x smaller scale.
- property buffering : bool¶
Whether block placement buffering is enabled.
When buffering is enabled, blocks passed to
placeBlock()/placeBlockGlobal()are not placed immediately, but are instead added to a buffer. When the buffer is full (bufferLimit), all blocks are then placed at once, using a single HTTP request.The block retrieval functions (such as
getBlock()) take the buffer into account: if the buffer contains a block at the specified position, that block is returned. If both buffering and caching (seecaching) are enabled, the buffer supersedes the cache for block retrieval.Note that, if the Minecraft world is changed by something other than the buffering
Editor(for example, by a player on the server) after blocks have been buffered but before they have been placed, then the buffered blocks may overwrite those changes.To manually flush the buffer, call
flushBuffer().
- property caching : bool¶
Whether caching placed and retrieved blocks is enabled.
When caching is enabled, the last
cacheLimitplaced and retrieved blocks and their positions are cached in thisEditorobject. If a block at a cached position is accessed (such as withgetBlock()), the block is retrieved from the cache, and no HTTP request is sent.If both buffering (see
buffering) and caching are enabled, the buffer supersedes the cache for block retrieval.Note that, if the Minecraft world is modified by something other than the caching
Editor(for example, by a player on the server), the cache will not reflect those changes, and retrieved blocks may be incorrect.
- property doBlockUpdates : bool¶
Whether placed blocks receive a block update.
When a player places a block in Minecraft, that block and its surrounding blocks receive a block update.* This triggers effects such as sand blocks falling and fences adjusting their shape.
If this setting is
True, blocks placed withplaceBlock()/placeBlockGlobal()will be given a block update, just like if they were placed by a player. We recommend leaving this on in most cases.*: The system is actually a bit more complex than this. See https://minecraft.wiki/w/Block_update for the full details.
- property spawnDrops : bool¶
Whether overwritten blocks drop items.
If
True, overwritten blocks drop as items (as if they were destroyed with a Silk Touch* tool). Blocks with an inventory (e.g. chests) will drop their inventory as well.Note that if this is
Falsebut :attr:.doBlockUpdates` isTrue, overwriting a block may still cause a different block to break and drop as an item. For example, if a block with a torch on its side is removed, the torch will drop.
- property retries : int¶
The amount of retries for requests to the GDMC HTTP interface.
If a request to the interface fails, it will be retried this many times (1 + this value in total) before an exception is thrown.
- property timeout : float | None¶
The timeout for requests to the GDMC HTTP interface
Behaves as described by the requests package).
- property host : str¶
The address (hostname+port) of the GDMC HTTP interface to use.
Changing the host will flush the buffer and invalidate all caches.
Note that the
transformis NOT reset or modified when the host is changed!
- property worldSlice : WorldSlice | None¶
The cached WorldSlice (see
loadWorldSlice()).
- property worldSliceDecay : ndarray[tuple[int, ...], dtype[bool]] | None¶
3D boolean array indicating whether the block at the specified position in the cached worldSlice is still valid.
Note that the lowest Y-layer is at
[:,0,:], despite Minecraft’s negative Y coordinates. IfworldSliceisNone, this property will also beNone.
-
runCommand(command: str, position: Vec3iLike | None =
None, syncWithBuffer: bool =False) None¶ Executes one or multiple Minecraft commands (separated by newlines).
The leading “/” must be omitted.
If buffering is enabled and
syncWithBufferisTrue, the command is deferred until after the next buffer flush. This can be useful if the command depends on possibly buffered block changes.If
positionis provided, the command’s execution position is set toposition, wherepositionis interpreted as local totransform. This means that, for example, the position “~ ~ ~” in the command will correspond toposition. Note however that any rotation or flip performed bytransformis NOT reflected in the command’s execution context! This means that the use of local coordinate offsets (e.g. “^1 ^2 ^3”) is in general not safe. For more details about relative and local command coordinates, see https://minecraft.wiki/Coordinates#Commands
-
runCommandGlobal(command: str, position: Vec3iLike | None =
None, syncWithBuffer: bool =False) None¶ Executes one or multiple Minecraft commands (separated by newlines), ignoring
transform.The leading “/” must be omitted.
If buffering is enabled and
syncWithBufferisTrue, the command is deferred until after the next buffer flush. This can be useful if the command depends on possibly buffered block changes.If
positionis provided, the command’s execution position is set toposition, ignoringtransform. This means that, for example, the position “~ ~ ~” in the command will correspond toposition. For more details about relative and local command coordinates, see https://minecraft.wiki/Coordinates#Commands
- getBuildArea() Box¶
Returns the build area that was specified by
/setbuildareain-game.The build area is always in global coordinates;
transformis ignored.
- setBuildArea(buildArea: Box) Box¶
Sets the build area to
buildArea, and returns it.The build area must be given in global coordinates;
transformis ignored.
- getBlock(position: Vec3iLike) Block¶
Returns the block at
position.positionis interpreted as local to the coordinate system defined bytransform. The returned block’s orientation is also from the perspective oftransform.If the given coordinates are invalid, returns
Block("minecraft:void_air").
- getBlockGlobal(position: Vec3iLike) Block¶
Returns the block at
position, ignoringtransform.If the given coordinates are invalid, returns
Block("minecraft:void_air").
- getBiome(position: Vec3iLike) str¶
Returns the biome at
position.positionis interpreted as local to the coordinate system defined bytransform.If the given coordinates are invalid, returns an empty string.
- getBiomeGlobal(position: Vec3iLike) str¶
Returns the biome at
position, ignoringtransform.If the given coordinates are invalid, returns an empty string.
-
placeBlock(position: Vec3iLike | Iterable[Vec3iLike], block: Block | Sequence[Block], replace: str | list[str] | None =
None) bool¶ Places
blockatposition.positionis interpreted as local to the coordinate system defined bytransform.If
positionis iterable (e.g. a list),blockis placed at all positions. This is more efficient than calling this method in a loop.If
blockis a sequence (e.g. a list), blocks are sampled randomly.Returns whether the placement succeeded fully.
-
placeBlockGlobal(position: Vec3iLike | Iterable[Vec3iLike], block: Block | Sequence[Block], replace: str | Iterable[str] | None =
None) bool¶ Places
blockatposition, ignoringtransform.If
positionis iterable (e.g. a list),blockis placed at all positions. In this case, buffering is temporarily enabled for better performance.If
blockis a sequence (e.g. a list), blocks are sampled randomly.Returns whether the placement succeeded fully.
- flushBuffer() None¶
Flushes the block placement buffer.
If multithreaded buffer flushing is enabled, the worker threads can be awaited with
awaitBufferFlushes().
-
awaitBufferFlushes(timeout: float | None =
None) None¶ Awaits all pending buffer flushes.
If
timeoutis notNone, waits for at mosttimeoutseconds.Does nothing if no buffer flushes have occured while multithreaded buffer flushing was enabled.
-
loadWorldSlice(rect: Rect | None =
None, heightmapTypes: Iterable[str] | None =None, cache: bool =False) WorldSlice¶ Loads the world slice for the given XZ-rectangle.
The rectangle must be given in global coordinates;
transformis ignored.If
rectis None, the world slice of the current build area is loaded.If
heightmapTypesis None, all heightmaps are loaded.If
cacheisTrue, the loaded worldSlice is cached in this editor. It can then be accessed throughworldSlice. If a world slice was already cached, it is replaced. The cached world slice is used for faster block and biome retrieval. Note that the editor assumes that nothing besides itself changes the given area of the world. If the given world area is changed other than through this editor, callupdateWorldSlice()to update the cached world slice.
- updateWorldSlice() WorldSlice¶
Updates the cached world slice.
Loads and caches new world slice for the same area and with the same heightmaps as the currently cached one. Raises a
RuntimeErrorif no world slice is cached.
- checkConnection() None¶
Raises an
InterfaceConnectionErrorif the GDMC HTTP interface cannot be reached.Does not perform any retries.
-
pushTransform(transformLike: TransformLike | None =
None) Generator[None, None, None]¶ Creates a context that reverts all changes to
transformon exit. IftransformLikeis notNone, it is pushed totransformon enter.Can be used to create a local coordinate system on top of the current local coordinate system.
Not to be confused with
Transform.push()!
-
__init__(transformLike: TransformLike | None =