我试图建立一个奖励你杀死玩家的奖励系统。 所以我完成了基本的检查,但我想检测受害者在受到攻击或用弓射击后是否陷入虚空,事情是,我无法找到解决方案。
代码:
@EventHandler
public void onEntityDamageByEntityEvent(EntityDamageByEntityEvent event) {
if(!(event.getDamager() instanceof Player) || !(event.getEntity() instanceof Player) ) return;
String damagerName = event.getDamager().getName();
String victimName = event.getEntity().getName();
damageCheck.put(victimName, damagerName);
Bukkit.broadcastMessage(ChatColor.GRAY + "" + ChatColor.STRIKETHROUGH + "-------------[ X ]-------------");
Bukkit.broadcastMessage(ChatColor.DARK_AQUA + damagerName + ChatColor.GRAY + " is the damager");
Bukkit.broadcastMessage(ChatColor.DARK_AQUA + victimName + ChatColor.GRAY + " is the victim");
Bukkit.broadcastMessage(ChatColor.GRAY + "" + ChatColor.STRIKETHROUGH + "-------------[ X ]-------------");
Bukkit.getLogger().info(damageCheck.toString());
}
@EventHandler
public void onEntityDeathEvent(EntityDeathEvent event){
damageCheck.remove(event.getEntity().getName());
damageCheck.remove(event.getEntity().getKiller().getName());
event.getEntity().getKiller().sendMessage("You are the killer, and receives rewards!");
event.getEntity().sendMessage("You died, thats horrible..");
Bukkit.broadcastMessage(ChatColor.GRAY + "" + ChatColor.STRIKETHROUGH + "-------------[ X ]-------------");
Bukkit.broadcastMessage(ChatColor.DARK_AQUA + event.getEntity().getKiller().getName() + ChatColor.GRAY + " is the damager");
Bukkit.broadcastMessage(ChatColor.DARK_AQUA + event.getEntity().getName() + ChatColor.GRAY + " is the victim");
Bukkit.broadcastMessage(ChatColor.GRAY + "" + ChatColor.STRIKETHROUGH + "-------------[ X ]-------------");
}
@EventHandler
public void onPlayerMovement(PlayerMoveEvent event) {
Player player = (Player)event.getPlayer();
// Checking if the players Y
if(event.getTo().getY() < 25.0) {
// do stuff here
}
}
答案 0 :(得分:1)
您应该使用EntityDeathEvent
块包围try & catch
代码,以避免出现错误......
在PlayerMoveEvent
上,将event.getPlayer()
转换为Player
是多余的,因为您已经指定了变量。 Y也应该低于10,相信我,它会更好。
好的,现在你首先需要检查hashmap是否包含该播放器(谁在虚空中死亡),然后从hashmap中获取杀手(damager)名称,之后你可以发送消息给那个杀手并最终杀死了虚空中的玩家
代码:D
if (!damageCheck.containsKey(player.getName())) return;
Player killer = Bukkit.getPlayer(damageCheck.get(player.getName()));
killer.sendMessage(ChatColor.GREEN + "You've earned " + ChatColor.RED + "" + ChatColor.BOLD + "$3" + ChatColor.GREEN + " for killing " + ChatColor.DARK_AQUA + player.getName()); // sends a message to the killer
player.setHealth(0); // kills the player
所以我们已经完成了处理void部分。现在你需要检测受害者是否被枪杀:
将您的if语句更改为if (!(event.getDamager() instanceof Player || event.getDamager() instanceof Projectile) || !(event.getEntity() instanceof Player)) return;
,以便它可以检测是否有箭头击中受害者......
现在您需要检测射手/伤害者名称,这可以通过if
声明轻松完成。
代码:
Player shooterName = event.getDamager() instanceof Projectile ? (Player) ((Projectile) event.getDamager()).getShooter() : null;
String damagerName = event.getDamager() instanceof Player ? event.getDamager().getName() : shooterName.getName();
您也可以删除Bukkit.getLogger().info(damageCheck.toString());
,因为它仅用于调试。
答案 1 :(得分:0)
你应该这样做:
private enum Cause {
ATTACK,
ARROW,
}
private HashMap<UUID, UUID> lastDamage = new HashMap<UUID, UUID>();
private HashMap<UUID, Cause> lastDamageCause = new HashMap<UUID, Cause>();
@EventHandler
public void onDamage(EntityDamageByEntityEvent e) {
if (!(e.getEntity() instanceof Player)) {
return;
}
Player damaged = (Player) e.getEntity();
if (e.getDamager() instanceof Arrow) {
Arrow a = (Arrow) e.getDamager();
if (a.getShooter() instanceof Player) {
Player damager = (Player) a.getShooter();
lastDamage.put(damaged.getUniqueId(), damager.getUniqueId());
lastDamageCause.put(damaged.getUniqueId(), Cause.ARROW);
}
} else if (e.getDamager() instanceof Player) {
Player damager = (Player) e.getDamager();
lastDamage.put(damaged.getUniqueId(), damager.getUniqueId());
lastDamageCause.put(damaged.getUniqueId(), Cause.ATTACK);
}
}
@EventHandler
public void onPlayerDeath(PlayerDeathEvent e) {
Player p = e.getEntity();
if (e.getDeathMessage().contains("fell out of the world")) {
if (lastDamage.containsKey(p.getUniqueId())) {
Player killer = Bukkit.getPlayer(lastDamage.get(p.getUniqueId()));
Cause cause = lastDamageCause.get(p.getUniqueId());
switch (cause) {
case ATTACK:
//Do whatever you want
break;
case ARROW:
//Do whatever you want
break;
default:
break;
}
}
}
}
如果您只想检查Y高度,请使用PlayerMoveEvent
不要忘记在播放器与服务器断开连接后删除HashMaps中的值以避免内存泄漏。