From 1080893be4670dc4755cecbd55bb9587bf060e92 Mon Sep 17 00:00:00 2001 From: ZeroEpoch1969 <13510767+ZeroEpoch1969@users.noreply.github.com> Date: Sun, 25 Mar 2018 16:29:30 -0700 Subject: [PATCH] No more waiting for windows to update TFM --- .../totalfreedommod/TotalFreedomMod.java | 3 +- .../totalfreedom/totalfreedommod/Updater.java | 97 +++++++++++++++++++ .../command/Command_totalfreedommod.java | 63 ++++++++---- 3 files changed, 142 insertions(+), 21 deletions(-) create mode 100644 src/main/java/me/totalfreedom/totalfreedommod/Updater.java diff --git a/src/main/java/me/totalfreedom/totalfreedommod/TotalFreedomMod.java b/src/main/java/me/totalfreedom/totalfreedommod/TotalFreedomMod.java index fe1522f1..93923310 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/TotalFreedomMod.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/TotalFreedomMod.java @@ -91,6 +91,7 @@ public class TotalFreedomMod extends AeroPlugin public EntityWiper ew; public FrontDoor fd; public ServerPing sp; + public Updater ud; public ItemFun it; public Landminer lm; public MP44 mp; @@ -178,7 +179,6 @@ public class TotalFreedomMod extends AeroPlugin pm = services.registerService(PermbanList.class); pa = services.registerService(ProtectArea.class); gr = services.registerService(GameRuleHandler.class); - snp = services.registerService(SignBlocker.class); // Single admin utils @@ -200,6 +200,7 @@ public class TotalFreedomMod extends AeroPlugin ew = services.registerService(EntityWiper.class); fd = services.registerService(FrontDoor.class); sp = services.registerService(ServerPing.class); + ud = services.registerService(Updater.class); pv = services.registerService(PlayerVerification.class); // Fun diff --git a/src/main/java/me/totalfreedom/totalfreedommod/Updater.java b/src/main/java/me/totalfreedom/totalfreedommod/Updater.java new file mode 100644 index 00000000..5b33faca --- /dev/null +++ b/src/main/java/me/totalfreedom/totalfreedommod/Updater.java @@ -0,0 +1,97 @@ +package me.totalfreedom.totalfreedommod; + +import me.totalfreedom.totalfreedommod.util.FLog; +import org.bukkit.entity.Player; +import java.io.FileOutputStream; +import java.net.URL; +import java.io.IOException; +import java.nio.channels.Channels; +import java.nio.channels.ReadableByteChannel; +import java.util.Scanner; + +/* + This class exists primarily due to the fact that it takes too long to get new builds onto the official + TotalFreedom server. If you wish to delete this class you may do so. +*/ + +public class Updater extends FreedomService +{ + + private final String UPDATE_SERVER_URL = "https://tfm.zeroepoch1969.rip"; + private final TotalFreedomMod.BuildProperties build = TotalFreedomMod.build; + public boolean updateAvailable = false; + + public Updater(TotalFreedomMod plugin) + { + super(plugin); + } + + @Override + protected void onStart() + { + if (build.number != null) + { + checkForUpdates(); + } + } + + @Override + protected void onStop() + { + } + + private void checkForUpdates() + { + try + { + URL url = new URL(UPDATE_SERVER_URL + "/build"); + String webBuild = new Scanner(url.openStream()).useDelimiter("\\Z").next(); + if (!build.number.equals(webBuild)) + { + updateAvailable = true; + } + FLog.info((updateAvailable ? "A new update is a available!" : "TFM is up-to-date!")); + + } + catch (IOException ex) + { + FLog.warning("Failed to connect to the update server."); + } + } + + + public void update() + { + try + { + URL url = new URL(UPDATE_SERVER_URL + "/TotalFreedomMod.jar"); + ReadableByteChannel input = Channels.newChannel(url.openStream()); + FileOutputStream output = new FileOutputStream("plugins/TotalFreedomMod.jar"); + FLog.info("Downloading latest version..."); + output.getChannel().transferFrom(input, 0, Long.MAX_VALUE); + input.close(); + output.close(); + FLog.info("The latest version has been installed! Restarting server..."); + + } + catch (IOException ex) + { + ex.printStackTrace(); + } + + for (Player player : server.getOnlinePlayers()) + { + player.kickPlayer("The server has restarted as TotalFreedomMod was just updated."); + } + + if (!plugin.amp.enabled) + { + server.shutdown(); + } + else + { + plugin.amp.restartServer(); + } + } + +} diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_totalfreedommod.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_totalfreedommod.java index 72a6dc75..e2cdb37f 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_totalfreedommod.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_totalfreedommod.java @@ -5,6 +5,7 @@ import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.config.MainConfig; import me.totalfreedom.totalfreedommod.rank.Rank; import me.totalfreedom.totalfreedommod.util.FLog; +import me.totalfreedom.totalfreedommod.util.FUtil; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -14,7 +15,7 @@ import org.bukkit.entity.Player; * See https://github.com/TotalFreedom/License - This file may not be edited or removed. */ @CommandPermissions(level = Rank.NON_OP, source = SourceType.BOTH) -@CommandParameters(description = "Shows information about TotalFreedomMod or reloads it", usage = "/ [reload]", aliases = "tfm") +@CommandParameters(description = "Shows information about TotalFreedomMod or reloads it", usage = "/ [reload | update]", aliases = "tfm") public class Command_totalfreedommod extends FreedomCommand { @@ -23,28 +24,50 @@ public class Command_totalfreedommod extends FreedomCommand { if (args.length == 1) { - if (!args[0].equals("reload")) + if (args[0].equals("reload")) + { + if (!plugin.al.isAdmin(sender)) + { + noPerms(); + return true; + } + + plugin.config.load(); + plugin.services.stop(); + plugin.services.start(); + + final String message = String.format("%s v%s reloaded.", + TotalFreedomMod.pluginName, + TotalFreedomMod.pluginVersion); + + msg(message); + FLog.info(message); + return true; + } + else if (args[0].equals("update")) + { + if (plugin.al.isAdmin(sender) && FUtil.DEVELOPERS.contains(sender.getName())) + { + if (plugin.ud.updateAvailable) + { + FUtil.adminAction(sender.getName(), "Updating TotalFreedomMod", false); + plugin.ud.update(); + } + else + { + msg("TFM is already up to date!"); + } + } + else + { + noPerms(); + } + return true; + } + else { return false; } - - if (!plugin.al.isAdmin(sender)) - { - noPerms(); - return true; - } - - plugin.config.load(); - plugin.services.stop(); - plugin.services.start(); - - final String message = String.format("%s v%s reloaded.", - TotalFreedomMod.pluginName, - TotalFreedomMod.pluginVersion); - - msg(message); - FLog.info(message); - return true; } TotalFreedomMod.BuildProperties build = TotalFreedomMod.build;