所以,我正在使用Bukkit API。基本上,我正在寻找要调用的PlayerInteractEvent,之后我会做一堆东西。但是,当我收到通知我实际踢了块时,我没有得到任何消息,即使它在我的代码中编译没有错误。我也没有从控制台得到任何例外。这是我的代码:
@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);
}
}
}
}
}
}
}
答案 0 :(得分:1)
因此,执行此操作的标准方法是创建一个implements Listener
然后在你内部以
的形式创建方法@EventHandler
public void nameDontMatter(PlayerInteractEvent event) {
// do stuff, you can get all that information you passed in from the event
}
此外,您需要确保告诉插件在哪里找到您的PlayerListener,所以通常您要做的是onEnable()
方法:
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
}
这应该让它发挥作用。
答案 1 :(得分:0)
该方法唯一的参数是PlayerIneractEvent
。
您无法在此处设置更多参数。
您设置的参数多于方法本身的参数。
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event, Player who, ...) {
...
}
改为使用:
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
// do stuff here
}
在方法中,您可以获得您设置为参数的所有其他内容。例如:
event.getPlayer(); // gets the player
event.getAction(); // gets the action
另请注意,您已在插件主类中注册了听众。
registerEventHandler(yourEventHandler, this);
答案 2 :(得分:0)
你只能拥有PlayerInteractEvent,否则它不会工作。此外,您不需要将播放器作为参数提供,您可以使用getPlayer()方法查看触发事件的人。
@EventHandler
public void onPlayerInteract(PlayerInteractEvent e) {
Player player = e.getPlayer();
}