Skip to content

Commit

Permalink
refactor: PlayerQuitEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Jan 25, 2025
1 parent 0102824 commit 2baf0e9
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 76 deletions.
1 change: 0 additions & 1 deletion src/bedrock/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ add_library(bedrock STATIC
resources/resource_pack_manager.cpp
resources/resource_pack_stack.cpp
server/server_instance.cpp
server/server_level.cpp
server/commands/command.cpp
server/commands/command_origin.cpp
server/commands/command_origin_loader.cpp
Expand Down
13 changes: 0 additions & 13 deletions src/bedrock/server/server_level.cpp

This file was deleted.

4 changes: 0 additions & 4 deletions src/bedrock/symbol_generator/symbols.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ version = "1.21.50"
"?updateServerAnnouncement@ServerNetworkHandler@@QEAAXXZ" = 14438928
"?_createNewPlayer@ServerNetworkHandler@@AEAAAEAVServerPlayer@@AEBVNetworkIdentifier@@AEBVSubClientConnectionRequest@@W4SubClientId@@@Z" = 14291952
"?_isServerTextEnabled@ServerNetworkHandler@@AEBA_NAEBW4ServerTextEvent@@@Z" = 14311280
# ServerPlayer
"?disconnect@ServerPlayer@@QEAAXXZ" = 20376320
# ServerScoreboard
"??0ServerScoreboard@@QEAA@VCommandSoftEnumRegistry@@PEAVLevelStorage@@V?$not_null@V?$NonOwnerPointer@VGameplayUserManager@@@Bedrock@@@gsl@@@Z" = 51136848
# StartGamePacket
Expand Down Expand Up @@ -179,8 +177,6 @@ version = "1.21.50"
"_ZN20ServerNetworkHandler24updateServerAnnouncementEv" = 91583744
"_ZN20ServerNetworkHandler16_createNewPlayerERK17NetworkIdentifierRK26SubClientConnectionRequest11SubClientId" = 91671520
"_ZNK20ServerNetworkHandler20_isServerTextEnabledERK15ServerTextEvent" = 91584688
# ServerPlayer
"_ZN12ServerPlayer10disconnectEv" = 103650656
# ServerScoreboard
"_ZN16ServerScoreboardC2E23CommandSoftEnumRegistryP12LevelStorageN3gsl8not_nullIN7Bedrock15NonOwnerPointerI19GameplayUserManagerEEEE" = 144814368
# StartGamePacket
Expand Down
33 changes: 30 additions & 3 deletions src/endstone/core/event/handlers/player_gameplay_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@

#include "endstone/core/event/handlers/player_gameplay_handler.h"

#include <endstone/core/message.h>
#include <endstone/event/player/player_join_event.h>

#include "bedrock/entity/components/replay_state_component.h"
#include "bedrock/locale/i18n.h"
#include "bedrock/network/packet/death_info_packet.h"
Expand All @@ -27,13 +24,16 @@
#include "endstone/core/game_mode.h"
#include "endstone/core/inventory/item_stack.h"
#include "endstone/core/json.h"
#include "endstone/core/message.h"
#include "endstone/core/player.h"
#include "endstone/core/server.h"
#include "endstone/event/player/player_death_event.h"
#include "endstone/event/player/player_emote_event.h"
#include "endstone/event/player/player_game_mode_change_event.h"
#include "endstone/event/player/player_interact_actor_event.h"
#include "endstone/event/player/player_interact_event.h"
#include "endstone/event/player/player_join_event.h"
#include "endstone/event/player/player_quit_event.h"
#include "endstone/event/player/player_respawn_event.h"

namespace endstone::core {
Expand All @@ -48,6 +48,7 @@ HandlerResult EndstonePlayerGameplayHandler::handleEvent(const PlayerGameplayEve
auto visitor = [&](auto &&arg) -> HandlerResult {
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, Details::ValueOrRef<const PlayerDamageEvent>> ||
std::is_same_v<T, Details::ValueOrRef<const PlayerDisconnectEvent>> ||
std::is_same_v<T, Details::ValueOrRef<const PlayerFormResponseEvent>> ||
std::is_same_v<T, Details::ValueOrRef<const PlayerFormCloseEvent>> ||
std::is_same_v<T, Details::ValueOrRef<const PlayerInitialSpawnEvent>>) {
Expand Down Expand Up @@ -130,6 +131,32 @@ bool EndstonePlayerGameplayHandler::handleEvent(const PlayerDamageEvent &event)
return true;
}

bool EndstonePlayerGameplayHandler::handleEvent(const PlayerDisconnectEvent &event)
{
if (auto *player = WeakEntityRef(event.player).tryUnwrap<::Player>(); player) {
const auto &server = entt::locator<EndstoneServer>::value();
auto &endstone_player = player->getEndstoneActor<EndstonePlayer>();
endstone_player.disconnect();

Translatable tr{ColorFormat::Yellow + "%multiplayer.player.left", {endstone_player.getName()}};
const std::string quit_message = EndstoneMessage::toString(tr);

PlayerQuitEvent e{endstone_player, quit_message};
server.getPluginManager().callEvent(e);

if (e.getQuitMessage() != quit_message) {
tr = Translatable{e.getQuitMessage(), {}};
}

if (!e.getQuitMessage().empty()) {
for (const auto &online_player : server.getOnlinePlayers()) {
online_player->sendMessage(tr);
}
}
}
return true;
}

bool EndstonePlayerGameplayHandler::handleEvent(const PlayerFormResponseEvent &event)
{
if (auto *player = WeakEntityRef(event.player).tryUnwrap<::Player>(); player) {
Expand Down
1 change: 1 addition & 0 deletions src/endstone/core/event/handlers/player_gameplay_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class EndstonePlayerGameplayHandler final : public PlayerGameplayHandler {

private:
bool handleEvent(const PlayerDamageEvent &event);
bool handleEvent(const PlayerDisconnectEvent &event);
bool handleEvent(const PlayerFormResponseEvent &event);
bool handleEvent(const PlayerFormCloseEvent &event);
bool handleEvent(const PlayerInitialSpawnEvent &event);
Expand Down
1 change: 0 additions & 1 deletion src/endstone/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ add_library(endstone_runtime SHARED
bedrock_hooks/say_command.cpp
bedrock_hooks/server_instance_event_coordinator.cpp
bedrock_hooks/server_network_handler.cpp
bedrock_hooks/server_player.cpp
bedrock_hooks/start_game_packet.cpp
main.cpp
hook.cpp
Expand Down
54 changes: 0 additions & 54 deletions src/endstone/runtime/bedrock_hooks/server_player.cpp

This file was deleted.

0 comments on commit 2baf0e9

Please sign in to comment.