Player Listener Commands

Discussion in 'Plugin Development' started by AppleMC, Apr 21, 2013.

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

    AppleMC

    Code:java
    1. @EventHandler
    2. public void onSpeak(AsyncPlayerChatEvent e) {
    3. Player p = e.getPlayer();
    4. String d = ("§aDone!");
    5. if (e.getMessage().toLowerCase().startsWith("@help")) {
    6. e.setCancelled(true);
    7. if (e.getMessage().length() == 0) {
    8. p.sendMessage("§8-----§bHelp Menu[1]§8-----");
    9. p.sendMessage("§b@help §7--- §aHelp menu");
    10. p.sendMessage("§b@kill §7--- §aKills chosen player");
    11. p.sendMessage("§b@heal §7--- §aHeal");
    12. p.sendMessage("§4COMMANDS MUST BE EXACT! (CASES)");
    13. } else if (e.getMessage().length() > 1) {
    14. String[] s = e.getMessage().split(" ");
    15. if (s[0].equalsIgnoreCase("1")) {
    16. p.sendMessage("§8-----§bHelp Menu[1]§8-----");
    17. p.sendMessage("§b*help §7--- §aHelp menu");
    18. p.sendMessage("§b*kill §7--- §aKills player");
    19. } else if (s[0].equalsIgnoreCase("2")) {
    20. p.sendMessage("§8-----§bHelp Menu [2]§8-----");
    21. } else if (s[0].equalsIgnoreCase("3")) {
    22. p.sendMessage("§8-----§bHelp Menu [3]§8-----");
    23. }
    24. }
    25. } else if (e.getMessage().toLowerCase().startsWith("@kill")) {
    26. e.setCancelled(true);
    27. if(e.getMessage().length() > 1){
    28. String[] s = e.getMessage().split(" ");
    29. if(Bukkit.getPlayer(s[1]) != null){
    30. Player tPlayer = Bukkit.getPlayer(s[1]);
    31. p.sendMessage(d);
    32. tPlayer.setGameMode(GameMode.SURVIVAL);
    33. tPlayer.setHealth(1);
    34. tPlayer.damage(1);
    35. }
    36. }
    37. } else if (e.getMessage().equalsIgnoreCase("@heal")) {
    38. e.setCancelled(true);
    39. p.setHealth(20);
    40. p.setFoodLevel(20);
    41. p.setFireTicks(0);
    42. p.sendMessage(d);
    43. }
    44. }


    Some error, no error codes, but Help doesn't work...
     
  2. Offline

    Techy4198

    that's because you set it to work if the message length is 0, but if it is only triggered when the message is Help, it will never be 0. I see no workaround, other than to use normal commands, which only accept / at the start.

    oh and then at line 15, 19, and 21, change (s[0]. to (s[1].

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  3. Offline

    AppleMC

  4. Offline

    Techy4198

    no I don't. did u try what I said? and if so did it fix it?
     
  5. Offline

    AppleMC

    Yeah I tried, it didn't work though.. another error showed up...
    Error code:
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:427)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:477)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:459)
    at net.minecraft.server.v1_5_R2.PlayerConnection.chat(PlayerConnection.j
    ava:889)
    at net.minecraft.server.v1_5_R2.PlayerConnection.a(PlayerConnection.java
    :841)
    at net.minecraft.server.v1_5_R2.Packet3Chat.handle(Packet3Chat.java:44)
    at net.minecraft.server.v1_5_R2.NetworkManager.i(NetworkManager.java:210
    )
    at net.minecraft.server.v1_5_R2.NetworkManager.c(NetworkManager.java:346
    )
    at net.minecraft.server.v1_5_R2.NetworkReaderThread.run(SourceFile:94)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
    at com.BetaCheatLess.BetaCheatLess.MyPlayerListener.onSpeak(MyPlayerList
    ener.java:34)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425)
    ... 9 more

    @EventHandler would be line 20 instead of line 1
     
  6. Offline

    Techy4198

    delete the last two lines and instead put
    Code:java
    1. } else {
    2. p.sendMessage("§cCommand not recognized");
    3. }
    4. }
    5.  

    then when u try it ingame see if it says Command not recognized

    ???

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  7. Offline

    AppleMC

    Techy4198 which lines?? and like for the code I posted, @EventHandler is line 20 in my whole code
     
  8. Offline

    Austy

    I think a length of an array, that You get after splitting a command argument, is too small, so You get ArrayIndexOutOfBoundException, because You're trying to refer to a not existing index.
     
  9. Offline

    AppleMC

    Austy so how shuld i fix iht :c
     
  10. Offline

    Austy

    You have to check if a length of Your array equals n, before You check its value on n-1 position.
     
  11. Offline

    AppleMC

    Austy can you help me with the code.. :/
     
  12. Offline

    Austy

    Sorry, but I already told You what should You do. Check some tutorials about arrays, so You can understand how they work.
     
  13. Offline

    Techy4198

    already said how to fix it. at line 15, 19, and 21, (line numbers from what you posted, may not be the same in your actual class) change (s[0]. to (s[1].
     
  14. Offline

    AppleMC

  15. Offline

    Techy4198

    what code errors show in eclipse now? if there are none, you will have to ask someone who knows more about these things. also, why are you so determined that you won't use proper commands?
     
  16. Offline

    Austy

    ...
    Before You access an index, check if Your array is long enough. You split a string into an array and You assume its length, but You don't know how many elements You have in Your array. If You split "one two three" You will get 3 elements, if You split "one two" You will get 2 elements. In second case, if You try access 3. element, You will get ArrayIndexOutOfBoundException. So if You use
    Code:
    @kill
    and You don't provide player's name, it will throw the exception.
     
  17. Offline

    AppleMC

    Austy but.. o.o im just using Help 1,2,3 and it doesnt work..
     
  18. Offline

    Austy

    Here's the fixed code:
    Code:
    @EventHandler
        public void onSpeak(AsyncPlayerChatEvent e) {
            Player p = e.getPlayer();
            String d = ("§aDone!");
            String[] s = e.getMessage().split(" ");
            if (e.getMessage().toLowerCase().startsWith("@help")) {
                e.setCancelled(true);
                if (s.length == 1) {
                    p.sendMessage("§8-----§bHelp Menu[1]§8-----");
                    p.sendMessage("§b@help §7--- §aHelp menu");
                    p.sendMessage("§b@kill §7--- §aKills chosen player");
                    p.sendMessage("§b@heal §7--- §aHeal");
                    p.sendMessage("§4COMMANDS MUST BE EXACT! (CASES)");
                } else if (s.length > 1) {
                    if (s[1].equalsIgnoreCase("1")) {
                        p.sendMessage("§8-----§bHelp Menu[1]§8-----");
                        p.sendMessage("§b*help §7--- §aHelp menu");
                        p.sendMessage("§b*kill §7--- §aKills player");
                    } else if (s[1].equalsIgnoreCase("2")) {
                        p.sendMessage("§8-----§bHelp Menu [2]§8-----");
                    } else if (s[1].equalsIgnoreCase("3")) {
                        p.sendMessage("§8-----§bHelp Menu [3]§8-----");
                    }
                }
            } else if (e.getMessage().toLowerCase().startsWith("@kill")) {
                e.setCancelled(true);
                if (s.length > 1) {
                    if (Bukkit.getPlayer(s[1]) != null) {
                        Player tPlayer = Bukkit.getPlayer(s[1]);
                        p.sendMessage(d);
                        tPlayer.setGameMode(GameMode.SURVIVAL);
                        tPlayer.setHealth(1);
                        tPlayer.damage(1);
                    }
                }
            } else if (e.getMessage().equalsIgnoreCase("@heal")) {
                e.setCancelled(true);
                p.setHealth(20);
                p.setFoodLevel(20);
                p.setFireTicks(0);
                p.sendMessage(d);
            }
        }
    The problem was that You tried to check how many words are in message with the length() method. And this method only checks a number of characters in string. So i moved a message splitting to the top, and then changed condition statements to check how many elements are in the array. This code isn't perfect, but it works.
     
Thread Status:
Not open for further replies.

Share This Page