Made large improvements to grabbing plugins. Saves a lot of bandwidth by
now checking the file size before attempting any downloading.
This commit is contained in:
parent
729d72e6bd
commit
bb9cf0634f
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,6 +33,53 @@ 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 {
|
||||||
|
FileUtils.copyURLToFile(new URL(plugins.get(i).url), new File(AutoPluginUpdate.datafolder,"updates/"+plugins.get(i).name));
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
//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();
|
||||||
|
}*/
|
||||||
|
URL url = null;
|
||||||
|
try {
|
||||||
|
url = new URL(plugins.get(i).url);
|
||||||
|
} catch (MalformedURLException e2) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e2.printStackTrace();
|
||||||
|
}
|
||||||
|
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 {
|
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) {
|
||||||
@ -55,10 +104,8 @@ public class PluginManager implements Runnable{
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
if (!md5.equalsIgnoreCase(plugins.get(i).hash)) {
|
||||||
if ((plugins.get(i).hash==null || !md5.equalsIgnoreCase(plugins.get(i).hash))) {
|
//AutoPluginUpdate.log("Last Modified Date Comparison: "+plugins.get(i).lastmodified+","+lastModifiedDate, AutoPluginUpdate.LOG_NORMAL);
|
||||||
//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() {
|
Bukkit.getScheduler().scheduleSyncDelayedTask(Bukkit.getPluginManager().getPlugin("AutoPluginUpdate"), new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@ -79,7 +126,9 @@ public class PluginManager implements Runnable{
|
|||||||
AutoPluginUpdate.updateServer();
|
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.
|
||||||
@ -92,6 +141,8 @@ public class PluginManager implements Runnable{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void AddPlugin(String name, String url) {
|
public void AddPlugin(String name, String url) {
|
||||||
plugins.add(new Plugin(name,url));
|
plugins.add(new Plugin(name,url));
|
||||||
@ -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…
x
Reference in New Issue
Block a user