No more waiting for windows to update TFM

This commit is contained in:
ZeroEpoch1969 2018-03-25 16:29:30 -07:00
parent d5a3742849
commit 1080893be4
No known key found for this signature in database
GPG Key ID: E0AAB104FB9F8FDF
3 changed files with 142 additions and 21 deletions

View File

@ -91,6 +91,7 @@ public class TotalFreedomMod extends AeroPlugin<TotalFreedomMod>
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<TotalFreedomMod>
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<TotalFreedomMod>
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

View File

@ -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();
}
}
}

View File

@ -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 = "/<command> [reload]", aliases = "tfm")
@CommandParameters(description = "Shows information about TotalFreedomMod or reloads it", usage = "/<command> [reload | update]", aliases = "tfm")
public class Command_totalfreedommod extends FreedomCommand
{
@ -23,11 +24,8 @@ public class Command_totalfreedommod extends FreedomCommand
{
if (args.length == 1)
{
if (!args[0].equals("reload"))
if (args[0].equals("reload"))
{
return false;
}
if (!plugin.al.isAdmin(sender))
{
noPerms();
@ -46,6 +44,31 @@ public class Command_totalfreedommod extends FreedomCommand
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;
}
}
TotalFreedomMod.BuildProperties build = TotalFreedomMod.build;
msg("TotalFreedomMod for 'Total Freedom', the original all-op server.", ChatColor.GOLD);