13
0
geforkt von Mirrors/Paper

156 Zeilen
3.9 KiB
Java

package org.bukkit;
import com.google.common.collect.ImmutableMap;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
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.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 <T> type of item in the registry
*/
public interface Registry<T extends Keyed> extends Iterable<T> {
/**
* Server advancements.
*
* @see Bukkit#getAdvancement(org.bukkit.NamespacedKey)
* @see Bukkit#advancementIterator()
*/
Registry<Advancement> ADVANCEMENT = new Registry<Advancement>() {
@Nullable
@Override
public Advancement get(@NotNull NamespacedKey key) {
return Bukkit.getAdvancement(key);
}
@NotNull
@Override
public Iterator<Advancement> iterator() {
return Bukkit.advancementIterator();
}
};
/**
* Server art.
*
* @see Art
*/
Registry<Art> ART = new SimpleRegistry<>(Art.class);
/**
* Server biomes.
*
* @see Biome
*/
Registry<Biome> BIOME = new SimpleRegistry<>(Biome.class);
/**
* Custom boss bars.
*
* @see Bukkit#getBossBar(org.bukkit.NamespacedKey)
* @see Bukkit#getBossBars()
*/
Registry<KeyedBossBar> BOSS_BARS = new Registry<KeyedBossBar>() {
@Nullable
@Override
public KeyedBossBar get(@NotNull NamespacedKey key) {
return Bukkit.getBossBar(key);
}
@NotNull
@Override
public Iterator<KeyedBossBar> iterator() {
return Bukkit.getBossBars();
}
};
/**
* Server enchantments.
*
* @see Enchantment#getByKey(org.bukkit.NamespacedKey)
*/
Registry<Enchantment> ENCHANTMENT = new Registry<Enchantment>() {
@Nullable
@Override
public Enchantment get(@NotNull NamespacedKey key) {
return Enchantment.getByKey(key);
}
@NotNull
@Override
public Iterator<Enchantment> iterator() {
return Arrays.asList(Enchantment.values()).iterator();
}
};
/**
* Server entity types.
*
* @see EntityType
*/
Registry<EntityType> ENTITY_TYPE = new SimpleRegistry<>(EntityType.class);
/**
* Default server loot tables.
*
* @see LootTables
*/
Registry<LootTables> LOOT_TABLES = new SimpleRegistry<>(LootTables.class);
/**
* Server materials.
*
* @see Material
*/
Registry<Material> MATERIAL = new SimpleRegistry<>(Material.class);
/**
* Server statistics.
*
* @see Statistic
*/
Registry<Statistic> STATISTIC = new SimpleRegistry<>(Statistic.class);
/**
* 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);
final static class SimpleRegistry<T extends Enum<T> & Keyed> implements Registry<T> {
private final Map<NamespacedKey, T> map;
protected SimpleRegistry(@NotNull Class<T> type) {
ImmutableMap.Builder<NamespacedKey, T> builder = ImmutableMap.builder();
for (T entry : type.getEnumConstants()) {
builder.put(entry.getKey(), entry);
}
map = builder.build();
}
@Nullable
@Override
public T get(@NotNull NamespacedKey key) {
return map.get(key);
}
@NotNull
@Override
public Iterator<T> iterator() {
return map.values().iterator();
}
}
}