<commit_before>package info.faceland.strife.managers;

import com.tealcube.minecraft.bukkit.TextUtils;
import com.tealcube.minecraft.bukkit.facecore.utilities.MessageUtils;
import info.faceland.strife.StrifePlugin;
import info.faceland.strife.conditions.Condition;
import info.faceland.strife.data.AbilityIconData;
import info.faceland.strife.data.StrifeMob;
import info.faceland.strife.data.ability.Ability;
import info.faceland.strife.data.ability.Ability.TargetType;
import info.faceland.strife.data.ability.EntityAbilitySet;
import info.faceland.strife.data.ability.EntityAbilitySet.AbilityType;
import info.faceland.strife.data.champion.ChampionSaveData.LifeSkillType;
import info.faceland.strife.data.champion.StrifeAttribute;
import info.faceland.strife.effects.Effect;
import info.faceland.strife.effects.Wait;
import info.faceland.strife.stats.AbilitySlot;
import info.faceland.strife.util.ItemUtil;
import info.faceland.strife.util.LogUtil;
import info.faceland.strife.util.PlayerDataUtil;
import io.pixeloutlaw.minecraft.spigot.hilt.ItemStackExtensionsKt;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Chicken;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Mob;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;

public class AbilityManager {

  private final StrifePlugin plugin;
  private final Map<String, Ability> loadedAbilities = new HashMap<>();
  private final Map<LivingEntity, Map<Ability, Integer>> coolingDownAbilities = new ConcurrentHashMap<>();
  private final Map<UUID, Map<Ability, Integer>> savedPlayerCooldowns = new ConcurrentHashMap<>();
  private final Set<Material> ignoredMaterials = new HashSet<>();

  private static final String NO_TARGET = TextUtils.color("&e&lNo Target Found!");
  private static final String TEST_CHICKEN = ChatColor.RED + "TEST CHICKEN PLS IGNORE";

  public AbilityManager(StrifePlugin plugin) {
    this.plugin = plugin;
    this.ignoredMaterials.add(Material.AIR);
    this.ignoredMaterials.add(Material.TALL_GRASS);
  }

  public Ability getAbility(String name) {
    if (loadedAbilities.containsKey(name)) {
      return loadedAbilities.get(name);
    }
    LogUtil.printWarning("Attempted to get unknown ability '" + name + "'.");
    return null;
  }

  public void startAbilityCooldown(LivingEntity livingEntity, Ability ability) {
    if (!coolingDownAbilities.containsKey(livingEntity)) {
      coolingDownAbilities.put(livingEntity, new ConcurrentHashMap<>());
    }
    coolingDownAbilities.get(livingEntity).put(ability, ability.getCooldown() * 20);
  }

  public double getCooldownTicks(LivingEntity livingEntity, Ability ability) {
    if (!coolingDownAbilities.containsKey(livingEntity)) {
      return 0;
    }
    return coolingDownAbilities.get(livingEntity).getOrDefault(ability, 0);
  }

  public void tickAbilityCooldowns(int tickRate) {
    for (LivingEntity le : coolingDownAbilities.keySet()) {
      if (le == null || !le.isValid()) {
        coolingDownAbilities.remove(le);
        continue;
      }
      for (Ability ability : coolingDownAbilities.get(le).keySet()) {
        int ticks = coolingDownAbilities.get(le).get(ability);
        if (ticks <= tickRate) {
          coolingDownAbilities.get(le).remove(ability);
          continue;
        }
        coolingDownAbilities.get(le).put(ability, ticks - tickRate);
      }
    }
  }

  public void savePlayerCooldowns(Player player) {
    if (coolingDownAbilities.containsKey(player)) {
      savedPlayerCooldowns.put(player.getUniqueId(), coolingDownAbilities.get(player));
      coolingDownAbilities.remove(player);
    }
  }

