Refactor potion healing to exclude non-teammates.

Split logic into smaller methods for clarity and maintainability. Added `shouldHeal` to determine team eligibility and `calculateActualHealing` to handle precise healing calculations. Updated intensity handling to exclude non-teammates during healing.
This commit is contained in:
Teriuihi 2025-02-23 01:22:52 +01:00
parent eeed5b4c54
commit e62d0df9df

View File

@ -13,6 +13,7 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.entity.PotionSplashEvent;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import java.util.Optional;
@ -64,23 +65,44 @@ public class OtherGameEvents implements Listener {
if (optionalTeamPlayer.isEmpty())
return;
TeamPlayer teamPlayer = optionalTeamPlayer.get();
event.getAffectedEntities().stream()
.filter(livingEntity -> livingEntity instanceof Player)
.map(livingEntity -> (Player) livingEntity)
.forEach(target -> {
if (shouldHeal(teamPlayer, target)) {
return;
}
event.setIntensity(target, 0);
});
double totalHealing = thrownPotion.getEffects().stream()
.filter(effect -> effect.getType() == PotionEffectType.INSTANT_HEALTH)
.flatMapToDouble(effect -> event.getAffectedEntities().stream()
.filter(livingEntity -> livingEntity instanceof Player)
.map(livingEntity -> (Player) livingEntity)
.mapToDouble(target -> {
AttributeInstance playerMaxHealth = target.getAttribute(Attribute.GENERIC_MAX_HEALTH);
if (playerMaxHealth == null) {
return 0;
}
double missingHealth = playerMaxHealth.getValue() - target.getHealth();
double potentialHealing = (effect.getAmplifier() + 1) * event.getIntensity(target);
return Math.min(potentialHealing, missingHealth);
}))
.mapToDouble(target -> calculateActualHealing(target, effect, event.getIntensity(target))))
.sum();
optionalTeamPlayer.get().increaseStat(Stat.DAMAGE_HEALED, totalHealing);
teamPlayer.increaseStat(Stat.DAMAGE_HEALED, totalHealing);
}
private boolean shouldHeal(TeamPlayer healer, Player target) {
Optional<TeamPlayer> optionalTeamTarget = gameManager.getTeamPlayer(target);
return optionalTeamTarget.isPresent() && healer.getTeam() == optionalTeamTarget.get().getTeam();
}
private double calculateActualHealing(Player target, PotionEffect effect, double intensity) {
AttributeInstance playerMaxHealth = target.getAttribute(Attribute.GENERIC_MAX_HEALTH);
if (playerMaxHealth == null) {
return 0;
}
double missingHealth = playerMaxHealth.getValue() - target.getHealth();
//Only counts healing on teammates since intensity was set to 0 for non teammates
double potentialHealing = (effect.getAmplifier() + 1) * intensity;
return Math.min(potentialHealing, missingHealth);
}
}