Skip to content

Commit b2d8a92

Browse files
committed
Correctly read exact component matchers in villager trades
1 parent 788fbdf commit b2d8a92

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,14 +564,31 @@ public static void writeHashedStack(ByteBuf buf, HashedStack hashedStack) {
564564
public static VillagerTrade.ItemCost readItemCost(ByteBuf buf) {
565565
int item = MinecraftTypes.readVarInt(buf);
566566
int count = MinecraftTypes.readVarInt(buf);
567-
List<DataComponentType<?>> components = MinecraftTypes.readList(buf, input -> DataComponentTypes.from(MinecraftTypes.readVarInt(input)));
568-
return new VillagerTrade.ItemCost(item, count, components);
567+
return new VillagerTrade.ItemCost(item, count, MinecraftTypes.readExactComponentMatcher(buf));
569568
}
570569

571570
public static void writeItemCost(ByteBuf buf, VillagerTrade.ItemCost itemCost) {
572571
MinecraftTypes.writeVarInt(buf, itemCost.itemId());
573572
MinecraftTypes.writeVarInt(buf, itemCost.count());
574-
MinecraftTypes.writeList(buf, itemCost.components(), (output, component) -> MinecraftTypes.writeVarInt(output, component.getId()));
573+
MinecraftTypes.writeExactComponentMatcher(buf, itemCost.components());
574+
}
575+
576+
public static Map<DataComponentType<?>, DataComponent<?, ?>> readExactComponentMatcher(ByteBuf buf) {
577+
Map<DataComponentType<?>, DataComponent<?, ?>> dataComponents = new HashMap<>();
578+
int length = MinecraftTypes.readVarInt(buf);
579+
for (int i = 0; i < length; i++) {
580+
DataComponentType<?> type = DataComponentTypes.from(MinecraftTypes.readVarInt(buf));
581+
dataComponents.put(type, type.readDataComponent(buf));
582+
}
583+
return dataComponents;
584+
}
585+
586+
public static void writeExactComponentMatcher(ByteBuf buf, Map<DataComponentType<?>, DataComponent<?, ?>> dataComponents) {
587+
MinecraftTypes.writeVarInt(buf, dataComponents.size());
588+
for (Map.Entry<DataComponentType<?>, DataComponent<?, ?>> entry : dataComponents.entrySet()) {
589+
MinecraftTypes.writeVarInt(buf, entry.getKey().getId());
590+
entry.getValue().write(buf);
591+
}
575592
}
576593

577594
public static TestInstanceBlockEntity readTestBlockEntity(ByteBuf buf) {

protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/inventory/VillagerTrade.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
import lombok.NonNull;
66
import org.checkerframework.checker.nullness.qual.Nullable;
77
import org.geysermc.mcprotocollib.protocol.data.game.item.ItemStack;
8+
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponent;
89
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponentType;
910

10-
import java.util.List;
11+
import java.util.Map;
1112

1213
@Data
1314
@AllArgsConstructor
@@ -23,6 +24,6 @@ public class VillagerTrade {
2324
private final float priceMultiplier;
2425
private final int demand;
2526

26-
public record ItemCost(int itemId, int count, List<DataComponentType<?>> components) {
27+
public record ItemCost(int itemId, int count, Map<DataComponentType<?>, DataComponent<?, ?>> components) {
2728
}
2829
}

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
import org.geysermc.mcprotocollib.protocol.data.game.entity.Effect;
1414
import org.geysermc.mcprotocollib.protocol.data.game.entity.EquipmentSlot;
1515
import org.geysermc.mcprotocollib.protocol.data.game.entity.attribute.ModifierOperation;
16-
import org.geysermc.mcprotocollib.protocol.data.game.level.sound.BuiltinSound;
17-
import org.geysermc.mcprotocollib.protocol.data.game.level.sound.CustomSound;
1816
import org.geysermc.mcprotocollib.protocol.data.game.level.sound.Sound;
1917

2018
import java.util.ArrayList;
@@ -107,12 +105,7 @@ public static void writeBlockPredicate(ByteBuf buf, AdventureModePredicate.Block
107105
}
108106

109107
public static DataComponentMatchers readDataComponentMatchers(ByteBuf buf) {
110-
Map<DataComponentType<?>, DataComponent<?, ?>> exactMatchers = new HashMap<>();
111-
int exactCount = MinecraftTypes.readVarInt(buf);
112-
for (int i = 0; i < exactCount; i++) {
113-
DataComponentType<?> type = DataComponentTypes.from(MinecraftTypes.readVarInt(buf));
114-
exactMatchers.put(type, type.readDataComponent(buf));
115-
}
108+
Map<DataComponentType<?>, DataComponent<?, ?>> exactMatchers = MinecraftTypes.readExactComponentMatcher(buf);
116109

117110
int[] partialMatchers = new int[MinecraftTypes.readVarInt(buf)];
118111
for (int i = 0; i < partialMatchers.length; i++) {
@@ -123,11 +116,7 @@ public static DataComponentMatchers readDataComponentMatchers(ByteBuf buf) {
123116
}
124117

125118
public static void writeDataComponentMatchers(ByteBuf buf, DataComponentMatchers matchers) {
126-
MinecraftTypes.writeVarInt(buf, matchers.exactMatchers().size());
127-
for (Map.Entry<DataComponentType<?>, DataComponent<?, ?>> entry : matchers.exactMatchers().entrySet()) {
128-
MinecraftTypes.writeVarInt(buf, entry.getKey().getId());
129-
entry.getValue().write(buf);
130-
}
119+
MinecraftTypes.writeExactComponentMatcher(buf, matchers.exactMatchers());
131120

132121
MinecraftTypes.writeVarInt(buf, matchers.partialMatchers().length);
133122
for (int id : matchers.partialMatchers()) {

0 commit comments

Comments
 (0)