Why isn't this working?

Discussion in 'Plugin Development' started by Anonomoose, Apr 26, 2014.

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

    Anonomoose

    Hey everyone,

    Really simple problem here, I'm probably just being a bit dozy and missing out something, but as far as I can tell - this should work..

    Code:java
    1. @EventHandler
    2. public void onCommandEvent(PlayerCommandPreprocessEvent event) {
    3. System.out.println("Matches: " + event.getMessage().startsWith("/test")); //just a debug statement
    4. if(!(event.getMessage().startsWith("/test"))) {
    5. event.setCancelled(true);
    6. event.getPlayer().sendMessage("Your command wasn't /test!");
    7. }
    8. }


    The problem is, even when I do /test, it still says the command isn't /test. I put that return statement there which returns whether it matches or not, and it returns true whenever I do /test - so I don't see why it doesn't work when it comes to the if statement.

    Thanks in advance!
     
  2. Offline

    GeorgeeeHD

    remove the / in if(!(event.getMessage().startsWith("/test"))){
    so its startsWith("test"))) {

    not sure but the think the e.getMessage() returns the string without the "/"
     
  3. Offline

    Anonomoose

    Nah, it definitely does pick-up on the /, I've been testing this out for a while :/
     
  4. Offline

    GeorgeeeHD

    Maybe instead of startsWith try contains?
     
  5. Offline

    Anonomoose

    That seems to work great, thanks for suggesting that!

    Another issue I've just encountered is, for some reason it doesn't seem to work on multiple commands, so for example:

    Code:java
    1. @EventHandler
    2. public void onCommandEvent(PlayerCommandPreprocessEvent event) {
    3. if(!(event.getMessage().contains("/test1")) || !(event.getMessage().contains("/test2"))) {
    4. event.setCancelled(true);
    5. event.getPlayer().sendMessage("Your command wasn't /test1 or test2!");
    6. }
    7. }


    Any ideas anyone?
     
  6. Offline

    GeorgeeeHD

    i dont know why that wouldnt work, but just try doing them in different if clauses?
     
  7. Offline

    Anonomoose

    Tried else if and just normal if, and it doesn't work.
     
  8. Offline

    BillyGalbreath

    You use OR where AND should be.
     
  9. Try using equalsIgnoreCase instead of contains
    Code:java
    1. @EventHandler
    2. public void onCommandEvent(PlayerCommandPreprocessEvent event) {
    3. if(!(event.getMessage().equalsIgnoreCase("/test1")) || !(event.getMessage().equalsIgnoreCase("/test2"))) {
    4. event.setCancelled(true);
    5. event.getPlayer().sendMessage("Your command wasn't /test1 or test2!");
    6. }
    7. }
     
Thread Status:
Not open for further replies.

Share This Page