Skip to content

Commit

Permalink
Redesign update system
Browse files Browse the repository at this point in the history
  • Loading branch information
monst committed Feb 21, 2023
1 parent 9dfb2bf commit 2bda591
Show file tree
Hide file tree
Showing 11 changed files with 483 additions and 465 deletions.
12 changes: 3 additions & 9 deletions src/main/java/com/monst/bankingplugin/BankingPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.monst.bankingplugin.listener.*;
import com.monst.bankingplugin.persistence.Database;
import com.monst.bankingplugin.persistence.service.*;
import com.monst.bankingplugin.update.UpdaterService;
import com.monst.bankingplugin.util.*;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
Expand Down Expand Up @@ -105,15 +106,8 @@ public void onEnable() {
schedulerService = new SchedulerService(this);
paymentService = new PaymentService(this);

if (config().enableStartupUpdateCheck.get()) {
updaterService.checkForUpdate().then(update -> {
if (update == null)
return;
log(Level.WARNING, "Version " + update.getVersion() + " of BankingPlugin is available!");
if (config().downloadUpdatesAutomatically.get())
update.download();
}).catchError(error -> getLogger().warning("Failed to check for updates!"));
}
if (config().enableStartupUpdateCheck.get())
updaterService.checkForUpdate().catchError(error -> getLogger().warning("Failed to check for updates!"));

new AccountCommand(this);
new BankCommand(this);
Expand Down
22 changes: 5 additions & 17 deletions src/main/java/com/monst/bankingplugin/command/plugin/BPUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.monst.bankingplugin.command.SubCommand;
import com.monst.bankingplugin.gui.UpdateGUI;
import com.monst.bankingplugin.lang.Message;
import com.monst.bankingplugin.lang.Placeholder;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

Expand Down Expand Up @@ -40,24 +39,13 @@ protected void execute(CommandSender sender, String[] args) {
sender.sendMessage(Message.NO_UPDATE_AVAILABLE.translate(plugin));
return;
}
boolean download = plugin.config().downloadUpdatesAutomatically.get()
|| args.length > 0 && args[0].equalsIgnoreCase("download");
if (sender instanceof Player) {

if (sender instanceof Player)
new UpdateGUI(plugin, (Player) sender, update).open();
if (download)
update.download();
} else {
sender.sendMessage(Message.UPDATE_AVAILABLE.with(Placeholder.VERSION).as(update.getVersion()).translate(plugin));
if (download) {
sender.sendMessage(Message.UPDATE_DOWNLOADING.translate(plugin));
update.download()
.onValidating(() -> sender.sendMessage(Message.UPDATE_VALIDATING.translate(plugin)))
.onDownloadComplete(() -> sender.sendMessage(Message.UPDATE_DOWNLOAD_COMPLETE.translate(plugin)))
.catchError(error -> sender.sendMessage(Message.UPDATE_DOWNLOAD_FAILED.translate(plugin)));
}
}

if (args.length > 0 && args[0].equalsIgnoreCase("download"))
update.download();
}).catchError(error -> sender.sendMessage(Message.UPDATE_CHECK_ERROR.translate(plugin)));
}


}
95 changes: 62 additions & 33 deletions src/main/java/com/monst/bankingplugin/gui/UpdateGUI.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.monst.bankingplugin.gui;

import com.monst.bankingplugin.BankingPlugin;
import com.monst.bankingplugin.util.Update;
import com.monst.bankingplugin.util.Observer;
import com.monst.bankingplugin.update.Update;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
Expand All @@ -12,16 +13,20 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.time.Duration;
import java.util.*;

