Dynamically add /msg and /r when configured.

This commit is contained in:
cnaude 2015-06-25 21:43:59 -07:00
parent d17b11a6de
commit 09e0d1f682
2 changed files with 57 additions and 5 deletions

View File

@ -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;

View File

@ -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<PluginCommand> 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;
}
}