寻找合适的活动。我想检查玩家是否右键点击了其他玩家。
这是我到目前为止所做的工作(没有工作。没有进入if
或else
声明:
public void onPlayerRightClicks(PlayerInteractEntityEvent e) {
Player p=e.getPlayer();
if(e.getRightClicked() instanceof Player) p.sendMessage("You have rightclicked a player.");
else p.sendMessage("You didn't hit anyone with your spell");
}
答案 0 :(得分:0)
通过将我自己的代码与来自另一个答案的代码组合在一个类似的问题上找到问题的答案:
@SuppressWarnings("deprecation")
@EventHandler
public void onPlayerClick(PlayerInteractEvent e) {
Player p=e.getPlayer();
NBTItem item = new NBTItem(p.getItemInHand());
Entity en=getNearestEntityInSight(p,5);
if(e.getAction()==Action.RIGHT_CLICK_AIR && en instanceof Player) p.sendMessage("You have rightclicked a player.");
else p.sendMessage("You didn't hit anyone with your spell");
}
public static Entity getNearestEntityInSight(Player player, int range) {
ArrayList<Entity> entities = (ArrayList<Entity>) player.getNearbyEntities(range, range, range);
ArrayList<Block> sightBlock = (ArrayList<Block>) player.getLineOfSight(null, range);
ArrayList<Location> sight = new ArrayList<Location>();
for (int i = 0;i<sightBlock.size();i++)
sight.add(sightBlock.get(i).getLocation());
for (int i = 0;i<sight.size();i++) {
for (int k = 0;k<entities.size();k++) {
if (Math.abs(entities.get(k).getLocation().getX()-sight.get(i).getX())<1.3) {
if (Math.abs(entities.get(k).getLocation().getY()-sight.get(i).getY())<1.5) {
if (Math.abs(entities.get(k).getLocation().getZ()-sight.get(i).getZ())<1.3) {
return entities.get(k);
}
}
}
}
}
return null;
}
即使它有效,我也不认为这是一个最佳答案,因为应该有一个事件可以检查一个玩家是否正确点击或至少左击另一个玩家。 getNearestEntitySight
方法找到玩家视线中最近的实体。我将它与我的代码结合使用,该代码在玩家右键单击时运行,使用PlayerInteractEvent
。
答案 1 :(得分:0)
尝试一下:
@EventHandler
public void onPlayerInteractEntity(final PlayerInteractAtEntityEvent e) {
final Player p = e.getPlayer();
if (e.getRightClicked() instanceof Player) {
final Player clicked = (Player) e.getRightClicked();
// DO STUFF
}
}