From a19a569a04e645b6a9e77ba82f3d314061d77dd3 Mon Sep 17 00:00:00 2001 From: Aust1n46 Date: Sun, 16 Jan 2022 17:06:57 -0600 Subject: [PATCH] Register commands dynamically. --- .../initiators/listeners/CommandListener.java | 26 ++- src/main/resources/plugin.yml | 221 ------------------ 2 files changed, 23 insertions(+), 224 deletions(-) diff --git a/src/main/java/venture/Aust1n46/chat/initiators/listeners/CommandListener.java b/src/main/java/venture/Aust1n46/chat/initiators/listeners/CommandListener.java index 1c46a0e..0799dea 100644 --- a/src/main/java/venture/Aust1n46/chat/initiators/listeners/CommandListener.java +++ b/src/main/java/venture/Aust1n46/chat/initiators/listeners/CommandListener.java @@ -1,13 +1,18 @@ package venture.Aust1n46.chat.initiators.listeners; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.List; import java.util.Map; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandMap; import org.bukkit.command.CommandSender; +import org.bukkit.command.PluginCommand; import org.bukkit.command.TabExecutor; +import org.bukkit.plugin.Plugin; import com.google.inject.Inject; import com.google.inject.Singleton; @@ -137,6 +142,9 @@ public class CommandListener implements TabExecutor { @Inject private Unmuteall unmuteall; + private CommandMap commandMap; + private Constructor pluginCommandConstructor; + @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] parameters) { commands.get(command.getName()).execute(sender, command.getName(), parameters); @@ -150,6 +158,14 @@ public class CommandListener implements TabExecutor { @Inject public void postConstruct() { + commandMap = plugin.getServer().getCommandMap(); + try { + pluginCommandConstructor = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class); + pluginCommandConstructor.setAccessible(true); + } catch (NoSuchMethodException | SecurityException e) { + e.printStackTrace(); + } + commands.put("broadcast", broadcast); commands.put("channel", channel); commands.put("join", channel); @@ -211,9 +227,13 @@ public class CommandListener implements TabExecutor { }, 0); } - private void registerCommand(String command, CommandExecutor commandExecutor) { - if (plugin.getCommand(command) != null) { - plugin.getCommand(command).setExecutor(commandExecutor); + private void registerCommand(final String command, final CommandExecutor commandExecutor) { + try { + final PluginCommand pluginCommand = pluginCommandConstructor.newInstance(command, plugin); + pluginCommand.setExecutor(commandExecutor); + commandMap.getKnownCommands().put(command, pluginCommand); + } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); } } } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 9e9c941..10c7279 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -7,224 +7,3 @@ softdepend: [Towny, Factions, EssentialsDiscord] author: Aust1n46 website: https://bitbucket.org/Aust1n46/venturechat/ description: #1 Channels Chat plugin! Spigot + Bungee. Supports PlaceholderAPI + JSON formatting. Moderation GUI! -commands: - mute: - usage: /mute [playername] [channel] - aliases: [mp,vmute] - description: Mutes player in a channel so they cannot talk. - permission-message: You don't have - unmute: - usage: /unmute [playername] [channel] - aliases: [ump,vunmute] - description: Unmutes player in a channel so they can talk again. - permission-message: You don't have - msg: - usage: /msg [playername] [msg] - aliases: [vmsg] - description: Send a message to a player - permission-message: You don't have - tell: - usage: /tell [playername] [msg] - aliases: [vtell] - description: Send a message to a player - permission-message: You don't have - whisper: - usage: /whisper [playername] [msg] - aliases: [vwhisper,w] - description: Send a message to a player - permission-message: You don't have - message: - usage: /message [playername] [msg] - aliases: [vmessage,pm] - description: Send a message to a player - permission-message: You don't have - ignore: - usage: /ignore [playername] or /ignore ? for more information - aliases: [vignore] - description: This allows you to prevent a player from sending you a tell /ignore list to see who you have ignored - permission-message: You don't have - channel: - usage: /channel [channelname] - aliases: [ch,vchannel] - description: Allows players to add the ability to listen to the channel - permission-message: You don't have - leave: - usage: /leave [channelname] - aliases: [lev,vleave] - description: Allows players to leave listening to a channel - permission-message: You don't have - join: - usage: /join [channelname] - aliases: [vjoin] - description: Allows players to join a channel - permission-message: You don't have - chlist: - usage: /chlist - aliases: [chl,vchlist] - description: Allows players to see a listing of available channels - permission-message: You don't have - chwho: - usage: /chwho [channelname] - aliases: [chw,vchwho] - description: Allows players to see a listing of who is listening on a channel - permission-message: You don't have - setchannel: - usage: /setchannel [playername] [channel] - aliases: [sc,vsetchannel] - description: Sets a players channel - permission-message: You don't have - kickchannel: - usage: /kickchannel [playername] [channel] - aliases: [kc,vkickchannel] - description: Kicks a player out of a channel - permission-message: You don't have - muteall: - usage: /muteall [playername] - aliases: [mpa,vmuteall] - description: Mute a player in all channels - permission-message: You don't have - unmuteall: - usage: /unmuteall [playername] - aliases: [umpa,vunmuteall] - description: Unmute a player in all channels - permission-message: You don't have - kickchannelall: - usage: /kickchannelall [playername] - aliases: [kca,vkickchannelall] - description: Kick a player from all channels - permission-message: You don't have - setchannelall: - usage: /setchannelall [playername] - aliases: [sca,vsetchannelall] - description: Set a player into all channels - permission-message: You don't have - force: - usage: /force [playername] [message] - aliases: [for,vforce] - description: Force a player to chat or execute a command - permission-message: You don't have - forceall: - usage: /forceall [message] - aliases: [fora,vforceall] - description: Force all players to chat or execute a command - permission-message: You don't have - listen: - usage: /listen [channel] - aliases: [lis,vlisten] - description: Listen to a channel without setting it as the one your chatting in - permission-message: You don't have - chatreload: - usage: /chatreload - aliases: [cr,vchatreload] - description: Reload the config file - permission-message: You don't have - reply: - usage: /reply [msg] - aliases: [vreply] - description: Reply to a message - permission-message: You don't have - r: - usage: /r [msg] - aliases: [] - description: Reply to a message - permission-message: You don't have - spy: - usage: /spy - aliases: [vspy] - description: Spy on tells - permission-message: You don't have - commandspy: - usage: /commandspy - aliases: [comspy,vcommandspy] - description: Spy on commands - permission-message: You don't have - chatinfo: - usage: /chatinfo - aliases: [ci,vchatinfo] - description: Check a players chat info - permission-message: You don't have - channelinfo: - usage: /channelinfo - aliases: [chi,vchannelinfo] - description: Check a channels info - permission-message: You don't have - venturechat: - usage: /venturechat - aliases: [vc] - description: Check plugin information - permission-message: You don't have - me: - usage: /me - aliases: [vme] - description: Send an emote - permission-message: You don't have - filter: - usage: /filter - aliases: [fil,vfilter] - description: Toggle filter on and off - permission-message: You don't have - broadcast: - usage: /broadcast [msg] - aliases: [bc,vbroadcast] - description: Broadcast a message - permission-message: You don't have - commandblock: - usage: /commandblock [player] [command] - aliases: [cb,vcommandblock] - description: Toggle a player blocked from entering a command - permission-message: You don't have - party: - usage: /party help - aliases: [p,chatparty,cp,vparty] - description: Party commands - permission-message: You don't have - config: - usage: /config help - aliases: [vconfig] - description: Edit commands - permission-message: You don't have - clearchat: - usage: /clearchat - aliases: [cc,vclearchat] - description: Clear every players chat - permission-message: You don't have - notifications: - usage: /notifications - aliases: [notify,vnotify] - description: Toggles your notifications - permission-message: You don't have - removemessage: - usage: /removemessage [hashcode] - aliases: [rm,vremovemessage] - description: Remove a message from the chat - permission-message: You don't have - edit: - usage: / - aliases: [vedit] - description: Edit your last chat message - permission-message: You don't have - rangedspy: - usage: / - aliases: [rspy,vrangedspy] - description: Toggle spying on ranged channels - permission-message: You don't have - buttons: - usage: / - aliases: [vbuttons] - description: Toggle viewing json buttons - permission-message: You don't have - venturechatgui: - usage: / - aliases: [vchatgui] - description: Opens the chat management gui - permission-message: You don't have - messagetoggle: - usage: / - aliases: [mtoggle,vmessagetoggle] - description: Toggle receiving messages - permission-message: You don't have - bungeetoggle: - usage: / - aliases: [btoggle,vbungeetoggle] - description: Toggle receiving BungeeCord chat - permission-message: You don't have \ No newline at end of file