Add RegistryContext record and use it for reading registries in RegistryCache

This commit is contained in:
Eclipse 2024-07-08 20:52:53 +00:00
parent d692ad9476
commit 623b0571d3
No known key found for this signature in database
GPG key ID: 95E6998F82EC938A
7 changed files with 92 additions and 44 deletions

View file

@ -32,9 +32,8 @@ import org.cloudburstmc.protocol.bedrock.data.TrimPattern;
import org.cloudburstmc.protocol.bedrock.data.inventory.descriptor.ItemDescriptorWithCount;
import org.cloudburstmc.protocol.bedrock.data.inventory.descriptor.ItemTagDescriptor;
import org.geysermc.geyser.registry.type.ItemMapping;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.session.cache.registry.RegistryContext;
import org.geysermc.geyser.translator.text.MessageTranslator;
import org.geysermc.mcprotocollib.protocol.data.game.RegistryEntry;
/**
* Stores information on trim materials and patterns, including smithing armor hacks for pre-1.20.
@ -46,18 +45,18 @@ public final class TrimRecipe {
public static final ItemDescriptorWithCount ADDITION = tagDescriptor("minecraft:trim_materials");
public static final ItemDescriptorWithCount TEMPLATE = tagDescriptor("minecraft:trim_templates");
public static TrimMaterial readTrimMaterial(GeyserSession session, RegistryEntry entry) {
String key = entry.getId().asMinimalString();
public static TrimMaterial readTrimMaterial(RegistryContext context) {
String key = context.id().asMinimalString();
// Color is used when hovering over the item
// Find the nearest legacy color from the RGB Java gives us to work with
// Also yes this is a COMPLETE hack but it works ok!!!!!
String colorTag = entry.getData().getCompound("description").getString("color");
String colorTag = context.data().getCompound("description").getString("color");
TextColor color = TextColor.fromHexString(colorTag);
String legacy = MessageTranslator.convertMessage(Component.space().color(color));
String itemIdentifier = entry.getData().getString("ingredient");
ItemMapping itemMapping = session.getItemMappings().getMapping(itemIdentifier);
String itemIdentifier = context.data().getString("ingredient");
ItemMapping itemMapping = context.session().getItemMappings().getMapping(itemIdentifier);
if (itemMapping == null) {
// This should never happen so not sure what to do here.
itemMapping = ItemMapping.AIR;
@ -66,11 +65,11 @@ public final class TrimRecipe {
return new TrimMaterial(key, legacy.substring(2).trim(), itemMapping.getBedrockIdentifier());
}
public static TrimPattern readTrimPattern(GeyserSession session, RegistryEntry entry) {
String key = entry.getId().asMinimalString();
public static TrimPattern readTrimPattern(RegistryContext context) {
String key = context.id().asMinimalString();
String itemIdentifier = entry.getData().getString("template_item");
ItemMapping itemMapping = session.getItemMappings().getMapping(itemIdentifier);
String itemIdentifier = context.data().getString("template_item");
ItemMapping itemMapping = context.session().getItemMappings().getMapping(itemIdentifier);
if (itemMapping == null) {
// This should never happen so not sure what to do here.
itemMapping = ItemMapping.AIR;

View file

@ -31,10 +31,10 @@ import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.cloudburstmc.nbt.NbtMap;
import org.geysermc.geyser.inventory.item.BedrockEnchantment;
import org.geysermc.geyser.item.type.Item;
import org.geysermc.geyser.item.Items;
import org.geysermc.geyser.registry.Registries;
import org.geysermc.geyser.session.cache.registry.RegistryContext;
import org.geysermc.geyser.translator.text.MessageTranslator;
import org.geysermc.mcprotocollib.protocol.data.game.RegistryEntry;
import java.util.HashSet;
import java.util.Map;
@ -54,27 +54,24 @@ public record Enchantment(String identifier,
HolderSet exclusiveSet,
@Nullable BedrockEnchantment bedrockEnchantment) {
public static Enchantment read(Function<Key, Integer> keyIdMapping, RegistryEntry entry) {
NbtMap data = entry.getData();
public static Enchantment read(RegistryContext context) {
NbtMap data = context.data();
Set<EnchantmentComponent> effects = readEnchantmentComponents(data.getCompound("effects"));
HolderSet supportedItems = readHolderSet(data.get("supported_items"), itemId -> {
Item item = Registries.JAVA_ITEM_IDENTIFIERS.get(itemId.asString());
return Registries.JAVA_ITEMS.get().indexOf(item);
});
HolderSet supportedItems = readHolderSet(data.get("supported_items"), itemId -> Registries.JAVA_ITEM_IDENTIFIERS.getOrDefault(itemId.asString(), Items.AIR).javaId());
int maxLevel = data.getInt("max_level");
int anvilCost = data.getInt("anvil_cost");
HolderSet exclusiveSet = readHolderSet(data.getOrDefault("exclusive_set", null), keyIdMapping);
HolderSet exclusiveSet = readHolderSet(data.getOrDefault("exclusive_set", null), context::getNetworkId);
BedrockEnchantment bedrockEnchantment = BedrockEnchantment.getByJavaIdentifier(entry.getId().asString());
BedrockEnchantment bedrockEnchantment = BedrockEnchantment.getByJavaIdentifier(context.id().asString());
// TODO - description is a component. So if a hardcoded literal string is given, this will display normally on Java,
// but Geyser will attempt to lookup the literal string as translation - and will fail, displaying an empty string as enchantment name.
String description = bedrockEnchantment == null ? MessageTranslator.deserializeDescription(data) : null;
return new Enchantment(entry.getId().asString(), effects, supportedItems, maxLevel,
return new Enchantment(context.id().asString(), effects, supportedItems, maxLevel,
description, anvilCost, exclusiveSet, bedrockEnchantment);
}

View file

@ -26,7 +26,7 @@
package org.geysermc.geyser.level;
import org.cloudburstmc.nbt.NbtMap;
import org.geysermc.mcprotocollib.protocol.data.game.RegistryEntry;
import org.geysermc.geyser.session.cache.registry.RegistryContext;
/**
* Represents the information we store from the current Java dimension
@ -35,8 +35,8 @@ import org.geysermc.mcprotocollib.protocol.data.game.RegistryEntry;
*/
public record JavaDimension(int minY, int maxY, boolean piglinSafe, double worldCoordinateScale) {
public static JavaDimension read(RegistryEntry entry) {
NbtMap dimension = entry.getData();
public static JavaDimension read(RegistryContext entry) {
NbtMap dimension = entry.data();
int minY = dimension.getInt("min_y");
int maxY = dimension.getInt("height");
// Logical height can be ignored probably - seems to be for artificial limits like the Nether.

View file

@ -27,13 +27,13 @@ package org.geysermc.geyser.level;
import org.cloudburstmc.nbt.NbtMap;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.session.cache.registry.RegistryContext;
import org.geysermc.geyser.translator.text.MessageTranslator;
import org.geysermc.mcprotocollib.protocol.data.game.RegistryEntry;
public record JukeboxSong(String soundEvent, String description) {
public static JukeboxSong read(RegistryEntry entry) {
NbtMap data = entry.getData();
public static JukeboxSong read(RegistryContext context) {
NbtMap data = context.data();
Object soundEventObject = data.get("sound_event");
String soundEvent;
if (soundEventObject instanceof NbtMap map) {
@ -42,7 +42,7 @@ public record JukeboxSong(String soundEvent, String description) {
soundEvent = string;
} else {
soundEvent = "";
GeyserImpl.getInstance().getLogger().debug("Sound event for " + entry.getId() + " was of an unexpected type! Expected string or NBT map, got " + soundEventObject);
GeyserImpl.getInstance().getLogger().debug("Sound event for " + context.id() + " was of an unexpected type! Expected string or NBT map, got " + soundEventObject);
}
String description = MessageTranslator.deserializeDescription(data);
return new JukeboxSong(soundEvent, description);

View file

@ -35,7 +35,6 @@ import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.nbt.NbtType;
import org.cloudburstmc.protocol.bedrock.data.TrimMaterial;
import org.cloudburstmc.protocol.bedrock.data.TrimPattern;
import org.cloudburstmc.protocol.common.util.TriFunction;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.entity.type.living.animal.tameable.WolfEntity;
import org.geysermc.geyser.inventory.item.BannerPattern;
@ -46,6 +45,7 @@ import org.geysermc.geyser.level.JukeboxSong;
import org.geysermc.geyser.level.PaintingType;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.session.cache.registry.JavaRegistry;
import org.geysermc.geyser.session.cache.registry.RegistryContext;
import org.geysermc.geyser.session.cache.registry.SimpleJavaRegistry;
import org.geysermc.geyser.text.TextDecoration;
import org.geysermc.geyser.translator.level.BiomeTranslator;
@ -76,16 +76,16 @@ public final class RegistryCache {
private static final Map<Key, BiConsumer<RegistryCache, List<RegistryEntry>>> REGISTRIES = new HashMap<>();
static {
register("chat_type", cache -> cache.chatTypes, ($, n, entry) -> TextDecoration.readChatType(entry));
register("dimension_type", cache -> cache.dimensions, ($, n, entry) -> JavaDimension.read(entry));
register("enchantment", cache -> cache.enchantments, ($, n, entry) -> Enchantment.read(n, entry));
register("jukebox_song", cache -> cache.jukeboxSongs, ($, n, entry) -> JukeboxSong.read(entry));
register("painting_variant", cache -> cache.paintings, ($, n, entry) -> PaintingType.getByName(entry.getId()));
register("trim_material", cache -> cache.trimMaterials, (session, n, entry) -> TrimRecipe.readTrimMaterial(session, entry));
register("trim_pattern", cache -> cache.trimPatterns, (session, n, entry) -> TrimRecipe.readTrimPattern(session, entry));
register("chat_type", cache -> cache.chatTypes, TextDecoration::readChatType);
register("dimension_type", cache -> cache.dimensions, JavaDimension::read);
register("enchantment", cache -> cache.enchantments, Enchantment::read);
register("jukebox_song", cache -> cache.jukeboxSongs, JukeboxSong::read);
register("painting_variant", cache -> cache.paintings, context -> PaintingType.getByName(context.id()));
register("trim_material", cache -> cache.trimMaterials, TrimRecipe::readTrimMaterial);
register("trim_pattern", cache -> cache.trimPatterns, TrimRecipe::readTrimPattern);
register("worldgen/biome", (cache, array) -> cache.biomeTranslations = array, BiomeTranslator::loadServerBiome);
register("banner_pattern", cache -> cache.bannerPatterns, ($, n, entry) -> BannerPattern.getByJavaIdentifier(entry.getId()));
register("wolf_variant", cache -> cache.wolfVariants, ($, n, entry) -> WolfEntity.BuiltInWolfVariant.getByJavaIdentifier(entry.getId().asString()));
register("banner_pattern", cache -> cache.bannerPatterns, context -> BannerPattern.getByJavaIdentifier(context.id()));
register("wolf_variant", cache -> cache.wolfVariants, context -> WolfEntity.BuiltInWolfVariant.getByJavaIdentifier(context.id().asString()));
// Load from MCProtocolLib's classloader
NbtMap tag = MinecraftProtocol.loadNetworkCodec();
@ -149,7 +149,7 @@ public final class RegistryCache {
* @param reader converts the RegistryEntry NBT into a class file
* @param <T> the class that represents these entries.
*/
private static <T> void register(String registry, Function<RegistryCache, JavaRegistry<T>> localCacheFunction, TriFunction<GeyserSession, Function<Key, Integer>, RegistryEntry, T> reader) {
private static <T> void register(String registry, Function<RegistryCache, JavaRegistry<T>> localCacheFunction, Function<RegistryContext, T> reader) {
Key registryKey = MinecraftKey.key(registry);
REGISTRIES.put(registryKey, (registryCache, entries) -> {
Map<Key, NbtMap> localRegistry = null;
@ -157,6 +157,8 @@ public final class RegistryCache {
// Clear each local cache every time a new registry entry is given to us
// (e.g. proxy server switches)
// Store each of the entries resource location IDs and their respective network ID,
// used for the key mapper that's currently only used by the Enchantment class
Map<Key, Integer> entryIdMap = new HashMap<>();
for (int i = 0; i < entries.size(); i++) {
entryIdMap.put(entries.get(i).getId(), i);
@ -172,8 +174,10 @@ public final class RegistryCache {
}
entry = new RegistryEntry(entry.getId(), localRegistry.get(entry.getId()));
}
RegistryContext context = new RegistryContext(entry, entryIdMap, registryCache.session);
// This is what Geyser wants to keep as a value for this registry.
T cacheEntry = reader.apply(registryCache.session, entryId -> entryIdMap.getOrDefault(entryId, 0), entry);
T cacheEntry = reader.apply(context);
builder.add(i, cacheEntry);
}
localCache.reset(builder);

View file

@ -0,0 +1,48 @@
/*
* Copyright (c) 2024 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.session.cache.registry;
import java.util.Map;
import net.kyori.adventure.key.Key;
import org.cloudburstmc.nbt.NbtMap;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.mcprotocollib.protocol.data.game.RegistryEntry;
public record RegistryContext(RegistryEntry entry, Map<Key, Integer> keyIdMap, GeyserSession session) {
public int getNetworkId(Key registryKey) {
return keyIdMap.getOrDefault(registryKey, 0);
}
public Key id() {
return entry.getId();
}
// Not annotated as nullable because data should never be null here
public NbtMap data() {
return entry.getData();
}
}

View file

@ -29,7 +29,7 @@ import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.Style;
import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.nbt.NbtType;
import org.geysermc.mcprotocollib.protocol.data.game.RegistryEntry;
import org.geysermc.geyser.session.cache.registry.RegistryContext;
import org.geysermc.mcprotocollib.protocol.data.game.chat.ChatType;
import org.geysermc.mcprotocollib.protocol.data.game.chat.ChatTypeDecoration;
@ -43,11 +43,11 @@ public record TextDecoration(String translationKey, List<Parameter> parameters,
throw new UnsupportedOperationException();
}
public static ChatType readChatType(RegistryEntry entry) {
public static ChatType readChatType(RegistryContext context) {
// Note: The ID is NOT ALWAYS THE SAME! ViaVersion as of 1.19 adds two registry entries that do NOT match vanilla.
// (This note has been passed around through several classes and iterations. It stays as a warning
// to anyone that dares to try and hardcode registry IDs.)
NbtMap tag = entry.getData();
NbtMap tag = context.data();
NbtMap chat = tag.getCompound("chat", null);
if (chat != null) {
String translationKey = chat.getString("translation_key");