It's a really simple thing to do, and it's something all servers should include. Nothing uglier than the default unknown command message now is there? Code:java @EventHandler public void onInvalidCommand(PlayerCommandPreprocessEvent event) { //Get the command String command = event.getMessage().split(" ")[0]; //Checks to see if a command by that name has been registered. if(Bukkit.getHelpMap().getHelpTopic(command) == null) { //Your error message here } }
with this code, you get the normal unkown command message and it doesnt send the player the custom unkown command message... use this: Code:Java @EventHandlerpublic void onUnkownCommand(PlayerCommandPreprocessEvent e) {if (Bukkit.getHelpMap().getHelpTopic(e.getMessage().split(" ")[0]) == null)e.setCancelled(true);e.getPlayer().sendMessage("Example Text For Unkown Command!");}
And if you would read the code section in the first post, you might see that there's a comment saying '// Your error message here' - which means (everyone programming for Bukkit should know that) that you have to send your own message..
FisheyLP /offtopic By the way, your signature implies you could have only one like ever because you are always a calling a new instance of 'FisheyLP' .. so because the default integer is 0, every time you make a new FisheyLP, you have 0 likes each time. So to give a new like, you could have only one like maximum. Just sayin'
This probably won't work for plugins that dynamically add commands. Normally, those who do dynamically add and remove commands, they don't add help topics because there's no point to sometimes. The only "reliable" way in doing this without using RawCode's string pool hack is: Code:java public boolean doesCommandExists(String str){ Field cmdMap_f = SimplePluginManager.class.getDeclaredField("commandMap"); cmdMap_f.setAccessible(true); Field kC_f = SimpleCommandMap.class.getDeclaredField("knownCommands"); kC_f.setAccessible(true); Map<String, Command> map = (Map<String, Command>)kC_f.get(cmdMap_f.get(Bukkit.getPluginManager()); for(Command c : map.valueSet()){ if(c.getName().equalsIgnoreCase(str)){ return true; } }} I don't recommend using this code since: I have not tested it. I only referenced the Bukkit API source files; I haven't had the slightest if this is actually updated. Reflection causes performance to drop. You can fix this by moving cmdMap_f and kC_f outside the method.
Is it wrong that I couldn't stop reading the kC_f variable as KFC... i must be hungry :3 On another note, wouldn't the dynamically added commands work with the first code snippet? I haven't looked but I would assume that "Bukkit.getHelpMap()" would get updated as new commands are added, even during runtime. Or am I wrong on that?
When the server starts up or is reloaded, help is processed and topics are added in the following order: General topics are loaded from the help.yml Plugins load and optionally call addTopic() Registered plugin commands are processed by HelpTopicFactory objects to create topics Topic contents are amended as directed in help.yml Taken from http://jd.bukkit.org/dev/apidocs/org/bukkit/help/HelpMap.html I would assume that adding in your own commands via a CommandMap doesn't automatically generate a help entry.