From 09e0d1f6829cc70f526ffb491197ea212bab9889 Mon Sep 17 00:00:00 2001 From: cnaude Date: Thu, 25 Jun 2015 21:43:59 -0700 Subject: [PATCH] Dynamically add /msg and /r when configured. --- .../com/cnaude/purpleirc/CommandHandlers.java | 4 +- .../java/com/cnaude/purpleirc/PurpleIRC.java | 58 ++++++++++++++++++- 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/cnaude/purpleirc/CommandHandlers.java b/src/main/java/com/cnaude/purpleirc/CommandHandlers.java index ac04e50..58710f3 100644 --- a/src/main/java/com/cnaude/purpleirc/CommandHandlers.java +++ b/src/main/java/com/cnaude/purpleirc/CommandHandlers.java @@ -128,7 +128,7 @@ public class CommandHandlers implements CommandExecutor { return true; } } - } else if (commandLabel.equalsIgnoreCase("ircm")) { + } else if (commandLabel.equalsIgnoreCase("msg")) { if (!sender.hasPermission("irc.smsg")) { sender.sendMessage(plugin.noPermission); return true; @@ -141,7 +141,7 @@ public class CommandHandlers implements CommandExecutor { commands.get("smsg").dispatch(sender, list.toArray(new String[list.size()])); return true; } - } else if (commandLabel.equalsIgnoreCase("ircr")) { + } else if (commandLabel.equalsIgnoreCase("r")) { if (!sender.hasPermission("irc.smsg")) { sender.sendMessage(plugin.noPermission); return true; diff --git a/src/main/java/com/cnaude/purpleirc/PurpleIRC.java b/src/main/java/com/cnaude/purpleirc/PurpleIRC.java index 9f458dd..c0a5ad5 100644 --- a/src/main/java/com/cnaude/purpleirc/PurpleIRC.java +++ b/src/main/java/com/cnaude/purpleirc/PurpleIRC.java @@ -71,10 +71,14 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.lang.management.ManagementFactory; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.net.InetAddress; import java.net.UnknownHostException; import java.text.Collator; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -84,13 +88,17 @@ import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.codec.binary.Base64; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.OfflinePlayer; +import org.bukkit.command.CommandMap; import org.bukkit.command.CommandSender; +import org.bukkit.command.PluginCommand; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.SimplePluginManager; import org.bukkit.plugin.java.JavaPlugin; import org.pircbotx.IdentServer; @@ -290,8 +298,9 @@ public class PurpleIRC extends JavaPlugin { getCommand("irc").setExecutor(commandHandlers); getCommand("irc").setTabCompleter(ircTabCompleter); if (overrideMsgCmd) { - getCommand("ircm").setExecutor(commandHandlers); - getCommand("ircr").setExecutor(commandHandlers); + registerCommand("msg", "r"); + getCommand("msg").setExecutor(commandHandlers); + getCommand("r").setExecutor(commandHandlers); } regexGlobber = new RegexGlobber(); tokenizer = new ChatTokenizer(this); @@ -1605,7 +1614,7 @@ public class PurpleIRC extends JavaPlugin { } /** - * Generic player counter. CB uses Player[] and Spigot uses List<>(). + * Generic player counter. CB uses Player[] and Spigot uses List<>(). */ public int getOnlinePlayerCount() { int count = 0; @@ -1615,4 +1624,47 @@ public class PurpleIRC extends JavaPlugin { return count; } + /* + * https://bukkit.org/threads/tutorial-registering-commands-at-runtime.158461/ + */ + public void registerCommand(String... aliases) { + PluginCommand command = getCommand(aliases[0], this); + + command.setAliases(Arrays.asList(aliases)); + getCommandMap().register(this.getDescription().getName(), command); +} + + private PluginCommand getCommand(String name, Plugin plugin) { + logInfo("Registering command: " + name); + PluginCommand command = null; + + try { + Constructor c = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class); + c.setAccessible(true); + + command = c.newInstance(name, plugin); + } catch (SecurityException | IllegalArgumentException | IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException e) { + logError(e.getMessage()); + } + + return command; + } + + private CommandMap getCommandMap() { + CommandMap commandMap = null; + + try { + if (Bukkit.getPluginManager() instanceof SimplePluginManager) { + Field f = SimplePluginManager.class.getDeclaredField("commandMap"); + f.setAccessible(true); + + commandMap = (CommandMap) f.get(Bukkit.getPluginManager()); + } + } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { + logError(e.getMessage()); + } + + return commandMap; + } + }