public class UpdateGUI extends SinglePageGUI {
import static org.bukkit.ChatColor.*;

public class UpdateGUI extends SinglePageGUI implements Observer {

private final Update update;
private final List<String> versionLore;

public UpdateGUI(BankingPlugin plugin, Player player, Update update) {
super(plugin, player);
this.update = update;
update.subscribe(this);
this.versionLore = Collections.singletonList(ChatColor.GOLD.toString() + ChatColor.BOLD + update.getVersion());
}

Expand All @@ -38,28 +43,36 @@ Map<Integer, ItemStack> createItems(Player player) {
return items;
}

private List<String> clickToDownload() {
List<String> lore = new ArrayList<>(versionLore);
lore.add(ChatColor.GREEN + "Click to download.");
return lore;
}

@Override
public void click(int slot, ClickType type) {
if (slot != 4)
return;
switch (update.getState()) {
case DOWNLOADING:
update.pauseDownload(); break;
update.pauseDownload();
break;
case INITIAL:
case DOWNLOAD_FAILED:
case PAUSED:
update.download();
}
}

@Override
public void update() {
switch (update.getState()) {
case INITIAL:
setStatus(clickToDownload()); break;
case DOWNLOADING:
setStatus(downloading(update.getDownload().getPercentComplete())); break;
case PAUSED:
case ERROR:
update.download()
.onDownloadPercentageChange(percentage -> setStatus(downloading(percentage)))
.onPause(percentage -> setStatus(paused(percentage)))
.onValidating(() -> setStatus(validating()))
.onDownloadComplete(() -> setStatus(updateComplete()))
.catchError(error -> setStatus(error()));
setStatus(paused(update.getDownload().getPercentComplete())); break;
case VALIDATING:
setStatus(validating(update.getDownload().getDuration())); break;
case SUCCESS:
setStatus(updateComplete(update.getDownload().getDuration(), update.getDownload().isValidated())); break;
case DOWNLOAD_FAILED:
setStatus(downloadError()); break;
}
}

Expand All @@ -71,37 +84,53 @@ private void setStatus(List<String> lore) {
inventory.setItem(4, item); // TODO: Line necessary, or does setItemMeta() already update the inventory?
}

private List<String> clickToDownload() {
return lore(GREEN + "Click to download.");
}

private List<String> downloading(int percentage) {
List<String> lore = new ArrayList<>(versionLore);
lore.add(ChatColor.RED + "Downloading... (" + percentage + "%)");
lore.add(ChatColor.RED + "Click to pause download.");
return lore;
return lore(
RED + "Downloading... (" + percentage + "%)",
RED + "Click to pause download."
);
}

private List<String> paused(int percentage) {
List<String> lore = new ArrayList<>(versionLore);
lore.add(ChatColor.GREEN + "Download paused. (" + percentage + "%)");
lore.add(ChatColor.RED + "Click to resume download.");
return lore;
return lore(
GREEN + "Download paused. (" + percentage + "%)",
RED + "Click to resume download."
);
}

private List<String> validating() {
List<String> lore = new ArrayList<>(versionLore);
lore.add(ChatColor.GREEN + "Download complete.");
lore.add(ChatColor.RED + "Validating... ");
return lore;
private List<String> validating(Duration duration) {
// Display duration formatted as "mm:ss.ss"
return lore(
GREEN + "Download complete. (" + formatDuration(duration) + ")",
RED + "Validating... "
);
}

private List<String> updateComplete() {
private List<String> updateComplete(Duration duration, boolean validated) {
List<String> lore = new ArrayList<>(versionLore);
lore.add(ChatColor.GREEN + "Download complete. File successfully validated.");
lore.add(GREEN + "Download complete. (" + formatDuration(duration) + ")");
if (validated)
lore.add(GREEN + "Update successfully validated.");
return lore;
}

private List<String> error() {
private List<String> downloadError() {
return lore(DARK_RED + "Download failed. Click to retry.");
}

private List<String> lore(String... lines) {
List<String> lore = new ArrayList<>(versionLore);
lore.add(ChatColor.DARK_RED + "Download failed. Click to retry.");
lore.addAll(Arrays.asList(lines));
return lore;
}

private static String formatDuration(Duration duration) {
// format as "mm:ss.ss"
return String.format("%02d:%02d.%02d", duration.toMinutes(), duration.getSeconds() % 60, duration.getNano() / 10_000_000) + "s";
}

}
16 changes: 0 additions & 16 deletions src/main/java/com/monst/bankingplugin/lang/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -958,22 +958,6 @@ public enum Message implements Translatable {
"An admin is notified that an error occurred while checking for updates.",
red().bold("Error while checking for updates.")
),
UPDATE_DOWNLOADING(
"An admin is notified that an update to BankingPlugin is being downloaded.",
gold().bold("Downloading update...")
),
UPDATE_VALIDATING(
"An admin is notified that a downloaded update to BankingPlugin is being validated.",
gold().bold("Validating download...")
),
UPDATE_DOWNLOAD_COMPLETE(
"An admin is notified that an update to BankingPlugin was successfully downloaded.",
gold().bold("Download successful.")
),
UPDATE_DOWNLOAD_FAILED(
"An admin is notified that an update to BankingPlugin could not be downloaded.",
red().bold("Download failed.")
),
CLICK_TO_DONATE(
"A player executes the donate command and they are shown the donation link.",
gold().bold("Thank you! Click to donate: ").green(URL),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.monst.bankingplugin.command.Permissions;
import com.monst.bankingplugin.lang.Message;
import com.monst.bankingplugin.lang.Placeholder;
import com.monst.bankingplugin.util.Update;
import com.monst.bankingplugin.update.Update;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
Expand All @@ -26,7 +26,7 @@ public void onPlayerJoin(PlayerJoinEvent e) {
Player player = e.getPlayer();

Update update = plugin.getUpdaterService().getUpdateIfAvailable();
if (update != null && !update.isCompleted() && Permissions.UPDATE.ownedBy(player))
if (update != null && !update.isDownloaded() && Permissions.UPDATE.ownedBy(player))
player.sendMessage(Message.UPDATE_AVAILABLE
.with(Placeholder.VERSION).as(update.getVersion())
.translate(plugin));
Expand Down
Loading

0 comments on commit 2bda591

Please sign in to comment.