Checking if EntityDamageByEntityEvent is cancelled

Discussion in 'Plugin Development' started by Joshuak52, Jan 28, 2023.

Thread Status:
Not open for further replies.
  1. Offline

    Joshuak52

    Hey everyone, was making a combat log plugin and getting some errors and not understanding why it's not working correctly.

    Code:
    @EventHandler
        public void PlayerHitPlayer(EntityDamageByEntityEvent  event) {
            if(event.getEntity() instanceof Player && event.getDamager() instanceof Player) {
                Player whoWasHit = (Player) event.getEntity();
                Player whoHit = (Player) event.getDamager();
                if(!event.isCancelled()) {
                    combatlog.put(whoHit, 30);
                    combatlog.put(whoWasHit, 30);
                }
            }
        }

    So the issues I'm having:
    - When I hit another player spawn even though pvp is denied in this region it will add them to combatlog hashmap even when no hit should be registering
    - The timer is counting in negatives after the 30 seconds?
    - [​IMG]

    Code:
    @Override
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
           
            Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                   
                    for (Map.Entry<Player, Integer> entry : combatlog.entrySet()) {
                        TextComponent tc1 = new TextComponent(ChatColor.translateAlternateColorCodes('&', "&4&lCombat Tagged: " + entry.getValue()));
                        entry.getKey().spigot().sendMessage(ChatMessageType.ACTION_BAR, tc1);
                    }
                   
                    for (Map.Entry<Player, Integer> entry : combatlog.entrySet()) {
                        entry.setValue(entry.getValue() - 1);
                    }
                   
                    for (Map.Entry<Player, Integer> entry : combatlog.entrySet()) {
                        if (entry.getValue() == 0) {
                            combatlog.remove(entry.getKey());
                        }
                    }
                }
            }, 0, 20);
        }
     
  2. Online

    timtower Administrator Administrator Moderator

    @Joshuak52
    Store the player UUID instead of a Player object
    Look at EventPriority, and event.isCancelled
     
  3. Offline

    Strahan

    For the timer issue, you cannot modify a collection you are iterating. Use an iterator.
     
Thread Status:
Not open for further replies.

Share This Page