mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-09 17:26:07 +00:00
api to access Loader
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
@@ -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 {
|
||||
@@ -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()) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user