我试图将破坏者名称作为键,将受害者名称作为值添加到我的称为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!");
    }
}


java - Bukkit/Spigot API:映射伤害者和受害者-LMLPHP

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

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

damageCheck.put(damagerName,受害人姓名);

但是现在它使受害者无效(第一次击中),这是为什么呢?

java - Bukkit/Spigot API:映射伤害者和受害者-LMLPHP

最佳答案

所以,我正在测试一种全新的方法来执行此操作,因为我并没有真正理解您所指的内容,无论如何这是代码,我认为这正是您真正需要的

无需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());

}


从哈希图中删除实体数据,尽管有时并不准确

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

10-08 03:25