geforkt von Mirrors/Paper
4ed27a6eca
By: md_5 <git@md-5.net>
177 Zeilen
4.6 KiB
Java
177 Zeilen
4.6 KiB
Java
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.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, (entity) -> entity != EntityType.UNKNOWN);
|
|
/**
|
|
* 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, (mat) -> !mat.isLegacy());
|
|
/**
|
|
* Server statistics.
|
|
*
|
|
* @see Statistic
|
|
*/
|
|
Registry<Statistic> STATISTIC = new SimpleRegistry<>(Statistic.class);
|
|
/**
|
|
* Villager profession.
|
|
*
|
|
* @see Villager.Profession
|
|
*/
|
|
Registry<Villager.Profession> VILLAGER_PROFESSION = new SimpleRegistry<>(Villager.Profession.class);
|
|
/**
|
|
* Villager type.
|
|
*
|
|
* @see Villager.Type
|
|
*/
|
|
Registry<Villager.Type> VILLAGER_TYPE = new SimpleRegistry<>(Villager.Type.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);
|
|
|
|
static final class SimpleRegistry<T extends Enum<T> & Keyed> implements Registry<T> {
|
|
|
|
private final Map<NamespacedKey, T> map;
|
|
|
|
protected SimpleRegistry(@NotNull Class<T> type) {
|
|
this(type, Predicates.<T>alwaysTrue());
|
|
}
|
|
|
|
protected SimpleRegistry(@NotNull Class<T> type, @NotNull Predicate<T> predicate) {
|
|
ImmutableMap.Builder<NamespacedKey, T> 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<T> iterator() {
|
|
return map.values().iterator();
|
|
}
|
|
}
|
|
}
|