1
0
mirror of https://github.com/nkomarn/harbor.git synced 2024-12-24 09:17:34 +01:00

🍉 Some more work on the update system!

This commit is contained in:
Mykyta 2019-11-08 07:35:43 -08:00
parent 9f06cecd2c
commit eeb98044ad
No known key found for this signature in database
GPG Key ID: C147E30C19EA3570
4 changed files with 101 additions and 31 deletions

View File

@ -25,7 +25,7 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId> <artifactId>spigot-api</artifactId>
<version>1.14.4-R0.1-SNAPSHOT</version> <version>1.14.4-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>

View File

@ -1,13 +1,19 @@
package xyz.nkomarn.Harbor.command; package xyz.nkomarn.Harbor.command;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Sound;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import xyz.nkomarn.Harbor.Harbor; import xyz.nkomarn.Harbor.Harbor;
import xyz.nkomarn.Harbor.util.Config; import xyz.nkomarn.Harbor.util.Config;
import xyz.nkomarn.Harbor.util.Updater; import xyz.nkomarn.Harbor.util.Updater;
import java.util.concurrent.ExecutionException;
public class HarborCommand implements CommandExecutor { public class HarborCommand implements CommandExecutor {
@Override @Override
@ -33,8 +39,46 @@ public class HarborCommand implements CommandExecutor {
return true; return true;
} }
else if (args[0].equalsIgnoreCase("update")) { else if (args[0].equalsIgnoreCase("update")) {
if (Updater.check()) {
// TODO // Fancy actionbar stuff
if (sender instanceof Player) {
Player player = (Player) sender;
player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 1.0f, 1.0f);
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&',
prefix + "&fChecking for updates.")));
}
boolean updateAvailable;
try {
updateAvailable = Updater.check().get();
System.out.println(updateAvailable);
} catch (ExecutionException | InterruptedException e) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix + "&7Failed to check for a "
+ "new update. Check console for full log."));
e.printStackTrace();
return true;
}
if (updateAvailable) {
try {
// More fancy actionbar stuff
if (sender instanceof Player) {
Player player = (Player) sender;
player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1.0f, 1.0f);
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&',
prefix + "&fUpdate found, upgrading.")));
}
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix + "&7"
+ Updater.upgrade().get()));
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
return true;
} else {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix + "&7You're already running "
+ "the latest version of Harbor. Great work!"));
return true;
} }
} }
@ -42,15 +86,5 @@ public class HarborCommand implements CommandExecutor {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix sender.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix
+ Config.getString("messages.miscellaneous.unrecognized"))); + Config.getString("messages.miscellaneous.unrecognized")));
return true; return true;
/*if (args.length == 1 && "reload".equalsIgnoreCase(args[0]) && sender.hasPermission("harbor.reload")) {
Harbor.instance.reloadConfig();
sender.sendMessage("§1[Harbor]: §2 Reloaded");
}*/
}
private void checkForUpdate() {
Updater.check();
Updater.upgrade();
} }
} }

View File

