Bukkit / Spigot API:映射损坏者和受害者

时间:2017-04-07 22:04:21

标签: java bukkit

我正在尝试将损坏者名称作为键添加,并将受害者名称作为值添加到我的名为 damageCheck 。这一切都在EntityDamageByEntityEvent完成。然而,当我试图存储受害者和伤害者时,一切都会出错。

代码:

    private HashMap<String, String> damageCheck = new HashMap<String, String>();

@EventHandler
public void onEntityDamageByEntityEvent(EntityDamageByEntityEvent event) {

    if(event.getEntity() instanceof Player || event.getDamager() instanceof Player) {
        Player victim = (Player) event.getEntity();
        Player damager = (Player) event.getDamager();

        String victimName = victim.getDisplayName(); // getting the victim name
        String damagerName = damager.getDisplayName(); // getting the damager name

        Bukkit.getServer().broadcastMessage("");
        Bukkit.getServer().broadcastMessage(victim.getDisplayName() + " is the victim!");
        Bukkit.getServer().broadcastMessage(damager.getDisplayName() + " is the damager!");
        Bukkit.getServer().broadcastMessage("");

        try {
            if(!damageCheck.containsKey(damagerName)) {
                damageCheck.put(victimName, damagerName);

                Bukkit.getServer().broadcastMessage("");
                Bukkit.getServer().broadcastMessage(damageCheck.get(victimName) + " is victim!");
                Bukkit.getServer().broadcastMessage(damageCheck.get(damagerName) + " is damager!");
                Bukkit.getServer().broadcastMessage("");
            } else if(damageCheck.containsKey(damagerName)) {
                damageCheck.replace(damagerName, damagerName, damagerName);

                Bukkit.getServer().broadcastMessage("");
                Bukkit.getServer().broadcastMessage(damageCheck.get(victimName) + " is victim!");
                Bukkit.getServer().broadcastMessage(damageCheck.get(damagerName) + " is damager!");
                Bukkit.getServer().broadcastMessage("");
            }
        } catch (NullPointerException e) {
            // do nothing lol
        }

    } else {
        Bukkit.getServer().broadcastMessage("There is no entity or damager!");
    }
}

enter image description here

第一条消息是正确的。 GetSparked 是受害者, Fendyk 是受害者。但是在第二条消息中,当试图从哈希映射中获取名称时,它会输出错误的值。

编辑:我改变了 damageCheck.put(victimName,damagerName); damageCheck.put(damagerName,victimName);

但它现在给受害者 null (第一次击中),为什么会这样?

enter image description here

1 个答案:

答案 0 :(得分:1)

所以,我正在测试一种全新的方法,因为我并不真正理解你所指的是什么,无论如何这是代码,我认为你真正需要的是

无需 new HashMap <String, String>

private HashMap<String, String> damageCheck = new HashMap<>();

代码:

@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());

}

从hashmap 中删除实体数据虽然有时不准确

@EventHandler
public void onEntityDeathEvent(EntityDeathEvent event){
    damageCheck.remove(event.getEntity().getName());
    damageCheck.remove(event.getEntity().getKiller().getName());
}

您可以仅删除对玩家的检查,它可以在任何实体上运行  只需删除

即可

if(!(event.getDamager() instanceof Player) || !(event.getEntity() instanceof Player) ) return;