Skip to content

Commit

Permalink
fix: fix some events crash
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamguxiang committed Dec 29, 2024
1 parent 82975b1 commit 5290d87
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src-server/ll/api/event/player/PlayerChatEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ LL_TYPE_INSTANCE_HOOK(
NetworkIdentifier const& identifier,
TextPacket const& packet
) {
if (auto player = _getServerPlayer(identifier, packet.mClientSubId); player) {
auto& handle = ll::memory::dAccess<ServerNetworkHandler>(this, -16);
if (auto player = handle._getServerPlayer(identifier, packet.mClientSubId); player) {
auto event = PlayerChatEvent{*player, const_cast<TextPacket&>(packet).mMessage};
EventBus::getInstance().publish(event);
if (event.isCancelled()) {
Expand Down
4 changes: 3 additions & 1 deletion src-server/ll/api/event/player/PlayerJoinEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "mc/network/ServerNetworkHandler.h"
#include "mc/network/packet/SetLocalPlayerAsInitializedPacket.h"
#include "mc/server/ServerPlayer.h"

namespace ll::event::inline player {

Expand All @@ -16,7 +17,8 @@ LL_TYPE_INSTANCE_HOOK(
NetworkIdentifier const& identifier,
SetLocalPlayerAsInitializedPacket const& packet
) {
if (auto player = _getServerPlayer(identifier, packet.mClientSubId); player) {
auto& handle = ll::memory::dAccess<ServerNetworkHandler>(this, -16);
if (auto player = handle._getServerPlayer(identifier, packet.mClientSubId); player) {
auto event = PlayerJoinEvent{*player};
EventBus::getInstance().publish(event);
if (event.isCancelled()) {
Expand Down
7 changes: 4 additions & 3 deletions src-server/ll/api/event/player/PlayerSneakAndSprintEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ LL_TYPE_INSTANCE_HOOK(
NetworkIdentifier const& id,
PlayerActionPacket const& packet
) {
auto& handle = ll::memory::dAccess<ServerNetworkHandler>(this, -16);
switch (packet.mAction) {
case PlayerActionType::StartSprinting:
if (auto player = this->_getServerPlayer(id, packet.mClientSubId); player) {
if (auto player = handle._getServerPlayer(id, packet.mClientSubId); player) {
EventBus::getInstance().publish(PlayerSprintingEvent(*player));
break;
}
case PlayerActionType::StopSprinting:
if (auto player = this->_getServerPlayer(id, packet.mClientSubId); player) {
if (auto player = handle._getServerPlayer(id, packet.mClientSubId); player) {
EventBus::getInstance().publish(PlayerSprintedEvent(*player));
break;
}
case PlayerActionType::StartSneaking:
if (auto player = this->_getServerPlayer(id, packet.mClientSubId); player) {
if (auto player = handle._getServerPlayer(id, packet.mClientSubId); player) {
auto ev = PlayerSneakingEvent(*player);
EventBus::getInstance().publish(ev);
if (ev.isCancelled()) {
Expand Down
3 changes: 2 additions & 1 deletion src-server/ll/api/event/player/PlayerSwingEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ LL_TYPE_INSTANCE_HOOK(
NetworkIdentifier const& id,
AnimatePacket const& packet
) {
auto& handle = ll::memory::dAccess<ServerNetworkHandler>(this, -16);
if (packet.mAction == AnimatePacket::Action::Swing) {
if (auto player = this->_getServerPlayer(id, packet.mClientSubId); player) {
if (auto player = handle._getServerPlayer(id, packet.mClientSubId); player) {
EventBus::getInstance().publish(PlayerSwingEvent(*player));
}
}
Expand Down
4 changes: 2 additions & 2 deletions src-server/ll/core/form/FormHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ LL_TYPE_INSTANCE_HOOK(
NetEventCallback& callback,
std::shared_ptr<Packet>& packet
) {
if (auto player = static_cast<ServerNetworkHandler&>(callback)._getServerPlayer(source, SubClientId::PrimaryClient);
player) {
auto& handle = ll::memory::dAccess<ServerNetworkHandler>(&callback, -16);
if (auto player = handle._getServerPlayer(source, packet->mClientSubId); player) {
auto& modalPacket = (ModalFormResponsePacket&)*packet;

if (ll::form::handler::handleFormPacket(
Expand Down
7 changes: 3 additions & 4 deletions src/ll/api/event/player/PlayerJumpEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "mc/server/ServerPlayer.h"

namespace ll::event::inline player {
LL_AUTO_TYPE_INSTANCE_HOOK(
LL_TYPE_INSTANCE_HOOK(
PlayerJumpEventHook,
HookPriority::Normal,
ServerNetworkHandler,
Expand All @@ -17,10 +17,9 @@ LL_AUTO_TYPE_INSTANCE_HOOK(
::PlayerAuthInputPacket const& packet
) {
auto& handle = ll::memory::dAccess<ServerNetworkHandler>(this, -16);
if (ServerPlayer* player = handle._getServerPlayer(source, packet.mClientSubId)) {
if (ServerPlayer* player = handle._getServerPlayer(source, packet.mClientSubId); player) {
if (packet.getInput(::PlayerAuthInputPacket::InputData::StartJumping)) {
std::cout << player->getRealName() << std::endl;
EventBus::getInstance().publish(PlayerJumpEvent(*(Player*)player));
EventBus::getInstance().publish(PlayerJumpEvent(*player));
}
}
return origin(source, packet);
Expand Down

0 comments on commit 5290d87

Please sign in to comment.