Added plugin saveResources + saveDefaultConfig methods. This completes a bleeding branch by deltahat at http://forums.bukkit.org/threads/branch-complete-savedefaultconfig.48721/

By: Nathan Adams <dinnerbone@dinnerbone.com>
This commit is contained in:
Bukkit/Spigot 2011-12-22 21:18:36 +00:00
parent 7540ab7e68
commit 98960fd73e
2 changed files with 65 additions and 3 deletions

View file

@ -59,6 +59,23 @@ public interface Plugin extends CommandExecutor {
*/
public void saveConfig();
/**
* Saves the raw contents of the default config.yml file to the location retrievable by {@link #getConfig()}.
* If there is no default config.yml embedded in the plugin, an empty config.yml file is saved.
*/
public void saveDefaultConfig();
/**
* Saves the raw contents of any resource embedded with a plugin's .jar file assuming it can be found using
* {@link #getResource(String)}. The resource is saved into the plugin's data folder using the same hierarchy
* as the .jar file (subdirectories are preserved).
*
* @param resourcePath the embedded resource path to look for within the plugin's .jar file. (No preceding slash).
* @param replace if true, the embedded resource will overwrite the contents of an existing file.
* @throws IllegalArgumentException if the resource path is null, empty, or points to a nonexistent resource.
*/
public void saveResource(String resourcePath, boolean replace);
/**
* Discards any data in {@link #getConfig()} and reloads from disk.
*/

View file

@ -6,9 +6,8 @@ import com.avaje.ebean.config.DataSourceConfig;
import com.avaje.ebean.config.ServerConfig;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
import com.avaje.ebeaninternal.server.ddl.DdlGenerator;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
@ -146,6 +145,52 @@ public abstract class JavaPlugin implements Plugin {
}
}
public void saveDefaultConfig() {
saveResource("config.yml", false);
}
public void saveResource(String resourcePath, boolean replace) {
if(resourcePath == null || resourcePath.equals("")) {
throw new IllegalArgumentException("ResourcePath cannot be null or empty");
}
resourcePath = resourcePath.replace('\\', '/');
InputStream in = getResource(resourcePath);
if(in == null) {
throw new IllegalArgumentException("The embedded resource '" + resourcePath + "' cannot be found in " + getFile());
}
File outFile = new File(getDataFolder(), resourcePath);
int lastIndex = resourcePath.lastIndexOf('/');
File outDir = new File(getDataFolder(), resourcePath.substring(0, lastIndex >= 0 ? lastIndex : 0));
if(!outDir.exists()) {
outDir.mkdirs();
}
if(in == null) {
in = new ByteArrayInputStream(new byte[0]);
}
try {
if(!outFile.exists() || replace) {
OutputStream out = new FileOutputStream(outFile);
byte[] buf = new byte[1024];
int len;
while((len=in.read(buf))>0) {
out.write(buf,0,len);
}
out.close();
in.close();
} else {
Logger.getLogger(JavaPlugin.class.getName()).log(Level.WARNING, "Could not save " + outFile.getName() + " to " + outFile + " because " + outFile.getName() + " already exists.");
}
} catch (IOException ex) {
Logger.getLogger(JavaPlugin.class.getName()).log(Level.SEVERE, "Could not save " + outFile.getName() + " to " + outFile, ex);
}
}
public InputStream getResource(String filename) {
if (filename == null) {
throw new IllegalArgumentException("Filename cannot be null");