Skip to content

Commit

Permalink
refactor: replace offset with type cast
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamguxiang committed Dec 30, 2024
1 parent f2a0816 commit 53b7780
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src-server/ll/api/event/player/PlayerChatEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ LL_TYPE_INSTANCE_HOOK(
NetworkIdentifier const& identifier,
TextPacket const& packet
) {
auto& handle = ll::memory::dAccess<ServerNetworkHandler>(this, -16);
if (auto player = handle._getServerPlayer(identifier, packet.mClientSubId); player) {
auto handle = static_cast<decltype(this)>(reinterpret_cast<NetEventCallback*>(this));
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: 2 additions & 2 deletions src-server/ll/api/event/player/PlayerJoinEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ LL_TYPE_INSTANCE_HOOK(
NetworkIdentifier const& identifier,
SetLocalPlayerAsInitializedPacket const& packet
) {
auto& handle = ll::memory::dAccess<ServerNetworkHandler>(this, -16);
if (auto player = handle._getServerPlayer(identifier, packet.mClientSubId); player) {
auto handle = static_cast<decltype(this)>(reinterpret_cast<NetEventCallback*>(this));
if (auto player = handle->_getServerPlayer(identifier, packet.mClientSubId); player) {
auto event = PlayerJoinEvent{*player};
EventBus::getInstance().publish(event);
if (event.isCancelled()) {
Expand Down
10 changes: 5 additions & 5 deletions src-server/ll/api/event/player/PlayerSneakAndSprintEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ LL_TYPE_INSTANCE_HOOK(
NetworkIdentifier const& id,
PlayerActionPacket const& packet
) {
auto& handle = ll::memory::dAccess<ServerNetworkHandler>(this, -16);
auto handle = static_cast<decltype(this)>(reinterpret_cast<NetEventCallback*>(this));
switch (packet.mAction) {
case PlayerActionType::StartSprinting:
if (auto player = handle._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 = handle._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 = handle._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 All @@ -39,7 +39,7 @@ LL_TYPE_INSTANCE_HOOK(
break;
}
case PlayerActionType::StopSneaking:
if (auto player = this->_getServerPlayer(id, packet.mClientSubId); player) {
if (auto player = handle->_getServerPlayer(id, packet.mClientSubId); player) {
auto ev = PlayerSneakedEvent(*player);
EventBus::getInstance().publish(ev);
if (ev.isCancelled()) {
Expand Down
4 changes: 2 additions & 2 deletions src-server/ll/api/event/player/PlayerSwingEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ LL_TYPE_INSTANCE_HOOK(
NetworkIdentifier const& id,
AnimatePacket const& packet
) {
auto& handle = ll::memory::dAccess<ServerNetworkHandler>(this, -16);
auto handle = static_cast<decltype(this)>(reinterpret_cast<NetEventCallback*>(this));
if (packet.mAction == AnimatePacket::Action::Swing) {
if (auto player = handle._getServerPlayer(id, packet.mClientSubId); player) {
if (auto player = handle->_getServerPlayer(id, packet.mClientSubId); player) {
EventBus::getInstance().publish(PlayerSwingEvent(*player));
}
}
Expand Down
5 changes: 2 additions & 3 deletions src-server/ll/core/form/FormHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,9 @@ LL_TYPE_INSTANCE_HOOK(
NetEventCallback& callback,
std::shared_ptr<Packet>& packet
) {
auto& handle = ll::memory::dAccess<ServerNetworkHandler>(&callback, -16);
if (auto player = handle._getServerPlayer(source, packet->mClientSubId); player) {
auto handle = static_cast<ServerNetworkHandler*>(&callback);
if (auto player = handle->_getServerPlayer(source, packet->mClientSubId); player) {
auto& modalPacket = (ModalFormResponsePacket&)*packet;

if (ll::form::handler::handleFormPacket(
*player,
modalPacket.mFormId,
Expand Down
4 changes: 2 additions & 2 deletions src/ll/api/event/player/PlayerJumpEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ LL_TYPE_INSTANCE_HOOK(
auto mode = settings->getPlayerMovementSettings().mUnk3c7e19.as<ServerAuthMovementMode>();
if (mode == ServerAuthMovementMode::LegacyClientAuthoritativeV1) {
if (packet.mAction == PlayerActionType::StartJump) {
auto& handle = ll::memory::dAccess<ServerNetworkHandler>(this, -16);
if (auto player = handle._getServerPlayer(source, packet.mClientSubId); player) {
auto handle = static_cast<decltype(this)>(reinterpret_cast<NetEventCallback*>(this));
if (auto player = handle->_getServerPlayer(source, packet.mClientSubId); player) {
EventBus::getInstance().publish(PlayerJumpEvent(*player));
}
}
Expand Down

0 comments on commit 53b7780

Please sign in to comment.