mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 02:20:57 +00:00
CheckedRegistry API
This commit is contained in:
@@ -6,11 +6,14 @@ import com.dfsek.tectonic.exception.ConfigException;
|
||||
import com.dfsek.tectonic.exception.LoadException;
|
||||
import com.dfsek.tectonic.loading.ConfigLoader;
|
||||
import com.dfsek.tectonic.loading.TypeRegistry;
|
||||
import com.dfsek.tectonic.loading.object.ObjectTemplate;
|
||||
import com.dfsek.terra.api.LoaderRegistrar;
|
||||
import com.dfsek.terra.api.core.TerraPlugin;
|
||||
import com.dfsek.terra.api.core.event.events.config.ConfigPackPostLoadEvent;
|
||||
import com.dfsek.terra.api.core.event.events.config.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.platform.block.BlockData;
|
||||
import com.dfsek.terra.api.structures.loot.LootTable;
|
||||
import com.dfsek.terra.api.structures.parser.lang.functions.FunctionBuilder;
|
||||
import com.dfsek.terra.api.structures.script.StructureScript;
|
||||
import com.dfsek.terra.api.util.seeded.NoiseSeeded;
|
||||
import com.dfsek.terra.api.world.biome.TerraBiome;
|
||||
@@ -46,6 +49,7 @@ import com.dfsek.terra.config.templates.OreTemplate;
|
||||
import com.dfsek.terra.config.templates.PaletteTemplate;
|
||||
import com.dfsek.terra.config.templates.StructureTemplate;
|
||||
import com.dfsek.terra.config.templates.TreeTemplate;
|
||||
import com.dfsek.terra.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.registry.TerraRegistry;
|
||||
import com.dfsek.terra.registry.config.BiomeRegistry;
|
||||
import com.dfsek.terra.registry.config.CarverRegistry;
|
||||
@@ -75,6 +79,7 @@ import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.zip.ZipEntry;
|
||||
@@ -255,10 +260,6 @@ public class ConfigPack implements LoaderRegistrar {
|
||||
return structureRegistry.entries().stream().map(terraStructure -> terraStructure.getTemplate().getID()).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public TreeRegistry getTreeRegistry() {
|
||||
return treeRegistry;
|
||||
}
|
||||
|
||||
public ConfigPackTemplate getTemplate() {
|
||||
return template;
|
||||
}
|
||||
@@ -287,14 +288,6 @@ public class ConfigPack implements LoaderRegistrar {
|
||||
.registerLoader(ImageSamplerTemplate.class, () -> new ImageProviderTemplate(biomeRegistry));
|
||||
}
|
||||
|
||||
public ScriptRegistry getScriptRegistry() {
|
||||
return scriptRegistry;
|
||||
}
|
||||
|
||||
public BiomeRegistry getBiomeRegistry() {
|
||||
return biomeRegistry;
|
||||
}
|
||||
|
||||
public SamplerCache getSamplerCache() {
|
||||
return samplerCache;
|
||||
}
|
||||
@@ -307,35 +300,47 @@ public class ConfigPack implements LoaderRegistrar {
|
||||
return biomeProviderBuilder;
|
||||
}
|
||||
|
||||
public FunctionRegistry getFunctionRegistry() {
|
||||
return functionRegistry;
|
||||
public CheckedRegistry<StructureScript> getScriptRegistry() {
|
||||
return new CheckedRegistry<>(scriptRegistry);
|
||||
}
|
||||
|
||||
public NoiseRegistry getNormalizerRegistry() {
|
||||
return noiseRegistry;
|
||||
public CheckedRegistry<TerraBiome> getBiomeRegistry() {
|
||||
return new CheckedRegistry<>(biomeRegistry);
|
||||
}
|
||||
|
||||
public CarverRegistry getCarverRegistry() {
|
||||
return carverRegistry;
|
||||
public CheckedRegistry<Tree> getTreeRegistry() {
|
||||
return new CheckedRegistry<>(treeRegistry);
|
||||
}
|
||||
|
||||
public FloraRegistry getFloraRegistry() {
|
||||
return floraRegistry;
|
||||
public CheckedRegistry<FunctionBuilder<?>> getFunctionRegistry() {
|
||||
return new CheckedRegistry<>(functionRegistry);
|
||||
}
|
||||
|
||||
public LootRegistry getLootRegistry() {
|
||||
return lootRegistry;
|
||||
public CheckedRegistry<Supplier<ObjectTemplate<NoiseSeeded>>> getNormalizerRegistry() {
|
||||
return new CheckedRegistry<>(noiseRegistry);
|
||||
}
|
||||
|
||||
public OreRegistry getOreRegistry() {
|
||||
return oreRegistry;
|
||||
public CheckedRegistry<UserDefinedCarver> getCarverRegistry() {
|
||||
return new CheckedRegistry<>(carverRegistry);
|
||||
}
|
||||
|
||||
public PaletteRegistry getPaletteRegistry() {
|
||||
return paletteRegistry;
|
||||
public CheckedRegistry<Flora> getFloraRegistry() {
|
||||
return new CheckedRegistry<>(floraRegistry);
|
||||
}
|
||||
|
||||
public StructureRegistry getStructureRegistry() {
|
||||
return structureRegistry;
|
||||
public CheckedRegistry<LootTable> getLootRegistry() {
|
||||
return new CheckedRegistry<>(lootRegistry);
|
||||
}
|
||||
|
||||
public CheckedRegistry<Ore> getOreRegistry() {
|
||||
return new CheckedRegistry<>(oreRegistry);
|
||||
}
|
||||
|
||||
public CheckedRegistry<Palette<BlockData>> getPaletteRegistry() {
|
||||
return new CheckedRegistry<>(paletteRegistry);
|
||||
}
|
||||
|
||||
public CheckedRegistry<TerraStructure> getStructureRegistry() {
|
||||
return new CheckedRegistry<>(structureRegistry);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
package com.dfsek.terra.registry;
|
||||
|
||||
import com.dfsek.tectonic.exception.LoadException;
|
||||
import com.dfsek.tectonic.loading.ConfigLoader;
|
||||
import com.dfsek.tectonic.loading.TypeLoader;
|
||||
import com.dfsek.terra.registry.exception.DuplicateEntryException;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Wrapper for a registry that ensures checked access.
|
||||
*
|
||||
* @param <T> Type in registry
|
||||
*/
|
||||
public class CheckedRegistry<T> implements TypeLoader<T> {
|
||||
private final TerraRegistry<T> registry;
|
||||
|
||||
public CheckedRegistry(TerraRegistry<T> registry) {
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a value to this registry, checking whether it is present first.
|
||||
*
|
||||
* @param identifier Identifier to assign value.
|
||||
* @param value Value to add.
|
||||
* @throws DuplicateEntryException If an entry with the same identifier is already present.
|
||||
*/
|
||||
public void add(String identifier, T value) throws DuplicateEntryException {
|
||||
if(registry.contains(identifier)) throw new DuplicateEntryException("Entry \"" + identifier + "\" is already present in registry.");
|
||||
registry.addChecked(identifier, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a value to the registry, without checking presence beforehand.
|
||||
* <p>
|
||||
* Use of this method is generally discouraged, as it is bad practice to overwrite registry values.
|
||||
*
|
||||
* @param identifier Identifier to assign value.
|
||||
* @param value Value to add.
|
||||
* @deprecated Use of {@link #add(String, Object)} is encouraged.
|
||||
*/
|
||||
@Deprecated
|
||||
public void addUnchecked(String identifier, T value) {
|
||||
registry.add(identifier, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a value from the registry.
|
||||
*
|
||||
* @param identifier Identifier of value.
|
||||
* @return Value matching the identifier, {@code null} if no value is present.
|
||||
*/
|
||||
public T get(String identifier) {
|
||||
return registry.get(identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the registry contains a value.
|
||||
*
|
||||
* @param identifier Identifier of value.
|
||||
* @return Whether the registry contains the value.
|
||||
*/
|
||||
public boolean contains(String identifier) {
|
||||
return registry.contains(identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform the given action for every value in the registry.
|
||||
*
|
||||
* @param consumer Action to perform on value.
|
||||
*/
|
||||
public void forEach(Consumer<T> consumer) {
|
||||
registry.forEach(consumer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform an action for every key-value pair in the registry.
|
||||
*
|
||||
* @param consumer Action to perform on pair.
|
||||
*/
|
||||
public void forEach(BiConsumer<String, T> consumer) {
|
||||
registry.forEach(consumer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the entries of this registry as a {@link Set}.
|
||||
*
|
||||
* @return Set containing all entries.
|
||||
*/
|
||||
public Set<T> entries() {
|
||||
return registry.entries();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T load(Type t, Object c, ConfigLoader loader) throws LoadException {
|
||||
return registry.load(t, c, loader);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.dfsek.terra.registry.exception;
|
||||
|
||||
public class DuplicateEntryException extends Exception {
|
||||
private static final long serialVersionUID = -7199021672428288780L;
|
||||
|
||||
public DuplicateEntryException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public DuplicateEntryException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package com.dfsek.terra.registry.master;
|
||||
|
||||
import com.dfsek.terra.api.platform.world.generator.ChunkGenerator;
|
||||
import com.dfsek.terra.registry.TerraRegistry;
|
||||
|
||||
public class ChunkGeneratorRegistry extends TerraRegistry<ChunkGenerator> {
|
||||
}
|
||||
@@ -9,7 +9,6 @@ import com.dfsek.terra.api.util.FastRandom;
|
||||
import com.dfsek.terra.api.world.tree.Tree;
|
||||
import com.dfsek.terra.bukkit.world.BukkitAdapter;
|
||||
import com.dfsek.terra.config.pack.ConfigPack;
|
||||
import com.dfsek.terra.registry.config.TreeRegistry;
|
||||
import com.dfsek.terra.world.TerraWorld;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.TreeType;
|
||||
@@ -53,8 +52,7 @@ public class CommonListener implements Listener {
|
||||
Block block = e.getLocation().getBlock();
|
||||
BlockData data = block.getBlockData();
|
||||
block.setType(Material.AIR);
|
||||
TreeRegistry registry = c.getTreeRegistry();
|
||||
Tree tree = registry.get(TREE_TYPE_STRING_TRANSFORMER.translate(e.getSpecies()));
|
||||
Tree tree = c.getTreeRegistry().get(TREE_TYPE_STRING_TRANSFORMER.translate(e.getSpecies()));
|
||||
org.bukkit.Location location = e.getLocation();
|
||||
if(!tree.plant(new Location(bukkit, location.getX(), location.getY(), location.getZ()), new FastRandom())) block.setBlockData(data);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user