From b16893dc8789e3ba187f9c72c0c43f5ff8f1e972 Mon Sep 17 00:00:00 2001 From: Vincent Date: Thu, 12 Sep 2024 00:23:40 +0100 Subject: [PATCH] feat(block): introduce Block::captureState method for capturing the current block state --- include/endstone/block/block.h | 12 ++++++++++++ include/endstone/detail/block/block.h | 1 + python/src/endstone/_internal/endstone_python.pyi | 4 ++++ src/endstone_core/block/block.cpp | 7 +++++++ src/endstone_python/block.cpp | 3 +++ 5 files changed, 27 insertions(+) diff --git a/include/endstone/block/block.h b/include/endstone/block/block.h index 3504d5d18..e23baff37 100644 --- a/include/endstone/block/block.h +++ b/include/endstone/block/block.h @@ -23,6 +23,8 @@ namespace endstone { +class BlockState; + class Dimension; /** @@ -156,6 +158,16 @@ class Block { * @return Location of block */ [[nodiscard]] virtual Location getLocation() const = 0; + + /** + * Captures the current state of this block. + *

+ * The returned object will never be updated, and you are not guaranteed that (for example) a sign is still a + * sign after you capture its state. + * + * @return BlockState with the current state of this block. + */ + [[nodiscard]] virtual std::shared_ptr captureState() const = 0; }; } // namespace endstone diff --git a/include/endstone/detail/block/block.h b/include/endstone/detail/block/block.h index 0c82de3b9..57cf7cabd 100644 --- a/include/endstone/detail/block/block.h +++ b/include/endstone/detail/block/block.h @@ -37,6 +37,7 @@ class EndstoneBlock : public Block { [[nodiscard]] int getY() const override; [[nodiscard]] int getZ() const override; [[nodiscard]] Location getLocation() const override; + [[nodiscard]] std::shared_ptr captureState() const override; [[nodiscard]] BlockPos getPosition() const; [[nodiscard]] ::Block &getMinecraftBlock() const; diff --git a/python/src/endstone/_internal/endstone_python.pyi b/python/src/endstone/_internal/endstone_python.pyi index 60c38d290..1b26269bb 100644 --- a/python/src/endstone/_internal/endstone_python.pyi +++ b/python/src/endstone/_internal/endstone_python.pyi @@ -319,6 +319,10 @@ class Block: """ def __str__(self) -> str: ... + def capture_state(self) -> BlockState: + """ + Captures the current state of this block. The returned object will never be updated, and you are not guaranteed that (for example) a sign is still a sign after you capture its state. + """ @typing.overload def get_relative(self, offset_x: int, offset_y: int, offset_z: int) -> Block: """ diff --git a/src/endstone_core/block/block.cpp b/src/endstone_core/block/block.cpp index 469e590a5..81f9a7c3f 100644 --- a/src/endstone_core/block/block.cpp +++ b/src/endstone_core/block/block.cpp @@ -14,6 +14,8 @@ #include "endstone/detail/block/block.h" +#include + #include "bedrock/world/level/dimension/dimension.h" #include "bedrock/world/level/level.h" #include "endstone/detail/block/block_data.h" @@ -140,6 +142,11 @@ Location EndstoneBlock::getLocation() const return {&getDimension(), getX(), getY(), getZ()}; } +std::shared_ptr EndstoneBlock::captureState() const +{ + return std::make_shared(*this); +} + BlockPos EndstoneBlock::getPosition() const { return block_pos_; diff --git a/src/endstone_python/block.cpp b/src/endstone_python/block.cpp index 6573c636f..5d6eedfeb 100644 --- a/src/endstone_python/block.cpp +++ b/src/endstone_python/block.cpp @@ -78,6 +78,9 @@ void init_block(py::module_ &m, py::class_ &block) .def_property_readonly("y", &Block::getY, "Gets the y-coordinate of this block") .def_property_readonly("z", &Block::getZ, "Gets the z-coordinate of this block") .def_property_readonly("location", &Block::getLocation, "Gets the Location of the block") + .def("capture_state", &Block::captureState, + "Captures the current state of this block. The returned object will never be updated, and you are not " + "guaranteed that (for example) a sign is still a sign after you capture its state.") .def("__str__", [](const Block &self) { return fmt::format("{}", self); }); }