Stuck with PlayerInteractEvent

Discussion in 'Plugin Development' started by Nuko32, Sep 19, 2012.

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

    Nuko32

    Hi guys! Actually, I'm developing a plug-in. I want that when an user right-clicks a block with a stick, a lightning will strike in that block. I know that I have to use the onPlayerInteract method, but I still have some problems, because I have never used this function before.

    The plug-in doesn't make any error logs when I start up the server, but if I right-click one block, the lightning does not strike. Any help? Thanks in advance ;)

    For anyone that wants the code, here it is:
    Code:
    public void onPlayerInteract(PlayerInteractEvent event)
        {
       
            Player player = event.getPlayer();
            Block targetblock = player.getTargetBlock(null, 50);
            Location location = targetblock.getLocation();
            World world = player.getWorld();
           
            if((event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR))
            {
                try
                {
                    if(event.getItem().getType() != Material.STICK){
                       
                        world.strikeLightning(location);
                       
                    }
                       
                }
                catch(NullPointerException e)
                {
                    return;
                }
               
                   
                   
     
            }
        }
     
  2. Offline

    stirante

    Make sure you registered your listener.Also add @EventHandler before this method.
     
  3. Offline

    Nuko32

    Yep, I've done this. Maybe I can't add "implements Listener" to the main class? Should I create a new class?
     
  4. You could, but its also possible to have your listener in the main class.

    Edit: Would look like this:
    Code:
    public class YourMainClass extends JavaPlugin implements Listener
     
  5. Offline

    Nuko32

    I've tried this, but it still not working.
     
  6. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    Did you register the listener with the plugin manager?
     
    Nuko32 likes this.
  7. That is probably it
     
  8. Offline

    Nuko32

    Ah. I forgot it D: Thanks for help!
     
Thread Status:
Not open for further replies.

Share This Page