Made large improvements to grabbing plugins. Saves a lot of bandwidth by

now checking the file size before attempting any downloading.
master
sigonasr2 8 years ago
parent 729d72e6bd
commit bb9cf0634f
  1. BIN
      AutoPluginUpdate.jar
  2. 18
      src/sig/plugin/AutoPluginUpdate/AutoPluginUpdate.java
  3. 129
      src/sig/plugin/AutoPluginUpdate/PluginManager.java

Binary file not shown.

@ -15,19 +15,19 @@ import org.bukkit.plugin.java.JavaPlugin;
public class AutoPluginUpdate extends JavaPlugin implements Listener{ public class AutoPluginUpdate extends JavaPlugin implements Listener{
public final String LOG_PREFIX = "[AutoPluginUpdate]"; public final static String LOG_PREFIX = "[AutoPluginUpdate]";
public AutoPluginUpdate plugin = this; public AutoPluginUpdate plugin = this;
public static File datafolder; public static File datafolder;
public static boolean restarting_server=false; public static boolean restarting_server=false;
public static boolean main_server=false; public static boolean main_server=false;
public final int LOG_ERROR=0; public final static int LOG_ERROR=0;
public final int LOG_WARNING=1; public final static int LOG_WARNING=1;
public final int LOG_NORMAL=2; public final static int LOG_NORMAL=2;
public final int LOG_DETAIL=3; public final static int LOG_DETAIL=3;
public final int LOG_VERBOSE=4; public final static int LOG_VERBOSE=4;
public final int LOG_DEBUG=5; public final static int LOG_DEBUG=5;
public static PluginManager pluginupdater; public static PluginManager pluginupdater;
@ -86,7 +86,7 @@ public class AutoPluginUpdate extends JavaPlugin implements Listener{
} }
} }
private void log(String msg, int loglv) { public static void log(String msg, int loglv) {
switch (loglv) { switch (loglv) {
case LOG_ERROR:{ case LOG_ERROR:{
Bukkit.getConsoleSender().sendMessage(LOG_PREFIX+" "+ChatColor.RED+"[ERROR]"+ChatColor.RESET+msg+ChatColor.RESET); Bukkit.getConsoleSender().sendMessage(LOG_PREFIX+" "+ChatColor.RED+"[ERROR]"+ChatColor.RESET+msg+ChatColor.RESET);
@ -105,7 +105,7 @@ public class AutoPluginUpdate extends JavaPlugin implements Listener{
public void run() { public void run() {
if (Bukkit.getOnlinePlayers().size()==0 && restarting_server) { if (Bukkit.getOnlinePlayers().size()==0 && restarting_server) {
Bukkit.savePlayers(); Bukkit.savePlayers();
BroadcastMessage(ChatColor.ITALIC+"Server is shutting down..."); //BroadcastMessage(ChatColor.ITALIC+"Server is shutting down...");
for (int i=0;i<Bukkit.getWorlds().size();i++) { for (int i=0;i<Bukkit.getWorlds().size();i++) {
Bukkit.getWorlds().get(i).save(); Bukkit.getWorlds().get(i).save();
} }

@ -4,6 +4,8 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -31,7 +33,7 @@ public class PluginManager implements Runnable{
void FetchPlugins(){ void FetchPlugins(){
for (int i=0;i<plugins.size();i++) { for (int i=0;i<plugins.size();i++) {
try { /*try {
FileUtils.copyURLToFile(new URL(plugins.get(i).url), new File(AutoPluginUpdate.datafolder,"updates/"+plugins.get(i).name)); FileUtils.copyURLToFile(new URL(plugins.get(i).url), new File(AutoPluginUpdate.datafolder,"updates/"+plugins.get(i).name));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -54,41 +56,90 @@ public class PluginManager implements Runnable{
file.close(); file.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
}*/
URL url = null;
try {
url = new URL(plugins.get(i).url);
} catch (MalformedURLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} }
if (url!=null) {
if ((plugins.get(i).hash==null || !md5.equalsIgnoreCase(plugins.get(i).hash))) { //AutoPluginUpdate.log("Valid URL. ", AutoPluginUpdate.LOG_NORMAL);
//This plugin is different! Update the hash for it. Prepare for a restart of the server! HttpURLConnection httpCon = null;
final int ii=i; try {
Bukkit.getScheduler().scheduleSyncDelayedTask(Bukkit.getPluginManager().getPlugin("AutoPluginUpdate"), new Runnable() { httpCon = (HttpURLConnection) url.openConnection();
@Override } catch (IOException e1) {
public void run() { // TODO Auto-generated catch block
if (!AutoPluginUpdate.restarting_server) { e1.printStackTrace();
if (Bukkit.getOnlinePlayers().size()!=0) { }
AutoPluginUpdate.BroadcastMessage("The server has detected a new version of "+ChatColor.YELLOW+plugins.get(ii).name+". The server will restart in 3 minutes!\n\n"+ChatColor.GRAY+ChatColor.ITALIC+"If all players leave, the update will occur immediately."); if (httpCon!=null) {
} else { //AutoPluginUpdate.log("Connection is good. ", AutoPluginUpdate.LOG_NORMAL);
AutoPluginUpdate.BroadcastMessage("The server has detected a new version of "+ChatColor.YELLOW+plugins.get(ii).name+"."); long lastModifiedDate = httpCon.getContentLengthLong();
} //AutoPluginUpdate.log("Last Modified Date Comparison: "+plugins.get(i).lastmodified+","+lastModifiedDate, AutoPluginUpdate.LOG_NORMAL);
restarting=true; if (plugins.get(i).lastmodified!=lastModifiedDate) {
AutoPluginUpdate.restarting_server=true; //This plugin is different! Update the hash for it. Prepare for a restart of the server!
//Save the new plugin hash. final int ii=i;
try {
FileUtils.copyURLToFile(new URL(plugins.get(i).url), new File(AutoPluginUpdate.datafolder,"updates/"+plugins.get(i).name));
} catch (IOException e) {
e.printStackTrace();
}
} else { //After that's done, check the hash.
AutoPluginUpdate.BroadcastMessage("The server has detected a new version of "+ChatColor.YELLOW+plugins.get(ii).name+"."); FileInputStream file = null;
try {
file = new FileInputStream(new File(AutoPluginUpdate.datafolder,"updates/"+plugins.get(i).name));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String md5 = null;
try {
md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(file);
} catch (IOException e) {
e.printStackTrace();
}
try {
file.close();
} catch (IOException e) {
e.printStackTrace();
} }
if (restarting) { if (!md5.equalsIgnoreCase(plugins.get(i).hash)) {
AutoPluginUpdate.updateServer(); //AutoPluginUpdate.log("Last Modified Date Comparison: "+plugins.get(i).lastmodified+","+lastModifiedDate, AutoPluginUpdate.LOG_NORMAL);
Bukkit.getScheduler().scheduleSyncDelayedTask(Bukkit.getPluginManager().getPlugin("AutoPluginUpdate"), new Runnable() {
@Override
public void run() {
if (!AutoPluginUpdate.restarting_server) {
if (Bukkit.getOnlinePlayers().size()!=0) {
AutoPluginUpdate.BroadcastMessage("The server has detected a new version of "+ChatColor.YELLOW+plugins.get(ii).name+". The server will restart in 3 minutes!\n\n"+ChatColor.GRAY+ChatColor.ITALIC+"If all players leave, the update will occur immediately.");
} else {
AutoPluginUpdate.BroadcastMessage("The server has detected a new version of "+ChatColor.YELLOW+plugins.get(ii).name+".");
}
restarting=true;
AutoPluginUpdate.restarting_server=true;
//Save the new plugin hash.
} else {
AutoPluginUpdate.BroadcastMessage("The server has detected a new version of "+ChatColor.YELLOW+plugins.get(ii).name+".");
}
if (restarting) {
AutoPluginUpdate.updateServer();
}
}},1);
} }
}},1); plugins.get(i).hash = md5;
plugins.get(i).hash = md5; plugins.get(i).lastmodified = lastModifiedDate;
SaveHash(plugins.get(i)); SaveHash(plugins.get(i));
//Move the file to the new location. //Move the file to the new location.
/*try { /*try {
FileUtils.copyFile(new File(AutoPluginUpdate.datafolder,"updates/"+plugins.get(i).name), FileUtils.copyFile(new File(AutoPluginUpdate.datafolder,"updates/"+plugins.get(i).name),
new File(AutoPluginUpdate.datafolder,"../"+plugins.get(i).name+".jar")); new File(AutoPluginUpdate.datafolder,"../"+plugins.get(i).name+".jar"));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
}*/ }*/
}
}
} }
} }
} }
@ -102,11 +153,13 @@ public class PluginManager implements Runnable{
File config = new File(AutoPluginUpdate.datafolder,"hashes.data"); File config = new File(AutoPluginUpdate.datafolder,"hashes.data");
FileConfiguration workable = YamlConfiguration.loadConfiguration(config); FileConfiguration workable = YamlConfiguration.loadConfiguration(config);
pluginname.hash = workable.getString(pluginname.name+"/HASH"); pluginname.hash = workable.getString(pluginname.name+"/HASH");
pluginname.lastmodified = workable.getLong(pluginname.name+"/HASHMOD");
} }
public void SaveHash(Plugin pluginname) { public void SaveHash(Plugin pluginname) {
File config = new File(AutoPluginUpdate.datafolder,"hashes.data"); File config = new File(AutoPluginUpdate.datafolder,"hashes.data");
FileConfiguration workable = YamlConfiguration.loadConfiguration(config); FileConfiguration workable = YamlConfiguration.loadConfiguration(config);
workable.set(pluginname.name+"/HASH",pluginname.hash); workable.set(pluginname.name+"/HASH",pluginname.hash);
workable.set(pluginname.name+"/HASHMOD",pluginname.lastmodified);
try { try {
workable.save(config); workable.save(config);
} catch (IOException e) { } catch (IOException e) {
@ -118,6 +171,7 @@ public class PluginManager implements Runnable{
FileConfiguration workable = YamlConfiguration.loadConfiguration(config); FileConfiguration workable = YamlConfiguration.loadConfiguration(config);
for (int i=0;i<pluginlist.size();i++) { for (int i=0;i<pluginlist.size();i++) {
workable.set(pluginlist.get(i).name+"/HASH",pluginlist.get(i).hash); workable.set(pluginlist.get(i).name+"/HASH",pluginlist.get(i).hash);
workable.set(pluginlist.get(i).name+"/HASHMOD",pluginlist.get(i).lastmodified);
} }
try { try {
workable.save(config); workable.save(config);
@ -131,17 +185,20 @@ class Plugin {
String name; String name;
String hash; String hash;
String url; String url;
long lastmodified=0;
public Plugin(String name,String url) { public Plugin(String name,String url) {
this.name=name; this.name=name;
this.url=url; this.url=url;
this.hash=FetchHash(); //Try to fetch the hash. this.hash=FetchHash(); //Try to fetch the hash.
this.lastmodified=FetchLastModified();
} }
public Plugin(String name,String hash,String url) { public Plugin(String name,String hash,long lastmodified,String url) {
this.name=name; this.name=name;
this.url=url; this.url=url;
this.hash=hash; this.hash=hash;
this.lastmodified=lastmodified;
} }
public String FetchHash() { public String FetchHash() {
@ -149,4 +206,10 @@ class Plugin {
FileConfiguration workable = YamlConfiguration.loadConfiguration(config); FileConfiguration workable = YamlConfiguration.loadConfiguration(config);
return workable.getString(this.name+"/HASH"); return workable.getString(this.name+"/HASH");
} }
public long FetchLastModified() {
File config = new File(AutoPluginUpdate.datafolder,"hashes.data");
FileConfiguration workable = YamlConfiguration.loadConfiguration(config);
return workable.getLong(this.name+"/HASHMOD");
}
} }

Loading…
Cancel
Save