Hold ConfigRegistry in Terra instance

This commit is contained in:
dfsek
2020-12-08 19:48:46 -07:00
parent 7e365c351d
commit 8f5a9b7b8e
9 changed files with 98 additions and 126 deletions

View File

@@ -1,26 +1,62 @@
package com.dfsek.terra;
import com.dfsek.tectonic.loading.TypeRegistry;
import com.dfsek.terra.biome.grid.master.TerraBiomeGrid;
import com.dfsek.terra.biome.palette.PaletteHolder;
import com.dfsek.terra.biome.palette.PaletteLayer;
import com.dfsek.terra.carving.CarverPalette;
import com.dfsek.terra.command.TerraCommand;
import com.dfsek.terra.command.structure.LocateCommand;
import com.dfsek.terra.config.base.PluginConfig;
import com.dfsek.terra.config.base.WorldConfig;
import com.dfsek.terra.config.lang.LangUtil;
import com.dfsek.terra.config.loaders.ImageLoaderLoader;
import com.dfsek.terra.config.loaders.MaterialSetLoader;
import com.dfsek.terra.config.loaders.ProbabilityCollectionLoader;
import com.dfsek.terra.config.loaders.RangeLoader;
import com.dfsek.terra.config.loaders.config.FloraLayerLoader;
import com.dfsek.terra.config.loaders.config.GridSpawnLoader;
import com.dfsek.terra.config.loaders.config.NoiseBuilderLoader;
import com.dfsek.terra.config.loaders.config.OreConfigLoader;
import com.dfsek.terra.config.loaders.config.OreHolderLoader;
import com.dfsek.terra.config.loaders.config.StructureFeatureLoader;
import com.dfsek.terra.config.loaders.config.TreeLayerLoader;
import com.dfsek.terra.config.loaders.palette.CarverPaletteLoader;
import com.dfsek.terra.config.loaders.palette.PaletteHolderLoader;
import com.dfsek.terra.config.loaders.palette.PaletteLayerLoader;
import com.dfsek.terra.debug.Debug;
import com.dfsek.terra.generation.TerraChunkGenerator;
import com.dfsek.terra.generation.config.NoiseBuilder;
import com.dfsek.terra.generation.items.flora.FloraLayer;
import com.dfsek.terra.generation.items.flora.TerraFlora;
import com.dfsek.terra.generation.items.ores.Ore;
import com.dfsek.terra.generation.items.ores.OreConfig;
import com.dfsek.terra.generation.items.ores.OreHolder;
import com.dfsek.terra.generation.items.tree.TreeLayer;
import com.dfsek.terra.image.ImageLoader;
import com.dfsek.terra.listeners.EventListener;
import com.dfsek.terra.listeners.SpigotListener;
import com.dfsek.terra.procgen.GridSpawn;
import com.dfsek.terra.registry.ConfigRegistry;
import com.dfsek.terra.util.ConfigUtil;
import com.dfsek.terra.structure.features.Feature;
import com.dfsek.terra.util.MaterialSet;
import com.dfsek.terra.util.PaperUtil;
import com.dfsek.terra.util.StructureTypeEnum;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Biome;
import org.bukkit.block.data.BlockData;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.EntityType;
import org.bukkit.generator.ChunkGenerator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.polydev.gaea.GaeaPlugin;
import org.polydev.gaea.generation.GaeaChunkGenerator;
import org.polydev.gaea.lang.Language;
import org.polydev.gaea.math.ProbabilityCollection;
import org.polydev.gaea.math.Range;
import java.util.HashMap;
import java.util.Map;
@@ -29,6 +65,7 @@ import java.util.Objects;
public class Terra extends GaeaPlugin {
private final Map<String, TerraChunkGenerator> generatorMap = new HashMap<>();
private final ConfigRegistry registry = new ConfigRegistry();
@Override
public void onDisable() {
@@ -38,7 +75,6 @@ public class Terra extends GaeaPlugin {
@Override
public void onEnable() {
Debug.setLogger(getLogger()); // Set debug logger.
ConfigUtil.setMain(this); // Set ConfigLoader's instance for use in some loaders
saveDefaultConfig();
@@ -50,7 +86,7 @@ public class Terra extends GaeaPlugin {
LangUtil.load(PluginConfig.getLanguage(), this); // Load language.
TerraWorld.invalidate(); // Clear/set up world cache.
ConfigRegistry.loadAll(this); // Load all config packs.
registry.loadAll(this); // Load all config packs.
PluginCommand c = Objects.requireNonNull(getCommand("terra"));
TerraCommand command = new TerraCommand(this); // Set up main Terra command.
@@ -95,4 +131,35 @@ public class Terra extends GaeaPlugin {
public Language getLanguage() {
return LangUtil.getLanguage();
}
public void registerAllLoaders(TypeRegistry registry) {
registry.registerLoader(ProbabilityCollection.class, new ProbabilityCollectionLoader())
.registerLoader(Range.class, new RangeLoader())
.registerLoader(CarverPalette.class, new CarverPaletteLoader())
.registerLoader(GridSpawn.class, new GridSpawnLoader())
.registerLoader(PaletteHolder.class, new PaletteHolderLoader())
.registerLoader(PaletteLayer.class, new PaletteLayerLoader())
.registerLoader(Biome.class, (t, o, l) -> Biome.valueOf((String) o))
.registerLoader(BlockData.class, (t, o, l) -> Bukkit.createBlockData((String) o))
.registerLoader(Material.class, (t, o, l) -> Material.matchMaterial((String) o))
.registerLoader(FloraLayer.class, new FloraLayerLoader())
.registerLoader(Ore.Type.class, (t, o, l) -> Ore.Type.valueOf((String) o))
.registerLoader(OreConfig.class, new OreConfigLoader())
.registerLoader(NoiseBuilder.class, new NoiseBuilderLoader())
.registerLoader(TreeLayer.class, new TreeLayerLoader(this))
.registerLoader(MaterialSet.class, new MaterialSetLoader())
.registerLoader(OreHolder.class, new OreHolderLoader())
.registerLoader(Feature.class, new StructureFeatureLoader())
.registerLoader(ImageLoader.class, new ImageLoaderLoader())
.registerLoader(EntityType.class, (t, o, l) -> EntityType.valueOf((String) o))
.registerLoader(TerraBiomeGrid.Type.class, (t, o, l) -> TerraBiomeGrid.Type.valueOf((String) o))
.registerLoader(StructureTypeEnum.class, (t, o, l) -> StructureTypeEnum.valueOf((String) o))
.registerLoader(ImageLoader.Channel.class, (t, o, l) -> ImageLoader.Channel.valueOf((String) o))
.registerLoader(ImageLoader.Align.class, (t, o, l) -> ImageLoader.Align.valueOf((String) o))
.registerLoader(TerraFlora.Search.class, (t, o, l) -> TerraFlora.Search.valueOf((String) o));
}
public ConfigRegistry getRegistry() {
return registry;
}
}

View File

@@ -71,9 +71,6 @@ public class TerraWorld {
public static synchronized void invalidate() {
map.clear();
for(WorldConfig config : loaded.values()) {
config.load(); // Reload all stored WorldConfigs
}
}
public static int numWorlds() {

View File

@@ -1,5 +1,6 @@
package com.dfsek.terra.command;
import com.dfsek.terra.Terra;
import com.dfsek.terra.config.base.ConfigPackTemplate;
import com.dfsek.terra.config.lang.LangUtil;
import com.dfsek.terra.registry.ConfigRegistry;
@@ -28,7 +29,7 @@ public class PacksCommand extends Command {
@Override
public boolean execute(@NotNull CommandSender commandSender, org.bukkit.command.@NotNull Command command, @NotNull String s, @NotNull String[] strings) {
ConfigRegistry registry = ConfigRegistry.getRegistry();
ConfigRegistry registry = ((Terra) getMain()).getRegistry();
if(registry.entries().size() == 0) {
LangUtil.send("command.packs.none", commandSender);

View File

@@ -1,9 +1,9 @@
package com.dfsek.terra.command;
import com.dfsek.terra.Terra;
import com.dfsek.terra.TerraWorld;
import com.dfsek.terra.config.base.PluginConfig;
import com.dfsek.terra.config.lang.LangUtil;
import com.dfsek.terra.registry.ConfigRegistry;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.polydev.gaea.command.Command;
@@ -31,7 +31,7 @@ public class ReloadCommand extends Command implements DebugCommand {
public boolean execute(@NotNull CommandSender sender, org.bukkit.command.@NotNull Command command, @NotNull String label, @NotNull String[] args) {
PluginConfig.load(getMain());
LangUtil.load(PluginConfig.getLanguage(), getMain()); // Load language.
if(!ConfigRegistry.loadAll(getMain())) LangUtil.send("command.reload-error", sender);
if(!((Terra) getMain()).getRegistry().loadAll((Terra) getMain())) LangUtil.send("command.reload-error", sender);
TerraWorld.invalidate();
LangUtil.send("command.reload", sender);
return true;

View File

@@ -4,6 +4,7 @@ import com.dfsek.tectonic.abstraction.AbstractConfigLoader;
import com.dfsek.tectonic.exception.ConfigException;
import com.dfsek.tectonic.exception.LoadException;
import com.dfsek.tectonic.loading.ConfigLoader;
import com.dfsek.terra.Terra;
import com.dfsek.terra.biome.UserDefinedBiome;
import com.dfsek.terra.biome.grid.master.TerraBiomeGrid;
import com.dfsek.terra.carving.UserDefinedCarver;
@@ -45,7 +46,6 @@ import com.dfsek.terra.registry.StructureRegistry;
import com.dfsek.terra.registry.TerraRegistry;
import com.dfsek.terra.registry.TreeRegistry;
import com.dfsek.terra.structure.Structure;
import com.dfsek.terra.util.ConfigUtil;
import com.dfsek.terra.util.StructureTypeEnum;
import org.polydev.gaea.biome.Biome;
import org.polydev.gaea.structures.loot.LootTable;
@@ -100,13 +100,14 @@ public class ConfigPack {
.registerLoader(Ore.class, oreRegistry)
.registerLoader(Tree.class, treeRegistry)
.registerLoader(TerraStructure.class, structureRegistry);
ConfigUtil.registerAllLoaders(abstractConfigLoader);
ConfigUtil.registerAllLoaders(selfLoader);
}
public ConfigPack(File folder) throws ConfigException {
public ConfigPack(File folder, Terra main) throws ConfigException {
long l = System.nanoTime();
main.registerAllLoaders(selfLoader);
main.registerAllLoaders(abstractConfigLoader);
File pack = new File(folder, "pack.yml");
try {
@@ -118,9 +119,12 @@ public class ConfigPack {
load(new FolderLoader(folder.toPath()), l);
}
public ConfigPack(ZipFile file) throws ConfigException {
public ConfigPack(ZipFile file, Terra main) throws ConfigException {
long l = System.nanoTime();
main.registerAllLoaders(selfLoader);
main.registerAllLoaders(abstractConfigLoader);
InputStream stream = null;
try {

View File

@@ -6,11 +6,9 @@ import com.dfsek.tectonic.config.ValidatedConfigTemplate;
import com.dfsek.tectonic.exception.ConfigException;
import com.dfsek.tectonic.exception.ValidationException;
import com.dfsek.tectonic.loading.ConfigLoader;
import com.dfsek.terra.Terra;
import com.dfsek.terra.image.ImageLoader;
import com.dfsek.terra.registry.ConfigRegistry;
import com.dfsek.terra.util.ConfigUtil;
import org.apache.commons.io.FileUtils;
import org.polydev.gaea.GaeaPlugin;
import java.io.File;
import java.io.FileInputStream;
@@ -20,13 +18,8 @@ import java.util.Objects;
public class WorldConfig implements ValidatedConfigTemplate {
private static final ConfigLoader LOADER = new ConfigLoader();
static {
ConfigUtil.registerAllLoaders(LOADER);
}
private final String worldID;
private final String configID;
private final GaeaPlugin main;
@Value("image.enable")
@Default
public boolean fromImage = false;
@@ -46,15 +39,15 @@ public class WorldConfig implements ValidatedConfigTemplate {
public ImageLoader imageLoader = null;
private ConfigPack tConfig;
public WorldConfig(String w, String configID, GaeaPlugin main) {
public WorldConfig(String w, String configID, Terra main) {
main.registerAllLoaders(LOADER);
this.worldID = w;
this.configID = configID;
this.main = main;
load();
load(main);
}
public void load() {
tConfig = ConfigRegistry.getRegistry().get(configID);
public void load(Terra main) {
tConfig = main.getRegistry().get(configID);
if(tConfig == null) throw new IllegalStateException("No such config pack \"" + configID + "\"");

View File

@@ -124,7 +124,7 @@ public class TerraChunkGenerator extends GaeaChunkGenerator {
paletteLevel++;
} else if(y <= sea) {
chunk.setBlock(x, y, z, seaPalette.get(sea - y, x + xOrig, z + zOrig));
if(!(justSet && c.doSlabs())) {
if(justSet && c.doSlabs()) {
SlabUtil.prepareBlockPartCeiling(data, chunk.getBlockData(x, y, z), chunk, new Vector(x, y, z), c.getSlabPalettes(), c.getStairPalettes(), c.getSlabThreshold(), sampler);
}
justSet = false;

View File

@@ -1,9 +1,9 @@
package com.dfsek.terra.registry;
import com.dfsek.tectonic.exception.ConfigException;
import com.dfsek.terra.Terra;
import com.dfsek.terra.config.base.ConfigPack;
import com.dfsek.terra.debug.Debug;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
@@ -13,28 +13,17 @@ import java.util.zip.ZipFile;
* Class to hold config packs
*/
public class ConfigRegistry extends TerraRegistry<ConfigPack> {
private static ConfigRegistry singleton;
private ConfigRegistry() {
}
public static ConfigRegistry getRegistry() {
if(singleton == null) singleton = new ConfigRegistry();
return singleton;
}
public void load(File folder) throws ConfigException {
ConfigPack pack = new ConfigPack(folder);
public void load(File folder, Terra main) throws ConfigException {
ConfigPack pack = new ConfigPack(folder, main);
add(pack.getTemplate().getID(), pack);
}
public static boolean loadAll(JavaPlugin main) {
public boolean loadAll(Terra main) {
boolean valid = true;
File packsFolder = new File(main.getDataFolder(), "packs");
for(File dir : packsFolder.listFiles(File::isDirectory)) {
try {
getRegistry().load(dir);
load(dir, main);
} catch(ConfigException e) {
e.printStackTrace();
valid = false;
@@ -43,7 +32,7 @@ public class ConfigRegistry extends TerraRegistry<ConfigPack> {
for(File zip : packsFolder.listFiles(file -> file.getName().endsWith(".zip") || file.getName().endsWith(".jar") || file.getName().endsWith(".terra"))) {
try {
Debug.info("Loading ZIP archive: " + zip.getName());
getRegistry().load(new ZipFile(zip));
load(new ZipFile(zip), main);
} catch(IOException | ConfigException e) {
e.printStackTrace();
valid = false;
@@ -52,8 +41,8 @@ public class ConfigRegistry extends TerraRegistry<ConfigPack> {
return valid;
}
public void load(ZipFile file) throws ConfigException {
ConfigPack pack = new ConfigPack(file);
public void load(ZipFile file, Terra main) throws ConfigException {
ConfigPack pack = new ConfigPack(file, main);
add(pack.getTemplate().getID(), pack);
}
}

View File

@@ -1,79 +0,0 @@
package com.dfsek.terra.util;
import com.dfsek.tectonic.loading.TypeRegistry;
import com.dfsek.terra.biome.grid.master.TerraBiomeGrid;
import com.dfsek.terra.biome.palette.PaletteHolder;
import com.dfsek.terra.biome.palette.PaletteLayer;
import com.dfsek.terra.carving.CarverPalette;
import com.dfsek.terra.config.loaders.ImageLoaderLoader;
import com.dfsek.terra.config.loaders.MaterialSetLoader;
import com.dfsek.terra.config.loaders.ProbabilityCollectionLoader;
import com.dfsek.terra.config.loaders.RangeLoader;
import com.dfsek.terra.config.loaders.config.FloraLayerLoader;
import com.dfsek.terra.config.loaders.config.GridSpawnLoader;
import com.dfsek.terra.config.loaders.config.NoiseBuilderLoader;
import com.dfsek.terra.config.loaders.config.OreConfigLoader;
import com.dfsek.terra.config.loaders.config.OreHolderLoader;
import com.dfsek.terra.config.loaders.config.StructureFeatureLoader;
import com.dfsek.terra.config.loaders.config.TreeLayerLoader;
import com.dfsek.terra.config.loaders.palette.CarverPaletteLoader;
import com.dfsek.terra.config.loaders.palette.PaletteHolderLoader;
import com.dfsek.terra.config.loaders.palette.PaletteLayerLoader;
import com.dfsek.terra.generation.config.NoiseBuilder;
import com.dfsek.terra.generation.items.flora.FloraLayer;
import com.dfsek.terra.generation.items.flora.TerraFlora;
import com.dfsek.terra.generation.items.ores.Ore;
import com.dfsek.terra.generation.items.ores.OreConfig;
import com.dfsek.terra.generation.items.ores.OreHolder;
import com.dfsek.terra.generation.items.tree.TreeLayer;
import com.dfsek.terra.image.ImageLoader;
import com.dfsek.terra.procgen.GridSpawn;
import com.dfsek.terra.structure.features.Feature;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Biome;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.EntityType;
import org.polydev.gaea.GaeaPlugin;
import org.polydev.gaea.math.ProbabilityCollection;
import org.polydev.gaea.math.Range;
public final class ConfigUtil {
private static GaeaPlugin main;
public static void setMain(GaeaPlugin main) {
ConfigUtil.main = main;
}
/**
* Register all Terra loaders to a {@link TypeRegistry}.
*
* @param registry Registry.
*/
public static void registerAllLoaders(TypeRegistry registry) {
registry.registerLoader(ProbabilityCollection.class, new ProbabilityCollectionLoader())
.registerLoader(Range.class, new RangeLoader())
.registerLoader(CarverPalette.class, new CarverPaletteLoader())
.registerLoader(GridSpawn.class, new GridSpawnLoader())
.registerLoader(PaletteHolder.class, new PaletteHolderLoader())
.registerLoader(PaletteLayer.class, new PaletteLayerLoader())
.registerLoader(Biome.class, (t, o, l) -> Biome.valueOf((String) o))
.registerLoader(BlockData.class, (t, o, l) -> Bukkit.createBlockData((String) o))
.registerLoader(Material.class, (t, o, l) -> Material.matchMaterial((String) o))
.registerLoader(FloraLayer.class, new FloraLayerLoader())
.registerLoader(Ore.Type.class, (t, o, l) -> Ore.Type.valueOf((String) o))
.registerLoader(OreConfig.class, new OreConfigLoader())
.registerLoader(NoiseBuilder.class, new NoiseBuilderLoader())
.registerLoader(TreeLayer.class, new TreeLayerLoader(main))
.registerLoader(MaterialSet.class, new MaterialSetLoader())
.registerLoader(OreHolder.class, new OreHolderLoader())
.registerLoader(Feature.class, new StructureFeatureLoader())
.registerLoader(ImageLoader.class, new ImageLoaderLoader())
.registerLoader(EntityType.class, (t, o, l) -> EntityType.valueOf((String) o))
.registerLoader(TerraBiomeGrid.Type.class, (t, o, l) -> TerraBiomeGrid.Type.valueOf((String) o))
.registerLoader(StructureTypeEnum.class, (t, o, l) -> StructureTypeEnum.valueOf((String) o))
.registerLoader(ImageLoader.Channel.class, (t, o, l) -> ImageLoader.Channel.valueOf((String) o))
.registerLoader(ImageLoader.Align.class, (t, o, l) -> ImageLoader.Align.valueOf((String) o))
.registerLoader(TerraFlora.Search.class, (t, o, l) -> TerraFlora.Search.valueOf((String) o));
}
}