From 5bfca3c218097d7293051f94afaa54abe7cfbd7d Mon Sep 17 00:00:00 2001 From: nossr50 Date: Tue, 13 Feb 2024 17:47:54 -0800 Subject: [PATCH 1/2] Fix spigot api break for entity damage events --- Changelog.txt | 4 ++ .../fake/FakeEntityDamageByEntityEvent.java | 14 ------- .../events/fake/FakeEntityDamageEvent.java | 14 ------- .../McMMOEntityDamageByRuptureEvent.java | 39 +++++++++---------- .../nossr50/listeners/EntityListener.java | 31 ++++----------- .../nossr50/runnables/skills/RuptureTask.java | 13 ++----- .../com/gmail/nossr50/util/EventUtils.java | 16 ++------ .../nossr50/util/skills/CombatUtils.java | 28 ++++--------- 8 files changed, 47 insertions(+), 112 deletions(-) delete mode 100644 src/main/java/com/gmail/nossr50/events/fake/FakeEntityDamageByEntityEvent.java delete mode 100644 src/main/java/com/gmail/nossr50/events/fake/FakeEntityDamageEvent.java diff --git a/Changelog.txt b/Changelog.txt index 72e414f9d..0c35d8bfb 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -1,3 +1,7 @@ +Version 2.1.228 + Fixed stack trace due to Spigot API break on EntityDamageEvent and EntityDamageByEntity event + (API) - Removed FakeEntityDamageEvent and FakeEntityDamageByEntityEvent, you can use our metadata constant to check if damage is from mcMMO instead + (API) - Temporarily removed McMMOEntityDamageByRuptureEvent, this needs to be reworked for the new spigot API changes Version 2.1.227 Fixed bug where some text would not be displayed to players (Adventure dependency update) Repair sound effect from mcMMO can now be adjusted in Minecraft sound options (Thanks MithicSpirit) diff --git a/src/main/java/com/gmail/nossr50/events/fake/FakeEntityDamageByEntityEvent.java b/src/main/java/com/gmail/nossr50/events/fake/FakeEntityDamageByEntityEvent.java deleted file mode 100644 index e88e01ded..000000000 --- a/src/main/java/com/gmail/nossr50/events/fake/FakeEntityDamageByEntityEvent.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.gmail.nossr50.events.fake; - -import org.bukkit.entity.Entity; -import org.bukkit.event.entity.EntityDamageByEntityEvent; -import org.jetbrains.annotations.NotNull; - -/** - * Called when mcMMO applies damage from an entity due to special abilities. - */ -public class FakeEntityDamageByEntityEvent extends EntityDamageByEntityEvent implements FakeEvent { - public FakeEntityDamageByEntityEvent(@NotNull Entity damager, @NotNull Entity damagee, @NotNull DamageCause cause, double damage) { - super(damager, damagee, cause, damage); - } -} diff --git a/src/main/java/com/gmail/nossr50/events/fake/FakeEntityDamageEvent.java b/src/main/java/com/gmail/nossr50/events/fake/FakeEntityDamageEvent.java deleted file mode 100644 index ac915eaf6..000000000 --- a/src/main/java/com/gmail/nossr50/events/fake/FakeEntityDamageEvent.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.gmail.nossr50.events.fake; - -import org.bukkit.entity.Entity; -import org.bukkit.event.entity.EntityDamageEvent; - -/** - * Called when mcMMO applies damage due to special abilities. - */ -public class FakeEntityDamageEvent extends EntityDamageEvent implements FakeEvent { - @Deprecated - public FakeEntityDamageEvent(Entity damagee, DamageCause cause, double damage) { - super(damagee, cause, damage); - } -} 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 index 41a7e11fd..1ed3ae6b2 100644 --- a/src/main/java/com/gmail/nossr50/events/skills/rupture/McMMOEntityDamageByRuptureEvent.java +++ b/src/main/java/com/gmail/nossr50/events/skills/rupture/McMMOEntityDamageByRuptureEvent.java @@ -1,20 +1,19 @@ -package com.gmail.nossr50.events.skills.rupture; - -import com.gmail.nossr50.datatypes.player.McMMOPlayer; -import org.bukkit.entity.Entity; -import org.bukkit.event.entity.EntityDamageByEntityEvent; -import org.jetbrains.annotations.NotNull; - -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, damage); - this.mcMMODamager = damager; - } - - @NotNull - public McMMOPlayer getMcMMODamager() { - return mcMMODamager; - } -} +//package com.gmail.nossr50.events.skills.rupture; +// +//import com.gmail.nossr50.datatypes.player.McMMOPlayer; +//import org.bukkit.entity.Entity; +//import org.bukkit.event.entity.EntityDamageByEntityEvent; +//import org.jetbrains.annotations.NotNull; +// +//public class McMMOEntityDamageByRuptureEvent extends EntityDamageByEntityEvent { +// private final McMMOPlayer mcMMODamager; +// +// public McMMOEntityDamageByRuptureEvent(@NotNull McMMOPlayer damager, @NotNull Entity damagee, double damage) { +// 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 34eaa2113..e3ac46c5c 100644 --- a/src/main/java/com/gmail/nossr50/listeners/EntityListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/EntityListener.java @@ -5,10 +5,7 @@ import com.gmail.nossr50.config.experience.ExperienceConfig; import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.skills.SubSkillType; 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.metadata.MobMetaFlagType; import com.gmail.nossr50.metadata.MobMetadataService; @@ -286,8 +283,10 @@ public class EntityListener implements Listener { */ @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onEntityDamageByEntity(EntityDamageByEntityEvent event) { - if (event instanceof FakeEntityDamageByEntityEvent || event instanceof McMMOEntityDamageByRuptureEvent) { - return; + if (event.getEntity() instanceof LivingEntity livingEntity) { + if (CombatUtils.hasIgnoreDamageMetadata(livingEntity)) { + return; + } } double damage = event.getFinalDamage(); @@ -447,14 +446,6 @@ public class EntityListener implements Listener { player.sendMessage("Final damage: " + entityDamageEvent.getFinalDamage()); - if(entityDamageEvent instanceof FakeEntityDamageByEntityEvent) { - player.sendMessage("This report is for a fake damage event used by mcMMO to test a players permission to hurt another"); - } - - if(entityDamageEvent instanceof McMMOEntityDamageByRuptureEvent) { - player.sendMessage("This report is for a Rupture damage event, which is sent out by mcMMO"); - } - if(entityDamageEvent.isCancelled()) { player.sendMessage("Event was cancelled, which means no damage should be done."); } @@ -481,14 +472,6 @@ public class EntityListener implements Listener { player.sendMessage("Target players max health: "+otherPlayer.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()); player.sendMessage("Target players current health: "+otherPlayer.getHealth()); - if(entityDamageEvent instanceof FakeEntityDamageByEntityEvent) { - player.sendMessage("This report is for a fake damage event used by mcMMO to test a players permission to hurt another"); - } - - if(entityDamageEvent instanceof McMMOEntityDamageByRuptureEvent) { - player.sendMessage("This report is for a Rupture damage event, which is sent out by mcMMO"); - } - if(entityDamageEvent.isCancelled()) { player.sendMessage("Event was cancelled, which means no damage should be done."); } @@ -558,8 +541,10 @@ public class EntityListener implements Listener { * Old code */ - if (event instanceof FakeEntityDamageEvent) { - return; + if (event.getEntity() instanceof LivingEntity livingEntity) { + if (CombatUtils.hasIgnoreDamageMetadata(livingEntity)) { + return; + } } double damage = event.getFinalDamage(); 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 32ecf72b2..fe7bf9f11 100644 --- a/src/main/java/com/gmail/nossr50/runnables/skills/RuptureTask.java +++ b/src/main/java/com/gmail/nossr50/runnables/skills/RuptureTask.java @@ -1,7 +1,6 @@ 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.CancellableRunnable; import com.gmail.nossr50.util.MetadataConstants; @@ -83,18 +82,14 @@ public class RuptureTask extends CancellableRunnable { //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); + // TODO: Needs updating for new spigot API + double damage = calculateAdjustedTickDamage(); //Use raw damage for Rupture - //Ensure the event wasn't cancelled and damage is still greater than 0 - double damage = event.getDamage(); //Use raw damage for Rupture - - if (event.isCancelled() || damage <= 0 || healthBeforeRuptureIsApplied - damage <= 0) + if (damage <= 0 || healthBeforeRuptureIsApplied - damage <= 0) { return true; + } double damagedHealth = healthBeforeRuptureIsApplied - damage; - targetEntity.setHealth(damagedHealth); //Hurt entity without the unwanted side effects of damage()} } diff --git a/src/main/java/com/gmail/nossr50/util/EventUtils.java b/src/main/java/com/gmail/nossr50/util/EventUtils.java index 3e5035b40..c28467e65 100644 --- a/src/main/java/com/gmail/nossr50/util/EventUtils.java +++ b/src/main/java/com/gmail/nossr50/util/EventUtils.java @@ -15,7 +15,10 @@ import com.gmail.nossr50.events.experience.McMMOPlayerLevelChangeEvent; import com.gmail.nossr50.events.experience.McMMOPlayerLevelDownEvent; import com.gmail.nossr50.events.experience.McMMOPlayerLevelUpEvent; import com.gmail.nossr50.events.experience.McMMOPlayerXpGainEvent; -import com.gmail.nossr50.events.fake.*; +import com.gmail.nossr50.events.fake.FakeBlockBreakEvent; +import com.gmail.nossr50.events.fake.FakeBlockDamageEvent; +import com.gmail.nossr50.events.fake.FakeEvent; +import com.gmail.nossr50.events.fake.FakePlayerFishEvent; import com.gmail.nossr50.events.hardcore.McMMOPlayerPreDeathPenaltyEvent; import com.gmail.nossr50.events.hardcore.McMMOPlayerStatLossEvent; import com.gmail.nossr50.events.hardcore.McMMOPlayerVampirismEvent; @@ -83,17 +86,6 @@ public final class EventUtils { return event instanceof FakeEvent; } - /** - * Checks to see if damage is from natural sources - * This cannot be used to determine if damage is from vanilla MC, it just checks to see if the damage is from a complex behaviour in mcMMO such as bleed. - * - * @param event this event - * @return true if damage is NOT from an unnatural mcMMO skill (such as bleed DOTs) - */ - public static boolean isDamageFromMcMMOComplexBehaviour(@NotNull Event event) { - return event instanceof FakeEntityDamageEvent; - } - /** * This little method is just to make the code more readable * diff --git a/src/main/java/com/gmail/nossr50/util/skills/CombatUtils.java b/src/main/java/com/gmail/nossr50/util/skills/CombatUtils.java index 5f4c51ac5..5f060f582 100644 --- a/src/main/java/com/gmail/nossr50/util/skills/CombatUtils.java +++ b/src/main/java/com/gmail/nossr50/util/skills/CombatUtils.java @@ -7,8 +7,6 @@ import com.gmail.nossr50.datatypes.meta.OldName; import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.skills.PrimarySkillType; import com.gmail.nossr50.datatypes.skills.SubSkillType; -import com.gmail.nossr50.events.fake.FakeEntityDamageByEntityEvent; -import com.gmail.nossr50.events.fake.FakeEntityDamageEvent; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.metadata.MobMetaFlagType; import com.gmail.nossr50.metadata.MobMetadataService; @@ -29,7 +27,6 @@ import org.bukkit.attribute.Attribute; import org.bukkit.attribute.AttributeInstance; import org.bukkit.entity.*; import org.bukkit.event.entity.EntityDamageByEntityEvent; -import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityDamageEvent.DamageCause; import org.bukkit.inventory.ItemStack; import org.bukkit.metadata.MetadataValue; @@ -527,7 +524,6 @@ public final class CombatUtils { * @param target LivingEntity which to attempt to damage * @param damage Amount of damage to attempt to do */ - @Deprecated public static void dealDamage(@NotNull LivingEntity target, double damage) { dealDamage(target, damage, DamageCause.CUSTOM, null); } @@ -572,10 +568,15 @@ public final class CombatUtils { if (target.isDead()) { return; } - - if(canDamage(attacker, target, cause, damage)) { + try { + // TODO: Check for Spigot API for DamageSource, if DamageSource is found then send out a damage() with a custom damage source applyIgnoreDamageMetadata(target); - target.damage(damage); + if (attacker != null) { + target.damage(damage, attacker); + } else { + target.damage(damage); + } + } finally { removeIgnoreDamageMetadata(target); } } @@ -885,19 +886,6 @@ public final class CombatUtils { return false; } - public static boolean canDamage(@Nullable Entity attacker, @NotNull Entity target, @NotNull DamageCause damageCause, double damage) { - EntityDamageEvent damageEvent; - if (attacker != null) { - damageEvent = new FakeEntityDamageByEntityEvent(attacker, target, damageCause, damage); - } else { - damageEvent = new FakeEntityDamageEvent(target, damageCause, damage); - } - - mcMMO.p.getServer().getPluginManager().callEvent(damageEvent); - - return !damageEvent.isCancelled(); - } - /** * Get the upgrade tier of the item in hand. * From 85f27fdbdb9547d337306107e56eee25f8970b21 Mon Sep 17 00:00:00 2001 From: nossr50 Date: Wed, 14 Feb 2024 13:11:32 -0800 Subject: [PATCH 2/2] 2.1.228 --- Changelog.txt | 10 ++- pom.xml | 2 +- .../McMMOEntityDamageByRuptureEvent.java | 76 ++++++++++++++----- .../nossr50/runnables/skills/RuptureTask.java | 17 ++++- .../gmail/nossr50/util/MobHealthbarUtils.java | 2 +- 5 files changed, 79 insertions(+), 28 deletions(-) diff --git a/Changelog.txt b/Changelog.txt index 0c35d8bfb..ef92067b1 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -1,7 +1,11 @@ Version 2.1.228 - Fixed stack trace due to Spigot API break on EntityDamageEvent and EntityDamageByEntity event - (API) - Removed FakeEntityDamageEvent and FakeEntityDamageByEntityEvent, you can use our metadata constant to check if damage is from mcMMO instead - (API) - Temporarily removed McMMOEntityDamageByRuptureEvent, this needs to be reworked for the new spigot API changes + Fixed a stack trace due to Spigot API break on EntityDamageEvent and EntityDamageByEntity event + (API) - Removed FakeEntityDamageEvent and FakeEntityDamageByEntityEvent + (API) - Reworked McMMOEntityDamageByRuptureEvent, this no longer extends EntityDamageByEntityEvent, instead it extends EntityEvent + + Notes: + Some care went into keeping mcMMO compatible with older versions despite a big API break from Spigot, this may introduce some new bugs, so please report them if you run into any. + Version 2.1.227 Fixed bug where some text would not be displayed to players (Adventure dependency update) Repair sound effect from mcMMO can now be adjusted in Minecraft sound options (Thanks MithicSpirit) diff --git a/pom.xml b/pom.xml index 49d4e7db8..e65b832e7 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 com.gmail.nossr50.mcMMO mcMMO - 2.1.228-SNAPSHOT + 2.1.228 mcMMO https://github.com/mcMMO-Dev/mcMMO 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 index 1ed3ae6b2..6612af381 100644 --- a/src/main/java/com/gmail/nossr50/events/skills/rupture/McMMOEntityDamageByRuptureEvent.java +++ b/src/main/java/com/gmail/nossr50/events/skills/rupture/McMMOEntityDamageByRuptureEvent.java @@ -1,19 +1,57 @@ -//package com.gmail.nossr50.events.skills.rupture; -// -//import com.gmail.nossr50.datatypes.player.McMMOPlayer; -//import org.bukkit.entity.Entity; -//import org.bukkit.event.entity.EntityDamageByEntityEvent; -//import org.jetbrains.annotations.NotNull; -// -//public class McMMOEntityDamageByRuptureEvent extends EntityDamageByEntityEvent { -// private final McMMOPlayer mcMMODamager; -// -// public McMMOEntityDamageByRuptureEvent(@NotNull McMMOPlayer damager, @NotNull Entity damagee, double damage) { -// this.mcMMODamager = damager; -// } -// -// @NotNull -// public McMMOPlayer getMcMMODamager() { -// return mcMMODamager; -// } -//} +package com.gmail.nossr50.events.skills.rupture; + +import com.gmail.nossr50.datatypes.player.McMMOPlayer; +import org.bukkit.entity.Entity; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; +import org.bukkit.event.entity.EntityEvent; +import org.jetbrains.annotations.NotNull; + +public class McMMOEntityDamageByRuptureEvent extends EntityEvent implements Cancellable { + private final McMMOPlayer damager; + private final Entity damagee; + private double damage; + private boolean isCancelled = false; + private static final HandlerList handlers = new HandlerList(); + + public McMMOEntityDamageByRuptureEvent(@NotNull McMMOPlayer damager, @NotNull Entity damagee, double damage) { + super(damagee); + this.damager = damager; + this.damagee = damagee; + this.damage = damage; + } + + @NotNull + @Deprecated + public McMMOPlayer getMcMMODamager() { + return damager; + } + + public McMMOPlayer getDamager() { + return damager; + } + + public double getDamage() { + return damage; + } + + public void setDamage(double damage) { + this.damage = Math.max(0, damage); + } + + @Override + public boolean isCancelled() { + return isCancelled; + } + + @Override + public void setCancelled(boolean cancel) { + isCancelled = cancel; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return handlers; + } +} 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 fe7bf9f11..f2e9c608f 100644 --- a/src/main/java/com/gmail/nossr50/runnables/skills/RuptureTask.java +++ b/src/main/java/com/gmail/nossr50/runnables/skills/RuptureTask.java @@ -1,9 +1,12 @@ package com.gmail.nossr50.runnables.skills; +import com.gmail.nossr50.datatypes.MobHealthbarType; 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.CancellableRunnable; import com.gmail.nossr50.util.MetadataConstants; +import com.gmail.nossr50.util.MobHealthbarUtils; import com.gmail.nossr50.util.skills.ParticleEffectUtils; import com.google.common.base.Objects; import org.bukkit.entity.LivingEntity; @@ -82,15 +85,21 @@ public class RuptureTask extends CancellableRunnable { //Ensure victim has health if (healthBeforeRuptureIsApplied > 0.01) { - // TODO: Needs updating for new spigot API - double damage = calculateAdjustedTickDamage(); //Use raw damage for Rupture + //Send a fake damage event + McMMOEntityDamageByRuptureEvent event = + new McMMOEntityDamageByRuptureEvent(ruptureSource, targetEntity, calculateAdjustedTickDamage()); + mcMMO.p.getServer().getPluginManager().callEvent(event); - if (damage <= 0 || healthBeforeRuptureIsApplied - damage <= 0) { + //Ensure the event wasn't cancelled 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; + targetEntity.setHealth(damagedHealth); //Hurt entity without the unwanted side effects of damage()} + MobHealthbarUtils.handleMobHealthbars(targetEntity, damage, mcMMO.p); } return false; diff --git a/src/main/java/com/gmail/nossr50/util/MobHealthbarUtils.java b/src/main/java/com/gmail/nossr50/util/MobHealthbarUtils.java index d4a660ca1..3d956afe7 100644 --- a/src/main/java/com/gmail/nossr50/util/MobHealthbarUtils.java +++ b/src/main/java/com/gmail/nossr50/util/MobHealthbarUtils.java @@ -39,7 +39,7 @@ public final class MobHealthbarUtils { return; } - if (isBoss(target)) { + if (isBoss(target) || target instanceof Player) { return; }