user2646941
user2646941

Reputation: 35

Why is PlayerInteractEvent not working for the Bukkit API?

So, I'm using the Bukkit API. Basically, I'm looking for the PlayerInteractEvent to be called, and then I do a bunch of stuff after that. However, when I should get notified that I actually kicked the block, I don't get any message, even though it's compiling without error in my code. I get no exceptions from the console as well. Here is my code:

@EventHandler(priority=EventPriority.HIGH)
public void onPlayerInteract(PlayerInteractEvent event, final Player who, final Action action, 
        final ItemStack item, final Block clickedBlock, final BlockFace clickedFace) {

    if (who != null) {

        if (clickedBlock != null) {

            if ((action == Action.LEFT_CLICK_BLOCK) && (clickedBlock.getType() == Material.ANVIL)) {

                if (item == null) {

                    who.sendMessage(ChatColor.YELLOW + "To repair an item, hold it in your inventory and " + 
                            ChatColor.UNDERLINE + "RIGHT CLICK" + ChatColor.RESET + "" + ChatColor.YELLOW + 
                            " the anvil with the item.");

                    event.setCancelled(true);

                }
                else {

                    Material type = item.getType();
                    short durability = item.getDurability();
                    short maximum = type.getMaxDurability();

                    if (maximum == 0) {

                        who.sendMessage(ChatColor.RED + "You can " + ChatColor.UNDERLINE + "NOT" + 
                                ChatColor.RESET + "" + ChatColor.RED + " repair that item.");

                    }
                    else {

                        short add = (short) Math.round(maximum * 0.03);

                        int result = (maximum - durability) / add;

                        int gems = getAmount(who, 388);

                        if (gems < result) {

                            who.sendMessage(ChatColor.RED + "You do " + ChatColor.UNDERLINE + "NOT" + 
                                    ChatColor.RESET + "" + ChatColor.RED + " have enough Gems to repair "
                                    + "that item.");
                            who.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Gems Needed: " + result);

                        }
                        else {

                            who.sendMessage(ChatColor.YELLOW + "It will cost " + ChatColor.WHITE + 
                                    result + "g " + ChatColor.GREEN + "to repair this item.");
                            who.sendMessage(ChatColor.YELLOW + "Continue repairing? " + ChatColor.GREEN + 
                                    "" + ChatColor.BOLD + "Y" + ChatColor.RESET + "" + ChatColor.WHITE + 
                                    " / " + ChatColor.RESET + "" + ChatColor.RED + "" + ChatColor.BOLD + 
                                    "N");

                            map.put(who, item);

                        }

                    }

                }

            }

        }

    }

}

Upvotes: 1

Views: 2906

Answers (3)

Devin Wall
Devin Wall

Reputation: 180

You can only have the PlayerInteractEvent otherwise it wont work. Also you don't need to supply the player as an argument, you can use the getPlayer() method to see who triggered the event.

@EventHandler
public void onPlayerInteract(PlayerInteractEvent e) {
Player player = e.getPlayer();
}

Upvotes: 0

bw2801
bw2801

Reputation: 317

The only parameter the method has is PlayerIneractEvent. You cannot set more parameters here.

You've set more parameters than the method itself has.

@EventHandler
public void onPlayerInteract(PlayerInteractEvent event, Player who, ...) {
    ...
}

use this instead:

@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
    // do stuff here
}

Inside the method you can get all the other things you've set as parameters. For example:

event.getPlayer(); // gets the player
event.getAction(); // gets the action

Also please notice that you have registered your listener in your plugins main class.

registerEventHandler(yourEventHandler, this);

Upvotes: 0

Kammeot
Kammeot

Reputation: 469

So the standard way to do this is to create a class that implements Listener

Then within you create methods in the form of

@EventHandler
public void nameDontMatter(PlayerInteractEvent event) {
 // do stuff, you can get all that information you passed in from the event

}

Also you need to be sure to tell the plugin where to find your PlayerListener, so typically what you would do is within the onEnable() method you would put:

PlayerListenerClass PL = new PlayerListenerClass();
//instantiate an instance of you player listener 

public void onEnable() {
    PluginManager pm = this.getServer().getPluginManager();
    pm.registerEvents(InspiredNationsPL, this);
    //Tell the plugin manager where it can find the player listener
}

That should get it working.

Upvotes: 1

Related Questions