Slompyy
Slompyy

Reputation: 1

Hit Counter Counts Twice in PaperMC Plugin

I'm working on a 1.19.4 PaperMC plugin that counts hits for a super attack. My problem is that when I hit an entity, the hit is counted as two. The hit counter is needed to activate the super attack, which activates when hits reach 20.

Here’s the relevant code for the hit counting method:

@EventHandler
public void onEntityDamage(EntityDamageByEntityEvent event) {

    // Sprawdzanie czy zadający jest graczem
    if (!(event.getDamager() instanceof Player)) {
        return;
    }

    Player player = (Player) event.getDamager();
    ItemStack item = player.getInventory().getItemInMainHand();

    if (item == null || item.getType() != Material.NETHERITE_AXE || !item.getItemMeta().hasDisplayName() || !item.getItemMeta().getDisplayName().equals(WEAPON_NAME)) {
        return;
    }

    if (player.getAttackCooldown() < 0.9) return;

    UUID playerId = player.getUniqueId();
    // Sprawdź, czy gracz trzyma przedmiot w głównej ręce
    if (player.getInventory().getItemInMainHand() != null && player.getInventory().getItemInMainHand().getType() != Material.AIR) {
        int hits = hitCounter.getOrDefault(playerId, 0) + 1;

        // Zaktualizuj licznik hitów
        hitCounter.put(playerId, hits);

        long currentTime = System.currentTimeMillis();
        long lastHit = lastHitTime.getOrDefault(playerId, 0L);

        // Jeśli atak był zbyt szybko po poprzednim ataku, nie licz go
        if (currentTime - lastHit < HIT_INTERVAL) return;

        lastHitTime.put(playerId, currentTime);
        updateActionBar(player, hits);

        // Sprawdzanie czy jest wystarczająca ilość hitów na super atak
        if (hits >= 20 && !superAttackReady.getOrDefault(playerId, false)) {
            superAttackReady.put(playerId, true);
            player.sendMessage(slMessageCustom + ChatColor.GREEN + "ꜱᴜᴘᴇʀ ᴀᴛᴀᴋ ɢᴏᴛᴏᴡʏ! ᴋʟɪᴋɴɪᴊ ᴘᴘᴍ ᴀʙʏ ᴀᴋᴛʏᴡᴏᴡᴀᴄ.");
        }

        // Dodawanie efektu withera graczom podczas super ataku
        if (isSuperAttackActive(player) && event.getEntity() instanceof LivingEntity) {
            LivingEntity target = (LivingEntity) event.getEntity();
            target.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 80, 1));
        }
    }
}

I want to know why my hits are counted twice.

Edit: I've solved it ;)

Upvotes: 0

Views: 46

Answers (0)

Related Questions