Skip to content

Commit

Permalink
feat(block): add block placed to BlockPlaceEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Sep 6, 2024
1 parent e5c631c commit bf0d044
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 7 deletions.
3 changes: 2 additions & 1 deletion include/endstone/detail/block/block_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Block> getBlock() const override;
[[nodiscard]] std::string getType() const override;
void setType(std::string type) override;
Expand All @@ -42,7 +43,7 @@ class EndstoneBlockState : public BlockState {
EndstoneDimension &dimension_;
BlockSource &block_source_;
BlockPos block_pos_;
::Block *block_;
::Block *block_;
};

} // namespace endstone::detail
18 changes: 16 additions & 2 deletions include/endstone/event/block/block_place_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#pragma once

#include "endstone/block/block_state.h"
#include "endstone/event/block/block_event.h"
#include "endstone/player.h"

Expand All @@ -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<BlockState> 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;
Expand All @@ -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.
*
Expand All @@ -74,6 +87,7 @@ class BlockPlaceEvent : public BlockEvent {
}

private:
std::unique_ptr<BlockState> placed_block_;
Block &placed_against_;
Player &player_;
// TODO(event): add ItemStack item
Expand Down
5 changes: 5 additions & 0 deletions python/src/endstone/_internal/endstone_python.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 7 additions & 3 deletions src/endstone_core/block/block_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
namespace endstone::detail {

EndstoneBlockState::EndstoneBlockState(const EndstoneBlock &block)
: dimension_(static_cast<EndstoneDimension &>(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<EndstoneDimension &>(dimension)),
block_source_(dimension_.getHandle().getBlockSourceFromMainChunkSource()), block_pos_(block_pos), block_(&block)
{
}

Expand Down
3 changes: 3 additions & 0 deletions src/endstone_python/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ void init_event(py::module_ &m, py::class_<Event> &event, py::enum_<EventPriorit
py::class_<BlockPlaceEvent, BlockEvent>(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,
Expand Down
6 changes: 5 additions & 1 deletion src/endstone_runtime/bedrock/world/item/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -35,10 +37,12 @@ CoordinatorResult Item::_sendTryPlaceBlockEvent(Block const &placement_block, Bl

if (actor.isPlayer()) {
auto &player = static_cast<const Player &>(actor).getEndstonePlayer();
auto &dimension = block_source.getDimension().getEndstoneDimension();
auto block_placed = std::make_unique<EndstoneBlockState>(dimension, pos, const_cast<Block &>(placement_block));
const auto block_replaced = EndstoneBlock::at(const_cast<BlockSource &>(block_source), pos);
const auto block_face = static_cast<endstone::BlockFace>(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;
Expand Down

0 comments on commit bf0d044

Please sign in to comment.