110 lines
5 KiB
Java
110 lines
5 KiB
Java
package eu.m724.giants.updater;
|
|
|
|
import net.md_5.bungee.api.ChatColor;
|
|
import net.md_5.bungee.api.chat.ClickEvent;
|
|
import net.md_5.bungee.api.chat.HoverEvent;
|
|
import net.md_5.bungee.api.chat.TextComponent;
|
|
import org.bukkit.command.CommandSender;
|
|
import org.bukkit.entity.Player;
|
|
|
|
import java.nio.file.NoSuchFileException;
|
|
import java.time.LocalDate;
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
/**
|
|
* not actually a command but deserves a separate file
|
|
*/
|
|
public class UpdateCommand {
|
|
private final PluginUpdater updater;
|
|
|
|
public UpdateCommand(PluginUpdater updater) {
|
|
this.updater = updater;
|
|
}
|
|
|
|
private void sendChangelogMessage(CommandSender sender, String changelogUrl) {
|
|
if (changelogUrl != null) {
|
|
if (sender instanceof Player) {
|
|
TextComponent textComponent = new TextComponent("Click here to open changelog");
|
|
textComponent.setUnderlined(true);
|
|
textComponent.setColor(ChatColor.AQUA);
|
|
textComponent.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.fromLegacyText(changelogUrl)));
|
|
textComponent.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, changelogUrl));
|
|
sender.spigot().sendMessage(textComponent);
|
|
} else {
|
|
sender.sendMessage("Changelog: " + changelogUrl);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void updateCommand(CommandSender sender, String[] args) {
|
|
sender.sendMessage(ChatColor.GRAY + "Please wait...");
|
|
sender.sendMessage(ChatColor.GRAY + "Channel: " + updater.getEnvironment().getChannel());
|
|
|
|
if (updater.updatePending) {
|
|
sender.sendMessage(ChatColor.YELLOW + "" + ChatColor.BOLD + "(!) Server restart required");
|
|
}
|
|
|
|
String action = args.length > 1 ? args[1] : null; // remember this function is proxied
|
|
|
|
if (action == null) {
|
|
updater.getLatestVersion().thenAccept(metadata -> {
|
|
updater.getCurrentVersion().thenAccept(metadata2 -> {
|
|
sender.sendMessage(ChatColor.GOLD + "You're on Giants " + metadata2.getLabel() + " released " + formatDate(metadata2.getTimestamp()));
|
|
sendChangelogMessage(sender, metadata2.getChangelogUrl());
|
|
}).exceptionally(e -> {
|
|
sender.sendMessage(ChatColor.RED + "Error retrieving information about current version, see console for details. " + e.getMessage());
|
|
e.printStackTrace();
|
|
return null;
|
|
});
|
|
|
|
if (metadata != null) {
|
|
sender.sendMessage(ChatColor.YELLOW + "" + ChatColor.BOLD + "An update is available!");
|
|
sender.sendMessage(ChatColor.GOLD + "Giants " + metadata.getLabel() + " released " + formatDate(metadata.getTimestamp()));
|
|
sendChangelogMessage(sender, metadata.getChangelogUrl());
|
|
sender.sendMessage(ChatColor.GOLD + "To download: /giants update download");
|
|
} else {
|
|
sender.sendMessage(ChatColor.GRAY + "No new updates");
|
|
}
|
|
}).exceptionally(e -> {
|
|
sender.sendMessage(ChatColor.RED + "Error checking for update. See console for details.");
|
|
e.printStackTrace();
|
|
return null;
|
|
});
|
|
} else {
|
|
if (!sender.hasPermission("giants.update." + action)) {
|
|
sender.sendMessage(ChatColor.GRAY + "You don't have permission to use this command, or it doesn't exist.");
|
|
return;
|
|
}
|
|
|
|
if (action.equals("download")) {
|
|
sender.sendMessage(ChatColor.GRAY + "Started download");
|
|
|
|
updater.downloadLatestVersion().thenAccept(file -> {
|
|
sender.sendMessage(ChatColor.GREEN + "Download finished, install with /giants update install"); // TODO make this clickable
|
|
}).exceptionally(e -> {
|
|
sender.sendMessage(ChatColor.RED + "Download failed. See console for details.");
|
|
e.printStackTrace();
|
|
return null;
|
|
});
|
|
} else if (action.equals("install")) {
|
|
try {
|
|
updater.installLatestVersion().thenAccept(v -> {
|
|
sender.sendMessage(ChatColor.GREEN + "Installation completed, restart server to apply.");
|
|
updater.updatePending = true;
|
|
}).exceptionally(e -> {
|
|
sender.sendMessage(ChatColor.RED + "Install failed, see console for details. " + e.getMessage());
|
|
e.printStackTrace();
|
|
return null;
|
|
});
|
|
} catch (NoSuchFileException e) {
|
|
sender.sendMessage(ChatColor.YELLOW + "Download the update first: /giants update download");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private String formatDate(long timestamp) {
|
|
return LocalDate.ofEpochDay(timestamp / 86400).format(DateTimeFormatter.ofPattern("dd.MM.yyyy"));
|
|
}
|
|
|
|
}
|