Changing invalid command message

Discussion in 'Plugin Development' started by Pezah, Mar 11, 2014.

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

    Pezah

    I'm trying to change the invalid command message, so for example if they typed /releport instead of /teleport it sends the message "TMod - Invalid command"; nothing happens when I do an invalid command, here is the code;

    Code:java
    1. package Commands;
    2.  
    3. import java.text.MessageFormat.Field;
    4.  
    5. import org.bukkit.Server;
    6. import org.bukkit.command.SimpleCommandMap;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    11. import org.bukkit.plugin.SimplePluginManager;
    12.  
    13. import Core.Core;
    14. import Core.F;
    15.  
    16. public class Unknown implements Listener
    17. {
    18.  
    19. {
    20. getCommandMap(Core.plugin.getServer());
    21. }
    22.  
    23. public SimpleCommandMap getCommandMap(Server server) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException
    24. {
    25. if(server.getPluginManager() instanceof SimplePluginManager)
    26. {
    27. java.lang.reflect.Field f = SimplePluginManager.class.getDeclaredField("commandMap");
    28. f.setAccessible(true);
    29. return (SimpleCommandMap) f.get(server.getPluginManager());
    30. }
    31. return null;
    32. }
    33.  
    34. public static boolean commandExists(String name)
    35. {
    36. return Core.getPlugin().getCommand(name) != null;
    37. }
    38.  
    39. @EventHandler
    40. public void onPlayerCommand(PlayerCommandPreprocessEvent event)
    41. {
    42. Player player = event.getPlayer();
    43. String cmd = event.getMessage();
    44. if(cmd.charAt(0) == '/') cmd = cmd.replaceFirst("/", "");
    45. cmd = cmd.split(" ")[0];
    46. if(commandExists(cmd))
    47. {
    48. event.setCancelled(true);
    49. player.sendMessage(F.M("TMod", "Invalid command"));
    50. }
    51. }
    52.  
    53. }
    54.  
     
  2. Offline

    MooshViolet

    couldnt you just make a boolean and just make it when they type the command releport or any others, send them that message?
     
  3. Offline

    Maurdekye

    MooshViolet I'm pretty sure he means to change the message sent when you use a command not listed in the plugin.yml. In which case, is fairly difficult to do.
     
  4. Offline

    GameplayJDK

  5. Offline

    MajorSkillage

    wrong
    if(cmd.isRegisterd() == false){//meh}
     
  6. Offline

    mythbusterma

    MajorSkillage

    Why in the world would you compare a boolean to false in an if-statement...?
     
    teej107 likes this.
  7. Offline

    teej107

    MajorSkillage

    [​IMG]
    Code:java
    1. boolean absurd = true == false;
    2.  
    3. //What are you going to do now? Make another boolean?
    4. if(absurd == false)
    5. {
    6. //Laughs. Just do: if(absurd)
    7. }
    8.  
    9.  
     
  8. Offline

    Watto

    Technically it would be;
    Code:java
    1. if(!absurd){
    2. }

    since you're only running the if statement when it's false >:D ;)
     
    teej107 likes this.
  9. Offline

    MajorSkillage

    well you got what i ment lol :D oh and also what if you compared boolean to null ;p null is false to
     
  10. MajorSkillage If you compare a boolean to null, that's when you get compilation errors.
     
Thread Status:
Not open for further replies.

Share This Page