Skip to content

Commit

Permalink
feat: add more checks in packet-handling
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Nov 6, 2024
1 parent 516e76d commit cd0e103
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public PacketSignal handlePacket(BedrockPacket packet) {

var processor = packetProcessorHolder.getProcessor(packet);
if (processor == null) {
log.warn("Received a packet without packet handler: {}", packet);
log.warn("Received a packet which doesn't have correspond packet handler: {}", packet);
return PacketSignal.HANDLED;
}

Expand All @@ -147,10 +147,14 @@ public PacketSignal handlePacket(BedrockPacket packet) {
}

if (processor.handleAsync(thisPlayer, packet, time) != PacketSignal.HANDLED) {
// Packet processors should make sure that PacketProcessor.handleSync() won't be called
// if player is not in any world
Objects.requireNonNull(world, "Player that is not in any world cannot handle sync packet");
((AllayWorld) world).addSyncPacketToQueue(thisPlayer, packet, time);
if (world == null) {
// Packet processors should make sure that PacketProcessor.handleSync()
// method won't be called if player is not in any world
log.warn("Cannot handle sync packet {} for player {} which is not in any world!", packet.getPacketType().name(), thisPlayer);
processor.handleSync(thisPlayer, packet, time);
} else {
((AllayWorld) world).addSyncPacketToQueue(thisPlayer, packet, time);
}
}

return PacketSignal.HANDLED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ protected void handleSyncPackets() {
PacketQueueEntry entry;
int count = 0;
while (count < MAX_PACKETS_HANDLE_COUNT_AT_ONCE && (entry = packetQueue.poll()) != null) {
entry.player().getManager().<EntityPlayerNetworkComponentImpl>getComponent(EntityPlayerNetworkComponentImpl.IDENTIFIER).handleDataPacket(entry.packet(), entry.time());
if (entry.player.getWorld() != this) {
log.warn("Trying to handle sync packet in world {} which the player {} is not in!", this.getWorldData().getName(), entry.player.getOriginName());
}
entry.player.getManager().<EntityPlayerNetworkComponentImpl>getComponent(EntityPlayerNetworkComponentImpl.IDENTIFIER).handleDataPacket(entry.packet(), entry.time());
count++;
}
} catch (Throwable throwable) {
Expand Down

0 comments on commit cd0e103

Please sign in to comment.