From f7c2e72c1e96c7afc6a781ed1c3ff0f4c27b824f Mon Sep 17 00:00:00 2001 From: Sxtanna Date: Sat, 25 Jul 2020 11:55:47 -0400 Subject: [PATCH] added ecloud update command --- .../commands/impl/cloud/CommandECloud.java | 1 + .../impl/cloud/CommandECloudUpdate.java | 121 ++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 src/main/java/me/clip/placeholderapi/commands/impl/cloud/CommandECloudUpdate.java diff --git a/src/main/java/me/clip/placeholderapi/commands/impl/cloud/CommandECloud.java b/src/main/java/me/clip/placeholderapi/commands/impl/cloud/CommandECloud.java index c3d8bbd..5573244 100644 --- a/src/main/java/me/clip/placeholderapi/commands/impl/cloud/CommandECloud.java +++ b/src/main/java/me/clip/placeholderapi/commands/impl/cloud/CommandECloud.java @@ -21,6 +21,7 @@ public final class CommandECloud extends PlaceholderCommand private static final List COMMANDS = ImmutableList.of(new CommandECloudClear(), new CommandECloudToggle(), new CommandECloudStatus(), + new CommandECloudUpdate(), new CommandECloudRefresh(), new CommandECloudDownload(), new CommandECloudExpansionInfo(), diff --git a/src/main/java/me/clip/placeholderapi/commands/impl/cloud/CommandECloudUpdate.java b/src/main/java/me/clip/placeholderapi/commands/impl/cloud/CommandECloudUpdate.java new file mode 100644 index 0000000..7f056ee --- /dev/null +++ b/src/main/java/me/clip/placeholderapi/commands/impl/cloud/CommandECloudUpdate.java @@ -0,0 +1,121 @@ +package me.clip.placeholderapi.commands.impl.cloud; + +import me.clip.placeholderapi.PlaceholderAPIPlugin; +import me.clip.placeholderapi.commands.PlaceholderCommand; +import me.clip.placeholderapi.expansion.PlaceholderExpansion; +import me.clip.placeholderapi.expansion.cloud.CloudExpansion; +import me.clip.placeholderapi.util.Msg; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Unmodifiable; + +import java.io.File; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.stream.Collectors; + +/** + * please don't flame me for this code, I will fix this shit later. + */ +public final class CommandECloudUpdate extends PlaceholderCommand +{ + + public CommandECloudUpdate() + { + super("update"); + } + + + @Override + public void evaluate(@NotNull final PlaceholderAPIPlugin plugin, @NotNull final CommandSender sender, @NotNull final String alias, @NotNull @Unmodifiable final List params) + { + if (params.isEmpty()) + { + Msg.msg(sender, + "&cYou must define 'all' or the name of an expansion to update."); + return; + } + + final boolean multiple = params.get(0).equalsIgnoreCase("all"); + final List expansions = new ArrayList<>(); + + // gather target expansions + if (multiple) + { + expansions.addAll(plugin.getCloudExpansionManager().getCloudExpansionsInstalled().values()); + } + else + { + plugin.getCloudExpansionManager().findCloudExpansionByName(params.get(0)).ifPresent(expansions::add); + } + + // remove the ones that are the latest version + expansions.removeIf(expansion -> !expansion.shouldUpdate()); + + if (expansions.isEmpty()) + { + Msg.msg(sender, + "&cNo updates available for " + (!multiple ? "this expansion." : "your active expansions.")); + return; + } + + Msg.msg(sender, + "&aUpdating expansions: " + expansions.stream().map(CloudExpansion::getName).collect(Collectors.joining("&7, &6", "&8[&6", "&8]&r"))); + + downloadExpansions(plugin, expansions) + .thenCompose(files -> discoverExpansions(plugin, files)) + .whenComplete((classes, exception) -> { + if (exception != null) + { + Msg.msg(sender, + "&cFailed to update expansions: &e" + exception.getMessage()); + return; + } + + Msg.msg(sender, + "&aSuccessfully downloaded updates, registering new versions."); + + Bukkit.getScheduler().runTask(plugin, () -> { + final String message = classes.stream() + .map(plugin.getLocalExpansionManager()::register) + .filter(Optional::isPresent) + .map(Optional::get) + .map(expansion -> " &a" + expansion.getName() + " &f" + expansion.getVersion()) + .collect(Collectors.joining("\n")); + Msg.msg(sender, + "&7Registered expansions:", + message); + }); + }); + } + + @Override + public void complete(@NotNull final PlaceholderAPIPlugin plugin, @NotNull final CommandSender sender, @NotNull final String alias, @NotNull @Unmodifiable final List params, @NotNull final List suggestions) + { + + } + + + public static CompletableFuture> downloadExpansions(@NotNull final PlaceholderAPIPlugin plugin, @NotNull final List expansions) + { + final List> futures = expansions.stream() + .map(expansion -> plugin.getCloudExpansionManager().downloadExpansion(expansion, expansion.getVersion())) + .collect(Collectors.toList()); + + return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenApplyAsync(v -> futures.stream().map(CompletableFuture::join).collect(Collectors.toList())); + } + + public static CompletableFuture>> discoverExpansions(@NotNull final PlaceholderAPIPlugin plugin, @NotNull final List files) + { + final List>>> futures = files.stream() + .map(file -> plugin.getLocalExpansionManager().findExpansionsInFile(file)) + .collect(Collectors.toList()); + + return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenApplyAsync(v -> futures.stream().map(CompletableFuture::join).flatMap(Collection::stream).collect(Collectors.toList())); + } + +}