summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/main/java/org/bukkit/plugin/Plugin.java17
-rw-r--r--src/main/java/org/bukkit/plugin/java/JavaPlugin.java51
2 files changed, 65 insertions, 3 deletions
diff --git a/src/main/java/org/bukkit/plugin/Plugin.java b/src/main/java/org/bukkit/plugin/Plugin.java
index d9894c38..ff867e19 100644
--- a/src/main/java/org/bukkit/plugin/Plugin.java
+++ b/src/main/java/org/bukkit/plugin/Plugin.java
@@ -58,6 +58,23 @@ public interface Plugin extends CommandExecutor {
* Saves the {@link FileConfiguration} retrievable by {@link #getConfig()}.
*/
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.
diff --git a/src/main/java/org/bukkit/plugin/java/JavaPlugin.java b/src/main/java/org/bukkit/plugin/java/JavaPlugin.java
index 05349f3b..5ffde7ac 100644
--- a/src/main/java/org/bukkit/plugin/java/JavaPlugin.java
+++ b/src/main/java/org/bukkit/plugin/java/JavaPlugin.java
@@ -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;
@@ -145,6 +144,52 @@ public abstract class JavaPlugin implements Plugin {
Logger.getLogger(JavaPlugin.class.getName()).log(Level.SEVERE, "Could not save config to " + configFile, ex);
}
}
+
+ 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) {