From 6d0fe4fb5803066c1f84451ce324550bc581e420 Mon Sep 17 00:00:00 2001 From: lexikiq Date: Tue, 20 Apr 2021 17:51:57 -0400 Subject: [PATCH] Add McMMOEntityDamageByRuptureEvent (#4498) --- .../fake/FakeEntityDamageByEntityEvent.java | 12 ++++---- .../McMMOEntityDamageByRuptureEvent.java | 23 +++++++++++++++ .../nossr50/listeners/EntityListener.java | 3 +- .../nossr50/runnables/skills/RuptureTask.java | 28 ++++++++++++------- 4 files changed, 49 insertions(+), 17 deletions(-) create mode 100644 src/main/java/com/gmail/nossr50/events/skills/rupture/McMMOEntityDamageByRuptureEvent.java diff --git a/src/main/java/com/gmail/nossr50/events/fake/FakeEntityDamageByEntityEvent.java b/src/main/java/com/gmail/nossr50/events/fake/FakeEntityDamageByEntityEvent.java index 47a33199d..51f2210da 100644 --- a/src/main/java/com/gmail/nossr50/events/fake/FakeEntityDamageByEntityEvent.java +++ b/src/main/java/com/gmail/nossr50/events/fake/FakeEntityDamageByEntityEvent.java @@ -1,9 +1,9 @@ package com.gmail.nossr50.events.fake; import com.google.common.base.Function; -import com.google.common.base.Functions; import org.bukkit.entity.Entity; import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.jetbrains.annotations.NotNull; import java.util.EnumMap; import java.util.Map; @@ -13,21 +13,21 @@ import java.util.Map; */ public class FakeEntityDamageByEntityEvent extends EntityDamageByEntityEvent implements FakeEvent { - public FakeEntityDamageByEntityEvent(Entity damager, Entity damagee, DamageCause cause, final Map modifiers) { + public FakeEntityDamageByEntityEvent(@NotNull Entity damager, @NotNull Entity damagee, @NotNull DamageCause cause, @NotNull final Map modifiers) { super(damager, damagee, cause, modifiers, getFunctionModifiers(modifiers)); } @Deprecated - public FakeEntityDamageByEntityEvent(Entity damager, Entity damagee, DamageCause cause, double damage) { + public FakeEntityDamageByEntityEvent(@NotNull Entity damager, @NotNull Entity damagee, @NotNull DamageCause cause, double damage) { super(damager, damagee, cause, damage); } - public static EnumMap> getFunctionModifiers(Map modifiers) { + @NotNull + public static EnumMap> getFunctionModifiers(@NotNull Map modifiers) { EnumMap> modifierFunctions = new EnumMap<>(DamageModifier.class); - Function ZERO = Functions.constant(-0.0); for (DamageModifier modifier : modifiers.keySet()) { - modifierFunctions.put(modifier, ZERO); + modifierFunctions.put(modifier, (o -> -0.0)); } return modifierFunctions; diff --git a/src/main/java/com/gmail/nossr50/events/skills/rupture/McMMOEntityDamageByRuptureEvent.java b/src/main/java/com/gmail/nossr50/events/skills/rupture/McMMOEntityDamageByRuptureEvent.java new file mode 100644 index 000000000..fd64ce27a --- /dev/null +++ b/src/main/java/com/gmail/nossr50/events/skills/rupture/McMMOEntityDamageByRuptureEvent.java @@ -0,0 +1,23 @@ +package com.gmail.nossr50.events.skills.rupture; + +import com.gmail.nossr50.datatypes.player.McMMOPlayer; +import com.google.common.collect.ImmutableMap; +import org.bukkit.entity.Entity; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.jetbrains.annotations.NotNull; + +import java.util.EnumMap; + +public class McMMOEntityDamageByRuptureEvent extends EntityDamageByEntityEvent { + private final McMMOPlayer mcMMODamager; + + public McMMOEntityDamageByRuptureEvent(@NotNull McMMOPlayer damager, @NotNull Entity damagee, double damage) { + super(damager.getPlayer(), damagee, DamageCause.CUSTOM, new EnumMap<>(ImmutableMap.of(DamageModifier.BASE, damage)), new EnumMap<>(ImmutableMap.of(DamageModifier.BASE, (o -> -0.0)))); + this.mcMMODamager = damager; + } + + @NotNull + public McMMOPlayer getMcMMODamager() { + return mcMMODamager; + } +} diff --git a/src/main/java/com/gmail/nossr50/listeners/EntityListener.java b/src/main/java/com/gmail/nossr50/listeners/EntityListener.java index 9042e1286..56843cd35 100644 --- a/src/main/java/com/gmail/nossr50/listeners/EntityListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/EntityListener.java @@ -8,6 +8,7 @@ import com.gmail.nossr50.datatypes.skills.subskills.interfaces.InteractType; import com.gmail.nossr50.events.fake.FakeEntityDamageByEntityEvent; import com.gmail.nossr50.events.fake.FakeEntityDamageEvent; import com.gmail.nossr50.events.fake.FakeEntityTameEvent; +import com.gmail.nossr50.events.skills.rupture.McMMOEntityDamageByRuptureEvent; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.party.PartyManager; import com.gmail.nossr50.skills.archery.Archery; @@ -290,7 +291,7 @@ public class EntityListener implements Listener { */ @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onEntityDamageByEntity(EntityDamageByEntityEvent event) { - if (event instanceof FakeEntityDamageByEntityEvent) { + if (event instanceof FakeEntityDamageByEntityEvent || event instanceof McMMOEntityDamageByRuptureEvent) { return; } diff --git a/src/main/java/com/gmail/nossr50/runnables/skills/RuptureTask.java b/src/main/java/com/gmail/nossr50/runnables/skills/RuptureTask.java index e740a3fcd..165ba9403 100644 --- a/src/main/java/com/gmail/nossr50/runnables/skills/RuptureTask.java +++ b/src/main/java/com/gmail/nossr50/runnables/skills/RuptureTask.java @@ -1,6 +1,7 @@ package com.gmail.nossr50.runnables.skills; import com.gmail.nossr50.datatypes.player.McMMOPlayer; +import com.gmail.nossr50.events.skills.rupture.McMMOEntityDamageByRuptureEvent; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.util.skills.ParticleEffectUtils; import com.google.common.base.Objects; @@ -45,18 +46,25 @@ public class RuptureTask extends BukkitRunnable { if(ruptureTick < expireTick) { //Is it time to damage? if(damageTickTracker >= DAMAGE_TICK_INTERVAL) { - damageTickTracker = 0; //Reset - ParticleEffectUtils.playBleedEffect(targetEntity); //Animate - if(targetEntity.getHealth() > 0.01) { - double healthBeforeRuptureIsApplied = targetEntity.getHealth(); - double damagedHealth = healthBeforeRuptureIsApplied - calculateAdjustedTickDamage(); + damageTickTracker = 0; //Reset timer + double healthBeforeRuptureIsApplied = targetEntity.getHealth(); - if(damagedHealth <= 0) { - mcMMO.p.getLogger().severe("DEBUG: Miscalculating Rupture tick damage"); - } else { - targetEntity.setHealth(damagedHealth); //Hurt entity without the unwanted side effects of damage() - } + //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 + double damage = event.getFinalDamage(); + if (event.isCancelled() || damage <= 0 || healthBeforeRuptureIsApplied - damage <= 0) + return; + + ParticleEffectUtils.playBleedEffect(targetEntity); //Animate + double damagedHealth = healthBeforeRuptureIsApplied - damage; + + targetEntity.setHealth(damagedHealth); //Hurt entity without the unwanted side effects of damage()} } } } else {