Stop throwing the exceptions and instead catch them earlier.

This commit is contained in:
sigonasr2 2016-07-06 23:24:50 -05:00
parent 0b8a86e6de
commit cae9917a8e
3 changed files with 37 additions and 20 deletions

Binary file not shown.

View File

@ -2,6 +2,7 @@ package sig.plugin.TwosideKeeper;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.nio.file.CopyOption; import java.nio.file.CopyOption;
@ -30,22 +31,39 @@ public class AutoUpdatePlugin implements Runnable {
@Override @Override
public void run() { public void run() {
try { FetchPlugins();
FetchPlugins();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
void FetchPlugins() throws IOException { void FetchPlugins(){
for (int i=0;i<plugins.size();i++) { for (int i=0;i<plugins.size();i++) {
FileUtils.copyURLToFile(new URL(plugins.get(i).url), new File(TwosideKeeper.filesave,"updates/"+plugins.get(i).name)); try {
FileUtils.copyURLToFile(new URL(plugins.get(i).url), new File(TwosideKeeper.filesave,"updates/"+plugins.get(i).name));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//After that's done, check the hash. //After that's done, check the hash.
FileInputStream file = new FileInputStream(new File(TwosideKeeper.filesave,"updates/"+plugins.get(i).name)); FileInputStream file = null;
String md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(file); try {
file.close(); file = new FileInputStream(new File(TwosideKeeper.filesave,"updates/"+plugins.get(i).name));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String md5 = null;
try {
md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
file.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (plugins.get(i).hash==null || !md5.equalsIgnoreCase(plugins.get(i).hash)) { if (plugins.get(i).hash==null || !md5.equalsIgnoreCase(plugins.get(i).hash)) {
//This plugin is different! Update the hash for it. Prepare for a restart of the server! //This plugin is different! Update the hash for it. Prepare for a restart of the server!
@ -55,8 +73,13 @@ public class AutoUpdatePlugin implements Runnable {
SaveHash(plugins.get(i)); SaveHash(plugins.get(i));
Bukkit.broadcastMessage("The server has detected a new version of "+ChatColor.YELLOW+plugins.get(i).name+". The server will restart in 3 minutes!"); Bukkit.broadcastMessage("The server has detected a new version of "+ChatColor.YELLOW+plugins.get(i).name+". The server will restart in 3 minutes!");
//Move the file to the new location. //Move the file to the new location.
FileUtils.copyFile(new File(TwosideKeeper.filesave,"updates/"+plugins.get(i).name), try {
new File(TwosideKeeper.filesave,"../"+plugins.get(i).name+".jar")); FileUtils.copyFile(new File(TwosideKeeper.filesave,"updates/"+plugins.get(i).name),
new File(TwosideKeeper.filesave,"../"+plugins.get(i).name+".jar"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
} }
if (restarting) { if (restarting) {

View File

@ -370,13 +370,7 @@ public class TwosideKeeper extends JavaPlugin implements Listener {
//Every 5 minutes, check for a plugin update. //Every 5 minutes, check for a plugin update.
getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){ getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
public void run(){ public void run(){
try { pluginupdater.FetchPlugins();
pluginupdater.FetchPlugins();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}, 20*300, 20*300); }}, 20*300, 20*300);
} }