[Resource] Custom Unknown Command Message

Discussion in 'Resources' started by ROTN, Jun 26, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    ROTN

    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
    1. @EventHandler
    2. public void onInvalidCommand(PlayerCommandPreprocessEvent event) {
    3. //Get the command
    4. String command = event.getMessage().split(" ")[0];
    5. //Checks to see if a command by that name has been registered.
    6. if(Bukkit.getHelpMap().getHelpTopic(command) == null) {
    7. //Your error message here
    8. }
    9. }
     
  2. 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
    1.  
    2. @EventHandler
    3. public void onUnkownCommand(PlayerCommandPreprocessEvent e) {
    4. if (Bukkit.getHelpMap().getHelpTopic(e.getMessage().split(" ")[0]) == null)
    5. e.setCancelled(true);
    6. e.getPlayer().sendMessage("Example Text For Unkown Command!");
    7. }
    8.  
     
  3. Offline

    Panjab

    FisheyLP

    You are doing exactly the same thing, just compressed to one line.
     
  4. Panjab
     
  5. Offline

    Panjab

    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..
     
  6. not everyone. you didnt say to cancel the event too
     
  7. Offline

    AoH_Ruthless

    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' :)
     
    ChipDev, CeramicTitan and iKeirNez like this.
  8. Offline

    mazentheamazin

    AoH_Ruthless
    Not the best place to post that, maybe on his profile posts :p
     
    AoH_Ruthless likes this.
  9. But why should I make a new FisheyLP? ^^
     
  10. Offline

    AoH_Ruthless

    FisheyLP
    Idk, ask your code why you are making a new instance every time :)
     
  11. FisheyLP
    You're asking why you made a new intance every time? Shouldn't you know that? :p
     
    AoH_Ruthless likes this.
  12. ?
     
  13. Offline

    RawCode

  14. Offline

    Onlineids

    Are you high?
     
  15. Offline

    MCraftGamer35

    Code:java
    1. Thank you, I was looking for this!
     
  16. Offline

    Cirno

    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
    1. public boolean doesCommandExists(String str){
    2. Field cmdMap_f = SimplePluginManager.class.getDeclaredField("commandMap");
    3. cmdMap_f.setAccessible(true);
    4. Field kC_f = SimpleCommandMap.class.getDeclaredField("knownCommands");
    5. kC_f.setAccessible(true);
    6. Map<String, Command> map = (Map<String, Command>)kC_f.get(cmdMap_f.get(Bukkit.getPluginManager());
    7.  
    8. for(Command c : map.valueSet()){
    9. if(c.getName().equalsIgnoreCase(str)){
    10. return true;
    11. }
    12. }
    13. }


    I don't recommend using this code since:
    1. I have not tested it.
    2. I only referenced the Bukkit API source files; I haven't had the slightest if this is actually updated.
    3. Reflection causes performance to drop. You can fix this by moving cmdMap_f and kC_f outside the method.
     
  17. Offline

    RingOfStorms

    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?
     
  18. Offline

    Cirno

    When the server starts up or is reloaded, help is processed and topics are added in the following order:
    1. General topics are loaded from the help.yml
    2. Plugins load and optionally call addTopic()
    3. Registered plugin commands are processed by HelpTopicFactory objects to create topics
    4. 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.
     
    RingOfStorms likes this.
Thread Status:
Not open for further replies.

Share This Page