PlayerInteractEvent Help

Discussion in 'Plugin Development' started by Onlineids, Mar 18, 2014.

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

    Onlineids

    I am trying to get it so when a player left clicks it does something but I setup this test code and it doesn't work it wont send the player or in this case me that message heres my code:
    Code:java
    1. package me.online.Stamina;
    2.  
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.block.Action;
    7. import org.bukkit.event.player.PlayerInteractEvent;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class Main extends JavaPlugin implements Listener{
    11. @SuppressWarnings("deprecation")
    12. @EventHandler
    13. public void onInteractEvent(PlayerInteractEvent event){
    14. Action action = event.getAction();
    15. if (action == Action.LEFT_CLICK_AIR || action == Action.LEFT_CLICK_BLOCK){
    16. Player player = event.getPlayer();
    17. player.sendMessage("Test");
    18.  
    19.  
    20.  
    21.  
    22.  
    23.  
    24.  
    25. }
    26. }
    27. }
    28.  
    29.  
    30.  
     
  2. Offline

    AoH_Ruthless

    Onlineids
    You didn't register your events.
     
  3. Offline

    Onlineids

    Tried a new one and registered event still doesnt work
    Code:java
    1. package me.online.Stamina;
    2.  
    3. import me.online.Stamina.Main;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.Material;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerJoinEvent;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class Main extends JavaPlugin implements Listener{
    15.  
    16. public void onEnable(){
    17. Bukkit.getPluginManager().registerEvents(this, this);
    18. }
    19.  
    20. @EventHandler
    21. public void onRespawn(PlayerJoinEvent event){
    22. Player player = (Player) event.getPlayer();
    23. ItemStack item = new ItemStack(Material.DIAMOND, 19);
    24. player.getInventory().addItem(item);
    25.  
    26.  
     
  4. Offline

    97WaterPolo

    Onlineids

    Never tried it without, but if you don't have your onEnable and onDisable will it still work? For the first one you aren't registering it, for the second it should be working... try sending a message instead?
     
  5. Offline

    jthort

    Onlineids Is it at least firing the event, or just completely not working? If not you might want to make sure your plugin.yml is in the proper format. Other than that, it seems fine.

    I don't think you need them
     
    97WaterPolo likes this.
  6. Offline

    Onlineids

    I even went as far as to copy bukkit apis example still didnt work -_-
    Code:java
    1. package me.online.Stamina;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.player.PlayerJoinEvent;
    8.  
    9. public final class Main extends JavaPlugin implements Listener {
    10. public void onEnable() {
    11. getServer().getPluginManager().registerEvents(this, this);
    12. }
    13.  
    14. @EventHandler
    15. public void normalLogin(PlayerJoinEvent event) {
    16. Player player = (Player) event.getPlayer();
    17. String msg = "Test";
    18. player.sendMessage(msg);
    19. }
    20. }
    21.  
    22.  
    23.  
    24.  
    25.  


    no errors with plugin.yml console is errorless

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

    Slideroller

    Forgot to return the statement?
     
  8. Offline

    97WaterPolo

    Onlineids
    For one, now that I look at it, I think it is missing a condition? Not sure if it would work if it is just right and left click, either way try this and tell me if it works. I looked it over and it is exactly how I would make mine, so I have no idea why it isn't working. I changed some variables and added the registering of the event. Hopefully this shall work.

    Code:java
    1. import org.bukkit.entity.Player;
    2. import org.bukkit.event.EventHandler;
    3. import org.bukkit.event.Listener;
    4. import org.bukkit.event.block.Action;
    5. import org.bukkit.event.player.PlayerInteractEvent;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7.  
    8. public class Main extends JavaPlugin implements Listener{
    9. public void onEnable(){getServer().getPluginManager().registerEvents(this, this);}
    10. public void onDisable(){}
    11. @EventHandler
    12. public void onInteractEvent(PlayerInteractEvent event){
    13. Player player = event.getPlayer();
    14. if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK){
    15. player.sendMessage("Test");
    16. }
    17. }
    18. }
     
  9. Offline

    Konkz

    Do you get any errors in console? I suspect that your plugin.yml has been set up incorrectly.

    Onlineids
     
  10. Offline

    AoH_Ruthless

    97WaterPolo
    That is false. You do not need empty methods: Who taught you that when you learned Java? Empty Methods never do you good (You don't need the onEnable() or onDisable(), but you need onEnable() when registering events).

    Onlineids
    Are you restarting your server after exporting? Are you sure you are exporting? Does the startup log say your plugin is being enabled?
     
  11. Offline

    Barinade

    You don't need onEnable or onDisable
    Code:
     
     
    [LIST]
     
     
    [*]
     
    @EventHandler
     
     
     
     
    [*]
     
    public void normalLogin(PlayerJoinEvent event) {
     
     
     
     
    [*]
     
    Player player = (Player) event.getPlayer();
     
     
     
     
    [*]
     
    [URL='http://www.google.com/search?hl=en&q=allinurl%3Astring+java.sun.com&btnI=I%27m%20Feeling%20Lucky']String[/URL] msg = "Test";
     
     
     
     
    [*]
     
    player.sendMessage(msg);
     
     
     
     
    [*]
     
    }
    There is no need to cast Player to Player
    You don't really need to assign a variable for your message
    event.getPlayer().sendMessage("Test"); //the only line you need

    As for the event not working, is your plugin.yml configured right?
    Upload your latestlog to pastebin.com for us.

    Edit: This nonsense is getting annoying.


    [/LIST]
     
  12. Offline

    Onlineids

    Got it to work think it was eclipse glitch i copied code and repasted in new project worked perfectly
     
  13. Offline

    97WaterPolo

    Onlineids

    Well what a nice fix! :p At least it is working now, good luck! ^_^
     
Thread Status:
Not open for further replies.

Share This Page