Skip to content

Commit

Permalink
feat(event): add PlayerInteractActorEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Aug 27, 2024
1 parent 4319bda commit c9a4611
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
6 changes: 6 additions & 0 deletions include/endstone/event/block/block_break_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@

namespace endstone {

/**
* @brief Called when a block is broken by a player.
*
* <p>
* 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) {}
Expand Down
54 changes: 54 additions & 0 deletions include/endstone/event/player/player_interact_actor_event.h
Original file line number Diff line number Diff line change
@@ -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
10 changes: 8 additions & 2 deletions src/endstone_runtime/bedrock/world/gamemode/game_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

#include <spdlog/spdlog.h>

#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;
Expand Down Expand Up @@ -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<EndstoneServer>::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);
}

0 comments on commit c9a4611

Please sign in to comment.