@ -18,20 +18,24 @@ public class Messages {
public static void sendRandomChatMessage(final World world, final String messageList) { public static void sendRandomChatMessage(final World world, final String messageList) {
final List<String> messages = Config.getList(messageList); final List<String> messages = Config.getList(messageList);
if (messages.size() < 1) return;
final int index = new Random().nextInt(Math.max(0, messages.size())); final int index = new Random().nextInt(Math.max(0, messages.size()));
sendWorldChatMessage(world, messages.get(index)); sendWorldChatMessage(world, messages.get(index));
} }
private static void sendPlayerChatMessage(final Player player, final String message) { private static void sendPlayerChatMessage(final Player player, final String message) {
if (message.length() < 1) return;
player.sendMessage(ChatColor.translateAlternateColorCodes('&', message)); player.sendMessage(ChatColor.translateAlternateColorCodes('&', message));
} }
private static void sendWorldChatMessage(final World world, final String message) { private static void sendWorldChatMessage(final World world, final String message) {
if (message.length() < 1) return;
world.getPlayers().forEach(player -> player.sendMessage(ChatColor.translateAlternateColorCodes('&', message))); world.getPlayers().forEach(player -> player.sendMessage(ChatColor.translateAlternateColorCodes('&', message)));
} }
public static void sendActionBarMessage(final Player player, final String message) { public static void sendActionBarMessage(final Player player, final String message) {
final World world = player.getWorld(); final World world = player.getWorld();
if (message.length() < 1) return;
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText( player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(
ChatColor.translateAlternateColorCodes('&', message ChatColor.translateAlternateColorCodes('&', message
.replace("[sleeping]", String.valueOf(Checker.getSleeping(world).size())) .replace("[sleeping]", String.valueOf(Checker.getSleeping(world).size()))

View File

@ -4,12 +4,15 @@ import org.bukkit.Bukkit;
import xyz.nkomarn.Harbor.Harbor; import xyz.nkomarn.Harbor.Harbor;
import java.io.*; import java.io.*;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.util.logging.Level; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class Updater { public class Updater {
@ -17,27 +20,56 @@ public class Updater {
public static String latest; public static String latest;
// Checks if an update is available // Checks if an update is available
public static boolean check() { public static Future<Boolean> check() {
CompletableFuture<Boolean> future = new CompletableFuture<>();
Executors.newCachedThreadPool().submit(() -> {
try { try {
URL url = new URL("https://api.spigotmc.org/legacy/update.php?resource=60088"); URL latestVersion = new URL("https://api.spigotmc.org/legacy/update.php?resource=60088");
URLConnection request = url.openConnection(); URLConnection request = latestVersion.openConnection();
request.addRequestProperty("User-Agent", "Harbor"); request.addRequestProperty("User-Agent", "Harbor");
request.connect(); request.connect();
InputStream inputStream = request.getInputStream();
InputStream inputStream = (InputStream) request.getContent(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); latest = bufferedReader.lines().collect(Collectors.joining(System.lineSeparator()));
latest = reader.lines().collect(Collectors.joining(System.lineSeparator())); System.out.println(latest); // TODO REMOVE
future.complete(!Harbor.version.equals(latest));
System.out.println(latest);
return !Harbor.version.equals(latest);
} catch (IOException e) { } catch (IOException e) {
future.complete(false);
e.printStackTrace(); e.printStackTrace();
return false;
} }
});
return future;
}
// Download latest JAR and put it in Bukkit's update folder
public static Future<String> upgrade() {
CompletableFuture<String> future = new CompletableFuture<>();
Executors.newCachedThreadPool().submit(() -> {
String jarName = new File(Updater.class.getProtectionDomain().getCodeSource().getLocation()
.getPath()).getName();
try {
URL downloadURL = new URL("http://aqua.api.spiget.org/v2/resources/60088/download");
// TODO File jarFile = new File("plugins" + File.separator + jarName);
File updatedJarFile = new File("plugins" + File.separator + "update"
+ File.separator + jarName);
updatedJarFile.mkdirs();
InputStream inputStream = downloadURL.openStream();
Files.copy(inputStream, Paths.get(updatedJarFile.toURI()), StandardCopyOption.REPLACE_EXISTING);
future.complete("Updated Harbor. Changes will take effect after a server reload/reboot.");
} catch (IOException e) {
future.complete("Failed to update Harbor. Check console for full log.");
e.printStackTrace();
}
});
return future;
} }
// Actually update the Harbor JAR // Actually update the Harbor JAR
public static boolean upgrade() { /*public static boolean upgrade() {
Harbor.instance.getLogger().log(Level.INFO, "Downloading Harbor version " + latest + "."); Harbor.instance.getLogger().log(Level.INFO, "Downloading Harbor version " + latest + ".");
try { try {
@ -72,6 +104,6 @@ public class Updater {
e.printStackTrace(); e.printStackTrace();
return false; return false;
} }
} }*/
} }