How do i the get player who triggered an event?

Discussion in 'Plugin Development' started by OblivionFan, Aug 4, 2011.

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

    OblivionFan

    I have my plugin,which will (hopefully) check if an entity (when right-clicked) is a NPC for citizens or not...i have a basic idea about how to do it,but i'm kinda stuck at this...this is my code:

    The main RightClickMessage:
    Show Spoiler

    package sddddgjd.rightclick;

    import java.util.logging.Logger;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    public class RightClickMessage extends JavaPlugin{
    Logger log = Logger.getLogger("Minecraft");
    private final CheckNPCPlayerListener playerListener = new CheckNPCPlayerListener(this);
    Player player= null;
    public void onEnable(){
    log.info("CheckNPC has been enabled!");
    PluginManager pm = this.getServer().getPluginManager();
    pm.registerEvent(Event.Type.PLAYER_INTERACT_ENTITY, playerListener, Event.Priority.Normal, this);
    }
    public void onDisable(){
    log.info("CheckNPC has been disabled!");
    }}

    And the listener:



    The listener:
    Show Spoiler

    package sddddgjd.rightclick;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerListener;


    public class CheckNPCPlayerListener extends PlayerListener{
    public static RightClickMessage plugin;
    Player player= null;
    public CheckNPCPlayerListener(RightClickMessage rightClickMessage) {
    plugin = rightClickMessage;}


    }

    Now,i want,for the start,the plugin to send the player a message that says: "You interacted with an entity!"
    i know i need to use this:
    Player player=partidon'tknow;
    player.sendMessage(ChatColor.RED + "You interacted with an entity!");

    So,how do i check which player triggered the even?

    LE: I tried setting:
    player=PlayerInteractEntityEvent.getPlayer();
    but i get an error that says "cannot make a static reference to the non-static method getPlayer() from the type PlayerEvent"...any ideas?
     
  2. Offline

    masteroftime

    Almost every event has a getPlayer() method. The event is passed as a parameter to your listener method.

    Code:
    Player player = e.getPlayer();
     
  3. Offline

    OblivionFan

    Bump,updated original post....
    LE:wow,didn't see your post:D
    That's just what i tried (i think),by setting:
    player=PlayerInteractEntityEvent.getPlayer();
    But i get the "static reference to the non-static method" error....any ideas?
     
  4. You need to register the event first, you can then get the players name.
     
  5. Offline

    OblivionFan

    You mean this?
    pm.registerEvent(Event.Type.PLAYER_INTERACT_ENTITY, playerListener, Event.Priority.Normal, this);
    I already got this,it's just in another class!
    Anyway,managed to get rid of all the errors by doing this:
    Show Spoiler

    public class CheckNPCPlayerListener extends PlayerListener{
    public PlayerInteractEntityEvent plugin;
    public CheckNPCPlayerListener(PlayerInteractEntityEvent event) {
    plugin = event;
    Player player= event.getPlayer();
    player.sendMessage(ChatColor.RED + "You interacted with an entity!");
    }

    }

    Is this right?
    LE: No,it's not right...
    Now the main class gives an error at this lime:
    private final CheckNPCPlayerListener playerListener = new CheckNPCPlayerListener(this);
    Because i use "this" which doesn't work anymore,since i changed the listener to request a "PlayerInteractEntityEvent"event...damn!
     
  6. Good :D
     
  7. Offline

    OblivionFan

    Which part is good? The one in the spoiler? Because i'm still an error at
    private final CheckNPCPlayerListener playerListener = new CheckNPCPlayerListener(this);
    Because "this" is my main class,and not a "PlayerInteractEntityEvent"...

    Still,thanks for the help!
     
  8. Oh I thought you'd fixed it.

    public CheckNPCPlayerListener(PlayerInteractEntityEvent event) {

    That is wrong, change it to the following below ;)

    Code:
    public void onPlayerInteractEntity(PlayerInteractEntityEvent evt){
    player = evt.getPlayer();
    }
     
  9. Offline

    OblivionFan

    Thanks,i'll test it now!
    But it gives me an error that says "this method has a constructor name"...should i worry about that?
     
  10. I think you've done something wrong, take a look at some of the templates in my HUGE plugin tutorial on the wiki for guidance.
     
  11. Offline

    masteroftime

    ^^ like always: adding some advertisement for his tutorial :D
     
  12. Offline

    OblivionFan

    I love your tutorial,it was very helpfull since i started to learn! :D
    The Listener seems to be right,so the problem must be in the main class...here's how it looks like:
    P.S: I checked my listener class using one of your templates from the huge tutorial,so it's right! :D
     
  13. Not for my benefit :D

    Try removing the final keyword from:

    private final CheckNPCPlayerListener playerListener = new CheckNPCPlayerListener();

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

    OblivionFan

    Which final keyword?
    I can't remove "playerListener",because i use it to call "pm.registerEvent"...
    I'll try removing final...

    LE: Lol,fail...i thought you meant the final,as in the last! And only after i clicked "send message" i realised that you meant the "final" keyboard... :))
     
  15. Offline

    masteroftime

    @Adamki11s was just a joke

    Also my answer to the "I am newbie help making plugin" threads is look at adamkis tutorial ^^

    this message usually means that a method has the same name as the class. Maybe you put a void before your contructor?

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

    OblivionFan

    well,i did,because that's adam told me to do,lol...without it,i get an error at:
    private CheckNPCPlayerListener playerListener = new CheckNPCPlayerListener();
    because "CheckNPCPlayerListener" requires "PlayerInteractEntityEvent event",and i call it without any constructors...

    I certainly hope this isn't as confusing for you as it is for me,lol! :D
    So,this are the 2 classes so far: ( i really don't want to give up on this,it's my first plugin,and it should be something easy...)

    The main RightClickMessage class:
    Show Spoiler

    package sddddgjd.rightclick;

    import java.util.logging.Logger;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    public class RightClickMessage extends JavaPlugin{
    Logger log = Logger.getLogger("Minecraft");
    private CheckNPCPlayerListener playerListener = new CheckNPCPlayerListener();
    public void onEnable(){
    log.info("CheckNPC has been enabled!");
    PluginManager pm = this.getServer().getPluginManager();
    pm.registerEvent(Event.Type.PLAYER_INTERACT_ENTITY, playerListener, Event.Priority.Normal, this);
    }
    public void onDisable(){
    log.info("CheckNPC has been disabled!");
    }}



    And the CheckNPCPlayerListener class:

    Show Spoiler

    package sddddgjd.rightclick;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.event.player.PlayerListener;


    public class CheckNPCPlayerListener extends PlayerListener{
    public PlayerInteractEntityEvent plugin;
    public void CheckNPCPlayerListener(PlayerInteractEntityEvent event) {
    plugin = event;
    Player player= event.getPlayer();
    player.sendMessage(ChatColor.RED + "You interacted with an entity!");
    }

    }

     
  17. Get rid of that : public PlayerInteractEntityEvent plugin;
     
  18. Offline

    OblivionFan

    Still doesn't work...
    P.S: I also had to delete "plugin=event",because it started showing errors...
    Anyway,i'll just try checking if the Listener is ever used,by making it post info in the server's log...

    LE: I used "System.out.println("This is a log");" inside the "CheckNPCPlayerListener" method,right before i get the event,and it doesn't show up....i guess that means that the event itself isn't registered,and the method isn't called...
     
  19. Offline

    nisovin

    You don't even have a listening method. Didn't you say you added one? Do you understand what constructors and methods and return types are? You should probably look up some basic Java tutorials before trying to continue with plugin development.

    Anyway, your listener class shouldn't have a "void" in the constructor, and it needs an onPlayerInteractInteractEntity() method.
     
  20. Offline

    OblivionFan

    I watched the first 20 of bucky's java tutorials'(i know,not much,but that's about all you need for bukkit,from what i've seen),and while im not sure what constructors are(well,i probably know them,but i don't know they're called like that),i know methods and return types (i'm really good at c++,trying to switch to java just for bukkit! :D)

    I've also looked at some plugin tutorials,the big problem is that (from what i understood) the bukkit team keeps updating the code (which is good) , and that makes tutorials older than a few months useless (which is bad...)

    The problem is,if i get rid of the void,i get an error because when i define the playerListener, i use
    " = new CheckNPCPlayerListener();", which gives an error because that's how the method is defined:
    "public CheckNPCPlayerListener(PlayerInteractEntityEvent event) "

    And what will the "onPlayerInteractInteractEntity() " (2 Interact's??) do?

    P.S:If you know any good tutorials,besides the HUGE plugin tutorial,that aren't outdated,can you please give me links?
     
  21. Offline

    nisovin

    If you're "really good at c++" you should know what constructors and methods are. The double interact was a typo. You should have two methods here, not just one. A constructor, and a listener method. The constructor is called CheckNPCPlayerListener (same as the class), and the listener method is onPlayerInteractEntity(PlayerInteractEntityEvent). Surely the HUGE plugin tutorial goes over this?
     
  22. Offline

    OblivionFan

    Well,the tutorial makes an easy plugin (with a listener,but that one has only got 1 method,i think...),and then goes over commands,permissions,creating configuration files,etc. Really usefull stuff,but not when you're stuck at this...

    Yea,googled,the constructor is the part which always executes,so atleast i've got that part right! :D
    Now,i think i understand what you mean...i'll rewrite the program,and tell you how it goes!
    Thanks to everyone for helping me!



    LE: Didn't quite work,but i'm close...this is the RightClickMessage.java:
    Show Spoiler

    package sddddgjd.rightclick;

    import java.util.logging.Logger;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    public class RightClickMessage extends JavaPlugin{
    private CheckNPCPlayerListener playerListener = new CheckNPCPlayerListener(this);
    Logger log = Logger.getLogger("Minecraft");
    public void onEnable(){
    log.info("CheckNPC has been,yet again,enabled!");
    PluginManager pm = this.getServer().getPluginManager();
    pm.registerEvent(Event.Type.PLAYER_INTERACT_ENTITY, playerListener, Event.Priority.Normal, this);
    }
    public void onDisable(){
    log.info("CheckNPC has been disabled!");
    }}


    And this is CheckNPCPlayerListener:
    Show Spoiler

    package sddddgjd.rightclick;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.event.player.PlayerListener;


    public class CheckNPCPlayerListener extends PlayerListener{
    public CheckNPCPlayerListener(RightClickMessage instance) {
    System.out.println("This is a log!");
    }
    public void PlayerInteractEntity(PlayerInteractEntityEvent event){
    Player player= event.getPlayer();
    player.sendMessage(ChatColor.RED + "You interacted with an entity!");
    }}


    I think i'm missing something,somewhere...

    LE: Modified the code again,now i'm sending a "RightClickMessage instance" purely because that's what the HUGE tutorial wants me to do...

    YES! I GOT IT WORKING!
    Turned out i only had to right click->source->override/implement methods, select onPlayerInteractEntity,and do my stuff in there!
    Huge thanks to everyone for helping!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 18, 2016
  23. :D
     
  24. I personally recommend downloading the bukkit API, you got everything that bukkit offers you there.
     
  25. Offline

    OblivionFan

    i've already got it...really usefull!
     
Thread Status:
Not open for further replies.

Share This Page