Skip to content

Commit

Permalink
refactor: use WeakEntityRef in event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Jan 22, 2025
1 parent b8c54ef commit f762b1e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 56 deletions.
14 changes: 7 additions & 7 deletions src/bedrock/world/events/player_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ struct PlayerDamageEvent {
std::unique_ptr<ActorDamageSource> damage_source;
};
struct PlayerOpenContainerEvent {
WeakRef<EntityContext> player;
WeakEntityRef player;
ContainerType container_type;
const BlockPos block_pos;
ActorUniqueID entity_unique_id;
Expand Down Expand Up @@ -181,27 +181,27 @@ struct PlayerFormCloseEvent {
PlayerFormCloseReason form_close_reason;
};
struct PlayerDisconnectEvent {
WeakRef<EntityContext> player;
WeakEntityRef player;
};
struct PlayerSkinLoadedClientEvent {
WeakRef<EntityContext> player;
WeakEntityRef player;
};
struct PlayerGameModeChangeEvent {
WeakRef<EntityContext> player;
WeakEntityRef player;
GameType from_game_mode;
GameType to_game_mode;
};
struct PlayerInputPermissionCategoryChangeEvent {
WeakRef<EntityContext> player;
WeakEntityRef player;
ClientInputLockCategory category;
bool state;
};
struct PlayerEmoteEvent {
WeakRef<EntityContext> player;
WeakEntityRef player;
std::string emote_piece_id;
};
struct PlayerScriptInputEvent {
WeakRef<EntityContext> player;
WeakEntityRef player;
ScriptingInputButton button;
ScriptingInputButtonState new_state;
};
Expand Down
36 changes: 15 additions & 21 deletions src/endstone/core/event/handlers/block_gameplay_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ bool EndstoneBlockGameplayHandler::handleEvent(const PistonActionEvent &event)

bool EndstoneBlockGameplayHandler::handleEvent(const BlockTryPlaceByPlayerEvent &event)
{
const StackResultStorageEntity stack_result(event.player);
const auto *entity = ::Actor::tryGetFromEntity(stack_result.getStackRef(), false);
const auto *entity = ::Actor::tryGetFromEntity(event.player.unwrap(), false);
if (!entity || !entity->isPlayer()) {
return true;
}
Expand Down Expand Up @@ -117,8 +116,7 @@ bool EndstoneBlockGameplayHandler::handleEvent(const BlockTryPlaceByPlayerEvent

bool EndstoneBlockGameplayHandler::handleEvent(ExplosionStartedEvent &event)
{
const StackResultStorageEntity stack_result(event.source);
const auto *source = ::Actor::tryGetFromEntity(stack_result.getStackRef(), false);
const auto *source = WeakEntityRef(event.source).tryUnwrap<::Actor>();
const auto &server = entt::locator<EndstoneServer>::value();

std::vector<std::shared_ptr<Block>> block_list;
Expand All @@ -131,6 +129,7 @@ bool EndstoneBlockGameplayHandler::handleEvent(ExplosionStartedEvent &event)
return true;
}
}

if (source) {
auto &actor = source->getEndstoneActor<>();
ActorExplodeEvent e{actor, actor.getLocation(), block_list};
Expand All @@ -153,24 +152,19 @@ bool EndstoneBlockGameplayHandler::handleEvent(ExplosionStartedEvent &event)

bool EndstoneBlockGameplayHandler::handleEvent(BlockTryDestroyByPlayerEvent &event)
{
const StackResultStorageEntity stack_result(event.player);
const auto *entity = ::Actor::tryGetFromEntity(stack_result.getStackRef(), false);
if (!entity || !entity->isPlayer()) {
return true;
}

const auto &server = entt::locator<EndstoneServer>::value();
auto &player = entity->getEndstoneActor<EndstonePlayer>();
auto &block_source = player.getHandle().getDimension().getBlockSourceFromMainChunkSource();
if (const auto block = EndstoneBlock::at(block_source, event.pos)) {
BlockBreakEvent e{block.value(), player};
server.getPluginManager().callEvent(e);
if (e.isCancelled()) {
return false;
if (const auto *player = WeakEntityRef(event.player).tryUnwrap<::Player>(); player) {
const auto &server = entt::locator<EndstoneServer>::value();
auto &block_source = player->getDimension().getBlockSourceFromMainChunkSource();
if (const auto block = EndstoneBlock::at(block_source, event.pos)) {
BlockBreakEvent e{block.value(), player->getEndstoneActor<EndstonePlayer>()};
server.getPluginManager().callEvent(e);
if (e.isCancelled()) {
return false;
}
}
else {
server.getLogger().error(block.error());
}
}
else {
server.getLogger().error(block.error());
}
return true;
}
Expand Down
3 changes: 1 addition & 2 deletions src/endstone/core/event/handlers/level_gameplay_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ GameplayHandlerResult<CoordinatorResult> EndstoneLevelGameplayHandler::handleEve

bool EndstoneLevelGameplayHandler::handleEvent(const LevelAddedActorEvent &event)
{
const StackResultStorageEntity stack_result(event.actor);
if (auto *actor = ::Actor::tryGetFromEntity(stack_result.getStackRef(), false); actor && !actor->isPlayer()) {
if (auto *actor = WeakEntityRef(event.actor).tryUnwrap<::Actor>(); actor && !actor->isPlayer()) {
const auto &server = entt::locator<EndstoneServer>::value();
ActorSpawnEvent e{actor->getEndstoneActor()};
server.getPluginManager().callEvent(e);
Expand Down
36 changes: 14 additions & 22 deletions src/endstone/core/event/handlers/player_gameplay_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,35 +82,31 @@ GameplayHandlerResult<CoordinatorResult> EndstonePlayerGameplayHandler::handleEv

bool EndstonePlayerGameplayHandler::handleEvent(const PlayerFormResponseEvent &event)
{
const StackResultStorageEntity stack_result(event.player);
if (const auto *actor = ::Actor::tryGetFromEntity(stack_result.getStackRef(), false); actor && actor->isPlayer()) {
actor->getEndstoneActor<EndstonePlayer>().onFormResponse(event.form_id, event.form_response);
if (auto *player = WeakEntityRef(event.player).tryUnwrap<::Player>(); player) {
player->getEndstoneActor<EndstonePlayer>().onFormResponse(event.form_id, event.form_response);
}
return true;
}

bool EndstonePlayerGameplayHandler::handleEvent(const PlayerFormCloseEvent &event)
{
const StackResultStorageEntity stack_result(event.player);
if (const auto *actor = ::Actor::tryGetFromEntity(stack_result.getStackRef(), false); actor && actor->isPlayer()) {
actor->getEndstoneActor<EndstonePlayer>().onFormClose(event.form_id, event.form_close_reason);
if (auto *player = WeakEntityRef(event.player).tryUnwrap<::Player>(); player) {
player->getEndstoneActor<EndstonePlayer>().onFormClose(event.form_id, event.form_close_reason);
}
return true;
}

bool EndstonePlayerGameplayHandler::handleEvent(const PlayerInteractWithBlockBeforeEvent &event)
{
const StackResultStorageEntity stack_result(event.player);
if (const auto *actor = ::Actor::tryGetFromEntity(stack_result.getStackRef(), false); actor && actor->isPlayer()) {
if (auto *player = WeakEntityRef(event.player).tryUnwrap<::Player>(); player) {
const auto &server = entt::locator<EndstoneServer>::value();
auto &player = actor->getEndstoneActor<EndstonePlayer>();
auto &block_source = player.getHandle().getDimension().getBlockSourceFromMainChunkSource();
auto &block_source = player->getDimension().getBlockSourceFromMainChunkSource();
if (auto block = EndstoneBlock::at(block_source, BlockPos(event.block_location))) {
const std::shared_ptr<EndstoneItemStack> item_stack =
event.item.isNull() ? nullptr : EndstoneItemStack::fromMinecraft(event.item);

PlayerInteractEvent e{
player,
player->getEndstoneActor<EndstonePlayer>(),
item_stack,
block.value(),
static_cast<BlockFace>(event.block_face),
Expand All @@ -130,13 +126,10 @@ bool EndstonePlayerGameplayHandler::handleEvent(const PlayerInteractWithBlockBef

bool EndstonePlayerGameplayHandler::handleEvent(const PlayerInteractWithEntityBeforeEvent &event)
{
const StackResultStorageEntity stack_result_player(event.player);
const auto *player = ::Actor::tryGetFromEntity(stack_result_player.getStackRef(), false);
const auto *player = WeakEntityRef(event.player).tryUnwrap<::Player>();
const auto *target = WeakEntityRef(event.target_entity).tryUnwrap<::Actor>();

const StackResultStorageEntity stack_result_target(event.target_entity);
const auto *target = ::Actor::tryGetFromEntity(stack_result_target.getStackRef(), false);

if (player && player->isPlayer() && target) {
if (player && target) {
const auto &server = entt::locator<EndstoneServer>::value();
PlayerInteractActorEvent e{player->getEndstoneActor<EndstonePlayer>(), target->getEndstoneActor()};
server.getPluginManager().callEvent(e);
Expand All @@ -149,19 +142,18 @@ bool EndstonePlayerGameplayHandler::handleEvent(const PlayerInteractWithEntityBe

bool EndstonePlayerGameplayHandler::handleEvent(::PlayerGameModeChangeEvent &event)
{
const StackResultStorageEntity stack_result(event.player);
if (const auto *actor = ::Actor::tryGetFromEntity(stack_result.getStackRef(), false); actor && actor->isPlayer()) {
if (auto *player = event.player.tryUnwrap<::Player>(); player) {
const auto &server = entt::locator<EndstoneServer>::value();
PlayerGameModeChangeEvent e{actor->getEndstoneActor<EndstonePlayer>(),
PlayerGameModeChangeEvent e{player->getEndstoneActor<EndstonePlayer>(),
EndstoneGameMode::fromMinecraft(event.to_game_mode)};
server.getPluginManager().callEvent(e);
if (e.isCancelled()) {
const auto packet = MinecraftPackets::createPacket(MinecraftPacketIds::UpdatePlayerGameType);
const auto pk = std::static_pointer_cast<UpdatePlayerGameTypePacket>(packet);
pk->player_game_type = event.from_game_mode;
pk->target_player = actor->getOrCreateUniqueID();
pk->target_player = player->getOrCreateUniqueID();
pk->tick = 0;
if (const auto *component = actor->tryGetComponent<ReplayStateComponent>(); component) {
if (const auto *component = player->tryGetComponent<ReplayStateComponent>(); component) {
pk->tick = component->getCurrentTick();
}
server.getServer().getPacketSender().sendBroadcast(*pk);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#include <variant>

#include "bedrock/locale/i18n.h"
#include "endstone/core/server.h"
#include "endstone/event/player/player_chat_event.h"

Expand Down Expand Up @@ -45,9 +44,8 @@ GameplayHandlerResult<CoordinatorResult> EndstoneServerNetworkEventHandler::hand
bool EndstoneServerNetworkEventHandler::handleEvent(ChatEvent &event)
{
const auto &server = entt::locator<EndstoneServer>::value();
const StackResultStorageEntity stack_result(event.sender);
if (const auto *actor = ::Actor::tryGetFromEntity(stack_result.getStackRef(), false); actor) {
PlayerChatEvent e{actor->getEndstoneActor<EndstonePlayer>(), event.message};
if (auto *player = WeakEntityRef(event.sender).tryUnwrap<::Player>(); player) {
PlayerChatEvent e{player->getEndstoneActor<EndstonePlayer>(), event.message};
server.getPluginManager().callEvent(e);
if (e.isCancelled()) {
return false;
Expand Down

0 comments on commit f762b1e

Please sign in to comment.