Skip to content

Commit

Permalink
Cleanup and update Adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
Camotoy committed Oct 31, 2024
1 parent 6cc2aa3 commit 4da8eca
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.geysermc.mcprotocollib.protocol.data.game.recipe.display.slot.SlotDisplay;

/**
* A more compact version of {link org.geysermc.mcprotocollib.protocol.data.game.recipe.Recipe}.
* A more compact version of {@link org.geysermc.mcprotocollib.protocol.data.game.recipe.display.RecipeDisplay}.
*/
public interface GeyserRecipe {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.entity.type.living.animal.tameable.WolfEntity;
import org.geysermc.geyser.inventory.item.BannerPattern;
import org.geysermc.geyser.inventory.item.GeyserInstrument;
import org.geysermc.geyser.inventory.recipe.TrimRecipe;
import org.geysermc.geyser.item.enchantment.Enchantment;
import org.geysermc.geyser.inventory.item.GeyserInstrument;
import org.geysermc.geyser.level.JavaDimension;
import org.geysermc.geyser.level.JukeboxSong;
import org.geysermc.geyser.level.PaintingType;
Expand Down Expand Up @@ -77,35 +77,35 @@
@Accessors(fluent = true)
@Getter
public final class RegistryCache {
private static final Map<JavaRegistryKey<?>, Map<Key, NbtMap>> DEFAULTS;
private static final Map<JavaRegistryKey<?>, BiConsumer<RegistryCache, List<RegistryEntry>>> REGISTRIES = new HashMap<>();
private static final Map<Key, Map<Key, NbtMap>> DEFAULTS;
private static final Map<Key, BiConsumer<RegistryCache, List<RegistryEntry>>> REGISTRIES = new HashMap<>();

static {
register(JavaRegistries.CHAT_TYPE, cache -> cache.chatTypes, ChatDecoration::readChatType);
register(JavaRegistries.DIMENSION_TYPE, cache -> cache.dimensions, JavaDimension::read);
register("chat_type", cache -> cache.chatTypes, ChatDecoration::readChatType);
register("dimension_type", cache -> cache.dimensions, JavaDimension::read);
register(JavaRegistries.ENCHANTMENT, cache -> cache.enchantments, Enchantment::read);
register(JavaRegistries.JUKEBOX_SONG, cache -> cache.jukeboxSongs, JukeboxSong::read);
register(JavaRegistries.PAINTING_VARIANT, cache -> cache.paintings, context -> PaintingType.getByName(context.id()));
register(JavaRegistries.TRIM_MATERIAL, cache -> cache.trimMaterials, TrimRecipe::readTrimMaterial);
register(JavaRegistries.TRIM_PATTERN, cache -> cache.trimPatterns, TrimRecipe::readTrimPattern);
register(JavaRegistries.BIOME, (cache, array) -> cache.biomeTranslations = array, BiomeTranslator::loadServerBiome);
register(JavaRegistries.BANNER_PATTERN, cache -> cache.bannerPatterns, context -> BannerPattern.getByJavaIdentifier(context.id()));
register(JavaRegistries.WOLF_VARIANT, cache -> cache.wolfVariants, context -> WolfEntity.BuiltInWolfVariant.getByJavaIdentifier(context.id().asString()));
register(JavaRegistries.INSTRUMENT, cache -> cache.instruments, GeyserInstrument::read);
register("instrument", cache -> cache.instruments, GeyserInstrument::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, 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();
Map<JavaRegistryKey<?>, Map<Key, NbtMap>> defaults = new HashMap<>();
Map<Key, Map<Key, NbtMap>> defaults = new HashMap<>();
// Don't create a keySet - no need to create the cached object in HashMap if we don't use it again
REGISTRIES.forEach((registry, $) -> {
List<NbtMap> rawValues = tag.getCompound(registry.registryKey().asString()).getList("value", NbtType.COMPOUND);
REGISTRIES.forEach((key, $) -> {
List<NbtMap> rawValues = tag.getCompound(key.asString()).getList("value", NbtType.COMPOUND);
Map<Key, NbtMap> values = new HashMap<>();
for (NbtMap value : rawValues) {
Key name = MinecraftKey.key(value.getString("name"));
values.put(name, value.getCompound("element"));
}
// Can make these maps immutable and as efficient as possible after initialization
defaults.put(registry, Map.copyOf(values));
defaults.put(key, Map.copyOf(values));
});

DEFAULTS = Map.copyOf(defaults);
Expand Down Expand Up @@ -141,7 +141,7 @@ public RegistryCache(GeyserSession session) {
* Loads a registry in, if we are tracking it.
*/
public void load(ClientboundRegistryDataPacket packet) {
var reader = REGISTRIES.get(JavaRegistries.fromKey(packet.getRegistry()));
var reader = REGISTRIES.get(packet.getRegistry());
if (reader != null) {
reader.accept(this, packet.getEntries());
} else {
Expand All @@ -155,7 +155,27 @@ public void load(ClientboundRegistryDataPacket packet) {
* @param reader converts the RegistryEntry NBT into a class file
* @param <T> the class that represents these entries.
*/
private static <T> void register(JavaRegistryKey<T> registry, Function<RegistryCache, JavaRegistry<T>> localCacheFunction, Function<RegistryEntryContext, T> reader) {
private static <T> void register(String registry, Function<RegistryCache, JavaRegistry<T>> localCacheFunction, Function<RegistryEntryContext, T> reader) {
register(MinecraftKey.key(registry), localCacheFunction, reader);
}

/**
* @param registry the Java registry resource location.
* @param localCacheFunction which local field in RegistryCache are we caching entries for this registry?
* @param reader converts the RegistryEntry NBT into a class file
* @param <T> the class that represents these entries.
*/
private static <T> void register(JavaRegistryKey<?> registry, Function<RegistryCache, JavaRegistry<T>> localCacheFunction, Function<RegistryEntryContext, T> reader) {
register(registry.registryKey(), localCacheFunction, reader);
}

/**
* @param registry the Java registry resource location.
* @param localCacheFunction which local field in RegistryCache are we caching entries for this registry?
* @param reader converts the RegistryEntry NBT into a class file
* @param <T> the class that represents these entries.
*/
private static <T> void register(Key registry, Function<RegistryCache, JavaRegistry<T>> localCacheFunction, Function<RegistryEntryContext, T> reader) {
REGISTRIES.put(registry, (registryCache, entries) -> {
Map<Key, NbtMap> localRegistry = null;
JavaRegistry<T> localCache = localCacheFunction.apply(registryCache);
Expand Down Expand Up @@ -192,8 +212,8 @@ private static <T> void register(JavaRegistryKey<T> registry, Function<RegistryC
/**
* @param localCacheFunction the int array to set the final values to.
*/
private static void register(JavaRegistryKey<?> registry, BiConsumer<RegistryCache, int[]> localCacheFunction, ToIntFunction<RegistryEntry> reader) {
REGISTRIES.put(registry, (registryCache, entries) -> {
private static void register(String registry, BiConsumer<RegistryCache, int[]> localCacheFunction, ToIntFunction<RegistryEntry> reader) {
REGISTRIES.put(MinecraftKey.key(registry), (registryCache, entries) -> {
Int2IntMap temp = new Int2IntOpenHashMap();
int greatestId = 0;
for (int i = 0; i < entries.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,14 @@

import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.cloudburstmc.protocol.bedrock.data.TrimMaterial;
import org.cloudburstmc.protocol.bedrock.data.TrimPattern;
import org.geysermc.geyser.entity.type.living.animal.tameable.WolfEntity;
import org.geysermc.geyser.inventory.item.BannerPattern;
import org.geysermc.geyser.item.enchantment.Enchantment;
import org.geysermc.geyser.item.type.Item;
import org.geysermc.geyser.inventory.item.GeyserInstrument;
import org.geysermc.geyser.level.JavaDimension;
import org.geysermc.geyser.level.JukeboxSong;
import org.geysermc.geyser.level.PaintingType;
import org.geysermc.geyser.level.block.type.Block;
import org.geysermc.geyser.registry.BlockRegistries;
import org.geysermc.geyser.registry.ListRegistry;
import org.geysermc.geyser.registry.Registries;
import org.geysermc.geyser.session.cache.RegistryCache;
import org.geysermc.geyser.util.MinecraftKey;
import org.geysermc.mcprotocollib.protocol.data.game.chat.ChatType;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -56,20 +47,7 @@ public class JavaRegistries {

public static final JavaRegistryKey<Block> BLOCK = create("block", BlockRegistries.JAVA_BLOCKS, Block::javaId);
public static final JavaRegistryKey<Item> ITEM = create("item", Registries.JAVA_ITEMS, Item::javaId);
public static final JavaRegistryKey<ChatType> CHAT_TYPE = create("chat_type", RegistryCache::chatTypes);
public static final JavaRegistryKey<JavaDimension> DIMENSION_TYPE = create("dimension_type", RegistryCache::dimensions);
public static final JavaRegistryKey<Enchantment> ENCHANTMENT = create("enchantment", RegistryCache::enchantments);
public static final JavaRegistryKey<JukeboxSong> JUKEBOX_SONG = create("jukebox_song", RegistryCache::jukeboxSongs);
public static final JavaRegistryKey<PaintingType> PAINTING_VARIANT = create("painting_variant", RegistryCache::paintings);
public static final JavaRegistryKey<TrimMaterial> TRIM_MATERIAL = create("trim_material", RegistryCache::trimMaterials);
public static final JavaRegistryKey<TrimPattern> TRIM_PATTERN = create("trim_pattern", RegistryCache::trimPatterns);
public static final JavaRegistryKey<GeyserInstrument> INSTRUMENT = create("instrument", RegistryCache::instruments);
/**
* This registry should not be used in holder sets, tags, etc. It's simply used as a mapping from Java biomes to Bedrock ones.
*/
public static final JavaRegistryKey<Integer> BIOME = create("worldgen/biome");
public static final JavaRegistryKey<BannerPattern> BANNER_PATTERN = create("banner_pattern", RegistryCache::bannerPatterns);
public static final JavaRegistryKey<WolfEntity.BuiltInWolfVariant> WOLF_VARIANT = create("wolf_variant", RegistryCache::wolfVariants);

private static <T> JavaRegistryKey<T> create(String key, JavaRegistryKey.NetworkSerializer<T> networkSerializer, JavaRegistryKey.NetworkDeserializer<T> networkDeserializer) {
JavaRegistryKey<T> registry = new JavaRegistryKey<>(MinecraftKey.key(key), networkSerializer, networkDeserializer);
Expand All @@ -85,11 +63,6 @@ private static <T> JavaRegistryKey<T> create(String key, RegistryGetter<T> gette
return create(key, (session, object) -> getter.get(session.getRegistryCache()).byValue(object), (session, id) -> getter.get(session.getRegistryCache()).byId(id));
}

private static <T> JavaRegistryKey<T> create(String key) {
// Cast for ambiguous call
return create(key, (JavaRegistryKey.NetworkSerializer<T>) null, null);
}

@Nullable
public static JavaRegistryKey<?> fromKey(Key registryKey) {
for (JavaRegistryKey<?> registry : VALUES) {
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jline = "3.21.0"
terminalconsoleappender = "1.2.0"
folia = "1.19.4-R0.1-SNAPSHOT"
viaversion = "4.9.2"
adapters = "1.13-SNAPSHOT"
adapters = "1.14-SNAPSHOT"
cloud = "2.0.0-rc.2"
cloud-minecraft = "2.0.0-beta.9"
cloud-minecraft-modded = "2.0.0-beta.7"
Expand Down

0 comments on commit 4da8eca

Please sign in to comment.