简体   繁体   中英

Bukkit PlayerInteractEvent doesn't work

I think this is correct but if I click with the Axe nothing happens, no error

I don't know what's wrong because in the internet it works

package mrleaw.tools.main;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin implements Listener{

String prefix = "§0[§4RollmopsMC | Tools§0] §5";

public void onEnable() {
    Bukkit.getConsoleSender().sendMessage(prefix + "§aDas Plugin wurde aktiviert!");
    Bukkit.getConsoleSender().sendMessage(prefix + "§6Coded by MrLeaw");
}
public void onDisable() {
    Bukkit.getConsoleSender().sendMessage(prefix + "§cDas Plugin wurde deaktiviert!");
    Bukkit.getConsoleSender().sendMessage(prefix + "§6Coded by MrLeaw");
}

@EventHandler
void onRightClickTool(PlayerInteractEvent e){
    try {
        if(e.getAction() == Action.LEFT_CLICK_AIR 
                || e.getAction() == Action.RIGHT_CLICK_AIR
                || e.getAction() == Action.LEFT_CLICK_BLOCK
                || e.getAction() == Action.RIGHT_CLICK_BLOCK){
            Player player = e.getPlayer();
            if(player.getItemInHand() != null && player.getItemInHand().getType() == Material.WOOD_AXE){
                player.sendMessage(prefix + "test");
            }
        }
    }catch(Exception exception){
    }catch(Error error){
    }
}
}

Add getServer().getPluginManager().registerEvents(this, this); to onEnable()

You have to do this to register your listener, as per the Bukkit API reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM