Compare commits

...
Author SHA1 Message Date
FlorianMichael 0dd7dfe2f3 Handle empty items in pre 1.20.5 protocols
Maps empty items to null on read in 1.13, 1.13.2 and 1.20.2 item types and handles the empty item->null transition in 1.10->1.11
2024-06-09 15:14:45 +02:00
5 changed files with 18 additions and 15 deletions

No files matched your search

@@ -44,13 +44,16 @@ public class ItemType1_13 extends Type<Item> {
item.setIdentifier(id);
item.setAmount(buffer.readByte());
item.setTag(Types.NAMED_COMPOUND_TAG.read(buffer));
if (item.isEmpty()) {
return null;
}
return item;
}
}
@Override
public void write(ByteBuf buffer, @Nullable Item object) {
if (object == null) {
if (object == null || object.isEmpty()) {
buffer.writeShort(-1);
} else {
buffer.writeShort(object.identifier());
@@ -44,13 +44,16 @@ public class ItemType1_13_2 extends Type<Item> {
item.setIdentifier(Types.VAR_INT.readPrimitive(buffer));
item.setAmount(buffer.readByte());
item.setTag(Types.NAMED_COMPOUND_TAG.read(buffer));
if (item.isEmpty()) {
return null;
}
return item;
}
}
@Override
public void write(ByteBuf buffer, @Nullable Item object) {
if (object == null) {
if (object == null || object.isEmpty()) {
buffer.writeBoolean(false);
} else {
buffer.writeBoolean(true);
@@ -45,12 +45,15 @@ public class ItemType1_20_2 extends Type<Item> {
item.setIdentifier(Types.VAR_INT.readPrimitive(buffer));
item.setAmount(buffer.readByte());
item.setTag(Types.COMPOUND_TAG.read(buffer));
if (item.isEmpty()) {
return null;
}
return item;
}
@Override
public void write(final ByteBuf buffer, @Nullable final Item object) {
if (object == null) {
if (object == null || object.isEmpty()) {
buffer.writeBoolean(false);
} else {
buffer.writeBoolean(true);
@@ -74,6 +74,10 @@ public class ItemPacketRewriter1_11 extends ItemRewriter<ClientboundPackets1_9_3
@Override
public Item handleItemToClient(UserConnection connection, Item item) {
if (item != null && item.isEmpty()) {
// Map empty items to null
return null;
}
EntityMappings1_11.toClientItem(item);
return item;
}
@@ -355,12 +355,7 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
}
@Override
public Item handleItemToClient(final UserConnection connection, @Nullable final Item item) {
if (item == null) {
// We no longer want null items, always unify them to empty
return StructuredItem.empty();
}
public Item handleItemToClient(final UserConnection connection, final Item item) {
// Add the original as custom data, to be re-used for creative clients as well
final CompoundTag tag = item.tag();
if (tag != null) {
@@ -380,12 +375,7 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
}
@Override
public @Nullable Item handleItemToServer(UserConnection connection, final Item item) {
if (item.isEmpty()) {
// Empty to null for the old protocols
return null;
}
public Item handleItemToServer(UserConnection connection, final Item item) {
super.handleItemToServer(connection, item);
return toOldItem(connection, item, DATA_CONVERTER);
}