api to access Loader

This commit is contained in:
dfsek
2021-07-05 02:05:26 -07:00
parent 9f37285c9a
commit 58a5160d53
8 changed files with 66 additions and 19 deletions

View File

@@ -1,8 +0,0 @@
package com.dfsek.terra.config.fileloaders;
import com.dfsek.tectonic.exception.ConfigException;
@FunctionalInterface
public interface ExceptionalConsumer<T> {
void accept(T t) throws ConfigException;
}

View File

@@ -12,7 +12,7 @@ import java.util.stream.Stream;
/**
* Load all {@code *.yml} files from a {@link java.nio.file.Path}.
*/
public class FolderLoader extends Loader {
public class FolderLoader extends LoaderImpl {
private final Path path;
public FolderLoader(Path path) {
@@ -24,7 +24,6 @@ public class FolderLoader extends Loader {
return new FileInputStream(new File(path.toFile(), singleFile));
}
@Override
protected void load(String directory, String extension) {
File newPath = new File(path.toFile(), directory);
newPath.mkdirs();

View File

@@ -2,6 +2,8 @@ package com.dfsek.terra.config.fileloaders;
import com.dfsek.tectonic.config.Configuration;
import com.dfsek.tectonic.exception.ConfigException;
import com.dfsek.terra.api.config.Loader;
import com.dfsek.terra.api.util.ExceptionalConsumer;
import com.dfsek.terra.api.util.GlueList;
import java.io.IOException;
@@ -11,7 +13,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
public abstract class Loader {
public abstract class LoaderImpl implements Loader {
protected final Map<String, InputStream> streams = new HashMap<>();
/**
@@ -19,6 +21,7 @@ public abstract class Loader {
*
* @param consumer Something to do with the streams.
*/
@Override
public Loader then(ExceptionalConsumer<List<Configuration>> consumer) throws ConfigException {
List<Configuration> list = new GlueList<>();
streams.forEach((id, stream) -> {
@@ -28,41 +31,35 @@ public abstract class Loader {
return this;
}
@Override
public Loader thenNames(ExceptionalConsumer<List<String>> consumer) throws ConfigException {
consumer.accept(new GlueList<>(streams.keySet()));
return this;
}
@Override
public Loader thenEntries(ExceptionalConsumer<Set<Map.Entry<String, InputStream>>> consumer) throws ConfigException {
consumer.accept(streams.entrySet());
return this;
}
/**
* Get a single file from this Loader.
*
* @param singleFile File to get
* @return InputStream from file.
*/
public abstract InputStream get(String singleFile) throws IOException;
/**
* Open a subdirectory.
*
* @param directory Directory to open
* @param extension
*/
public Loader open(String directory, String extension) {
@Override
public LoaderImpl open(String directory, String extension) {
if(streams.size() != 0) throw new IllegalStateException("Attempted to load new directory before closing existing InputStreams");
load(directory, extension);
return this;
}
protected abstract void load(String directory, String extension);
/**
* Close all InputStreams opened.
*/
@Override
public Loader close() {
streams.forEach((name, input) -> {
try {

View File

@@ -6,7 +6,7 @@ import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class ZIPLoader extends Loader {
public class ZIPLoader extends LoaderImpl {
private final ZipFile file;
public ZIPLoader(ZipFile file) {
@@ -23,7 +23,6 @@ public class ZIPLoader extends Loader {
throw new IllegalArgumentException("No such file: " + singleFile);
}
@Override
protected void load(String directory, String extension) {
Enumeration<? extends ZipEntry> entries = file.entries();
while(entries.hasMoreElements()) {

View File

@@ -3,7 +3,7 @@ package com.dfsek.terra.config.loaders.config;
import com.dfsek.tectonic.exception.LoadException;
import com.dfsek.tectonic.loading.ConfigLoader;
import com.dfsek.tectonic.loading.TypeLoader;
import com.dfsek.terra.config.fileloaders.Loader;
import com.dfsek.terra.api.config.Loader;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

View File

@@ -17,6 +17,7 @@ import com.dfsek.terra.api.config.AbstractableTemplate;
import com.dfsek.terra.api.config.ConfigFactory;
import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.config.ConfigType;
import com.dfsek.terra.api.config.Loader;
import com.dfsek.terra.api.event.events.config.ConfigPackPostLoadEvent;
import com.dfsek.terra.api.event.events.config.ConfigPackPreLoadEvent;
import com.dfsek.terra.api.registry.CheckedRegistry;
@@ -31,7 +32,6 @@ import com.dfsek.terra.api.world.TerraWorld;
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
import com.dfsek.terra.config.dummy.DummyWorld;
import com.dfsek.terra.config.fileloaders.FolderLoader;
import com.dfsek.terra.config.fileloaders.Loader;
import com.dfsek.terra.config.fileloaders.ZIPLoader;
import com.dfsek.terra.config.loaders.config.BufferedImageLoader;
import com.dfsek.terra.config.prototype.ProtoConfig;
@@ -318,6 +318,11 @@ public class ConfigPackImpl implements ConfigPack {
configTypes.computeIfAbsent(priority, p -> new ArrayList<>()).add(ImmutablePair.of(id, type));
}
@Override
public Loader getLoader() {
return loader;
}
@Override
public Set<TerraAddon> addons() {
return template.getAddons();