mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-10-26 14:00:14 +00:00
move resource dumping to AbstractTerraPlugin
This commit is contained in:
1 parent
b407ca8821
commit
1dbcb031ea
3 files changed
+49
-29
No files matched your search
@@ -30,4 +30,6 @@ public interface PluginConfig {
|
|||||||
int getBiomeCache();
|
int getBiomeCache();
|
||||||
|
|
||||||
int getProviderCache();
|
int getProviderCache();
|
||||||
|
|
||||||
|
boolean dumpDefaultConfig();
|
||||||
}
|
}
|
||||||
@@ -27,10 +27,18 @@ import com.dfsek.terra.registry.master.AddonRegistry;
|
|||||||
import com.dfsek.terra.registry.master.ConfigRegistry;
|
import com.dfsek.terra.registry.master.ConfigRegistry;
|
||||||
import com.dfsek.terra.util.logging.DebugLogger;
|
import com.dfsek.terra.util.logging.DebugLogger;
|
||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.yaml.snakeyaml.Yaml;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.UncheckedIOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -80,6 +88,39 @@ public abstract class AbstractTerraPlugin implements TerraPlugin {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(config.dumpDefaultConfig()) {
|
||||||
|
try(InputStream resourcesConfig = getClass().getResourceAsStream("/resources.yml")) {
|
||||||
|
if(resourcesConfig == null) {
|
||||||
|
logger().info("No resources config found. Skipping resource dumping.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String resourceYaml = IOUtils.toString(resourcesConfig, StandardCharsets.UTF_8);
|
||||||
|
Map<String, List<String>> resources = new Yaml().load(resourceYaml);
|
||||||
|
resources.forEach((dir, entries) -> entries.forEach(entry -> {
|
||||||
|
String resourcePath = dir + "/" + entry;
|
||||||
|
File resource = new File(getDataFolder(), resourcePath);
|
||||||
|
if(resource.exists()) return; // dont overwrite
|
||||||
|
logger().info("Dumping resource " + resource.getAbsolutePath());
|
||||||
|
try {
|
||||||
|
resource.getParentFile().mkdirs();
|
||||||
|
resource.createNewFile();
|
||||||
|
} catch(IOException e) {
|
||||||
|
throw new UncheckedIOException(e);
|
||||||
|
}
|
||||||
|
try(InputStream is = getClass().getResourceAsStream("/" + resourcePath);
|
||||||
|
OutputStream os = new FileOutputStream(resource)) {
|
||||||
|
IOUtils.copy(is, os);
|
||||||
|
} catch(IOException e) {
|
||||||
|
throw new UncheckedIOException(e);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
} catch(IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
getDebugLogger().info("Skipping resource dumping.");
|
||||||
|
}
|
||||||
|
|
||||||
config.load(this); // load config.yml
|
config.load(this); // load config.yml
|
||||||
|
|
||||||
LangUtil.load(config.getLanguage(), this); // load language
|
LangUtil.load(config.getLanguage(), this); // load language
|
||||||
|
|||||||
@@ -88,36 +88,8 @@ public class PluginConfigImpl implements ConfigTemplate, com.dfsek.terra.api.con
|
|||||||
try(FileInputStream file = new FileInputStream(new File(main.getDataFolder(), "config.yml"))) {
|
try(FileInputStream file = new FileInputStream(new File(main.getDataFolder(), "config.yml"))) {
|
||||||
ConfigLoader loader = new ConfigLoader();
|
ConfigLoader loader = new ConfigLoader();
|
||||||
loader.load(this, new YamlConfiguration(file, "config.yml"));
|
loader.load(this, new YamlConfiguration(file, "config.yml"));
|
||||||
if(dumpDefaultData) {
|
|
||||||
try(InputStream resourcesConfig = getClass().getResourceAsStream("/resources.yml")) {
|
|
||||||
if(resourcesConfig == null) {
|
|
||||||
logger.info("No resources config found. Skipping resource dumping.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
String resourceYaml = IOUtils.toString(resourcesConfig, StandardCharsets.UTF_8);
|
|
||||||
Map<String, List<String>> resources = new Yaml().load(resourceYaml);
|
|
||||||
resources.forEach((dir, entries) -> entries.forEach(entry -> {
|
|
||||||
String resourcePath = dir + "/" + entry;
|
|
||||||
File resource = new File(main.getDataFolder(), resourcePath);
|
|
||||||
if(resource.exists()) return; // dont overwrite
|
|
||||||
main.logger().info("Dumping resource " + resource.getAbsolutePath());
|
|
||||||
try {
|
|
||||||
resource.getParentFile().mkdirs();
|
|
||||||
resource.createNewFile();
|
|
||||||
} catch(IOException e) {
|
|
||||||
throw new UncheckedIOException(e);
|
|
||||||
}
|
|
||||||
try(InputStream is = getClass().getResourceAsStream("/" + resourcePath);
|
|
||||||
OutputStream os = new FileOutputStream(resource)) {
|
|
||||||
IOUtils.copy(is, os);
|
|
||||||
} catch(IOException e) {
|
|
||||||
throw new UncheckedIOException(e);
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch(ConfigException | IOException | UncheckedIOException e) {
|
} catch(ConfigException | IOException | UncheckedIOException e) {
|
||||||
logger.severe("Failed to dump resources");
|
logger.severe("Failed to load config");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,4 +163,9 @@ public class PluginConfigImpl implements ConfigTemplate, com.dfsek.terra.api.con
|
|||||||
public int getProviderCache() {
|
public int getProviderCache() {
|
||||||
return providerCache;
|
return providerCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean dumpDefaultConfig() {
|
||||||
|
return dumpDefaultData;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user