Skip to content

Commit

Permalink
Prevent IllegalArgumentException with Rupture and provide more detail…
Browse files Browse the repository at this point in the history
…ed logging when Rupture cannot activate from illegal state Fixes #5063
  • Loading branch information
nossr50 committed Aug 11, 2024
1 parent 684583c commit c7409a8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
25 changes: 22 additions & 3 deletions src/main/java/com/gmail/nossr50/runnables/skills/RuptureTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
import com.gmail.nossr50.util.MobHealthbarUtils;
import com.gmail.nossr50.util.skills.ParticleEffectUtils;
import com.google.common.base.Objects;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import static org.bukkit.attribute.Attribute.GENERIC_MAX_HEALTH;

public class RuptureTask extends CancellableRunnable {

public static final int DAMAGE_TICK_INTERVAL = 10;
Expand Down Expand Up @@ -82,20 +85,36 @@ private void playAnimation() {
private boolean applyRupture() {
double healthBeforeRuptureIsApplied = targetEntity.getHealth();

//Ensure victim has health
// Ensure victim has health
if (healthBeforeRuptureIsApplied > 0.01) {
//Send a fake damage event
McMMOEntityDamageByRuptureEvent event =
new McMMOEntityDamageByRuptureEvent(ruptureSource, targetEntity, calculateAdjustedTickDamage());
mcMMO.p.getServer().getPluginManager().callEvent(event);

//Ensure the event wasn't cancelled and damage is still greater than 0
//Ensure the event wasn't canceled and damage is still greater than 0
double damage = event.getDamage(); //Use raw damage for Rupture

if (event.isCancelled() || damage <= 0 || healthBeforeRuptureIsApplied - damage <= 0)
return true;

double damagedHealth = healthBeforeRuptureIsApplied - damage;
final double damagedHealth = healthBeforeRuptureIsApplied - damage;

final AttributeInstance maxHealthAttribute = targetEntity.getAttribute(GENERIC_MAX_HEALTH);
if (maxHealthAttribute == null) {
// Can't remove health if max health is null
mcMMO.p.getLogger().info("RuptureTask: Target entity has an illegal state for its health." +
" Cancelling Rupture. Target has null " + GENERIC_MAX_HEALTH + " attribute.");
return true;
}

if (damagedHealth > maxHealthAttribute.getValue()) {
// Something went very wrong here, target has an illegal state for its health
mcMMO.p.getLogger().info("RuptureTask: Target entity has an illegal state for its health." +
" Cancelling Rupture. Target has " + targetEntity.getHealth() + " health," +
" but max health is " + maxHealthAttribute.getValue());
return true;
}

targetEntity.setHealth(damagedHealth); //Hurt entity without the unwanted side effects of damage()}
MobHealthbarUtils.handleMobHealthbars(targetEntity, damage, mcMMO.p);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/gmail/nossr50/util/skills/CombatUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

import java.util.List;

import static com.gmail.nossr50.datatypes.experience.XPGainReason.PVP;
import static com.gmail.nossr50.util.MobMetadataUtils.hasMobFlag;

public final class CombatUtils {
Expand Down Expand Up @@ -891,7 +892,7 @@ public static void processCombatXP(@NotNull McMMOPlayer mcMMOPlayer,
return;
}

xpGainReason = XPGainReason.PVP;
xpGainReason = PVP;

if (defender.isOnline()
&& SkillUtils.cooldownExpired(mcMMOPlayer.getRespawnATS(), Misc.PLAYER_RESPAWN_COOLDOWN_SECONDS)) {
Expand Down

0 comments on commit c7409a8

Please sign in to comment.