From c9a46110b23dff31aa282d2c534db72233a86656 Mon Sep 17 00:00:00 2001 From: Vincent Date: Tue, 27 Aug 2024 15:47:24 +0100 Subject: [PATCH] feat(event): add PlayerInteractActorEvent --- .../endstone/event/block/block_break_event.h | 6 +++ .../player/player_interact_actor_event.h | 54 +++++++++++++++++++ .../bedrock/world/gamemode/game_mode.cpp | 10 +++- 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 include/endstone/event/player/player_interact_actor_event.h diff --git a/include/endstone/event/block/block_break_event.h b/include/endstone/event/block/block_break_event.h index 262d58755..41a86dea8 100644 --- a/include/endstone/event/block/block_break_event.h +++ b/include/endstone/event/block/block_break_event.h @@ -19,6 +19,12 @@ namespace endstone { +/** + * @brief Called when a block is broken by a player. + * + *

+ * If a BlockBreakEvent is cancelled, the block will not break and experience will not drop. + */ class BlockBreakEvent : public BlockEvent { public: explicit BlockBreakEvent(Block &block, Player &player) : BlockEvent(block), player_(player) {} diff --git a/include/endstone/event/player/player_interact_actor_event.h b/include/endstone/event/player/player_interact_actor_event.h new file mode 100644 index 000000000..d47b956f6 --- /dev/null +++ b/include/endstone/event/player/player_interact_actor_event.h @@ -0,0 +1,54 @@ +// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include "endstone/event/player/player_event.h" + +namespace endstone { + +/** + * @brief Represents an event that is called when a player right-clicks an actor. + */ +class PlayerInteractActorEvent : public PlayerEvent { +public: + explicit PlayerInteractActorEvent(Player &player, Actor &actor) : PlayerEvent(player), actor_(actor) {} + ~PlayerInteractActorEvent() override = default; + + inline static const std::string NAME = "PlayerInteractActorEvent"; + [[nodiscard]] std::string getEventName() const override + { + return NAME; + } + + [[nodiscard]] bool isCancellable() const override + { + return true; + } + + /** + * @brief Gets the actor that was right-clicked by the player. + * + * @return actor right-clicked by player + */ + [[nodiscard]] Actor &getActor() const + { + return actor_; + } + +private: + Actor &actor_; +}; + +} // namespace endstone diff --git a/src/endstone_runtime/bedrock/world/gamemode/game_mode.cpp b/src/endstone_runtime/bedrock/world/gamemode/game_mode.cpp index 211536022..a3bc38270 100644 --- a/src/endstone_runtime/bedrock/world/gamemode/game_mode.cpp +++ b/src/endstone_runtime/bedrock/world/gamemode/game_mode.cpp @@ -16,12 +16,12 @@ #include -#include "bedrock/server/commands/command_utils.h" #include "bedrock/world/actor/player/player.h" #include "endstone/detail/block/block.h" #include "endstone/detail/hook.h" #include "endstone/detail/server.h" #include "endstone/event/block/block_break_event.h" +#include "endstone/event/player/player_interact_actor_event.h" using endstone::detail::EndstoneBlock; using endstone::detail::EndstoneServer; @@ -58,6 +58,12 @@ InteractionResult GameMode::useItemOn(ItemStack &item, BlockPos const &at, Facin bool GameMode::interact(Actor &actor, Vec3 const &location) { - // TODO(event): fire PlayerInteractActorEvent + const auto &server = entt::locator::value(); + auto &player = player_->getEndstonePlayer(); + endstone::PlayerInteractActorEvent e{player, actor.getEndstoneActor()}; + server.getPluginManager().callEvent(e); + if (e.isCancelled()) { + return false; + } return ENDSTONE_HOOK_CALL_ORIGINAL_NAME(&GameMode::interact, __FUNCDNAME__, this, actor, location); }