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 final String LOG_PREFIX = "[AutoPluginUpdate]";
public final static String LOG_PREFIX = "[AutoPluginUpdate]";
public AutoPluginUpdate plugin = this;
public static File datafolder;
public static boolean restarting_server=false;
public static boolean main_server=false;
public final int LOG_ERROR=0;
public final int LOG_WARNING=1;
public final int LOG_NORMAL=2;
public final int LOG_DETAIL=3;
public final int LOG_VERBOSE=4;
public final int LOG_DEBUG=5;
public final static int LOG_ERROR=0;
public final static int LOG_WARNING=1;
public final static int LOG_NORMAL=2;
public final static int LOG_DETAIL=3;
public final static int LOG_VERBOSE=4;
public final static int LOG_DEBUG=5;
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) {
case LOG_ERROR:{
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() {
if (Bukkit.getOnlinePlayers().size()==0 && restarting_server) {
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++) {
Bukkit.getWorlds().get(i).save();
}

@ -4,6 +4,8 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
@ -31,7 +33,7 @@ public class PluginManager implements Runnable{
void FetchPlugins(){
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));
} catch (IOException e) {
e.printStackTrace();
@ -54,41 +56,90 @@ public class PluginManager implements Runnable{
file.close();
} catch (IOException e) {
e.printStackTrace();
}*/
URL url = null;
try {
url = new URL(plugins.get(i).url);
} catch (MalformedURLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
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!
final int ii=i;
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.
if (url!=null) {
//AutoPluginUpdate.log("Valid URL. ", AutoPluginUpdate.LOG_NORMAL);
HttpURLConnection httpCon = null;
try {
httpCon = (HttpURLConnection) url.openConnection();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (httpCon!=null) {
//AutoPluginUpdate.log("Connection is good. ", AutoPluginUpdate.LOG_NORMAL);
long lastModifiedDate = httpCon.getContentLengthLong();
//AutoPluginUpdate.log("Last Modified Date Comparison: "+plugins.get(i).lastmodified+","+lastModifiedDate, AutoPluginUpdate.LOG_NORMAL);
if (plugins.get(i).lastmodified!=lastModifiedDate) {
//This plugin is different! Update the hash for it. Prepare for a restart of the server!
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 {
AutoPluginUpdate.BroadcastMessage("The server has detected a new version of "+ChatColor.YELLOW+plugins.get(ii).name+".");
//After that's done, check the hash.
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) {
AutoPluginUpdate.updateServer();
if (!md5.equalsIgnoreCase(plugins.get(i).hash)) {
//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;
SaveHash(plugins.get(i));
//Move the file to the new location.
/*try {
FileUtils.copyFile(new File(AutoPluginUpdate.datafolder,"updates/"+plugins.get(i).name),
new File(AutoPluginUpdate.datafolder,"../"+plugins.get(i).name+".jar"));
} catch (IOException e) {
e.printStackTrace();
}*/
plugins.get(i).hash = md5;
plugins.get(i).lastmodified = lastModifiedDate;
SaveHash(plugins.get(i));
//Move the file to the new location.
/*try {
FileUtils.copyFile(new File(AutoPluginUpdate.datafolder,"updates/"+plugins.get(i).name),
new File(AutoPluginUpdate.datafolder,"../"+plugins.get(i).name+".jar"));
} catch (IOException e) {
e.printStackTrace();
}*/
}
}
}
}
}
@ -102,11 +153,13 @@ public class PluginManager implements Runnable{
File config = new File(AutoPluginUpdate.datafolder,"hashes.data");
FileConfiguration workable = YamlConfiguration.loadConfiguration(config);
pluginname.hash = workable.getString(pluginname.name+"/HASH");
pluginname.lastmodified = workable.getLong(pluginname.name+"/HASHMOD");
}
public void SaveHash(Plugin pluginname) {
File config = new File(AutoPluginUpdate.datafolder,"hashes.data");
FileConfiguration workable = YamlConfiguration.loadConfiguration(config);
workable.set(pluginname.name+"/HASH",pluginname.hash);
workable.set(pluginname.name+"/HASHMOD",pluginname.lastmodified);
try {
workable.save(config);
} catch (IOException e) {
@ -118,6 +171,7 @@ public class PluginManager implements Runnable{
FileConfiguration workable = YamlConfiguration.loadConfiguration(config);
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+"/HASHMOD",pluginlist.get(i).lastmodified);
}
try {
workable.save(config);
@ -131,17 +185,20 @@ class Plugin {
String name;
String hash;
String url;
long lastmodified=0;
public Plugin(String name,String url) {
this.name=name;
this.url=url;
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.url=url;
this.hash=hash;
this.lastmodified=lastmodified;
}
public String FetchHash() {
@ -149,4 +206,10 @@ class Plugin {
FileConfiguration workable = YamlConfiguration.loadConfiguration(config);
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