Gracefully fail on unknown types/values in ClientboundBlockEventPacket, update to 1.21.4 release
Some checks failed
Java CI with Gradle / build (push) Has been cancelled
Deploy / build (push) Has been cancelled

This commit is contained in:
onebeastchris 2024-12-05 20:14:07 +08:00
parent ce8109ef3b
commit 9332151a49
2 changed files with 44 additions and 53 deletions

View file

@ -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)
)

View file

@ -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);
}