diff --git a/src/de/xite/scoreboard/commands/ScoreboardCommand.java b/src/de/xite/scoreboard/commands/ScoreboardCommand.java index 34c45bb..4c2a267 100644 --- a/src/de/xite/scoreboard/commands/ScoreboardCommand.java +++ b/src/de/xite/scoreboard/commands/ScoreboardCommand.java @@ -53,21 +53,29 @@ public boolean onCommand(CommandSender s, Command arg1, String arg2, String[] ar }else s.sendMessage(PowerBoard.pr+ChatColor.RED+"Sorry, but you need the permission "+ChatColor.GRAY+"powerboard.reload"+ChatColor.RED+" to do that."); }else if(args.length >= 1 && args[0].equalsIgnoreCase("update")) { - if(args.length == 2 && args[1].equalsIgnoreCase("confirm")) { + if(args.length >= 2 && args[1].equalsIgnoreCase("confirm")) { if(!(s instanceof Player) || (s instanceof Player && ((Player) s).hasPermission("powerboard.update"))) { s.sendMessage(PowerBoard.pr+ChatColor.GREEN+"Downloading the newest version..."); - if(Updater.downloadFile()) { - s.sendMessage(PowerBoard.pr+ChatColor.GREEN+"Download finished! Stopping server.."); - Bukkit.spigot().restart(); + if(Updater.downloadFile(false)) { + s.sendMessage(PowerBoard.pr+ChatColor.GREEN+"Download finished. Please restart your server as soon as possible!"); return true; }else { s.sendMessage(PowerBoard.pr+ChatColor.RED+"Download failed! Please try it later again. More infos are available in the console."); + s.sendMessage(PowerBoard.pr+ChatColor.RED+"If your server is running on Windows, that could cause this problem. Try it with '/pb update confirm force'"); + } + if(args[2].equalsIgnoreCase("force")) { + if(Updater.downloadFile(true)) { + s.sendMessage(PowerBoard.pr+ChatColor.GREEN+"Download finished. Please restart your server as soon as possible!"); + }else { + s.sendMessage(PowerBoard.pr+ChatColor.RED+"Sorry, force update did not work. If the download was successful you can manually stop your server and rename " + + "PowerBoard.jar.update to PowerBoard.jar. If there is no update file or that file is corrupted, you have to download the plugin manually."); + } } }else s.sendMessage(PowerBoard.pr+ChatColor.RED+"Sorry, but you need the permission "+ChatColor.GRAY+"powerboard.update"+ChatColor.RED+" to do that."); }else - s.sendMessage(PowerBoard.pr+ChatColor.RED+"Warning: If you update the plugin, the server will be automatically restarted (if you have a restart script) when the download is finished. " - + "Please type "+ChatColor.YELLOW+"/pb update confirm"+ChatColor.RED+" to update the plugin."); + s.sendMessage(PowerBoard.pr+ChatColor.RED+"Warning: After successful update, you have to restart your server as soon as possible! " + + "Please type "+ChatColor.YELLOW+"/pb update confirm"+ChatColor.RED+" to confirm."); }else if(args.length == 1 && args[0].equalsIgnoreCase("debug")) { if(!(s instanceof Player) || (s instanceof Player && ((Player) s).hasPermission("powerboard.debug"))) { diff --git a/src/de/xite/scoreboard/main/PowerBoard.java b/src/de/xite/scoreboard/main/PowerBoard.java index 17d7617..0bc3b7b 100644 --- a/src/de/xite/scoreboard/main/PowerBoard.java +++ b/src/de/xite/scoreboard/main/PowerBoard.java @@ -127,7 +127,7 @@ public void onDisable() { // Download newest version if update is available if(pl.getConfig().getBoolean("update.autoupdater")) if(Updater.checkVersion()) - Updater.downloadFile(); + Updater.downloadFile(false); // Unregister scoreboards and teams for(Player all : Bukkit.getOnlinePlayers()) { diff --git a/src/de/xite/scoreboard/utils/Updater.java b/src/de/xite/scoreboard/utils/Updater.java index 3c7fabf..652c5be 100644 --- a/src/de/xite/scoreboard/utils/Updater.java +++ b/src/de/xite/scoreboard/utils/Updater.java @@ -8,11 +8,15 @@ import java.net.URL; import java.util.Scanner; +import org.apache.commons.io.FileUtils; import org.bukkit.Bukkit; import de.xite.scoreboard.main.PowerBoard; public class Updater { + static String updaterPrefix = "Updater -> "; + static boolean updateSuccessful = false; + private static PowerBoard pl = PowerBoard.pl; private static int pluginID = 73854; private static String version; @@ -46,37 +50,95 @@ public static boolean checkVersion() { return false; return true; } - public static boolean downloadFile() { + public static boolean downloadFile(boolean forceUpdate) { + if(updateSuccessful) { + pl.getLogger().info("Ignoring update request. Plugin has already been updated."); + return false; + } + + String pluginName = PowerBoard.pl.getDescription().getName(); + try { + // Download new PowerBoard.jar to plugins/PowerBoard.update.jar pl.getLogger().info("Updater -> Downloading newest version..."); - File file = new File("plugins/"+PowerBoard.pl.getDescription().getName()+".jar"); - if(!file.exists()) { + File file = new File("plugins/" + pluginName + ".jar.update"); + if(forceUpdate) + file = new File("plugins/" + pluginName + ".jar"); + + if (!file.exists()) { try { file.createNewFile(); - }catch(IOException e) { - e.printStackTrace(); - return false; - } + } catch (IOException e) { + e.printStackTrace(); + return false; + } } - String url = "https://github.com/Xitee1/PowerBoard/releases/latest/download/"+PowerBoard.pl.getDescription().getName()+".jar"; - HttpURLConnection connection = (HttpURLConnection)(new URL(url)).openConnection(); + String url = "https://github.com/Xitee1/PowerBoard/releases/latest/download/" + pluginName + ".jar"; + HttpURLConnection connection = (HttpURLConnection) (new URL(url)).openConnection(); connection.connect(); FileOutputStream outputStream = new FileOutputStream(file); InputStream inputStream = connection.getInputStream(); byte[] buffer = new byte[1024]; - int readBytes = 0; + int readBytes = 0; while ((readBytes = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, readBytes); } outputStream.close(); inputStream.close(); connection.disconnect(); - pl.getLogger().info("Updater -> Download finished! To apply the new update, you have to restart your server."); + } catch (Exception e) { + pl.getLogger().info(updaterPrefix+"Download failed! Please try it later again."); + e.printStackTrace(); + return false; + } + + if(forceUpdate) return true; - }catch(Exception e) { - pl.getLogger().info("Updater -> Download failed! Please try it later again."); + + // Move new file in place + + File newFile = new File("plugins/" + pluginName + ".jar.update"); + File oldFile = new File("plugins/" + pluginName + ".jar.old"); + File currentFile = new File("plugins/" + pluginName + ".jar"); + + // Delete PowerBoard.old.jar if exists + if (oldFile.exists()) { + if (!oldFile.delete()) { + pl.getLogger().severe(updaterPrefix+"Could not delete PowerBoard.old.jar even tough it exists!"); + return false; + } + } + + // PowerBoard.jar -> PowerBoard.old.jar + try { + FileUtils.moveFile(currentFile, oldFile); + } catch (IOException e) { + pl.getLogger().severe(updaterPrefix+"Could not rename current PowerBoard.jar file."); e.printStackTrace(); return false; } + + // PowerBoard.update.jar -> PowerBoard.jar + if(!currentFile.exists()) { + try { + FileUtils.moveFile(newFile, currentFile); + } catch (IOException e) { + pl.getLogger().severe(updaterPrefix+"Could not rename new PowerBoard.jar file."); + e.printStackTrace(); + return false; + } + }else { + pl.getLogger().severe(updaterPrefix+"Old file still exists. Could not update PowerBoard!"); + } + + + // Clear files + if(!newFile.delete()) { + pl.getLogger().warning(updaterPrefix+"Could not delete update-file. Please manually delete plugins/"+pluginName+".update.jar"); + } + + updateSuccessful = true; + pl.getLogger().info(updaterPrefix+"Update finished! To apply the new update, you have to restart your server."); + return true; } }