mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-06-18 14:50:56 +00:00
api to access Loader
This commit is contained in:
@@ -21,6 +21,8 @@ public interface ConfigPack extends LoaderRegistrar, LoaderHolder, RegistryHolde
|
|||||||
|
|
||||||
void registerConfigType(ConfigType<?, ?> type, String id, int priority);
|
void registerConfigType(ConfigType<?, ?> type, String id, int priority);
|
||||||
|
|
||||||
|
Loader getLoader();
|
||||||
|
|
||||||
Set<TerraAddon> addons();
|
Set<TerraAddon> addons();
|
||||||
|
|
||||||
String getID();
|
String getID();
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package com.dfsek.terra.api.config;
|
||||||
|
|
||||||
|
import com.dfsek.tectonic.config.Configuration;
|
||||||
|
import com.dfsek.tectonic.exception.ConfigException;
|
||||||
|
import com.dfsek.terra.api.util.ExceptionalConsumer;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public interface Loader {
|
||||||
|
/**
|
||||||
|
* Do something with the InputStreams.
|
||||||
|
*
|
||||||
|
* @param consumer Something to do with the streams.
|
||||||
|
*/
|
||||||
|
Loader then(ExceptionalConsumer<List<Configuration>> consumer) throws ConfigException;
|
||||||
|
|
||||||
|
Loader thenNames(ExceptionalConsumer<List<String>> consumer) throws ConfigException;
|
||||||
|
|
||||||
|
Loader thenEntries(ExceptionalConsumer<Set<Map.Entry<String, InputStream>>> consumer) throws ConfigException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a single file from this Loader.
|
||||||
|
*
|
||||||
|
* @param singleFile File to get
|
||||||
|
* @return InputStream from file.
|
||||||
|
*/
|
||||||
|
InputStream get(String singleFile) throws IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Open a subdirectory.
|
||||||
|
*
|
||||||
|
* @param directory Directory to open
|
||||||
|
* @param extension
|
||||||
|
*/
|
||||||
|
Loader open(String directory, String extension);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close all InputStreams opened.
|
||||||
|
*/
|
||||||
|
Loader close();
|
||||||
|
}
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.dfsek.terra.config.fileloaders;
|
package com.dfsek.terra.api.util;
|
||||||
|
|
||||||
import com.dfsek.tectonic.exception.ConfigException;
|
import com.dfsek.tectonic.exception.ConfigException;
|
||||||
|
|
||||||
+1
-2
@@ -12,7 +12,7 @@ import java.util.stream.Stream;
|
|||||||
/**
|
/**
|
||||||
* Load all {@code *.yml} files from a {@link java.nio.file.Path}.
|
* 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;
|
private final Path path;
|
||||||
|
|
||||||
public FolderLoader(Path path) {
|
public FolderLoader(Path path) {
|
||||||
@@ -24,7 +24,6 @@ public class FolderLoader extends Loader {
|
|||||||
return new FileInputStream(new File(path.toFile(), singleFile));
|
return new FileInputStream(new File(path.toFile(), singleFile));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void load(String directory, String extension) {
|
protected void load(String directory, String extension) {
|
||||||
File newPath = new File(path.toFile(), directory);
|
File newPath = new File(path.toFile(), directory);
|
||||||
newPath.mkdirs();
|
newPath.mkdirs();
|
||||||
|
|||||||
+9
-12
@@ -2,6 +2,8 @@ package com.dfsek.terra.config.fileloaders;
|
|||||||
|
|
||||||
import com.dfsek.tectonic.config.Configuration;
|
import com.dfsek.tectonic.config.Configuration;
|
||||||
import com.dfsek.tectonic.exception.ConfigException;
|
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 com.dfsek.terra.api.util.GlueList;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -11,7 +13,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public abstract class Loader {
|
public abstract class LoaderImpl implements Loader {
|
||||||
protected final Map<String, InputStream> streams = new HashMap<>();
|
protected final Map<String, InputStream> streams = new HashMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -19,6 +21,7 @@ public abstract class Loader {
|
|||||||
*
|
*
|
||||||
* @param consumer Something to do with the streams.
|
* @param consumer Something to do with the streams.
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public Loader then(ExceptionalConsumer<List<Configuration>> consumer) throws ConfigException {
|
public Loader then(ExceptionalConsumer<List<Configuration>> consumer) throws ConfigException {
|
||||||
List<Configuration> list = new GlueList<>();
|
List<Configuration> list = new GlueList<>();
|
||||||
streams.forEach((id, stream) -> {
|
streams.forEach((id, stream) -> {
|
||||||
@@ -28,41 +31,35 @@ public abstract class Loader {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public Loader thenNames(ExceptionalConsumer<List<String>> consumer) throws ConfigException {
|
public Loader thenNames(ExceptionalConsumer<List<String>> consumer) throws ConfigException {
|
||||||
consumer.accept(new GlueList<>(streams.keySet()));
|
consumer.accept(new GlueList<>(streams.keySet()));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public Loader thenEntries(ExceptionalConsumer<Set<Map.Entry<String, InputStream>>> consumer) throws ConfigException {
|
public Loader thenEntries(ExceptionalConsumer<Set<Map.Entry<String, InputStream>>> consumer) throws ConfigException {
|
||||||
consumer.accept(streams.entrySet());
|
consumer.accept(streams.entrySet());
|
||||||
return this;
|
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.
|
* Open a subdirectory.
|
||||||
*
|
*
|
||||||
* @param directory Directory to open
|
* @param directory Directory to open
|
||||||
* @param extension
|
* @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");
|
if(streams.size() != 0) throw new IllegalStateException("Attempted to load new directory before closing existing InputStreams");
|
||||||
load(directory, extension);
|
load(directory, extension);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void load(String directory, String extension);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Close all InputStreams opened.
|
* Close all InputStreams opened.
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public Loader close() {
|
public Loader close() {
|
||||||
streams.forEach((name, input) -> {
|
streams.forEach((name, input) -> {
|
||||||
try {
|
try {
|
||||||
+1
-2
@@ -6,7 +6,7 @@ import java.util.Enumeration;
|
|||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipFile;
|
import java.util.zip.ZipFile;
|
||||||
|
|
||||||
public class ZIPLoader extends Loader {
|
public class ZIPLoader extends LoaderImpl {
|
||||||
private final ZipFile file;
|
private final ZipFile file;
|
||||||
|
|
||||||
public ZIPLoader(ZipFile file) {
|
public ZIPLoader(ZipFile file) {
|
||||||
@@ -23,7 +23,6 @@ public class ZIPLoader extends Loader {
|
|||||||
throw new IllegalArgumentException("No such file: " + singleFile);
|
throw new IllegalArgumentException("No such file: " + singleFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void load(String directory, String extension) {
|
protected void load(String directory, String extension) {
|
||||||
Enumeration<? extends ZipEntry> entries = file.entries();
|
Enumeration<? extends ZipEntry> entries = file.entries();
|
||||||
while(entries.hasMoreElements()) {
|
while(entries.hasMoreElements()) {
|
||||||
|
|||||||
+1
-1
@@ -3,7 +3,7 @@ package com.dfsek.terra.config.loaders.config;
|
|||||||
import com.dfsek.tectonic.exception.LoadException;
|
import com.dfsek.tectonic.exception.LoadException;
|
||||||
import com.dfsek.tectonic.loading.ConfigLoader;
|
import com.dfsek.tectonic.loading.ConfigLoader;
|
||||||
import com.dfsek.tectonic.loading.TypeLoader;
|
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 javax.imageio.ImageIO;
|
||||||
import java.awt.image.BufferedImage;
|
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.ConfigFactory;
|
||||||
import com.dfsek.terra.api.config.ConfigPack;
|
import com.dfsek.terra.api.config.ConfigPack;
|
||||||
import com.dfsek.terra.api.config.ConfigType;
|
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.ConfigPackPostLoadEvent;
|
||||||
import com.dfsek.terra.api.event.events.config.ConfigPackPreLoadEvent;
|
import com.dfsek.terra.api.event.events.config.ConfigPackPreLoadEvent;
|
||||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
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.api.world.biome.generation.BiomeProvider;
|
||||||
import com.dfsek.terra.config.dummy.DummyWorld;
|
import com.dfsek.terra.config.dummy.DummyWorld;
|
||||||
import com.dfsek.terra.config.fileloaders.FolderLoader;
|
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.fileloaders.ZIPLoader;
|
||||||
import com.dfsek.terra.config.loaders.config.BufferedImageLoader;
|
import com.dfsek.terra.config.loaders.config.BufferedImageLoader;
|
||||||
import com.dfsek.terra.config.prototype.ProtoConfig;
|
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));
|
configTypes.computeIfAbsent(priority, p -> new ArrayList<>()).add(ImmutablePair.of(id, type));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Loader getLoader() {
|
||||||
|
return loader;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<TerraAddon> addons() {
|
public Set<TerraAddon> addons() {
|
||||||
return template.getAddons();
|
return template.getAddons();
|
||||||
|
|||||||
Reference in New Issue
Block a user