From 54ad85496ccba036ebdfdc83b4efb48919fe3f5c Mon Sep 17 00:00:00 2001 From: Timon Date: Fri, 15 Dec 2023 17:29:43 +0100 Subject: [PATCH] Updated CommandAPI -> Untested 1.20.3/1.20.4 support Removed deprecated stuff Config consistency --- pom.xml | 2 +- src/main/java/timongcraft/system/Main.java | 17 +-- .../system/commands/PluginCommand.java | 127 ------------------ .../system/util/AutoSaveHandler.java | 31 ----- .../system/util/CommandAPILoader.java | 6 +- src/main/resources/config.yml | 9 +- 6 files changed, 9 insertions(+), 183 deletions(-) delete mode 100644 src/main/java/timongcraft/system/commands/PluginCommand.java delete mode 100644 src/main/java/timongcraft/system/util/AutoSaveHandler.java diff --git a/pom.xml b/pom.xml index 88e4a85..c6be197 100644 --- a/pom.xml +++ b/pom.xml @@ -63,7 +63,7 @@ dev.jorel commandapi-bukkit-core - 9.2.0 + 9.3.0 provided diff --git a/src/main/java/timongcraft/system/Main.java b/src/main/java/timongcraft/system/Main.java index 48aec39..6c44e6e 100644 --- a/src/main/java/timongcraft/system/Main.java +++ b/src/main/java/timongcraft/system/Main.java @@ -18,7 +18,6 @@ public class Main extends JavaPlugin { private static Main instance; private final String prefix = getConfig().getString("prefix.pluginPrefix"); private DataConfigHandler dataConfigHandler; - private AutoSaveHandler autoSaveHandler; private boolean noLoad; @Override @@ -27,9 +26,9 @@ public void onLoad() { noLoad = !new File(getDataFolder(), "config.yml").exists(); - if (noLoad || Main.get().getConfig().getBoolean("CommandAPI.autoDownload")) { + if (noLoad || Main.get().getConfig().getBoolean("commandAPI.autoDownload")) { try { - CommandAPILoader.load("9.2.0", noLoad); + CommandAPILoader.load("9.3.0", noLoad); } catch (Exception e) { throw new RuntimeException(e); } @@ -44,16 +43,12 @@ public void onEnable() { configVersionCheck(); if (noLoad) return; - PluginCommand.disablePluginsOnBoot(); - if (getConfig().getBoolean("newUpdateNotifications.console")) UpdateCheckHandler.checkForUpdate(Double.parseDouble(getDescription().getVersion())); registerCommandsInOnEnable(); registerEvents(); - - enableAutoSave(); } @Override @@ -61,7 +56,6 @@ public void onDisable() { if (noLoad) return; if (dataConfigHandler != null) dataConfigHandler.save(); - if (getConfig().getBoolean("autoSave.enabled")) autoSaveHandler.cancel(); } private void configVersionCheck() { @@ -92,7 +86,6 @@ private void registerCommandsInOnEnable() { MaintenanceCommand.register(); if (Main.get().getConfig().getBoolean("permissionSystem.enabled")) PermissionManagerCommand.register(); - PluginCommand.register(); RebootCommand.register(); ReloadConfigsCommand.register(); ResourcePackCommand.register(); @@ -116,12 +109,6 @@ private void registerEvents() { pluginManager.registerEvents(new HopperFilterHandler(), this); } - private void enableAutoSave() { - autoSaveHandler = new AutoSaveHandler(); - if (getConfig().getBoolean("autoSave.enabled")) - autoSaveHandler.runTaskTimer(this, autoSaveHandler.parseInterval(Main.get().getConfig().getString("autoSave.time")), autoSaveHandler.parseInterval(Main.get().getConfig().getString("autoSave.time"))); - } - private void loadConfigs() { if (!new File(getDataFolder(), "config.yml").exists()) { saveDefaultConfig(); diff --git a/src/main/java/timongcraft/system/commands/PluginCommand.java b/src/main/java/timongcraft/system/commands/PluginCommand.java deleted file mode 100644 index 557b41b..0000000 --- a/src/main/java/timongcraft/system/commands/PluginCommand.java +++ /dev/null @@ -1,127 +0,0 @@ -package timongcraft.system.commands; - -import dev.jorel.commandapi.CommandTree; -import dev.jorel.commandapi.arguments.ArgumentSuggestions; -import dev.jorel.commandapi.arguments.LiteralArgument; -import dev.jorel.commandapi.arguments.StringArgument; -import dev.jorel.commandapi.executors.CommandArguments; -import jdk.jfr.Description; -import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; -import org.bukkit.plugin.Plugin; -import org.bukkit.plugin.PluginManager; -import timongcraft.system.Main; - -import java.util.ArrayList; -import java.util.List; - -@Deprecated(forRemoval = true) -@Description("Plugin loading and unloading can cause unexpected failures for unfamiliar users") -public class PluginCommand { - - public static void register() { - new CommandTree("plugin") - .withShortDescription("Toggle plugins") - .withUsage("/plugin ") - .withPermission("tgc-system.team") - .then(new LiteralArgument("enable") - .then(new StringArgument("plugin") - .replaceSuggestions(ArgumentSuggestions.strings(info -> getDisabledPlugins())) - .executes(PluginCommand::pluginEnableManager))) - .then(new LiteralArgument("disable") - .then(new StringArgument("plugin") - .replaceSuggestions(ArgumentSuggestions.strings(info -> getEnabledPlugins())) - .executes(PluginCommand::pluginDisableManager))) - .register(); - } - - private static String[] getDisabledPlugins() { - List plugins = new ArrayList<>(); - List disabledPlugins = Main.get().getDataConfig().getStringList("disabledPlugins"); - for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) { - if (plugin.getName().equalsIgnoreCase(Main.get().getName())) continue; - if (!(disabledPlugins.contains(plugin.getName()))) continue; - plugins.add(plugin.getName()); - } - return plugins.toArray(new String[0]); - } - - private static String[] getEnabledPlugins() { - List plugins = new ArrayList<>(); - for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) { - if (plugin.getName().equalsIgnoreCase(Main.get().getName())) continue; - if (!plugin.isEnabled()) continue; - plugins.add(plugin.getName()); - } - return plugins.toArray(new String[0]); - } - - private static int pluginEnableManager(CommandSender sender, CommandArguments args) { - String pluginName = (String) args.get("plugin"); - - Plugin targetPlugin = Main.get().getServer().getPluginManager().getPlugin(pluginName); - if (targetPlugin == null) { - sender.sendMessage(Main.get().getPrefix() + "§c" + pluginName + " not found."); - return 0; - } - - if (!Main.get().getDataConfig().isSet("disabledPlugins") || Main.get().getDataConfig().getStringList("disabledPlugins").isEmpty()) { - sender.sendMessage(Main.get().getPrefix() + "§c" + pluginName + " can't be enabled."); - return 0; - } - - List disabledPlugins = Main.get().getDataConfig().getStringList("disabledPlugins"); - if (disabledPlugins.contains(pluginName)) { - Main.get().getServer().getPluginManager().enablePlugin(targetPlugin); - disabledPlugins.remove(pluginName); - Main.get().getDataConfig().set("disabledPlugins", disabledPlugins); - Main.get().getDataConfig().save(); - sender.sendMessage(Main.get().getPrefix() + pluginName + " has been enabled."); - } else { - sender.sendMessage(Main.get().getPrefix() + "§c" + pluginName + " is not disabled."); - } - return 1; - } - - private static int pluginDisableManager(CommandSender sender, CommandArguments args) { - String pluginName = (String) args.get("plugin"); - - Plugin targetPlugin = Main.get().getServer().getPluginManager().getPlugin(pluginName); - if (targetPlugin == null) { - sender.sendMessage(Main.get().getPrefix() + "§c" + pluginName + " not found."); - return 0; - } - - if (targetPlugin.getName().equals(Main.get().getName())) { - sender.sendMessage(Main.get().getPrefix() + "§cThis plugin can't be disabled."); - return 0; - } - - Main.get().getServer().getPluginManager().disablePlugin(targetPlugin); - if (!Main.get().getDataConfig().isSet("disabledPlugins")) - Main.get().getDataConfig().set("disabledPlugins", new ArrayList<>()); - List disabledPlugins = Main.get().getDataConfig().getStringList("disabledPlugins"); - disabledPlugins.add(pluginName); - Main.get().getDataConfig().set("disabledPlugins", disabledPlugins); - Main.get().getDataConfig().save(); - sender.sendMessage(Main.get().getPrefix() + pluginName + " has been disabled."); - return 1; - } - - public static void disablePluginsOnBoot() { - List disabledPlugins = Main.get().getDataConfig().getStringList("disabledPlugins"); - if (disabledPlugins.isEmpty()) return; - - PluginManager pluginManager = Main.get().getServer().getPluginManager(); - for (String pluginName : disabledPlugins) { - Plugin targetPlugin = pluginManager.getPlugin(pluginName); - if (targetPlugin == null) continue; - - Bukkit.getScheduler().runTaskLater(Main.get(), () -> { - Main.get().getLogger().info("Disabling " + targetPlugin.getName()); - pluginManager.disablePlugin(targetPlugin); - }, 200); - } - } - -} diff --git a/src/main/java/timongcraft/system/util/AutoSaveHandler.java b/src/main/java/timongcraft/system/util/AutoSaveHandler.java deleted file mode 100644 index f6aa3f4..0000000 --- a/src/main/java/timongcraft/system/util/AutoSaveHandler.java +++ /dev/null @@ -1,31 +0,0 @@ -package timongcraft.system.util; - -import net.md_5.bungee.api.chat.TextComponent; -import org.bukkit.Bukkit; -import org.bukkit.World; -import org.bukkit.scheduler.BukkitRunnable; - -@Deprecated(forRemoval = true) -public class AutoSaveHandler extends BukkitRunnable { - - @Override - public void run() { - Bukkit.getLogger().info("[AutoSave] saving the game.."); - MessageUtils.sendAdminMessage("AutoSave", new TextComponent("Saving the game...")); - - for (World world : Bukkit.getWorlds()) - world.save(); - Bukkit.savePlayers(); - } - - public long parseInterval(String interval) { - if (interval == null) return 30 * 60 * 20; - int number = Integer.parseInt(interval.substring(0, interval.length() - 1)); - char unit = interval.charAt(interval.length() - 1); - if (unit == 'm') return (long) number * 60 * 20; - else if (unit == 'h') return (long) number * 60 * 60 * 20; - else return 30 * 60 * 20; - } - -} - diff --git a/src/main/java/timongcraft/system/util/CommandAPILoader.java b/src/main/java/timongcraft/system/util/CommandAPILoader.java index d337598..e36bdbe 100644 --- a/src/main/java/timongcraft/system/util/CommandAPILoader.java +++ b/src/main/java/timongcraft/system/util/CommandAPILoader.java @@ -13,6 +13,7 @@ import java.util.stream.Stream; public class CommandAPILoader { + public static void load(String version, boolean noConfig) throws IOException, InvalidPluginException, InvalidDescriptionException { try (Stream pathStream = Files.list(Path.of("plugins"))) { if (pathStream.anyMatch(file -> { @@ -20,7 +21,7 @@ public static void load(String version, boolean noConfig) throws IOException, In return false; if (!file.getFileName().toString().toLowerCase().equals("commandapi-" + version + ".jar")) { - if (noConfig || Main.get().getConfig().getBoolean("CommandAPI.warnings")) { + if (noConfig || Main.get().getConfig().getBoolean("commandAPI.warnings")) { Main.get().getLogger().warning("You may have loaded an incompatible version of CommandAPI. For best compatibility, use version " + version + "."); } } @@ -36,7 +37,7 @@ public static void load(String version, boolean noConfig) throws IOException, In int responseCode = con.getResponseCode(); if (responseCode != 200) { - Main.get().getLogger().warning("Failed to check for updates. Response code: " + responseCode); + Main.get().getLogger().warning("Failed to load required library CommandAPI. Response code: " + responseCode); } Path commandAPIPath = Path.of("plugins/CommandAPI-" + version + ".jar"); @@ -45,4 +46,5 @@ public static void load(String version, boolean noConfig) throws IOException, In con.disconnect(); Bukkit.getPluginManager().loadPlugin(commandAPIPath.toFile()); } + } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index be397d4..817ef62 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,5 +1,5 @@ #Do not edit config version or things might break! -configVersion: 1.8 +configVersion: 1.9 prefix: pluginPrefix: '§7[§1System§7] §f' @@ -101,11 +101,6 @@ maintenance: #Must be called maintenance-icon.png icon: false -#Auto save your world if your hoster doesn't have that functionality -autoSave: - enabled: false - time: '1h' - #This makes to so if you right-click on wheat, potatoes, carrots, beetroots and cocoa beans easyHarvest: enabled: false @@ -121,7 +116,7 @@ blockedCommands: [] #These prefixes are blocked e.g. 'bukkit:' blockedPrefixes: [] -CommandAPI: +commandAPI: #Warns if an incompatible version of CommandAPI is loaded etc. warnings: true #Automaticly downloads and loads the CommandAPI if it isn't present