  public void loadPlayerCooldowns(Player player) {
    coolingDownAbilities.put(player, new ConcurrentHashMap<>());
    if (savedPlayerCooldowns.containsKey(player.getUniqueId())) {
      coolingDownAbilities.put(player, savedPlayerCooldowns.get(player.getUniqueId()));
      savedPlayerCooldowns.remove(player.getUniqueId());
    }
  }

  public boolean isCooledDown(LivingEntity livingEntity, Ability ability) {
    return !coolingDownAbilities.get(livingEntity).containsKey(ability);
  }

  public void execute(final Ability ability, final StrifeMob caster,
      StrifeMob target) {
    if (ability.getCooldown() != 0 && !isCooledDown(caster.getEntity(), ability)) {
      LogUtil.printDebug("Failed. Ability " + ability.getId() + " is on cooldown");
      return;
    }
    if (ability.getTargetType() == TargetType.SELF) {
      target = caster;
    }
    if (!PlayerDataUtil.areConditionsMet(caster, target, ability.getConditions())) {
      LogUtil.printDebug("Conditions not met for ability. Failed.");
      return;
    }
    LivingEntity targetEntity;
    if (target == null) {
      targetEntity = getTarget(caster, ability);
      if (targetEntity == null) {
        if (ability.isDisplayCd() && caster instanceof Player) {
          MessageUtils.sendActionBar((Player) caster.getEntity(), NO_TARGET);
        }
        LogUtil.printDebug("Failed. No target found for ability " + ability.getId());
        return;
      }
      target = plugin.getStrifeMobManager().getStatMob(targetEntity);
    } else {
      targetEntity = target.getEntity();
    }
    LogUtil.printDebug("Target: " + PlayerDataUtil.getName(targetEntity));
    if (ability.getCooldown() != 0) {
      startAbilityCooldown(caster.getEntity(), ability);
    }
    List<Effect> taskEffects = new ArrayList<>();
    int waitTicks = 0;
    for (Effect effect : ability.getEffects()) {
      if (effect instanceof Wait) {
        LogUtil.printDebug("Effects in this chunk: " + taskEffects.toString());
        runEffects(caster, target, taskEffects, waitTicks);
        waitTicks += ((Wait) effect).getTickDelay();
        taskEffects = new ArrayList<>();
        continue;
      }
      taskEffects.add(effect);
      LogUtil.printDebug("Added effect " + effect.getName() + " to task list");
    }
    runEffects(caster, target, taskEffects, waitTicks);
    if (TEST_CHICKEN.equals(targetEntity.getCustomName())) {
      targetEntity.remove();
    }
  }

  public void execute(Ability ability, final StrifeMob caster) {
    execute(ability, caster, null);
  }

  public void uniqueAbilityCast(StrifeMob caster, AbilityType type) {
    EntityAbilitySet abilitySet = plugin.getUniqueEntityManager().getAbilitySet(caster.getEntity());
    if (abilitySet == null) {
      return;
    }
    int phase = plugin.getUniqueEntityManager().getPhase(caster.getEntity());
    switch (type) {
      case ON_HIT:
        abilityPhaseCast(caster, abilitySet.getOnHitAbilities(), phase);
        break;
      case WHEN_HIT:
        abilityPhaseCast(caster, abilitySet.getWhenHitAbilities(), phase);
        break;
      case TIMER:
        abilityPhaseCast(caster, abilitySet.getTimerAbilities(), phase);
        break;
      case PHASE_SHIFT:
        abilityPhaseCast(caster, abilitySet.getPhaseShiftAbilities(), phase);
        break;
    }
  }

