move resource dumping to AbstractTerraPlugin

This commit is contained in:
dfsek
2021-07-30 08:09:37 -07:00
parent b407ca8821
commit 1dbcb031ea
3 changed files with 49 additions and 29 deletions

View File

@@ -30,4 +30,6 @@ public interface PluginConfig {
int getBiomeCache();
int getProviderCache();
boolean dumpDefaultConfig();
}

View File

@@ -27,10 +27,18 @@ import com.dfsek.terra.registry.master.AddonRegistry;
import com.dfsek.terra.registry.master.ConfigRegistry;
import com.dfsek.terra.util.logging.DebugLogger;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.yaml.snakeyaml.Yaml;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
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;
/**
@@ -80,6 +88,39 @@ public abstract class AbstractTerraPlugin implements TerraPlugin {
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
LangUtil.load(config.getLanguage(), this); // load language

View File

@@ -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"))) {
ConfigLoader loader = new ConfigLoader();
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) {
logger.severe("Failed to dump resources");
logger.severe("Failed to load config");
e.printStackTrace();
}
@@ -191,4 +163,9 @@ public class PluginConfigImpl implements ConfigTemplate, com.dfsek.terra.api.con
public int getProviderCache() {
return providerCache;
}
@Override
public boolean dumpDefaultConfig() {
return dumpDefaultData;
}
}