From bf0d0440aac347fd9cce20fb644a2420e6446143 Mon Sep 17 00:00:00 2001 From: Vincent Date: Fri, 6 Sep 2024 17:53:36 +0100 Subject: [PATCH] feat(block): add block placed to BlockPlaceEvent --- include/endstone/detail/block/block_state.h | 3 ++- .../endstone/event/block/block_place_event.h | 18 ++++++++++++++++-- .../src/endstone/_internal/endstone_python.pyi | 5 +++++ src/endstone_core/block/block_state.cpp | 10 +++++++--- src/endstone_python/event.cpp | 3 +++ .../bedrock/world/item/item.cpp | 6 +++++- 6 files changed, 38 insertions(+), 7 deletions(-) diff --git a/include/endstone/detail/block/block_state.h b/include/endstone/detail/block/block_state.h index 937b41a29..41d6e7137 100644 --- a/include/endstone/detail/block/block_state.h +++ b/include/endstone/detail/block/block_state.h @@ -24,6 +24,7 @@ namespace endstone::detail { class EndstoneBlockState : public BlockState { public: explicit EndstoneBlockState(const EndstoneBlock &block); + explicit EndstoneBlockState(Dimension &dimension_, BlockPos block_pos, ::Block &block); [[nodiscard]] std::unique_ptr getBlock() const override; [[nodiscard]] std::string getType() const override; void setType(std::string type) override; @@ -42,7 +43,7 @@ class EndstoneBlockState : public BlockState { EndstoneDimension &dimension_; BlockSource &block_source_; BlockPos block_pos_; - ::Block *block_; + ::Block *block_; }; } // namespace endstone::detail diff --git a/include/endstone/event/block/block_place_event.h b/include/endstone/event/block/block_place_event.h index ae33d8f8f..c1adc0afc 100644 --- a/include/endstone/event/block/block_place_event.h +++ b/include/endstone/event/block/block_place_event.h @@ -14,6 +14,7 @@ #pragma once +#include "endstone/block/block_state.h" #include "endstone/event/block/block_event.h" #include "endstone/player.h" @@ -26,8 +27,10 @@ namespace endstone { */ class BlockPlaceEvent : public BlockEvent { public: - explicit BlockPlaceEvent(Block &replaced_block, Block &placed_against, Player &player) - : BlockEvent(replaced_block), placed_against_(placed_against), player_(player) + explicit BlockPlaceEvent(std::unique_ptr placed_block, Block &replaced_block, Block &placed_against, + Player &player) + : BlockEvent(replaced_block), placed_block_(std::move(placed_block)), placed_against_(placed_against), + player_(player) { } ~BlockPlaceEvent() override = default; @@ -53,6 +56,16 @@ class BlockPlaceEvent : public BlockEvent { return player_; } + /** + * @brief Gets the BlockState for the block which was placed. + * + * @return The BlockState for the block which was placed. + */ + [[nodiscard]] BlockState &getBlockPlacedState() const + { + return *placed_block_; + } + /** * @brief Gets the block which was replaced. * @@ -74,6 +87,7 @@ class BlockPlaceEvent : public BlockEvent { } private: + std::unique_ptr placed_block_; Block &placed_against_; Player &player_; // TODO(event): add ItemStack item diff --git a/python/src/endstone/_internal/endstone_python.pyi b/python/src/endstone/_internal/endstone_python.pyi index 59988f6e7..767b75f81 100644 --- a/python/src/endstone/_internal/endstone_python.pyi +++ b/python/src/endstone/_internal/endstone_python.pyi @@ -456,6 +456,11 @@ class BlockPlaceEvent(BlockEvent): Gets the block that this block was placed against """ @property + def block_placed_state(self) -> BlockState: + """ + Gets the BlockState for the block which was placed. + """ + @property def block_replaced(self) -> Block: """ Gets the block which was replaced. diff --git a/src/endstone_core/block/block_state.cpp b/src/endstone_core/block/block_state.cpp index b7a82c0b4..a4868bc1b 100644 --- a/src/endstone_core/block/block_state.cpp +++ b/src/endstone_core/block/block_state.cpp @@ -22,9 +22,13 @@ namespace endstone::detail { EndstoneBlockState::EndstoneBlockState(const EndstoneBlock &block) - : dimension_(static_cast(block.getDimension())), - block_source_(dimension_.getHandle().getBlockSourceFromMainChunkSource()), block_pos_(block.getPosition()), - block_(&block.getMinecraftBlock()) + : EndstoneBlockState(block.getDimension(), block.getPosition(), block.getMinecraftBlock()) +{ +} + +EndstoneBlockState::EndstoneBlockState(Dimension &dimension, BlockPos block_pos, ::Block &block) + : dimension_(static_cast(dimension)), + block_source_(dimension_.getHandle().getBlockSourceFromMainChunkSource()), block_pos_(block_pos), block_(&block) { } diff --git a/src/endstone_python/event.cpp b/src/endstone_python/event.cpp index 4d0b050ca..22b119542 100644 --- a/src/endstone_python/event.cpp +++ b/src/endstone_python/event.cpp @@ -105,6 +105,9 @@ void init_event(py::module_ &m, py::class_ &event, py::enum_(m, "BlockPlaceEvent", "Called when a block is placed by a player.") .def_property_readonly("player", &BlockPlaceEvent::getPlayer, py::return_value_policy::reference, "Gets the player who placed the block involved in this event.") + .def_property_readonly("block_placed_state", &BlockPlaceEvent::getBlockPlacedState, + py::return_value_policy::reference, + "Gets the BlockState for the block which was placed.") .def_property_readonly("block_replaced", &BlockPlaceEvent::getBlockReplaced, py::return_value_policy::reference, "Gets the block which was replaced.") .def_property_readonly("block_against", &BlockPlaceEvent::getBlockAgainst, py::return_value_policy::reference, diff --git a/src/endstone_runtime/bedrock/world/item/item.cpp b/src/endstone_runtime/bedrock/world/item/item.cpp index ab7e736e7..b43deb4c3 100644 --- a/src/endstone_runtime/bedrock/world/item/item.cpp +++ b/src/endstone_runtime/bedrock/world/item/item.cpp @@ -20,11 +20,13 @@ #include "bedrock/world/level/block/block.h" #include "endstone/detail/block/block.h" #include "endstone/detail/block/block_face.h" +#include "endstone/detail/block/block_state.h" #include "endstone/detail/server.h" #include "endstone/event/block/block_place_event.h" using endstone::detail::EndstoneBlock; using endstone::detail::EndstoneBlockFace; +using endstone::detail::EndstoneBlockState; using endstone::detail::EndstoneServer; CoordinatorResult Item::_sendTryPlaceBlockEvent(Block const &placement_block, BlockSource const &block_source, @@ -35,10 +37,12 @@ CoordinatorResult Item::_sendTryPlaceBlockEvent(Block const &placement_block, Bl if (actor.isPlayer()) { auto &player = static_cast(actor).getEndstonePlayer(); + auto &dimension = block_source.getDimension().getEndstoneDimension(); + auto block_placed = std::make_unique(dimension, pos, const_cast(placement_block)); const auto block_replaced = EndstoneBlock::at(const_cast(block_source), pos); const auto block_face = static_cast(face); const auto block_against = block_replaced->getRelative(EndstoneBlockFace::getOpposite(block_face)); - endstone::BlockPlaceEvent e{*block_replaced, *block_against, player}; + endstone::BlockPlaceEvent e{std::move(block_placed), *block_replaced, *block_against, player}; server.getPluginManager().callEvent(e); if (e.isCancelled()) { return CoordinatorResult::Deny;