Skip to content

Commit 2a6025f

Browse files
Fix reading enchantments from server (#4836)
1 parent 7fdb410 commit 2a6025f

File tree

9 files changed

+166
-63
lines changed

9 files changed

+166
-63
lines changed

core/src/main/java/org/geysermc/geyser/inventory/recipe/TrimRecipe.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@
3232
import org.cloudburstmc.protocol.bedrock.data.inventory.descriptor.ItemDescriptorWithCount;
3333
import org.cloudburstmc.protocol.bedrock.data.inventory.descriptor.ItemTagDescriptor;
3434
import org.geysermc.geyser.registry.type.ItemMapping;
35-
import org.geysermc.geyser.session.GeyserSession;
35+
import org.geysermc.geyser.session.cache.registry.RegistryEntryContext;
3636
import org.geysermc.geyser.translator.text.MessageTranslator;
37-
import org.geysermc.mcprotocollib.protocol.data.game.RegistryEntry;
3837

3938
/**
4039
* Stores information on trim materials and patterns, including smithing armor hacks for pre-1.20.
@@ -46,18 +45,18 @@ public final class TrimRecipe {
4645
public static final ItemDescriptorWithCount ADDITION = tagDescriptor("minecraft:trim_materials");
4746
public static final ItemDescriptorWithCount TEMPLATE = tagDescriptor("minecraft:trim_templates");
4847

49-
public static TrimMaterial readTrimMaterial(GeyserSession session, RegistryEntry entry) {
50-
String key = entry.getId().asMinimalString();
48+
public static TrimMaterial readTrimMaterial(RegistryEntryContext context) {
49+
String key = context.id().asMinimalString();
5150

5251
// Color is used when hovering over the item
5352
// Find the nearest legacy color from the RGB Java gives us to work with
5453
// Also yes this is a COMPLETE hack but it works ok!!!!!
55-
String colorTag = entry.getData().getCompound("description").getString("color");
54+
String colorTag = context.data().getCompound("description").getString("color");
5655
TextColor color = TextColor.fromHexString(colorTag);
5756
String legacy = MessageTranslator.convertMessage(Component.space().color(color));
5857

59-
String itemIdentifier = entry.getData().getString("ingredient");
60-
ItemMapping itemMapping = session.getItemMappings().getMapping(itemIdentifier);
58+
String itemIdentifier = context.data().getString("ingredient");
59+
ItemMapping itemMapping = context.session().getItemMappings().getMapping(itemIdentifier);
6160
if (itemMapping == null) {
6261
// This should never happen so not sure what to do here.
6362
itemMapping = ItemMapping.AIR;
@@ -66,11 +65,11 @@ public static TrimMaterial readTrimMaterial(GeyserSession session, RegistryEntry
6665
return new TrimMaterial(key, legacy.substring(2).trim(), itemMapping.getBedrockIdentifier());
6766
}
6867

69-
public static TrimPattern readTrimPattern(GeyserSession session, RegistryEntry entry) {
70-
String key = entry.getId().asMinimalString();
68+
public static TrimPattern readTrimPattern(RegistryEntryContext context) {
69+
String key = context.id().asMinimalString();
7170

72-
String itemIdentifier = entry.getData().getString("template_item");
73-
ItemMapping itemMapping = session.getItemMappings().getMapping(itemIdentifier);
71+
String itemIdentifier = context.data().getString("template_item");
72+
ItemMapping itemMapping = context.session().getItemMappings().getMapping(itemIdentifier);
7473
if (itemMapping == null) {
7574
// This should never happen so not sure what to do here.
7675
itemMapping = ItemMapping.AIR;

core/src/main/java/org/geysermc/geyser/inventory/updater/AnvilInventoryUpdater.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import it.unimi.dsi.fastutil.objects.Object2IntMap;
2929
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
30+
import java.util.stream.IntStream;
3031
import net.kyori.adventure.text.Component;
3132
import org.cloudburstmc.nbt.NbtMap;
3233
import org.cloudburstmc.nbt.NbtMapBuilder;
@@ -41,11 +42,14 @@
4142
import org.geysermc.geyser.item.enchantment.Enchantment;
4243
import org.geysermc.geyser.item.Items;
4344
import org.geysermc.geyser.session.GeyserSession;
45+
import org.geysermc.geyser.session.cache.tags.EnchantmentTag;
46+
import org.geysermc.geyser.session.cache.tags.ItemTag;
4447
import org.geysermc.geyser.translator.inventory.InventoryTranslator;
4548
import org.geysermc.geyser.translator.text.MessageTranslator;
4649
import org.geysermc.geyser.util.ItemUtils;
4750
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.GameMode;
4851
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponentType;
52+
import org.geysermc.mcprotocollib.protocol.data.game.item.component.HolderSet;
4953
import org.geysermc.mcprotocollib.protocol.data.game.item.component.ItemEnchantments;
5054
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.inventory.ServerboundRenameItemPacket;
5155

@@ -310,17 +314,18 @@ private int calcMergeEnchantmentCost(GeyserSession session, GeyserItemStack inpu
310314
for (Object2IntMap.Entry<Enchantment> entry : getEnchantments(session, material).object2IntEntrySet()) {
311315
Enchantment enchantment = entry.getKey();
312316

313-
boolean canApply = isEnchantedBook(input) || session.getTagCache().is(enchantment.supportedItems(), input);
314-
var exclusiveSet = enchantment.exclusiveSet();
315-
if (exclusiveSet != null) {
316-
int[] incompatibleEnchantments = session.getTagCache().get(exclusiveSet);
317-
for (int i : incompatibleEnchantments) {
318-
Enchantment incompatible = session.getRegistryCache().enchantments().byId(i);
319-
if (combinedEnchantments.containsKey(incompatible)) {
320-
canApply = false;
321-
if (!bedrock) {
322-
cost++;
323-
}
317+
HolderSet supportedItems = enchantment.supportedItems();
318+
int[] supportedItemIds = supportedItems.resolve(tagId -> session.getTagCache().get(ItemTag.ALL_ITEM_TAGS.get(tagId)));
319+
boolean canApply = isEnchantedBook(input) || IntStream.of(supportedItemIds).anyMatch(id -> id == input.getJavaId());
320+
321+
HolderSet exclusiveSet = enchantment.exclusiveSet();
322+
int[] incompatibleEnchantments = exclusiveSet.resolve(tagId -> session.getTagCache().get(EnchantmentTag.ALL_ENCHANTMENT_TAGS.get(tagId)));
323+
for (int i : incompatibleEnchantments) {
324+
Enchantment incompatible = session.getRegistryCache().enchantments().byId(i);
325+
if (combinedEnchantments.containsKey(incompatible)) {
326+
canApply = false;
327+
if (!bedrock) {
328+
cost++;
324329
}
325330
}
326331
}

core/src/main/java/org/geysermc/geyser/item/enchantment/Enchantment.java

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,47 +25,54 @@
2525

2626
package org.geysermc.geyser.item.enchantment;
2727

28+
import java.util.List;
29+
import java.util.function.Function;
30+
import net.kyori.adventure.key.Key;
2831
import org.checkerframework.checker.nullness.qual.Nullable;
2932
import org.cloudburstmc.nbt.NbtMap;
3033
import org.geysermc.geyser.inventory.item.BedrockEnchantment;
31-
import org.geysermc.geyser.session.cache.tags.EnchantmentTag;
32-
import org.geysermc.geyser.session.cache.tags.ItemTag;
34+
import org.geysermc.geyser.item.Items;
35+
import org.geysermc.geyser.registry.Registries;
36+
import org.geysermc.geyser.session.cache.registry.RegistryEntryContext;
3337
import org.geysermc.geyser.translator.text.MessageTranslator;
34-
import org.geysermc.geyser.util.MinecraftKey;
35-
import org.geysermc.mcprotocollib.protocol.data.game.RegistryEntry;
3638

3739
import java.util.HashSet;
3840
import java.util.Map;
3941
import java.util.Set;
42+
import org.geysermc.mcprotocollib.protocol.data.game.item.component.HolderSet;
4043

4144
/**
4245
* @param description only populated if {@link #bedrockEnchantment()} is not null.
4346
* @param anvilCost also as a rarity multiplier
4447
*/
4548
public record Enchantment(String identifier,
4649
Set<EnchantmentComponent> effects,
47-
ItemTag supportedItems,
50+
HolderSet supportedItems,
4851
int maxLevel,
4952
String description,
5053
int anvilCost,
51-
@Nullable EnchantmentTag exclusiveSet,
54+
HolderSet exclusiveSet,
5255
@Nullable BedrockEnchantment bedrockEnchantment) {
5356

54-
// Implementation note: I have a feeling the tags can be a list of items, because in vanilla they're HolderSet classes.
55-
// I'm not sure how that's wired over the network, so we'll put it off.
56-
public static Enchantment read(RegistryEntry entry) {
57-
NbtMap data = entry.getData();
57+
public static Enchantment read(RegistryEntryContext context) {
58+
NbtMap data = context.data();
5859
Set<EnchantmentComponent> effects = readEnchantmentComponents(data.getCompound("effects"));
59-
String supportedItems = data.getString("supported_items").substring(1); // Remove '#' at beginning that indicates tag
60+
61+
HolderSet supportedItems = readHolderSet(data.get("supported_items"), itemId -> Registries.JAVA_ITEM_IDENTIFIERS.getOrDefault(itemId.asString(), Items.AIR).javaId());
62+
6063
int maxLevel = data.getInt("max_level");
6164
int anvilCost = data.getInt("anvil_cost");
62-
String exclusiveSet = data.getString("exclusive_set", null);
63-
EnchantmentTag exclusiveSetTag = exclusiveSet == null ? null : EnchantmentTag.ALL_ENCHANTMENT_TAGS.get(MinecraftKey.key(exclusiveSet.substring(1)));
64-
BedrockEnchantment bedrockEnchantment = BedrockEnchantment.getByJavaIdentifier(entry.getId().asString());
65+
66+
HolderSet exclusiveSet = readHolderSet(data.getOrDefault("exclusive_set", null), context::getNetworkId);
67+
68+
BedrockEnchantment bedrockEnchantment = BedrockEnchantment.getByJavaIdentifier(context.id().asString());
69+
70+
// TODO - description is a component. So if a hardcoded literal string is given, this will display normally on Java,
71+
// but Geyser will attempt to lookup the literal string as translation - and will fail, displaying an empty string as enchantment name.
6572
String description = bedrockEnchantment == null ? MessageTranslator.deserializeDescription(data) : null;
6673

67-
return new Enchantment(entry.getId().asString(), effects, ItemTag.ALL_ITEM_TAGS.get(MinecraftKey.key(supportedItems)), maxLevel,
68-
description, anvilCost, exclusiveSetTag, bedrockEnchantment);
74+
return new Enchantment(context.id().asString(), effects, supportedItems, maxLevel,
75+
description, anvilCost, exclusiveSet, bedrockEnchantment);
6976
}
7077

7178
private static Set<EnchantmentComponent> readEnchantmentComponents(NbtMap effects) {
@@ -77,4 +84,24 @@ private static Set<EnchantmentComponent> readEnchantmentComponents(NbtMap effect
7784
}
7885
return Set.copyOf(components); // Also ensures any empty sets are consolidated
7986
}
87+
88+
// TODO holder set util?
89+
private static HolderSet readHolderSet(@Nullable Object holderSet, Function<Key, Integer> keyIdMapping) {
90+
if (holderSet == null) {
91+
return new HolderSet(new int[]{});
92+
}
93+
94+
if (holderSet instanceof String stringTag) {
95+
// Tag
96+
if (stringTag.startsWith("#")) {
97+
return new HolderSet(Key.key(stringTag.substring(1))); // Remove '#' at beginning that indicates tag
98+
} else {
99+
return new HolderSet(new int[]{keyIdMapping.apply(Key.key(stringTag))});
100+
}
101+
} else if (holderSet instanceof List<?> list) {
102+
// Assume the list is a list of strings
103+
return new HolderSet(list.stream().map(o -> (String) o).map(Key::key).map(keyIdMapping).mapToInt(Integer::intValue).toArray());
104+
}
105+
throw new IllegalArgumentException("Holder set must either be a tag, a string ID or a list of string IDs");
106+
}
80107
}

core/src/main/java/org/geysermc/geyser/level/JavaDimension.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
package org.geysermc.geyser.level;
2727

2828
import org.cloudburstmc.nbt.NbtMap;
29-
import org.geysermc.mcprotocollib.protocol.data.game.RegistryEntry;
29+
import org.geysermc.geyser.session.cache.registry.RegistryEntryContext;
3030

3131
/**
3232
* Represents the information we store from the current Java dimension
@@ -35,8 +35,8 @@
3535
*/
3636
public record JavaDimension(int minY, int maxY, boolean piglinSafe, double worldCoordinateScale) {
3737

38-
public static JavaDimension read(RegistryEntry entry) {
39-
NbtMap dimension = entry.getData();
38+
public static JavaDimension read(RegistryEntryContext entry) {
39+
NbtMap dimension = entry.data();
4040
int minY = dimension.getInt("min_y");
4141
int maxY = dimension.getInt("height");
4242
// Logical height can be ignored probably - seems to be for artificial limits like the Nether.

core/src/main/java/org/geysermc/geyser/level/JukeboxSong.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727

2828
import org.cloudburstmc.nbt.NbtMap;
2929
import org.geysermc.geyser.GeyserImpl;
30+
import org.geysermc.geyser.session.cache.registry.RegistryEntryContext;
3031
import org.geysermc.geyser.translator.text.MessageTranslator;
31-
import org.geysermc.mcprotocollib.protocol.data.game.RegistryEntry;
3232

3333
public record JukeboxSong(String soundEvent, String description) {
3434

35-
public static JukeboxSong read(RegistryEntry entry) {
36-
NbtMap data = entry.getData();
35+
public static JukeboxSong read(RegistryEntryContext context) {
36+
NbtMap data = context.data();
3737
Object soundEventObject = data.get("sound_event");
3838
String soundEvent;
3939
if (soundEventObject instanceof NbtMap map) {
@@ -42,7 +42,7 @@ public static JukeboxSong read(RegistryEntry entry) {
4242
soundEvent = string;
4343
} else {
4444
soundEvent = "";
45-
GeyserImpl.getInstance().getLogger().debug("Sound event for " + entry.getId() + " was of an unexpected type! Expected string or NBT map, got " + soundEventObject);
45+
GeyserImpl.getInstance().getLogger().debug("Sound event for " + context.id() + " was of an unexpected type! Expected string or NBT map, got " + soundEventObject);
4646
}
4747
String description = MessageTranslator.deserializeDescription(data);
4848
return new JukeboxSong(soundEvent, description);

core/src/main/java/org/geysermc/geyser/session/cache/RegistryCache.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
import it.unimi.dsi.fastutil.ints.Int2IntMap;
2929
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
30+
import it.unimi.dsi.fastutil.objects.Object2IntMap;
31+
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
3032
import lombok.AccessLevel;
3133
import lombok.Getter;
3234
import lombok.experimental.Accessors;
@@ -45,6 +47,7 @@
4547
import org.geysermc.geyser.level.PaintingType;
4648
import org.geysermc.geyser.session.GeyserSession;
4749
import org.geysermc.geyser.session.cache.registry.JavaRegistry;
50+
import org.geysermc.geyser.session.cache.registry.RegistryEntryContext;
4851
import org.geysermc.geyser.session.cache.registry.SimpleJavaRegistry;
4952
import org.geysermc.geyser.text.TextDecoration;
5053
import org.geysermc.geyser.translator.level.BiomeTranslator;
@@ -59,7 +62,6 @@
5962
import java.util.List;
6063
import java.util.Map;
6164
import java.util.function.BiConsumer;
62-
import java.util.function.BiFunction;
6365
import java.util.function.Function;
6466
import java.util.function.ToIntFunction;
6567

@@ -76,16 +78,16 @@ public final class RegistryCache {
7678
private static final Map<Key, BiConsumer<RegistryCache, List<RegistryEntry>>> REGISTRIES = new HashMap<>();
7779

7880
static {
79-
register("chat_type", cache -> cache.chatTypes, ($, entry) -> TextDecoration.readChatType(entry));
80-
register("dimension_type", cache -> cache.dimensions, ($, entry) -> JavaDimension.read(entry));
81-
register("enchantment", cache -> cache.enchantments, ($, entry) -> Enchantment.read(entry));
82-
register("jukebox_song", cache -> cache.jukeboxSongs, ($, entry) -> JukeboxSong.read(entry));
83-
register("painting_variant", cache -> cache.paintings, ($, entry) -> PaintingType.getByName(entry.getId()));
81+
register("chat_type", cache -> cache.chatTypes, TextDecoration::readChatType);
82+
register("dimension_type", cache -> cache.dimensions, JavaDimension::read);
83+
register("enchantment", cache -> cache.enchantments, Enchantment::read);
84+
register("jukebox_song", cache -> cache.jukeboxSongs, JukeboxSong::read);
85+
register("painting_variant", cache -> cache.paintings, context -> PaintingType.getByName(context.id()));
8486
register("trim_material", cache -> cache.trimMaterials, TrimRecipe::readTrimMaterial);
8587
register("trim_pattern", cache -> cache.trimPatterns, TrimRecipe::readTrimPattern);
8688
register("worldgen/biome", (cache, array) -> cache.biomeTranslations = array, BiomeTranslator::loadServerBiome);
87-
register("banner_pattern", cache -> cache.bannerPatterns, ($, entry) -> BannerPattern.getByJavaIdentifier(entry.getId()));
88-
register("wolf_variant", cache -> cache.wolfVariants, ($, entry) -> WolfEntity.BuiltInWolfVariant.getByJavaIdentifier(entry.getId().asString()));
89+
register("banner_pattern", cache -> cache.bannerPatterns, context -> BannerPattern.getByJavaIdentifier(context.id()));
90+
register("wolf_variant", cache -> cache.wolfVariants, context -> WolfEntity.BuiltInWolfVariant.getByJavaIdentifier(context.id().asString()));
8991

9092
// Load from MCProtocolLib's classloader
9193
NbtMap tag = MinecraftProtocol.loadNetworkCodec();
@@ -149,25 +151,35 @@ public void load(ClientboundRegistryDataPacket packet) {
149151
* @param reader converts the RegistryEntry NBT into a class file
150152
* @param <T> the class that represents these entries.
151153
*/
152-
private static <T> void register(String registry, Function<RegistryCache, JavaRegistry<T>> localCacheFunction, BiFunction<GeyserSession, RegistryEntry, T> reader) {
153-
Key key = MinecraftKey.key(registry);
154-
REGISTRIES.put(key, (registryCache, entries) -> {
154+
private static <T> void register(String registry, Function<RegistryCache, JavaRegistry<T>> localCacheFunction, Function<RegistryEntryContext, T> reader) {
155+
Key registryKey = MinecraftKey.key(registry);
156+
REGISTRIES.put(registryKey, (registryCache, entries) -> {
155157
Map<Key, NbtMap> localRegistry = null;
156158
JavaRegistry<T> localCache = localCacheFunction.apply(registryCache);
157159
// Clear each local cache every time a new registry entry is given to us
158160
// (e.g. proxy server switches)
161+
162+
// Store each of the entries resource location IDs and their respective network ID,
163+
// used for the key mapper that's currently only used by the Enchantment class
164+
Object2IntMap<Key> entryIdMap = new Object2IntOpenHashMap<>();
165+
for (int i = 0; i < entries.size(); i++) {
166+
entryIdMap.put(entries.get(i).getId(), i);
167+
}
168+
159169
List<T> builder = new ArrayList<>(entries.size());
160170
for (int i = 0; i < entries.size(); i++) {
161171
RegistryEntry entry = entries.get(i);
162172
// If the data is null, that's the server telling us we need to use our default values.
163173
if (entry.getData() == null) {
164174
if (localRegistry == null) { // Lazy initialize
165-
localRegistry = DEFAULTS.get(key);
175+
localRegistry = DEFAULTS.get(registryKey);
166176
}
167177
entry = new RegistryEntry(entry.getId(), localRegistry.get(entry.getId()));
168178
}
179+
180+
RegistryEntryContext context = new RegistryEntryContext(entry, entryIdMap, registryCache.session);
169181
// This is what Geyser wants to keep as a value for this registry.
170-
T cacheEntry = reader.apply(registryCache.session, entry);
182+
T cacheEntry = reader.apply(context);
171183
builder.add(i, cacheEntry);
172184
}
173185
localCache.reset(builder);

core/src/main/java/org/geysermc/geyser/session/cache/TagCache.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,12 @@ public boolean is(ItemTag tag, Item item) {
130130
return contains(values, item.javaId());
131131
}
132132

133-
public int[] get(EnchantmentTag tag) {
134-
return this.enchantments[tag.ordinal()];
133+
public int[] get(ItemTag itemTag) {
134+
return this.items[itemTag.ordinal()];
135+
}
136+
137+
public int[] get(EnchantmentTag enchantmentTag) {
138+
return this.enchantments[enchantmentTag.ordinal()];
135139
}
136140

137141
private static boolean contains(int[] array, int i) {

0 commit comments

Comments
 (0)