Skip to content

Commit

Permalink
feat(block): introduce Block::captureState method for capturing the c…
Browse files Browse the repository at this point in the history
…urrent block state
  • Loading branch information
wu-vincent committed Sep 11, 2024
1 parent e8316ff commit b16893d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions include/endstone/block/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

namespace endstone {

class BlockState;

class Dimension;

/**
Expand Down Expand Up @@ -156,6 +158,16 @@ class Block {
* @return Location of block
*/
[[nodiscard]] virtual Location getLocation() const = 0;

/**
* Captures the current state of this block.
* <p>
* 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<BlockState> captureState() const = 0;
};

} // namespace endstone
Expand Down
1 change: 1 addition & 0 deletions include/endstone/detail/block/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlockState> captureState() const override;

[[nodiscard]] BlockPos getPosition() const;
[[nodiscard]] ::Block &getMinecraftBlock() const;
Expand Down
4 changes: 4 additions & 0 deletions python/src/endstone/_internal/endstone_python.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
7 changes: 7 additions & 0 deletions src/endstone_core/block/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "endstone/detail/block/block.h"

#include <endstone/detail/block/block_state.h>

#include "bedrock/world/level/dimension/dimension.h"
#include "bedrock/world/level/level.h"
#include "endstone/detail/block/block_data.h"
Expand Down Expand Up @@ -140,6 +142,11 @@ Location EndstoneBlock::getLocation() const
return {&getDimension(), getX(), getY(), getZ()};
}

std::shared_ptr<BlockState> EndstoneBlock::captureState() const
{
return std::make_shared<EndstoneBlockState>(*this);
}

BlockPos EndstoneBlock::getPosition() const
{
return block_pos_;
Expand Down
3 changes: 3 additions & 0 deletions src/endstone_python/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ void init_block(py::module_ &m, py::class_<Block> &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); });
}

Expand Down

0 comments on commit b16893d

Please sign in to comment.