From 9332151a49e38a9705998609075a8a8c8a887903 Mon Sep 17 00:00:00 2001 From: onebeastchris Date: Thu, 5 Dec 2024 20:14:07 +0800 Subject: [PATCH] Gracefully fail on unknown types/values in ClientboundBlockEventPacket, update to 1.21.4 release --- .../protocol/codec/MinecraftCodec.java | 4 +- .../level/ClientboundBlockEventPacket.java | 93 +++++++++---------- 2 files changed, 44 insertions(+), 53 deletions(-) diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/codec/MinecraftCodec.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/codec/MinecraftCodec.java index 5335ec2c..5b55befd 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/codec/MinecraftCodec.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/codec/MinecraftCodec.java @@ -216,9 +216,9 @@ import org.geysermc.mcprotocollib.protocol.packet.status.serverbound.Serverbound public class MinecraftCodec { public static final PacketCodec CODEC = PacketCodec.builder() - .protocolVersion((1 << 30) | 228) + .protocolVersion(769) .helper(MinecraftCodecHelper::new) - .minecraftVersion("1.21.4-rc3") + .minecraftVersion("1.21.4") .state(ProtocolState.HANDSHAKE, MinecraftPacketRegistry.builder() .registerServerboundPacket(ClientIntentionPacket.class, ClientIntentionPacket::new) ) diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/level/ClientboundBlockEventPacket.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/level/ClientboundBlockEventPacket.java index 369f85ba..0e31d7b0 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/level/ClientboundBlockEventPacket.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/level/ClientboundBlockEventPacket.java @@ -5,6 +5,7 @@ import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; import lombok.With; +import org.checkerframework.checker.nullness.qual.Nullable; import org.cloudburstmc.math.vector.Vector3i; import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodecHelper; import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket; @@ -28,6 +29,8 @@ import org.geysermc.mcprotocollib.protocol.data.game.level.block.value.NoteBlock import org.geysermc.mcprotocollib.protocol.data.game.level.block.value.NoteBlockValueType; import org.geysermc.mcprotocollib.protocol.data.game.level.block.value.PistonValue; import org.geysermc.mcprotocollib.protocol.data.game.level.block.value.PistonValueType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; @Data @With @@ -46,73 +49,61 @@ public class ClientboundBlockEventPacket implements MinecraftPacket { private static final int SHULKER_BOX_HIGHER = 657; private static final int BELL = 811; private static final int DECORATED_POT = 1083; + private static final Logger log = LoggerFactory.getLogger(ClientboundBlockEventPacket.class); private final @NonNull Vector3i position; - private final @NonNull BlockValueType type; - private final @NonNull BlockValue value; + private final int rawType; + private final int rawValue; + private @Nullable BlockValueType type; + private @Nullable BlockValue value; private final int blockId; public ClientboundBlockEventPacket(ByteBuf in, MinecraftCodecHelper helper) { this.position = helper.readPosition(in); - int type = in.readUnsignedByte(); - int value = in.readUnsignedByte(); + this.rawType = in.readUnsignedByte(); + this.rawValue = in.readUnsignedByte(); this.blockId = helper.readVarInt(in); // TODO: Handle this in MinecraftCodecHelper - if (this.blockId == NOTE_BLOCK) { - this.type = NoteBlockValueType.from(type); - this.value = new NoteBlockValue(); - } else if (this.blockId == STICKY_PISTON || this.blockId == PISTON) { - this.type = PistonValueType.from(type); - this.value = new PistonValue(Direction.from(Math.abs((value & 7) % 6))); - } else if (this.blockId == MOB_SPAWNER) { - this.type = MobSpawnerValueType.from(type - 1); - this.value = new MobSpawnerValue(); - } else if (this.blockId == CHEST || this.blockId == ENDER_CHEST || this.blockId == TRAPPED_CHEST + try { + if (this.blockId == NOTE_BLOCK) { + this.type = NoteBlockValueType.from(rawType); + this.value = new NoteBlockValue(); + } else if (this.blockId == STICKY_PISTON || this.blockId == PISTON) { + this.type = PistonValueType.from(rawType); + this.value = new PistonValue(Direction.from(Math.abs((rawValue & 7) % 6))); + } else if (this.blockId == MOB_SPAWNER) { + this.type = MobSpawnerValueType.from(rawType - 1); + this.value = new MobSpawnerValue(); + } else if (this.blockId == CHEST || this.blockId == ENDER_CHEST || this.blockId == TRAPPED_CHEST || (this.blockId >= SHULKER_BOX_LOWER && this.blockId <= SHULKER_BOX_HIGHER)) { - this.type = ChestValueType.from(type - 1); - this.value = new ChestValue(value); - } else if (this.blockId == END_GATEWAY) { - this.type = EndGatewayValueType.from(type - 1); - this.value = new EndGatewayValue(); - } else if (this.blockId == BELL) { - this.type = BellValueType.from(type - 1); - this.value = new BellValue(Direction.from(Math.abs(value % 6))); - } else if (this.blockId == DECORATED_POT) { - this.type = DecoratedPotValueType.from(type - 1); - this.value = new DecoratedPotValue(WobbleStyle.from(Math.abs(value % 2))); - } else { - this.type = GenericBlockValueType.from(type); - this.value = new GenericBlockValue(value); + this.type = ChestValueType.from(rawType - 1); + this.value = new ChestValue(rawValue); + } else if (this.blockId == END_GATEWAY) { + this.type = EndGatewayValueType.from(rawType - 1); + this.value = new EndGatewayValue(); + } else if (this.blockId == BELL) { + this.type = BellValueType.from(rawType - 1); + this.value = new BellValue(Direction.from(Math.abs(rawValue % 6))); + } else if (this.blockId == DECORATED_POT) { + this.type = DecoratedPotValueType.from(rawType - 1); + this.value = new DecoratedPotValue(WobbleStyle.from(Math.abs(rawValue % 2))); + } else { + this.type = GenericBlockValueType.from(rawType); + this.value = new GenericBlockValue(rawValue); + } + } catch (Throwable t) { + this.type = null; + this.value = null; + log.warn("Unable to deserialize type and value! Message: {} (block: {}, type: {}, value: {})", t.getMessage(), blockId, rawType, rawValue); } } @Override public void serialize(ByteBuf out, MinecraftCodecHelper helper) { - int val = 0; - int type = 0; - // TODO: Handle this in MinecraftCodecHelper - if (this.type instanceof PistonValueType) { - val = ((PistonValue) this.value).getDirection().ordinal(); - type = ((PistonValueType) this.type).ordinal(); - } else if (this.type instanceof MobSpawnerValueType) { - type = ((MobSpawnerValueType) this.type).ordinal() + 1; - } else if (this.type instanceof ChestValueType) { - val = ((ChestValue) this.value).getViewers(); - type = ((ChestValueType) this.type).ordinal() + 1; - } else if (this.type instanceof EndGatewayValueType) { - type = ((EndGatewayValueType) this.type).ordinal() + 1; - } else if (this.type instanceof BellValueType) { - val = ((BellValue) this.value).getDirection().ordinal(); - type = ((BellValueType) this.type).ordinal() + 1; - } else if (this.type instanceof GenericBlockValueType) { - val = ((GenericBlockValue) this.value).getValue(); - type = ((GenericBlockValueType) this.type).ordinal(); - } - helper.writePosition(out, this.position); - out.writeByte(type); - out.writeByte(val); + out.writeByte(rawType); + out.writeByte(rawValue); helper.writeVarInt(out, this.blockId); }