  public void checkPhaseChange(LivingEntity entity) {
    if (plugin.getUniqueEntityManager().isUniqueEntity(entity)) {
      LogUtil.printDebug("Trying to check phase on non-unique: " + PlayerDataUtil.getName(entity));
      return;<commit_msg>Fixing ability cooldowns for unique enemies<commit_after>package info.faceland.strife.managers;

import com.tealcube.minecraft.bukkit.TextUtils;
import com.tealcube.minecraft.bukkit.facecore.utilities.MessageUtils;
import info.faceland.strife.StrifePlugin;
import info.faceland.strife.conditions.Condition;
import info.faceland.strife.data.AbilityIconData;
import info.faceland.strife.data.StrifeMob;
import info.faceland.strife.data.ability.Ability;
import info.faceland.strife.data.ability.Ability.TargetType;
import info.faceland.strife.data.ability.EntityAbilitySet;
import info.faceland.strife.data.ability.EntityAbilitySet.AbilityType;
import info.faceland.strife.data.champion.ChampionSaveData.LifeSkillType;
import info.faceland.strife.data.champion.StrifeAttribute;
import info.faceland.strife.effects.Effect;
import info.faceland.strife.effects.Wait;
import info.faceland.strife.stats.AbilitySlot;
import info.faceland.strife.util.ItemUtil;
import info.faceland.strife.util.LogUtil;
import info.faceland.strife.util.PlayerDataUtil;
import io.pixeloutlaw.minecraft.spigot.hilt.ItemStackExtensionsKt;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Chicken;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Mob;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;

public class AbilityManager {

  private final StrifePlugin plugin;
  private final Map<String, Ability> loadedAbilities = new HashMap<>();
  private final Map<LivingEntity, Map<Ability, Integer>> coolingDownAbilities = new ConcurrentHashMap<>();
  private final Map<UUID, Map<Ability, Integer>> savedPlayerCooldowns = new ConcurrentHashMap<>();
  private final Set<Material> ignoredMaterials = new HashSet<>();

  private static final String NO_TARGET = TextUtils.color("&e&lNo Target Found!");
  private static final String TEST_CHICKEN = ChatColor.RED + "TEST CHICKEN PLS IGNORE";

  public AbilityManager(StrifePlugin plugin) {
    this.plugin = plugin;
    this.ignoredMaterials.add(Material.AIR);
    this.ignoredMaterials.add(Material.TALL_GRASS);
  }

  public Ability getAbility(String name) {
    if (loadedAbilities.containsKey(name)) {
      return loadedAbilities.get(name);
    }
    LogUtil.printWarning("Attempted to get unknown ability '" + name + "'.");
    return null;
  }

  public void startAbilityCooldown(LivingEntity livingEntity, Ability ability) {
    if (!coolingDownAbilities.containsKey(livingEntity)) {
      coolingDownAbilities.put(livingEntity, new ConcurrentHashMap<>());
    }
    coolingDownAbilities.get(livingEntity).put(ability, ability.getCooldown() * 20);
  }

  public double getCooldownTicks(LivingEntity livingEntity, Ability ability) {
    if (!coolingDownAbilities.containsKey(livingEntity)) {
      return 0;
    }
    return coolingDownAbilities.get(livingEntity).getOrDefault(ability, 0);
  }

  public void tickAbilityCooldowns(int tickRate) {
    for (LivingEntity le : coolingDownAbilities.keySet()) {
      if (le == null || !le.isValid()) {
        coolingDownAbilities.remove(le);
        continue;
      }
      for (Ability ability : coolingDownAbilities.get(le).keySet()) {
        int ticks = coolingDownAbilities.get(le).get(ability);
        if (ticks <= tickRate) {
          coolingDownAbilities.get(le).remove(ability);
          continue;
        }
        coolingDownAbilities.get(le).put(ability, ticks - tickRate);
      }
    }
  }

  public void savePlayerCooldowns(Player player) {
    if (coolingDownAbilities.containsKey(player)) {
      savedPlayerCooldowns.put(player.getUniqueId(), coolingDownAbilities.get(player));
      coolingDownAbilities.remove(player);
    }
  }

  public void loadPlayerCooldowns(Player player) {
    coolingDownAbilities.put(player, new ConcurrentHashMap<>());
    if (savedPlayerCooldowns.containsKey(player.getUniqueId())) {
      coolingDownAbilities.put(player, savedPlayerCooldowns.get(player.getUniqueId()));
      savedPlayerCooldowns.remove(player.getUniqueId());
    }
  }

  public boolean isCooledDown(LivingEntity livingEntity, Ability ability) {
    if (!coolingDownAbilities.containsKey(livingEntity)) {
      coolingDownAbilities.put(livingEntity, new ConcurrentHashMap<>());
    }
    return !coolingDownAbilities.get(livingEntity).containsKey(ability);
  }

  public void execute(final Ability ability, final StrifeMob caster, StrifeMob target) {
    if (ability.getCooldown() != 0 && !isCooledDown(caster.getEntity(), ability)) {
      LogUtil.printDebug("Failed. Ability " + ability.getId() + " is on cooldown");
      return;
    }
    if (ability.getTargetType() == TargetType.SELF) {
      target = caster;
    }
    if (!PlayerDataUtil.areConditionsMet(caster, target, ability.getConditions())) {
      LogUtil.printDebug("Conditions not met for ability. Failed.");
      return;
    }
    LivingEntity targetEntity;
    if (target == null) {
      targetEntity = getTarget(caster, ability);
      if (targetEntity == null) {
        if (ability.isDisplayCd() && caster instanceof Player) {
          MessageUtils.sendActionBar((Player) caster.getEntity(), NO_TARGET);
        }
        LogUtil.printDebug("Failed. No target found for ability " + ability.getId());
        return;
      }
      target = plugin.getStrifeMobManager().getStatMob(targetEntity);
    } else {
      targetEntity = target.getEntity();
    }
    LogUtil.printDebug("Target: " + PlayerDataUtil.getName(targetEntity));
    if (ability.getCooldown() != 0) {
      startAbilityCooldown(caster.getEntity(), ability);
    }
    List<Effect> taskEffects = new ArrayList<>();
    int waitTicks = 0;
    for (Effect effect : ability.getEffects()) {
      if (effect instanceof Wait) {
        LogUtil.printDebug("Effects in this chunk: " + taskEffects.toString());
        runEffects(caster, target, taskEffects, waitTicks);
        waitTicks += ((Wait) effect).getTickDelay();
        taskEffects = new ArrayList<>();
        continue;
      }
      taskEffects.add(effect);
      LogUtil.printDebug("Added effect " + effect.getName() + " to task list");
    }
    runEffects(caster, target, taskEffects, waitTicks);
    if (TEST_CHICKEN.equals(targetEntity.getCustomName())) {
      targetEntity.remove();
    }
  }

  public void execute(Ability ability, final StrifeMob caster) {
    execute(ability, caster, null);
  }

  public void uniqueAbilityCast(StrifeMob caster, AbilityType type) {
    EntityAbilitySet abilitySet = plugin.getUniqueEntityManager().getAbilitySet(caster.getEntity());
    if (abilitySet == null) {
      return;
    }
    int phase = plugin.getUniqueEntityManager().getPhase(caster.getEntity());
    switch (type) {
      case ON_HIT:
        abilityPhaseCast(caster, abilitySet.getOnHitAbilities(), phase);
        break;
      case WHEN_HIT:
        abilityPhaseCast(caster, abilitySet.getWhenHitAbilities(), phase);
        break;
      case TIMER:
        abilityPhaseCast(caster, abilitySet.getTimerAbilities(), phase);
        break;
      case PHASE_SHIFT:
        abilityPhaseCast(caster, abilitySet.getPhaseShiftAbilities(), phase);
        break;
    }
  }

  public void checkPhaseChange(LivingEntity entity) {
    if (!plugin.getUniqueEntityManager().isUniqueEntity(entity)) {
      LogUtil.printDebug("Trying to check phase on non-unique: " + PlayerDataUtil.getName(entity));
      return;
{'fraction_non_alphanumeric': 0.11395754016906498, 'fraction_numerical': 0.0009034006581919082, 'mean_word_length': 4.140298507462687, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '417072', 'n_tokens_mistral': 4775, 'n_tokens_neox': 4656, 'n_words': 877}