package org.bukkit; import com.google.common.base.Predicates; import com.google.common.collect.ImmutableMap; import java.util.Arrays; import java.util.Iterator; import java.util.Map; import java.util.function.Predicate; import org.bukkit.advancement.Advancement; import org.bukkit.block.Biome; import org.bukkit.boss.KeyedBossBar; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.EntityType; import org.bukkit.entity.Villager; import org.bukkit.entity.memory.MemoryKey; import org.bukkit.loot.LootTables; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** * Represents a registry of Bukkit objects that may be retrieved by * {@link NamespacedKey}. * * @param type of item in the registry */ public interface Registry extends Iterable { /** * Server advancements. * * @see Bukkit#getAdvancement(org.bukkit.NamespacedKey) * @see Bukkit#advancementIterator() */ Registry ADVANCEMENT = new Registry() { @Nullable @Override public Advancement get(@NotNull NamespacedKey key) { return Bukkit.getAdvancement(key); } @NotNull @Override public Iterator iterator() { return Bukkit.advancementIterator(); } }; /** * Server art. * * @see Art */ Registry ART = new SimpleRegistry<>(Art.class); /** * Server biomes. * * @see Biome */ Registry BIOME = new SimpleRegistry<>(Biome.class); /** * Custom boss bars. * * @see Bukkit#getBossBar(org.bukkit.NamespacedKey) * @see Bukkit#getBossBars() */ Registry BOSS_BARS = new Registry() { @Nullable @Override public KeyedBossBar get(@NotNull NamespacedKey key) { return Bukkit.getBossBar(key); } @NotNull @Override public Iterator iterator() { return Bukkit.getBossBars(); } }; /** * Server enchantments. * * @see Enchantment#getByKey(org.bukkit.NamespacedKey) */ Registry ENCHANTMENT = new Registry() { @Nullable @Override public Enchantment get(@NotNull NamespacedKey key) { return Enchantment.getByKey(key); } @NotNull @Override public Iterator iterator() { return Arrays.asList(Enchantment.values()).iterator(); } }; /** * Server entity types. * * @see EntityType */ Registry ENTITY_TYPE = new SimpleRegistry<>(EntityType.class, (entity) -> entity != EntityType.UNKNOWN); /** * Default server loot tables. * * @see LootTables */ Registry LOOT_TABLES = new SimpleRegistry<>(LootTables.class); /** * Server materials. * * @see Material */ Registry MATERIAL = new SimpleRegistry<>(Material.class, (mat) -> !mat.isLegacy()); /** * Server statistics. * * @see Statistic */ Registry STATISTIC = new SimpleRegistry<>(Statistic.class); /** * Villager profession. * * @see Villager.Profession */ Registry VILLAGER_PROFESSION = new SimpleRegistry<>(Villager.Profession.class); /** * Villager type. * * @see Villager.Type */ Registry VILLAGER_TYPE = new SimpleRegistry<>(Villager.Type.class); /** * Memory Keys. * * @see MemoryKey */ Registry MEMORY_MODULE_TYPE = new Registry() { @NotNull @Override public Iterator iterator() { return MemoryKey.values().iterator(); } @Nullable @Override public MemoryKey get(@NotNull NamespacedKey key) { return MemoryKey.getByKey(key); } }; /** * Get the object by its key. * * @param key non-null key * @return item or null if does not exist */ @Nullable T get(@NotNull NamespacedKey key); static final class SimpleRegistry & Keyed> implements Registry { private final Map map; protected SimpleRegistry(@NotNull Class type) { this(type, Predicates.alwaysTrue()); } protected SimpleRegistry(@NotNull Class type, @NotNull Predicate predicate) { ImmutableMap.Builder builder = ImmutableMap.builder(); for (T entry : type.getEnumConstants()) { if (predicate.test(entry)) { builder.put(entry.getKey(), entry); } } map = builder.build(); } @Nullable @Override public T get(@NotNull NamespacedKey key) { return map.get(key); } @NotNull @Override public Iterator iterator() { return map.values().iterator(); } } }