Skip to content

Commit

Permalink
saveDefaultConfig() doesn't error now. Biatch.
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed Jan 5, 2014
1 parent e11de7c commit 7e937fe
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 10 deletions.
14 changes: 13 additions & 1 deletion src/main/java/net/lahwran/bukkit/jython/PluginDataFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.lang.Boolean;

/**
* Used on initialization of a plugin, because I now have three different kinds
Expand All @@ -14,6 +15,8 @@
*/
public abstract class PluginDataFile {

public Boolean closed = true; // Whether the data file is closed or not.

/**
* Close up shop.
* @throws IOException thrown if closing fails
Expand All @@ -26,7 +29,7 @@ public void close() throws IOException {
* Get a stream for a file inside the datafile.
* @param filename name to get
* @return stream or null if file does not exist
* @throws IOException thrown if opening fails
* @throws IOException thrown if opening fails
*/
public abstract InputStream getStream(String filename) throws IOException;

Expand All @@ -39,4 +42,13 @@ public void close() throws IOException {
* @return true if an exception should be thrown for missing solid metadata
*/
public abstract boolean getNeedsSolidMeta();

/**
* Use this to reload any files.
* @author gdude2002
* @throws IOException thrown if reloading fails
*/
public void reload() throws IOException {
// This is an optionally overridable method.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.Boolean;

/**
* @author lahwran
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.lang.Boolean;

/**
* @author lahwran
Expand Down
33 changes: 29 additions & 4 deletions src/main/java/net/lahwran/bukkit/jython/PluginPythonZip.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
/**
*
* Why is there a comment up here?
*/
package net.lahwran.bukkit.jython;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.Boolean;
import java.lang.Override;
import java.lang.String;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

Expand All @@ -20,22 +23,44 @@ public class PluginPythonZip extends PluginDataFile {
/**
* Zipfile we belong to
*/
public final ZipFile zip;
public ZipFile zip;

/**
* Absolute path of the zipfile, for reloading purposes.
* @author gdude2002
*/
public String filepath = null;

/**
* @param file Zipfile we belong to
* @throws InvalidPluginException thrown if there is an error opening zip
*/
public PluginPythonZip(File file) throws InvalidPluginException {
filepath = file.getAbsolutePath(); // Store the path of the file
try {
zip = new ZipFile(file);
} catch (IOException e) {
this.reload();
}
catch (IOException e) {
throw new InvalidPluginException(e);
}
}

/**
* @throws IOException thrown if there is an error opening zip
*/

@Override
public void reload() throws IOException {
if (closed) {
File fh = new File(filepath);
zip = new ZipFile(fh);
closed = false;
}
}

public void close() throws IOException {
zip.close();
closed = true;
}

@Override
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/net/lahwran/bukkit/jython/PythonPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ public InputStream getResource(String filename) {
}

try {
dataFile.reload();
return dataFile.getStream(filename);
} catch (IOException e) {
//just return null and do not print stack trace as JavaPlugin's getResource does not print it either
Expand Down Expand Up @@ -375,13 +376,20 @@ public void saveResource(String resourcePath, boolean replace) {
}

public void reloadConfig() {
newConfig = YamlConfiguration.loadConfiguration(configFile);
if (configFile != null){
newConfig = YamlConfiguration.loadConfiguration(configFile);

InputStream defConfigStream = getResource("config.yml");
if (defConfigStream != null) {
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
InputStream defConfigStream = getResource("config.yml");
if (defConfigStream != null) {
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);

newConfig.setDefaults(defConfig);
newConfig.setDefaults(defConfig);
}
} else {
InputStream defConfigStream = getResource("config.yml");
if (defConfigStream != null) {
newConfig = YamlConfiguration.loadConfiguration(defConfigStream);
}
}
}

Expand Down

0 comments on commit 7e937fe

Please sign in to comment.