From f12490951a2e5c87c36c29e465ee4f7973d02a28 Mon Sep 17 00:00:00 2001 From: William Date: Wed, 24 Aug 2022 16:56:21 +0100 Subject: [PATCH] Finish /huskhomes command --- .../william278/huskhomes/BukkitHuskHomes.java | 20 +++--- .../net/william278/huskhomes/HuskHomes.java | 19 ++++++ .../huskhomes/command/HuskHomesCommand.java | 64 ++++++++++++++++--- 3 files changed, 82 insertions(+), 21 deletions(-) diff --git a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java index 2f4a1ac1..e53e6545 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java +++ b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java @@ -2,6 +2,7 @@ package net.william278.huskhomes; import io.papermc.lib.PaperLib; import net.william278.annotaml.Annotaml; +import net.william278.desertwell.Version; import net.william278.huskhomes.command.BukkitCommand; import net.william278.huskhomes.command.BukkitCommandType; import net.william278.huskhomes.command.CommandBase; @@ -21,14 +22,15 @@ import net.william278.huskhomes.messenger.PluginMessenger; import net.william278.huskhomes.messenger.RedisMessenger; import net.william278.huskhomes.player.BukkitPlayer; import net.william278.huskhomes.player.OnlineUser; -import net.william278.huskhomes.position.*; +import net.william278.huskhomes.position.Location; +import net.william278.huskhomes.position.SavedPositionManager; +import net.william278.huskhomes.position.Server; +import net.william278.huskhomes.position.World; import net.william278.huskhomes.random.NormalDistributionEngine; import net.william278.huskhomes.random.RtpEngine; import net.william278.huskhomes.request.RequestManager; import net.william278.huskhomes.teleport.TeleportManager; import net.william278.huskhomes.util.*; -import net.william278.desertwell.Version; -import net.william278.desertwell.UpdateChecker; import org.bstats.bukkit.Metrics; import org.bukkit.Bukkit; import org.bukkit.command.PluginCommand; @@ -212,14 +214,10 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes { // Check for updates if (settings.checkForUpdates) { getLoggingAdapter().log(Level.INFO, "Checking for updates..."); - final UpdateChecker updateChecker = UpdateChecker.create(getPluginVersion(), 83767); - updateChecker.isUpToDate().thenAccept(isUpToDate -> { - if (!isUpToDate) { - getLoggingAdapter().log(Level.WARNING, "An update is available for HuskHomes, v" + - updateChecker.getLatestVersion().join() - + " (Currently running v" + getPluginVersion() + ")"); - } - }); + getLatestVersionIfOutdated().thenAccept(newestVersion -> + newestVersion.ifPresent(newVersion -> getLoggingAdapter().log(Level.WARNING, + "An update is available for HuskHomes, v" + newVersion + + " (Currently running v" + getPluginVersion() + ")"))); } } catch (HuskHomesInitializationException exception) { getLoggingAdapter().log(Level.SEVERE, exception.getMessage()); diff --git a/common/src/main/java/net/william278/huskhomes/HuskHomes.java b/common/src/main/java/net/william278/huskhomes/HuskHomes.java index b7058bb3..1fd96ab5 100644 --- a/common/src/main/java/net/william278/huskhomes/HuskHomes.java +++ b/common/src/main/java/net/william278/huskhomes/HuskHomes.java @@ -1,5 +1,6 @@ package net.william278.huskhomes; +import net.william278.desertwell.UpdateChecker; import net.william278.huskhomes.command.CommandBase; import net.william278.huskhomes.config.Locales; import net.william278.huskhomes.config.Settings; @@ -29,6 +30,8 @@ import java.util.concurrent.CompletableFuture; */ public interface HuskHomes { + int SPIGOT_RESOURCE_ID = 83767; + /** * The platform plugin console logger * @@ -138,6 +141,22 @@ public interface HuskHomes { */ Optional getServerSpawn(); + /** + * Returns a future returning the latest plugin {@link Version} if the plugin is out-of-date + * + * @return a {@link CompletableFuture} returning the latest {@link Version} if the current one is out-of-date + */ + default CompletableFuture> getLatestVersionIfOutdated() { + final UpdateChecker updateChecker = UpdateChecker.create(getPluginVersion(), SPIGOT_RESOURCE_ID); + return updateChecker.isUpToDate().thenApply(upToDate -> { + if (upToDate) { + return Optional.empty(); + } else { + return Optional.of(updateChecker.getLatestVersion().join()); + } + }); + } + /** * Update the {@link Spawn} position to a location on the server * diff --git a/common/src/main/java/net/william278/huskhomes/command/HuskHomesCommand.java b/common/src/main/java/net/william278/huskhomes/command/HuskHomesCommand.java index faf58454..52d26075 100644 --- a/common/src/main/java/net/william278/huskhomes/command/HuskHomesCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/HuskHomesCommand.java @@ -1,8 +1,9 @@ package net.william278.huskhomes.command; +import de.themoep.minedown.MineDown; +import net.william278.desertwell.AboutMenu; import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.player.OnlineUser; -import net.william278.desertwell.AboutMenu; import net.william278.huskhomes.util.Permission; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -56,6 +57,11 @@ public class HuskHomesCommand extends CommandBase implements ConsoleExecutable, switch (args[0].toLowerCase()) { case "about" -> sendAboutMenu(onlineUser); case "help" -> { + if (!onlineUser.hasPermission(Permission.COMMAND_HUSKHOMES_HELP.node)) { + plugin.getLocales().getLocale("error_no_permission") + .ifPresent(onlineUser::sendMessage); + return; + } int page = 1; if (args.length == 2) { try { @@ -70,10 +76,33 @@ public class HuskHomesCommand extends CommandBase implements ConsoleExecutable, plugin.getLocales(), plugin.getCommands(), plugin.getSettings().listItemsPerPage, page)); } case "reload" -> { - + if (!onlineUser.hasPermission(Permission.COMMAND_HUSKHOMES_RELOAD.node)) { + plugin.getLocales().getLocale("error_no_permission") + .ifPresent(onlineUser::sendMessage); + return; + } + plugin.reload().thenAccept(reloaded -> { + if (!reloaded) { + onlineUser.sendMessage(new MineDown("[Error:](#ff3300) [Failed to reload the plugin. Check console for errors.](#ff7e5e)")); + return; + } + onlineUser.sendMessage(new MineDown("[HuskHomes](#00fb9a bold) �fb9a&| Reloaded config & message files.")); + }); } case "update" -> { - + if (!onlineUser.hasPermission(Permission.COMMAND_HUSKHOMES_UPDATE.node)) { + plugin.getLocales().getLocale("error_no_permission") + .ifPresent(onlineUser::sendMessage); + return; + } + plugin.getLatestVersionIfOutdated().thenAccept(newestVersion -> + newestVersion.ifPresentOrElse( + newVersion -> onlineUser.sendMessage( + new MineDown("[HuskHomes](#00fb9a bold) [| A new version of HuskHomes is available!" + + " (v" + newVersion + " (Currently running: v" + plugin.getPluginVersion() + ")](#00fb9a)")), + () -> onlineUser.sendMessage( + new MineDown("[HuskHomes](#00fb9a bold) [| HuskHomes is up-to-date." + + " (Currently running: v" + plugin.getPluginVersion() + "](#00fb9a)")))); } } } @@ -89,17 +118,32 @@ public class HuskHomesCommand extends CommandBase implements ConsoleExecutable, case "about" -> Arrays.stream(aboutMenu.toString().split("\n")).forEach(message -> plugin.getLoggingAdapter().log(Level.INFO, message)); case "help" -> { - - } - case "reload" -> { - - } - case "update" -> { - + plugin.getLoggingAdapter().log(Level.INFO, "List of enabled console-executable commands:"); + plugin.getCommands() + .stream().filter(command -> command instanceof ConsoleExecutable) + .forEach(command -> plugin.getLoggingAdapter().log(Level.INFO, + command.command + + (command.command.length() < 16 ? " ".repeat(16 - command.command.length()) : "") + + " - " + command.getDescription())); } + case "reload" -> plugin.reload().thenAccept(reloaded -> { + if (!reloaded) { + plugin.getLoggingAdapter().log(Level.SEVERE, "Failed to reload the plugin."); + return; + } + plugin.getLoggingAdapter().log(Level.INFO, "Reloaded config & message files."); + }); + case "update" -> plugin.getLatestVersionIfOutdated().thenAccept(newestVersion -> + newestVersion.ifPresentOrElse(newVersion -> plugin.getLoggingAdapter().log(Level.WARNING, + "An update is available for HuskHomes, v" + newVersion + + " (Currently running v" + plugin.getPluginVersion() + ")"), + () -> plugin.getLoggingAdapter().log(Level.INFO, + "HuskHomes is up to date" + + " (Currently running v" + plugin.getPluginVersion() + ")"))); } } + private void sendAboutMenu(@NotNull OnlineUser onlineUser) { if (!onlineUser.hasPermission(Permission.COMMAND_HUSKHOMES_ABOUT.node)) { plugin.getLocales().getLocale("error_no_permission")