Solved Turning a listener on/off with a command

Discussion in 'Plugin Development' started by jflory7, Jul 22, 2013.

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

    jflory7

    Bump to see if anyone else can help?
     
  2. Offline

    Tirelessly

    Show your main class
     
  3. Offline

    PogoStick29

    That's really odd. I believe it also shows usage if there's an error or something. Try adding a return true somewhere else, like after you check for the command and handle it before the end of onCommand.
     
  4. Offline

    jflory7

    Tirelessly PogoStick29
    I can't believe I could've possibly forgotten this. When I was originally working on the plugin, it was just a listener and I hadn't implemented the command part in any way. So, my main file didn't even have the command executor line in it. I feel like the biggest idiot ever... lmao. Anyways, here is how my main class look now: Main.java

    However, I am at a new issue - I tried running my "/witherbow disable" command and it kept firing wither skulls. How could I fix that? I'm looking over the code now and trying to see what I could be doing wrong.
     
  5. Offline

    AmShaegar

    how does your current code look like: Main class, CommandExecutor and plugin.yml?
     
  6. Offline

    jflory7

  7. Offline

    AmShaegar

    Sorry, you posted shortly before I wrote my answer. First of all, I would recommend to not double create your BowListener. Create it once with new BowListener() and save the instance. Then pass this single instance to setExecutor and registerEvents.
     
  8. Offline

    jflory7

    AmShaegar
    No worries.

    How would I go about doing that? I'm still new to coding and I'm not sure what method I would use to do that.
     
  9. Offline

    AmShaegar

  10. Offline

    jflory7

    AmShaegar
    Okay, that has been done in my code now. And just so I understand, what we did was point the setExecutor and registerEvents to the method we added, which is then pointed to the BowListener class itself. And by doing this, are we making it more streamlined? I think I see why we did it, but I just want to be clear.
     
  11. Offline

    AmShaegar

    The point is, if you change something with your command, it's done in the executor instance. If you want to access this change in your second Listener instance, it wont work unless it's a static variable. So this, with only one instance, is the clean way.

    Do you still have problems with your command returning usage although it should finish with return true? Which exact command are you using for testing?
     
  12. Offline

    jflory7

    AmShaegar
    Okay, thanks - that makes sense.

    My command no longer returns usage anymore, thankfully. It seems the problem I have now is likely with the "enabledPlayers" string I am using to tell if a player has enabled the bow or not. I am testing this on a server where there is only OP or non-OP permissions, though.

    For testing, I'm using both "/witherbow enable" and "/witherbow disable".
     
  13. Offline

    AmShaegar

    What is your exact problem now, please?
     
  14. Offline

    jflory7

    AmShaegar
    When I type "/witherbow disable", I am still shooting wither skulls instead of the normal arrow. I would think this would have to be some sort of problem with my string.
     
  15. Offline

    AmShaegar

    Do you get the message "Your bow has returned to its normal state." on disable?
     
  16. Offline

    jflory7

    AmShaegar
    I have gotten that message. It just doesn't seem to actually check the string (or perhaps doesn't remove from the string) to see if a player should be firing an arrow or a skull.
     
  17. Offline

    AmShaegar

    From my point of view, this should work totally fine.
     
  18. Offline

    jflory7

    AmShaegar
    I've been messing around with this for the past fifteen minutes to no avail - I tried completely removing my permissions and just using it as is, but no matter what, I still shoot wither skulls. Just to check, here it is my NEWES code for the BowListener file.

    BowListener.java
     
  19. Offline

    Nova20012

    Hey jflory7 . I was looking at your code and I noticed this:
    Code:java
    1. if(enabledPlayers.contains(p.getName())); //The semi-colon on this line
    2. {
    3. e.setCancelled(true);
    4. p.launchProjectile(WitherSkull.class).setVelocity(e.getProjectile().getVelocity().multiply(0.4));
    5. }

    I think the semi-colon is stopping the code!
     
    jflory7 likes this.
  20. Offline

    AmShaegar

    Ha, got it. This if statement in your Event is finished by a ";". Thus it does not matter if the player is in that list or not. It's simply ignored. "{...}" is just a block that is executed always.
     
    jflory7 likes this.
  21. Offline

    jflory7

    Nova20012 AmShaegar
    That did it! The command is now working perfectly!

    Thanks everyone for all of the help in solving this problem - even though most of it was beginner's errors. Thanks for your patience with me too. xD
     
    Nova20012 likes this.
  22. Offline

    xTrollxDudex

    jflory7
    You have a ; in your if statement in the listener
     
Thread Status:
Not open for further replies.

Share This Page