From a63681d4a347d68acd4a105de8ee416a8551e727 Mon Sep 17 00:00:00 2001 From: Lemon Date: Mon, 26 Mar 2018 16:26:21 +0500 Subject: [PATCH] Improvements --- .../totalfreedommod/TotalFreedomMod.java | 2 +- .../totalfreedom/totalfreedommod/Updater.java | 28 ++++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/TotalFreedomMod.java b/src/main/java/me/totalfreedom/totalfreedommod/TotalFreedomMod.java index 93923310..26f6b12c 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/TotalFreedomMod.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/TotalFreedomMod.java @@ -248,7 +248,7 @@ public class TotalFreedomMod extends AeroPlugin plugin.pa.autoAddSpawnpoints(); } }.runTaskLater(plugin, 60L); - //little workaround to stop spigot from autorestarting - causing AMP to detach from process. + // little workaround to stop spigot from autorestarting - causing AMP to detach from process. SpigotConfig.config.set("settings.restart-on-crash", false); } diff --git a/src/main/java/me/totalfreedom/totalfreedommod/Updater.java b/src/main/java/me/totalfreedom/totalfreedommod/Updater.java index 5b33faca..839f26e7 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/Updater.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/Updater.java @@ -2,9 +2,13 @@ package me.totalfreedom.totalfreedommod; import me.totalfreedom.totalfreedommod.util.FLog; import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; + +import java.io.File; import java.io.FileOutputStream; -import java.net.URL; import java.io.IOException; +import java.lang.reflect.Method; +import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.util.Scanner; @@ -66,7 +70,7 @@ public class Updater extends FreedomService { URL url = new URL(UPDATE_SERVER_URL + "/TotalFreedomMod.jar"); ReadableByteChannel input = Channels.newChannel(url.openStream()); - FileOutputStream output = new FileOutputStream("plugins/TotalFreedomMod.jar"); + FileOutputStream output = new FileOutputStream(getFilePath()); FLog.info("Downloading latest version..."); output.getChannel().transferFrom(input, 0, Long.MAX_VALUE); input.close(); @@ -76,12 +80,12 @@ public class Updater extends FreedomService } catch (IOException ex) { - ex.printStackTrace(); + FLog.severe(ex); } for (Player player : server.getOnlinePlayers()) { - player.kickPlayer("The server has restarted as TotalFreedomMod was just updated."); + player.kickPlayer("The server is restarting for a TFM update."); } if (!plugin.amp.enabled) @@ -93,5 +97,21 @@ public class Updater extends FreedomService plugin.amp.restartServer(); } } + public String getFilePath() + { + try + { + Method method = JavaPlugin.class.getDeclaredMethod("getFile"); + boolean wasAccessible = method.isAccessible(); + method.setAccessible(true); + File file = (File) method.invoke(plugin); + method.setAccessible(wasAccessible); + return file.getPath(); + } + catch (Exception e) + { + return "plugins" + File.separator + plugin.getName(); + } + } }