not working player.getLevel() ?

Discussion in 'Plugin Development' started by dead4y, Feb 28, 2012.

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

    dead4y

    Hello again everyone!
    I tried to output the player.getlevel to console but nothing seems happens
    My code:
    public void onPlayerChatEvent(Player player, String message)
    {
    int playerlvl = player.getLevel();
    System.out.print(playerlvl);
    } //It is like that because i want to test it.
    So my question is : Why is not working? Or maybe i am wrong?And can i use this to get level from player and to use it for something like checking...?
    Thanks!
    Dead4y
     
  2. Offline

    Njol

    What is not working?
    If nothing is displayed at all, you should register the event correctly.
     
  3. Offline

    Sorroko

    My best guess is that it needs to be a string, use .toString() something like System.out.print(player.getLevel().toString());
     
  4. Offline

    dead4y

    doesnt work again it doesnt show anything...
    Edit:Then how to register it right ?
     
  5. Offline

    Njol

  6. where is the listener?
     
  7. Offline

    Sorroko

    Try this:
    Code:
    @EventListener
    public void onPlayerChatEvent(Player player, String message){
        int playerlvl = player.getLevel();
        System.out.print(playerlvl);
    }
    and

    Code:
    getServer().getPluginManager().registerEvents(new MyEventListener(), this);
     
  8. Offline

    dead4y

    can everything (listener + main) to be in one class file
     
  9. Yes, just do
    Code:
    getServer().getPluginManager().registerEvents(this, this);
     
  10. Offline

    dead4y

    and it will work just like that without any changes?
     
  11. Of course, just copy the method and the annotation in your main class file. But be aware that this can get a real mess if you have more and bigger listeners only in one class ;)
     
  12. Offline

    dead4y

    yes but i am new and i want everything to be in one class.
    Edit: FORGOT TO THANK YOU. MAN YOU ROCK!

    i test it today but it didnt work again... help again ; D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  13. Offline

    Njol

    Does the class which contains the @EventListener implement Listener?
     
  14. Offline

    dead4y

    I was type EventHandler instead of EventListener...
     
  15. Offline

    Sorroko

    Yes use EventHandler and make sure that your main class extends JavaPlugin AND implements Listener
     
  16. Offline

    dead4y

    not working... for me... Can you give me example for new event system?And can it be in one class?
     
  17. dead4y If you would have read that page, you would see your error.
    Your code has to be
    Code:java
    1.  
    2. @EventHandler
    3. public void myDerpingEvent(PlayerChatEvent event) {
    4. // Your code here...
    5. }
    6.  
     
  18. Offline

    dead4y

    There is no error...Just nothing happens...
     
  19. Offline

    Njol

    You method has a wrong signature, it should look like this:
    Code:java
    1. @EventHandler
    2. public void onPlayerChatEvent(PlayerChatEvent event) {


    I didn't notice that at first, since others usually only mess up with the registering of events...
     
  20. Here is how I would do it:

    Code:
    class MyAwesomePlugin extends JavaPlugin implements Listener {
     
      public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);
      }
     
      @EventHandler
      public void calledWhenAPlayerChats(PlayerChatEvent event) {
        System.out.println(event.getPlayer().getLevel());
      }
     
    }
    Njol I think it should be "@EventHandler" ;)
     
  21. Offline

    Njol

    I copied it from ryan7745...
     
Thread Status:
Not open for further replies.

Share This Page