Skip to content

Commit

Permalink
Lazily fetch Registry instances in BukkitProtocolConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
2008Choco committed Feb 18, 2024
1 parent c09c602 commit 3babf81
Showing 1 changed file with 32 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.google.common.base.Preconditions;

import java.util.function.Supplier;

import org.bukkit.Art;
import org.bukkit.Fluid;
import org.bukkit.GameEvent;
Expand Down Expand Up @@ -78,30 +80,30 @@ public void configure(@NotNull MessageProtocol<?, ?> protocol) {
protocol.registerCustomDataType(NamespacedKey.class, this::writeNamespacedKey, this::readNamespacedKey);

// Registry-backed Keyed types
this.registerKeyed(protocol, Advancement.class, Registry.ADVANCEMENT);
this.registerKeyed(protocol, Art.class, Registry.ART);
this.registerKeyed(protocol, Biome.class, Registry.BIOME);
this.registerKeyed(protocol, Cat.Type.class, Registry.CAT_VARIANT);
this.registerKeyed(protocol, Enchantment.class, Registry.ENCHANTMENT);
this.registerKeyed(protocol, EntityType.class, Registry.ENTITY_TYPE);
this.registerKeyed(protocol, Fluid.class, Registry.FLUID);
this.registerKeyed(protocol, Frog.Variant.class, Registry.FROG_VARIANT);
this.registerKeyed(protocol, GameEvent.class, Registry.GAME_EVENT);
this.registerKeyed(protocol, LootTables.class, Registry.LOOT_TABLES);
this.registerKeyed(protocol, Material.class, Registry.MATERIAL);
this.registerKeyed(protocol, MusicInstrument.class, Registry.INSTRUMENT);
this.registerKeyed(protocol, Particle.class, Registry.PARTICLE_TYPE);
this.registerKeyed(protocol, PatternType.class, Registry.BANNER_PATTERN);
this.registerKeyed(protocol, PotionEffectType.class, Registry.EFFECT);
this.registerKeyed(protocol, PotionType.class, Registry.POTION);
this.registerKeyed(protocol, Sound.class, Registry.SOUNDS);
this.registerKeyed(protocol, Statistic.class, Registry.STATISTIC);
this.registerKeyed(protocol, Structure.class, Registry.STRUCTURE);
this.registerKeyed(protocol, StructureType.class, Registry.STRUCTURE_TYPE);
this.registerKeyed(protocol, TrimMaterial.class, Registry.TRIM_MATERIAL);
this.registerKeyed(protocol, TrimPattern.class, Registry.TRIM_PATTERN);
this.registerKeyed(protocol, Villager.Profession.class, Registry.VILLAGER_PROFESSION);
this.registerKeyed(protocol, Villager.Type.class, Registry.VILLAGER_TYPE);
this.registerKeyed(protocol, Advancement.class, () -> Registry.ADVANCEMENT);
this.registerKeyed(protocol, Art.class, () -> Registry.ART);
this.registerKeyed(protocol, Biome.class, () -> Registry.BIOME);
this.registerKeyed(protocol, Cat.Type.class, () -> Registry.CAT_VARIANT);
this.registerKeyed(protocol, Enchantment.class, () -> Registry.ENCHANTMENT);
this.registerKeyed(protocol, EntityType.class, () -> Registry.ENTITY_TYPE);
this.registerKeyed(protocol, Fluid.class, () -> Registry.FLUID);
this.registerKeyed(protocol, Frog.Variant.class, () -> Registry.FROG_VARIANT);
this.registerKeyed(protocol, GameEvent.class, () -> Registry.GAME_EVENT);
this.registerKeyed(protocol, LootTables.class, () -> Registry.LOOT_TABLES);
this.registerKeyed(protocol, Material.class, () -> Registry.MATERIAL);
this.registerKeyed(protocol, MusicInstrument.class, () -> Registry.INSTRUMENT);
this.registerKeyed(protocol, Particle.class, () -> Registry.PARTICLE_TYPE);
this.registerKeyed(protocol, PatternType.class, () -> Registry.BANNER_PATTERN);
this.registerKeyed(protocol, PotionEffectType.class, () -> Registry.EFFECT);
this.registerKeyed(protocol, PotionType.class, () -> Registry.POTION);
this.registerKeyed(protocol, Sound.class, () -> Registry.SOUNDS);
this.registerKeyed(protocol, Statistic.class, () -> Registry.STATISTIC);
this.registerKeyed(protocol, Structure.class, () -> Registry.STRUCTURE);
this.registerKeyed(protocol, StructureType.class, () -> Registry.STRUCTURE_TYPE);
this.registerKeyed(protocol, TrimMaterial.class, () -> Registry.TRIM_MATERIAL);
this.registerKeyed(protocol, TrimPattern.class, () -> Registry.TRIM_PATTERN);
this.registerKeyed(protocol, Villager.Profession.class, () -> Registry.VILLAGER_PROFESSION);
this.registerKeyed(protocol, Villager.Type.class, () -> Registry.VILLAGER_TYPE);
}

private void writeVector(Vector vector, MessageByteBuffer buffer) {
Expand Down Expand Up @@ -140,8 +142,12 @@ private <T extends Keyed> T readRegistered(MessageByteBuffer buffer, Registry<T>
return registry.get(buffer.read(NamespacedKey.class));
}

private <T extends Keyed> void registerKeyed(MessageProtocol<?, ?> protocol, Class<T> type, Registry<T> registry) {
protocol.registerCustomDataType(type, this::writeKeyed, buffer -> readRegistered(buffer, registry));
// Using a Supplier<Registry<T>> because VeinMiner, the primary user of this library, supports 1.17.x where some of these registries do not exist
private <T extends Keyed> void registerKeyed(MessageProtocol<?, ?> protocol, Class<T> type, Supplier<Registry<T>> registrySupplier) {
try {
Registry<T> registry = registrySupplier.get();
protocol.registerCustomDataType(type, this::writeKeyed, buffer -> readRegistered(buffer, registry));
} catch (Throwable ignore) { }
}

}

0 comments on commit 3babf81

Please sign in to comment.