SPIGOT-7011, SPIGOT-7065: Overhaul of structures
By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package org.bukkit.craftbukkit;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.stream.Stream;
|
||||
import net.minecraft.core.IRegistry;
|
||||
import net.minecraft.core.IRegistryCustom;
|
||||
import org.bukkit.Keyed;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.Registry;
|
||||
import org.bukkit.craftbukkit.generator.strucutre.CraftStructure;
|
||||
import org.bukkit.craftbukkit.generator.strucutre.CraftStructureType;
|
||||
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
|
||||
import org.bukkit.generator.structure.Structure;
|
||||
import org.bukkit.generator.structure.StructureType;
|
||||
|
||||
public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
|
||||
|
||||
public static <B extends Keyed> Registry<?> createRegistry(Class<B> bukkitClass, IRegistryCustom registryHolder) {
|
||||
if (bukkitClass == Structure.class) {
|
||||
return new CraftRegistry<>(registryHolder.registryOrThrow(IRegistry.STRUCTURE_REGISTRY), CraftStructure::new);
|
||||
}
|
||||
if (bukkitClass == StructureType.class) {
|
||||
return new CraftRegistry<>(IRegistry.STRUCTURE_TYPES, CraftStructureType::new);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private final Map<NamespacedKey, B> cache = new HashMap<>();
|
||||
private final IRegistry<M> minecraftRegistry;
|
||||
private final BiFunction<NamespacedKey, M, B> minecraftToBukkit;
|
||||
|
||||
public CraftRegistry(IRegistry<M> minecraftRegistry, BiFunction<NamespacedKey, M, B> minecraftToBukkit) {
|
||||
this.minecraftRegistry = minecraftRegistry;
|
||||
this.minecraftToBukkit = minecraftToBukkit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public B get(NamespacedKey namespacedKey) {
|
||||
B cached = cache.get(namespacedKey);
|
||||
if (cached != null) {
|
||||
return cached;
|
||||
}
|
||||
|
||||
B bukkit = createBukkit(namespacedKey, minecraftRegistry.getOptional(CraftNamespacedKey.toMinecraft(namespacedKey)).orElse(null));
|
||||
if (bukkit == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
cache.put(namespacedKey, bukkit);
|
||||
|
||||
return bukkit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<B> iterator() {
|
||||
return values().iterator();
|
||||
}
|
||||
|
||||
public B createBukkit(NamespacedKey namespacedKey, M minecraft) {
|
||||
if (minecraft == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return minecraftToBukkit.apply(namespacedKey, minecraft);
|
||||
}
|
||||
|
||||
public Stream<B> values() {
|
||||
return minecraftRegistry.keySet().stream().map(minecraftKey -> get(CraftNamespacedKey.fromMinecraft(minecraftKey)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user