Skip to content

Commit 290d84c

Browse files
committed
Fix unit reader/writer for the INTANGIBLE_PROJECTILE component
1 parent 47c3ded commit 290d84c

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

protocol/src/main/java/org/geysermc/mcprotocollib/protocol/codec/MinecraftTypes.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,13 @@ public static ItemStack readOptionalItemStack(ByteBuf buf, boolean untrusted) {
443443
}
444444

445445
int item = MinecraftTypes.readVarInt(buf);
446-
return new ItemStack(item, count, MinecraftTypes.readDataComponentPatch(buf, untrusted));
446+
DataComponents components;
447+
try {
448+
components = MinecraftTypes.readDataComponentPatch(buf, untrusted);
449+
} catch (IllegalArgumentException e) {
450+
throw new IllegalArgumentException("Exception while reading components for item " + item, e);
451+
}
452+
return new ItemStack(item, count, components);
447453
}
448454

449455
public static void writeOptionalItemStack(ByteBuf buf, ItemStack item, boolean untrusted) {
@@ -475,21 +481,21 @@ public static DataComponents readDataComponentPatch(ByteBuf buf, boolean untrust
475481
Map<DataComponentType<?>, DataComponent<?, ?>> dataComponents = new HashMap<>();
476482
if (untrusted) {
477483
for (int k = 0; k < nonNullComponents; k++) {
478-
DataComponentType<?> dataComponentType = DataComponentTypes.from(MinecraftTypes.readVarInt(buf));
484+
DataComponentType<?> dataComponentType = DataComponentTypes.read(buf);
479485
MinecraftTypes.readVarInt(buf);
480486
DataComponent<?, ?> dataComponent = dataComponentType.readDataComponent(buf);
481487
dataComponents.put(dataComponentType, dataComponent);
482488
}
483489
} else {
484490
for (int k = 0; k < nonNullComponents; k++) {
485-
DataComponentType<?> dataComponentType = DataComponentTypes.from(MinecraftTypes.readVarInt(buf));
491+
DataComponentType<?> dataComponentType = DataComponentTypes.read(buf);
486492
DataComponent<?, ?> dataComponent = dataComponentType.readDataComponent(buf);
487493
dataComponents.put(dataComponentType, dataComponent);
488494
}
489495
}
490496

491497
for (int k = 0; k < nullComponents; k++) {
492-
DataComponentType<?> dataComponentType = DataComponentTypes.from(MinecraftTypes.readVarInt(buf));
498+
DataComponentType<?> dataComponentType = DataComponentTypes.read(buf);
493499
DataComponent<?, ?> dataComponent = dataComponentType.readNullDataComponent();
494500
dataComponents.put(dataComponentType, dataComponent);
495501
}

protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/DataComponentTypes.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class DataComponentTypes {
4343
public static final IntComponentType REPAIR_COST = register(id -> new IntComponentType(id, "repair_cost", MinecraftTypes::readVarInt, MinecraftTypes::writeVarInt, IntDataComponent::new));
4444
public static final DataComponentType<Unit> CREATIVE_SLOT_LOCK = register(id -> new DataComponentType<>(id, "creative_slot_lock", unitReader(), unitWriter(), ObjectDataComponent::new));
4545
public static final BooleanComponentType ENCHANTMENT_GLINT_OVERRIDE = register(id -> new BooleanComponentType(id, "enchantment_glint_override", ByteBuf::readBoolean, ByteBuf::writeBoolean, BooleanDataComponent::new));
46-
public static final DataComponentType<Unit> INTANGIBLE_PROJECTILE = register(id -> new DataComponentType<>(id, "intangible_projectile", unitReader(), unitWriter(), ObjectDataComponent::new));
46+
public static final DataComponentType<Unit> INTANGIBLE_PROJECTILE = register(id -> new DataComponentType<>(id, "intangible_projectile", emptyNbtReader(), emptyNbtWriter(), ObjectDataComponent::new));
4747
public static final DataComponentType<FoodProperties> FOOD = register(id -> new DataComponentType<>(id, "food", ItemTypes::readFoodProperties, ItemTypes::writeFoodProperties, ObjectDataComponent::new));
4848
public static final DataComponentType<Consumable> CONSUMABLE = register(id -> new DataComponentType<>(id, "consumable", ItemTypes::readConsumable, ItemTypes::writeConsumable, ObjectDataComponent::new));
4949
public static final DataComponentType<ItemStack> USE_REMAINDER = register(id -> new DataComponentType<>(id, "use_remainder", MinecraftTypes::readItemStack, MinecraftTypes::writeItemStack, ObjectDataComponent::new));
@@ -157,6 +157,17 @@ private static DataComponentType.Writer<Unit> unitWriter() {
157157
};
158158
}
159159

160+
private static DataComponentType.Reader<Unit> emptyNbtReader() {
161+
return (input) -> {
162+
MinecraftTypes.readAnyTag(input);
163+
return Unit.INSTANCE;
164+
};
165+
}
166+
167+
private static DataComponentType.Writer<Unit> emptyNbtWriter() {
168+
return (output, value) -> MinecraftTypes.writeAnyTag(output, NbtMap.EMPTY);
169+
}
170+
160171
public static DataComponentType<?> read(ByteBuf in) {
161172
int id = MinecraftTypes.readVarInt(in);
162173
if (id >= VALUES.size()) {

0 commit comments

Comments
 (0)