Help with Player Interact Event

Discussion in 'Plugin Development' started by Snicktrix, Aug 24, 2013.

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


    This is the first ever plugin I'm working on and have ran into a bit of a problem.

    The goal is to have lightening strike where the player looks when the Player clicks. I cant seem to figure out why this isn't working.

    The plugin loads in correctly, however when I click with a Gold Sword, nothing happens.

    I have come to believe its my if statement that is causing the problem, because I have tried to replace the lightning strike event with a simple send player message. I may be completely incorrect however.

    package me.Snicktrix.Plugin;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerInteractEvent;
    public class Main extends JavaPlugin {
        public void onEnable() {
            getLogger().info("It works!");
        public void onDisable() {
            getLogger().info("It Turned Off!");
        public void onPlayerInteractBlock(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            if(player.getItemInHand().getType() == Material.getMaterial(283)) {
            // Creates a bolt of lightning at a given location. In this case, that location is where the player is looking.
            // Can only create lightning up to 200 blocks away.
                player.getWorld().strikeLightning(player.getTargetBlock(null, 200).getLocation());
    Thanks for you help in advanced :)
  2. Offline


    Snicktrix You have not registered the Listener.
  3. Offline


    You should put the eventhandler method on a separate class (for example, PlayerListener), and implement Listener. In the Main class, create a new istance of PlayerListener, and then call this method:
    getServer().getPluginManager().registerEvents(<instance of PlayerListener>, this);
    Now it should work...
Thread Status:
Not open for further replies.

Share This Page