From e954d091b9e159a60cc987231b8dbdee98f1a3c9 Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Sun, 5 Dec 2021 19:03:47 -0500 Subject: [PATCH] Prevent errors from simply being swallowed --- .../geyser/entity/type/LivingEntity.java | 9 +++-- .../network/ConnectorServerEventHandler.java | 33 +++++++++++++------ .../geyser/network/UpstreamPacketHandler.java | 5 +++ .../player/BedrockActionTranslator.java | 20 +++++------ 4 files changed, 45 insertions(+), 22 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/entity/type/LivingEntity.java b/core/src/main/java/org/geysermc/geyser/entity/type/LivingEntity.java index 2aff7f9e1..cd1a2ef3f 100644 --- a/core/src/main/java/org/geysermc/geyser/entity/type/LivingEntity.java +++ b/core/src/main/java/org/geysermc/geyser/entity/type/LivingEntity.java @@ -255,8 +255,13 @@ public class LivingEntity extends Entity { if (javaAttribute.getType() instanceof AttributeType.Builtin type) { switch (type) { case GENERIC_MAX_HEALTH -> { - this.maxHealth = (float) AttributeUtils.calculateValue(javaAttribute); - newAttributes.add(createHealthAttribute()); + float maxHealth = (float) AttributeUtils.calculateValue(javaAttribute); + if (this.maxHealth != maxHealth) { + // Helps prnt respawn bugs where the player respawns twice + // Because we re-sent the health attribute as 0 + this.maxHealth = maxHealth; + newAttributes.add(createHealthAttribute()); + } } case GENERIC_ATTACK_DAMAGE -> newAttributes.add(calculateAttribute(javaAttribute, GeyserAttributeType.ATTACK_DAMAGE)); case GENERIC_FLYING_SPEED -> newAttributes.add(calculateAttribute(javaAttribute, GeyserAttributeType.FLYING_SPEED)); diff --git a/core/src/main/java/org/geysermc/geyser/network/ConnectorServerEventHandler.java b/core/src/main/java/org/geysermc/geyser/network/ConnectorServerEventHandler.java index 2cd866365..97ed35785 100644 --- a/core/src/main/java/org/geysermc/geyser/network/ConnectorServerEventHandler.java +++ b/core/src/main/java/org/geysermc/geyser/network/ConnectorServerEventHandler.java @@ -165,19 +165,32 @@ public class ConnectorServerEventHandler implements BedrockServerEventHandler { } @Override - public void onSessionCreation(BedrockServerSession bedrockServerSession) { - bedrockServerSession.setLogging(true); - bedrockServerSession.setCompressionLevel(geyser.getConfig().getBedrock().getCompressionLevel()); - bedrockServerSession.setPacketHandler(new UpstreamPacketHandler(geyser, new GeyserSession(geyser, bedrockServerSession, eventLoopGroup.next()))); - // Set the packet codec to default just in case we need to send disconnect packets. - bedrockServerSession.setPacketCodec(MinecraftProtocol.DEFAULT_BEDROCK_CODEC); + public void onSessionCreation(@Nonnull BedrockServerSession bedrockServerSession) { + try { + bedrockServerSession.setPacketCodec(MinecraftProtocol.DEFAULT_BEDROCK_CODEC); + bedrockServerSession.setLogging(true); + bedrockServerSession.setCompressionLevel(geyser.getConfig().getBedrock().getCompressionLevel()); + bedrockServerSession.setPacketHandler(new UpstreamPacketHandler(geyser, new GeyserSession(geyser, bedrockServerSession, eventLoopGroup.next()))); + // Set the packet codec to default just in case we need to send disconnect packets. + } catch (Throwable e) { + // Error must be caught or it will be swallowed + geyser.getLogger().error("Error occurred while initializing player!", e); + bedrockServerSession.disconnect(e.getMessage()); + } } @Override - public void onUnhandledDatagram(@Nonnull ChannelHandlerContext ctx, DatagramPacket packet) { - ByteBuf content = packet.content(); - if (QueryPacketHandler.isQueryPacket(content)) { - new QueryPacketHandler(geyser, packet.sender(), content); + public void onUnhandledDatagram(@Nonnull ChannelHandlerContext ctx, @Nonnull DatagramPacket packet) { + try { + ByteBuf content = packet.content(); + if (QueryPacketHandler.isQueryPacket(content)) { + new QueryPacketHandler(geyser, packet.sender(), content); + } + } catch (Throwable e) { + // Error must be caught or it will be swallowed + if (geyser.getConfig().isDebugMode()) { + geyser.getLogger().error("Error occurred during unhandled datagram!", e); + } } } } \ No newline at end of file diff --git a/core/src/main/java/org/geysermc/geyser/network/UpstreamPacketHandler.java b/core/src/main/java/org/geysermc/geyser/network/UpstreamPacketHandler.java index 4d22bd7ce..38673769b 100644 --- a/core/src/main/java/org/geysermc/geyser/network/UpstreamPacketHandler.java +++ b/core/src/main/java/org/geysermc/geyser/network/UpstreamPacketHandler.java @@ -91,6 +91,11 @@ public class UpstreamPacketHandler extends LoggingPacketHandler { LoginEncryptionUtils.encryptPlayerConnection(session, loginPacket); + if (session.isClosed()) { + // Can happen if Xbox validation fails + return true; + } + PlayStatusPacket playStatus = new PlayStatusPacket(); playStatus.setStatus(PlayStatusPacket.Status.LOGIN_SUCCESS); session.sendUpstreamPacket(playStatus); diff --git a/core/src/main/java/org/geysermc/geyser/translator/protocol/bedrock/entity/player/BedrockActionTranslator.java b/core/src/main/java/org/geysermc/geyser/translator/protocol/bedrock/entity/player/BedrockActionTranslator.java index 711285948..8494daade 100644 --- a/core/src/main/java/org/geysermc/geyser/translator/protocol/bedrock/entity/player/BedrockActionTranslator.java +++ b/core/src/main/java/org/geysermc/geyser/translator/protocol/bedrock/entity/player/BedrockActionTranslator.java @@ -40,16 +40,14 @@ import org.geysermc.geyser.entity.type.Entity; import org.geysermc.geyser.entity.type.ItemFrameEntity; import org.geysermc.geyser.entity.type.player.SessionPlayerEntity; import org.geysermc.geyser.inventory.PlayerInventory; -import org.geysermc.geyser.session.GeyserSession; -import org.geysermc.geyser.translator.protocol.PacketTranslator; -import org.geysermc.geyser.translator.protocol.Translator; import org.geysermc.geyser.level.block.BlockStateValues; import org.geysermc.geyser.registry.BlockRegistries; import org.geysermc.geyser.registry.type.ItemMapping; +import org.geysermc.geyser.session.GeyserSession; +import org.geysermc.geyser.translator.protocol.PacketTranslator; +import org.geysermc.geyser.translator.protocol.Translator; import org.geysermc.geyser.util.BlockUtils; -import java.util.ArrayList; - @Translator(packet = PlayerActionPacket.class) public class BedrockActionTranslator extends PacketTranslator { @@ -63,7 +61,6 @@ public class BedrockActionTranslator extends PacketTranslator(entity.getAttributes().values())); + attributesPacket.getAttributes().addAll(entity.getAttributes().values()); session.sendUpstreamPacket(attributesPacket); break; case START_SWIMMING: @@ -147,6 +144,7 @@ public class BedrockActionTranslator extends PacketTranslator(entity.getAttributes().values())); + attributesPacket.getAttributes().addAll(entity.getAttributes().values()); session.sendUpstreamPacket(attributesPacket); session.getEntityCache().updateBossBars();