From a0c211f4beedcbc668388e704f0b8fcdad4f0313 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 24 Feb 2020 12:57:57 +0100 Subject: [PATCH] Attempt to register custom commands --- BuildConfigUpdater/pom.xml | 42 ----------- BuildConfigUpdater/src/main/java/BCUMain.java | 16 ---- .../java/buttondevteam/core/MainPlugin.java | 5 +- .../core/component/updater/PluginUpdater.java | 75 ------------------- .../updater/PluginUpdaterComponent.java | 21 ------ .../updater/UpdatePluginCommand.java | 42 ----------- .../java/buttondevteam/lib/TBMCCoreAPI.java | 23 ------ .../buttondevteam/lib/chat/Command2MC.java | 17 +++++ pom.xml | 1 - 9 files changed, 18 insertions(+), 224 deletions(-) delete mode 100644 BuildConfigUpdater/pom.xml delete mode 100644 BuildConfigUpdater/src/main/java/BCUMain.java delete mode 100755 Chroma-Core/src/main/java/buttondevteam/core/component/updater/PluginUpdater.java delete mode 100644 Chroma-Core/src/main/java/buttondevteam/core/component/updater/PluginUpdaterComponent.java delete mode 100755 Chroma-Core/src/main/java/buttondevteam/core/component/updater/UpdatePluginCommand.java diff --git a/BuildConfigUpdater/pom.xml b/BuildConfigUpdater/pom.xml deleted file mode 100644 index da52ad6..0000000 --- a/BuildConfigUpdater/pom.xml +++ /dev/null @@ -1,42 +0,0 @@ - - - - ChromaCore - com.github.TBMCPlugins - master-SNAPSHOT - - 4.0.0 - - BuildConfigUpdater - - - - Jitpack - https://jitpack.io/ - - - spigot-repo - https://hub.spigotmc.org/nexus/content/repositories/snapshots/ - - - - - - com.github.TBMCPlugins.ChromaCore - Chroma-Core - master-SNAPSHOT - - - org.spigotmc - spigot-api - 1.12.2-R0.1-SNAPSHOT - - - commons-io - commons-io - 2.6 - - - \ No newline at end of file diff --git a/BuildConfigUpdater/src/main/java/BCUMain.java b/BuildConfigUpdater/src/main/java/BCUMain.java deleted file mode 100644 index a5e2007..0000000 --- a/BuildConfigUpdater/src/main/java/BCUMain.java +++ /dev/null @@ -1,16 +0,0 @@ -import buttondevteam.core.component.updater.PluginUpdater; - -import java.util.List; -import java.util.stream.Collectors; - -public class BCUMain { - public static void main(String[] args) { - System.out.println("Getting list of repositories..."); - List plugins = PluginUpdater.GetPluginNames(); - System.out.println("Removing non-Maven projects..."); - plugins.removeIf(plugin -> PluginUpdater.isNotMaven(plugin, "master")); - System.out.println(plugins.stream().collect(Collectors.joining("\n"))); - for (String plugin : plugins) { //TODO: We don't want to apply it all at once, especially to unused/unowned repos - } //TODO: Add it to ButtonCore - or actually as a plugin or ButtonProcessor - } -} diff --git a/Chroma-Core/src/main/java/buttondevteam/core/MainPlugin.java b/Chroma-Core/src/main/java/buttondevteam/core/MainPlugin.java index e250095..3f6cc76 100755 --- a/Chroma-Core/src/main/java/buttondevteam/core/MainPlugin.java +++ b/Chroma-Core/src/main/java/buttondevteam/core/MainPlugin.java @@ -8,8 +8,6 @@ import buttondevteam.core.component.randomtp.RandomTPComponent; import buttondevteam.core.component.restart.RestartComponent; import buttondevteam.core.component.spawn.SpawnComponent; import buttondevteam.core.component.towny.TownyComponent; -import buttondevteam.core.component.updater.PluginUpdater; -import buttondevteam.core.component.updater.PluginUpdaterComponent; import buttondevteam.core.component.votifier.VotifierComponent; import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.architecture.ButtonPlugin; @@ -101,10 +99,9 @@ public class MainPlugin extends ButtonPlugin { logger = getLogger(); if (!setupPermissions()) throw new NullPointerException("No permission plugin found!"); - if (!setupEconomy()) //Though Essentials always provides economy so this shouldn't happen + if (!setupEconomy()) //Though Essentials always provides economy, but we don't require Essentials getLogger().warning("No economy plugin found! Components using economy will not be registered."); saveConfig(); - Component.registerComponent(this, new PluginUpdaterComponent()); Component.registerComponent(this, new RestartComponent()); //noinspection unchecked - needed for testing Component.registerComponent(this, new ChannelComponent()); diff --git a/Chroma-Core/src/main/java/buttondevteam/core/component/updater/PluginUpdater.java b/Chroma-Core/src/main/java/buttondevteam/core/component/updater/PluginUpdater.java deleted file mode 100755 index 594124d..0000000 --- a/Chroma-Core/src/main/java/buttondevteam/core/component/updater/PluginUpdater.java +++ /dev/null @@ -1,75 +0,0 @@ -package buttondevteam.core.component.updater; - -import buttondevteam.lib.TBMCCoreAPI; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; - -public class PluginUpdater { - - private final File updatedir = Bukkit.getServer().getUpdateFolderFile(); - /** - * See {@link TBMCCoreAPI#UpdatePlugin(String, CommandSender, String)} - */ - public boolean UpdatePlugin(String name, CommandSender sender, String branch) { - if (!updatedir.exists() && !updatedir.mkdirs()) { - error(sender, "Failed to create update directory!"); - return false; - } - /*info(sender, "Checking plugin name..."); - List plugins = GetPluginNames(); - String correctname = null; - for (String plugin : plugins) { - if (plugin.equalsIgnoreCase(name)) { - correctname = plugin; // Fixes capitalization - break; - } - } - if (correctname == null) { - error(sender, "Can't find plugin: " + name); - return false; - } - info(sender, "Checking branch name..."); - if (!TBMCCoreAPI.IsTestServer() && !branch.equalsIgnoreCase("master")) { - error(sender, "The server is in production mode, updating only allowed from master!"); - return false; - } - Optional correctbranch = GetPluginBranches(correctname).stream().filter(b -> b.equalsIgnoreCase(branch)) - .findAny(); - if (!correctbranch.isPresent()) { - error(sender, "Can't find branch \"" + branch + "\" for plugin \"" + correctname + "\""); - return false; - } - if (isNotMaven(correctname, correctbranch.get())) { - error(sender, "The plugin doesn't appear to have a pom.xml. Make sure it's a Maven project."); - return false; - } - info(sender, "Updating TBMC plugin: " + correctname + " from " + correctbranch.get()); - return updatePluginJitPack(sender, correctname, correctbranch.get());*/ - info(sender, "Not implemented"); - return true; - } - - private static void error(CommandSender sender, String message) { - if (!sender.equals(Bukkit.getConsoleSender())) - Bukkit.getLogger().warning(message); - sender.sendMessage("§c" + message); - } - - private static void info(CommandSender sender, String message) { - if (!sender.equals(Bukkit.getConsoleSender())) - Bukkit.getLogger().info(message); - sender.sendMessage("§b" + message); - } -} diff --git a/Chroma-Core/src/main/java/buttondevteam/core/component/updater/PluginUpdaterComponent.java b/Chroma-Core/src/main/java/buttondevteam/core/component/updater/PluginUpdaterComponent.java deleted file mode 100644 index 4ed8f63..0000000 --- a/Chroma-Core/src/main/java/buttondevteam/core/component/updater/PluginUpdaterComponent.java +++ /dev/null @@ -1,21 +0,0 @@ -package buttondevteam.core.component.updater; - -import buttondevteam.core.MainPlugin; -import buttondevteam.lib.architecture.Component; -import buttondevteam.lib.architecture.ComponentMetadata; - -/** - * Downloads plugin updates built from their source using JitPack - doesn't work anymore - */ -@ComponentMetadata(enabledByDefault = false) -public class PluginUpdaterComponent extends Component { //TODO: Config - @Override - public void enable() { - registerCommand(new UpdatePluginCommand()); - } - - @Override - public void disable() { //Commands are automatically unregistered - - } -} diff --git a/Chroma-Core/src/main/java/buttondevteam/core/component/updater/UpdatePluginCommand.java b/Chroma-Core/src/main/java/buttondevteam/core/component/updater/UpdatePluginCommand.java deleted file mode 100755 index d236be5..0000000 --- a/Chroma-Core/src/main/java/buttondevteam/core/component/updater/UpdatePluginCommand.java +++ /dev/null @@ -1,42 +0,0 @@ -package buttondevteam.core.component.updater; - -import buttondevteam.core.MainPlugin; -import buttondevteam.lib.TBMCCoreAPI; -import buttondevteam.lib.chat.Command2; -import buttondevteam.lib.chat.CommandClass; -import buttondevteam.lib.chat.ICommand2MC; -import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; - -import java.lang.reflect.Method; - -@CommandClass(modOnly = true) -public class UpdatePluginCommand extends ICommand2MC { - public void def(CommandSender sender, @Command2.OptionalArg String plugin, @Command2.OptionalArg String branch) { - Bukkit.getScheduler().runTaskAsynchronously(MainPlugin.Instance, () -> { - if (plugin == null) { - sender.sendMessage("Downloading plugin names..."); - boolean first = true; - for (String plugin2 : PluginUpdater.GetPluginNames()) { - if (first) { - sender.sendMessage("§6---- Plugin names ----"); - first = false; - } - sender.sendMessage("- " + plugin2); - } - } else { - TBMCCoreAPI.UpdatePlugin(plugin, sender, branch == null ? "master" : branch); - } - }); - } - - @Override - public String[] getHelpText(Method method, Command2.Subcommand ann) { - return new String[]{ // - "§6---- Update plugin ----", // - "This command downloads the latest version of a custom plugin from GitHub", // - "To update a plugin: add its name", // - "To list the plugin names: don't type a name" // - }; - } -} diff --git a/Chroma-Core/src/main/java/buttondevteam/lib/TBMCCoreAPI.java b/Chroma-Core/src/main/java/buttondevteam/lib/TBMCCoreAPI.java index 7e8939b..244a16b 100755 --- a/Chroma-Core/src/main/java/buttondevteam/lib/TBMCCoreAPI.java +++ b/Chroma-Core/src/main/java/buttondevteam/lib/TBMCCoreAPI.java @@ -1,7 +1,6 @@ package buttondevteam.lib; import buttondevteam.core.MainPlugin; -import buttondevteam.core.component.updater.PluginUpdater; import buttondevteam.lib.player.ChromaGamerBase; import buttondevteam.lib.potato.DebugPotato; import org.bukkit.Bukkit; @@ -30,28 +29,6 @@ public class TBMCCoreAPI { } }; - /** - * Updates or installs the specified plugin. The plugin must use Maven. - * - * @param name The plugin's repository name. - * @param sender The command sender (if not console, messages will be printed to console as well). - */ - public static void UpdatePlugin(String name, CommandSender sender) { - UpdatePlugin(name, sender, "master"); - } - - /** - * Updates or installs the specified plugin from the specified branch. The plugin must use Maven. - * - * @param name The plugin's repository name. - * @param sender The command sender (if not console, messages will be printed to console as well). - * @param branch The branch to download the plugin from. - * @return Success or not - */ - public static boolean UpdatePlugin(String name, CommandSender sender, String branch) { - return PluginUpdater.UpdatePlugin(name, sender, branch); - } - public static String DownloadString(String urlstr) throws IOException { URL url = new URL(urlstr); URLConnection con = url.openConnection(); diff --git a/Chroma-Core/src/main/java/buttondevteam/lib/chat/Command2MC.java b/Chroma-Core/src/main/java/buttondevteam/lib/chat/Command2MC.java index ff4b7f4..25138c7 100644 --- a/Chroma-Core/src/main/java/buttondevteam/lib/chat/Command2MC.java +++ b/Chroma-Core/src/main/java/buttondevteam/lib/chat/Command2MC.java @@ -1,6 +1,7 @@ package buttondevteam.lib.chat; import buttondevteam.core.MainPlugin; +import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.architecture.ButtonPlugin; import buttondevteam.lib.architecture.Component; import lombok.val; @@ -8,6 +9,7 @@ import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.command.SimpleCommandMap; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -16,6 +18,7 @@ import org.bukkit.permissions.Permission; import org.bukkit.permissions.PermissionDefault; import java.lang.annotation.Annotation; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Arrays; import java.util.HashMap; @@ -51,6 +54,8 @@ public class Command2MC extends Command2 implemen Bukkit.getPluginManager().addPermission(new Permission(perm, PermissionDefault.OP)); //Do not allow any commands that belong to a group } + + registerOfficially(command); } @Override @@ -255,4 +260,16 @@ public class Command2MC extends Command2 implemen }*/ } } + + private boolean shouldRegisterOfficially=true; + private void registerOfficially(ICommand2MC command) { + if(!shouldRegisterOfficially) return; + try { + var cmdmap=(SimpleCommandMap)Bukkit.getServer().getClass().getMethod("getCommandMap").invoke(Bukkit.getServer()); + //cmdmap.register(command.getPlugin()) + } catch (Exception e) { + TBMCCoreAPI.SendException("Failed to register command in command map!", e); + shouldRegisterOfficially=false; + } + } } diff --git a/pom.xml b/pom.xml index c598874..e1bbf23 100755 --- a/pom.xml +++ b/pom.xml @@ -17,7 +17,6 @@ CorePOM Chroma-Core ButtonProcessor - BuildConfigUpdater