From 54a535d28b87b2d00b55dbfc8a00ae6e517e2e72 Mon Sep 17 00:00:00 2001 From: clcment446 <125682659+clcment446@users.noreply.github.com> Date: Thu, 10 Oct 2024 21:27:21 +0200 Subject: [PATCH] Added events to Summoning and Counterspell (#609) * ISS CreatureSummonedEvent Added the "PlayerSummonsCreature" event, used in summoning spells. Added the "CounterSpellEvent" event, used whenever someone uses counterspell. * Fix unsafe Casting in caused by summoning event - PlayerSummonEvents -> SpellSummonEvent - SpellSummonEvents now works with a generic to avoid potential class casting issues. --- .../api/events/CounterSpellEvent.java | 15 ++++++ .../api/events/SpellSummonEvent.java | 39 ++++++++++++++ .../spells/blood/RaiseDeadSpell.java | 5 +- .../spells/ender/CounterspellSpell.java | 54 ++++++++++--------- .../spells/evocation/SummonHorseSpell.java | 6 ++- .../spells/evocation/SummonVexSpell.java | 5 +- .../spells/ice/SummonPolarBearSpell.java | 6 ++- 7 files changed, 100 insertions(+), 30 deletions(-) create mode 100644 src/main/java/io/redspace/ironsspellbooks/api/events/CounterSpellEvent.java create mode 100644 src/main/java/io/redspace/ironsspellbooks/api/events/SpellSummonEvent.java diff --git a/src/main/java/io/redspace/ironsspellbooks/api/events/CounterSpellEvent.java b/src/main/java/io/redspace/ironsspellbooks/api/events/CounterSpellEvent.java new file mode 100644 index 000000000..1f73e448b --- /dev/null +++ b/src/main/java/io/redspace/ironsspellbooks/api/events/CounterSpellEvent.java @@ -0,0 +1,15 @@ +package io.redspace.ironsspellbooks.api.events; + +import net.minecraft.world.entity.Entity; +import net.neoforged.bus.api.Event; +import net.neoforged.bus.api.ICancellableEvent; + +public class CounterSpellEvent extends Event implements ICancellableEvent { + public final Entity caster; + public final Entity target; + + public CounterSpellEvent(Entity caster, Entity target){ + this.caster = caster; + this.target = target; + } +} diff --git a/src/main/java/io/redspace/ironsspellbooks/api/events/SpellSummonEvent.java b/src/main/java/io/redspace/ironsspellbooks/api/events/SpellSummonEvent.java new file mode 100644 index 000000000..e319e4a8e --- /dev/null +++ b/src/main/java/io/redspace/ironsspellbooks/api/events/SpellSummonEvent.java @@ -0,0 +1,39 @@ +package io.redspace.ironsspellbooks.api.events; + +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.entity.LivingEntity; +import net.neoforged.neoforge.event.entity.living.LivingEvent; + +public class SpellSummonEvent extends LivingEvent { + private LivingEntity caster = null; + private K creature = null; + private final ResourceLocation spellId; + private int spellLevel = 0; + public SpellSummonEvent(LivingEntity caster, K creature, ResourceLocation spellId, int spellLevel) { + super(caster); + this.caster = caster; + this.creature= creature; + this.spellId = spellId; + this.spellLevel = spellLevel; + } + + public K getCreature() { + return creature; + } + + public void setCreature(K creature) { + this.creature = creature; + } + + public LivingEntity getCaster() { + return caster; + } + + public ResourceLocation getSpellId() { + return spellId; + } + + public int getSpellLevel() { + return spellLevel; + } +} \ No newline at end of file diff --git a/src/main/java/io/redspace/ironsspellbooks/spells/blood/RaiseDeadSpell.java b/src/main/java/io/redspace/ironsspellbooks/spells/blood/RaiseDeadSpell.java index 8ef45bd3f..4a511d14d 100644 --- a/src/main/java/io/redspace/ironsspellbooks/spells/blood/RaiseDeadSpell.java +++ b/src/main/java/io/redspace/ironsspellbooks/spells/blood/RaiseDeadSpell.java @@ -2,6 +2,7 @@ import io.redspace.ironsspellbooks.IronsSpellbooks; import io.redspace.ironsspellbooks.api.config.DefaultConfig; +import io.redspace.ironsspellbooks.api.events.SpellSummonEvent; import io.redspace.ironsspellbooks.api.magic.MagicData; import io.redspace.ironsspellbooks.api.registry.SchoolRegistry; import io.redspace.ironsspellbooks.api.spells.*; @@ -28,6 +29,7 @@ import net.minecraft.world.item.Items; import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec3; +import net.neoforged.neoforge.common.NeoForge; import java.util.List; import java.util.Optional; @@ -98,7 +100,8 @@ public void onCast(Level world, int spellLevel, LivingEntity entity, CastSource undead.setPos(spawn.x, spawn.y, spawn.z); undead.setYRot(entity.getYRot()); undead.setOldPosAndRot(); - world.addFreshEntity(undead); + var event = NeoForge.EVENT_BUS.post(new SpellSummonEvent<>(entity, undead, this.spellId, spellLevel)); + world.addFreshEntity(event.getEntity()); } int effectAmplifier = spellLevel - 1; diff --git a/src/main/java/io/redspace/ironsspellbooks/spells/ender/CounterspellSpell.java b/src/main/java/io/redspace/ironsspellbooks/spells/ender/CounterspellSpell.java index 57d5979f8..fc0043995 100644 --- a/src/main/java/io/redspace/ironsspellbooks/spells/ender/CounterspellSpell.java +++ b/src/main/java/io/redspace/ironsspellbooks/spells/ender/CounterspellSpell.java @@ -3,6 +3,7 @@ import io.redspace.ironsspellbooks.IronsSpellbooks; import io.redspace.ironsspellbooks.api.config.DefaultConfig; import io.redspace.ironsspellbooks.api.entity.IMagicEntity; +import io.redspace.ironsspellbooks.api.events.CounterSpellEvent; import io.redspace.ironsspellbooks.api.magic.MagicData; import io.redspace.ironsspellbooks.api.registry.SchoolRegistry; import io.redspace.ironsspellbooks.api.spells.*; @@ -24,6 +25,7 @@ import net.minecraft.world.phys.EntityHitResult; import net.minecraft.world.phys.HitResult; import net.minecraft.world.phys.Vec3; +import net.neoforged.neoforge.common.NeoForge; @AutoSpellConfig public class CounterspellSpell extends AbstractSpell { @@ -74,38 +76,42 @@ public void onCast(Level world, int spellLevel, LivingEntity entity, CastSource } //if (entityHitResult.getEntity() instanceof AntiMagicSusceptible antiMagicSusceptible && !(antiMagicSusceptible instanceof MagicSummon summon && summon.getSummoner() == entity)) { - if (hitEntity instanceof AntiMagicSusceptible antiMagicSusceptible) { - if (antiMagicSusceptible instanceof IMagicSummon summon) { - if (summon.getSummoner() == entity) { - if (summon instanceof Mob mob && mob.getTarget() == null) { + if (!(NeoForge.EVENT_BUS.post(new CounterSpellEvent(entity, hitEntity)).isCanceled())) { + if (hitEntity instanceof AntiMagicSusceptible antiMagicSusceptible) { + if (antiMagicSusceptible instanceof IMagicSummon summon) { + if (summon.getSummoner() == entity) { + if (summon instanceof Mob mob && mob.getTarget() == null) { + antiMagicSusceptible.onAntiMagic(playerMagicData); + } + } else { antiMagicSusceptible.onAntiMagic(playerMagicData); } + + } else { antiMagicSusceptible.onAntiMagic(playerMagicData); } - } else { - antiMagicSusceptible.onAntiMagic(playerMagicData); + } else if (hitEntity instanceof ServerPlayer serverPlayer) { + Utils.serverSideCancelCast(serverPlayer, true); + MagicData.getPlayerMagicData(serverPlayer).getPlayerRecasts().removeAll(RecastResult.COUNTERSPELL); + } else if (hitEntity instanceof IMagicEntity abstractSpellCastingMob) { + abstractSpellCastingMob.cancelCast(); } - } else if (hitEntity instanceof ServerPlayer serverPlayer) { - Utils.serverSideCancelCast(serverPlayer, true); - MagicData.getPlayerMagicData(serverPlayer).getPlayerRecasts().removeAll(RecastResult.COUNTERSPELL); - } else if (hitEntity instanceof IMagicEntity abstractSpellCastingMob) { - abstractSpellCastingMob.cancelCast(); - } - if (hitEntity instanceof LivingEntity livingEntity) { - //toList to avoid concurrent modification - for (Holder mobEffect : livingEntity.getActiveEffectsMap().keySet().stream().toList()) { - if (mobEffect.value() instanceof MagicMobEffect) { - livingEntity.removeEffect(mobEffect); + if (hitEntity instanceof LivingEntity livingEntity) { + //toList to avoid concurrent modification + for (Holder mobEffect : livingEntity.getActiveEffectsMap().keySet().stream().toList()) { + if (mobEffect.value() instanceof MagicMobEffect) { + livingEntity.removeEffect(mobEffect); + } } } - } - } else { - for (float i = 1; i < 40; i += .5f) { - Vec3 pos = entity.getEyePosition().add(forward.scale(i)); - MagicManager.spawnParticles(world, ParticleTypes.ENCHANT, pos.x, pos.y, pos.z, 1, 0, 0, 0, 0, false); - if (!world.getBlockState(BlockPos.containing(pos)).isAir()) { - break; + } else { + for (float i = 1; i < 40; i += .5f) { + Vec3 pos = entity.getEyePosition().add(forward.scale(i)); + MagicManager.spawnParticles(world, ParticleTypes.ENCHANT, pos.x, pos.y, pos.z, 1, 0, 0, 0, 0, false); + if (!world.getBlockState(BlockPos.containing(pos)).isAir()) { + break; + } } } } diff --git a/src/main/java/io/redspace/ironsspellbooks/spells/evocation/SummonHorseSpell.java b/src/main/java/io/redspace/ironsspellbooks/spells/evocation/SummonHorseSpell.java index 25c93f1c1..e2d36495e 100644 --- a/src/main/java/io/redspace/ironsspellbooks/spells/evocation/SummonHorseSpell.java +++ b/src/main/java/io/redspace/ironsspellbooks/spells/evocation/SummonHorseSpell.java @@ -2,6 +2,7 @@ import io.redspace.ironsspellbooks.IronsSpellbooks; import io.redspace.ironsspellbooks.api.config.DefaultConfig; +import io.redspace.ironsspellbooks.api.events.SpellSummonEvent; import io.redspace.ironsspellbooks.api.magic.MagicData; import io.redspace.ironsspellbooks.api.registry.SchoolRegistry; import io.redspace.ironsspellbooks.api.spells.*; @@ -18,6 +19,7 @@ import net.minecraft.world.entity.animal.horse.AbstractHorse; import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec3; +import net.neoforged.neoforge.common.NeoForge; import java.util.Optional; @@ -80,8 +82,8 @@ public void onCast(Level world, int spellLevel, LivingEntity entity, CastSource horse.removeEffectNoUpdate(MobEffectRegistry.SUMMON_HORSE_TIMER); horse.forceAddEffect(new MobEffectInstance(MobEffectRegistry.SUMMON_HORSE_TIMER, summonTime, 0, false, false, false), null); setAttributes(horse, getSpellPower(spellLevel, entity)); - - world.addFreshEntity(horse); + var event = NeoForge.EVENT_BUS.post(new SpellSummonEvent(entity, horse, this.spellId, spellLevel)); + world.addFreshEntity(event.getCreature()); entity.addEffect(new MobEffectInstance(MobEffectRegistry.SUMMON_HORSE_TIMER, summonTime, 0, false, false, true)); super.onCast(world, spellLevel, entity, castSource, playerMagicData); diff --git a/src/main/java/io/redspace/ironsspellbooks/spells/evocation/SummonVexSpell.java b/src/main/java/io/redspace/ironsspellbooks/spells/evocation/SummonVexSpell.java index e46676242..a662fb0cd 100644 --- a/src/main/java/io/redspace/ironsspellbooks/spells/evocation/SummonVexSpell.java +++ b/src/main/java/io/redspace/ironsspellbooks/spells/evocation/SummonVexSpell.java @@ -2,6 +2,7 @@ import io.redspace.ironsspellbooks.IronsSpellbooks; import io.redspace.ironsspellbooks.api.config.DefaultConfig; +import io.redspace.ironsspellbooks.api.events.SpellSummonEvent; import io.redspace.ironsspellbooks.api.magic.MagicData; import io.redspace.ironsspellbooks.api.registry.SchoolRegistry; import io.redspace.ironsspellbooks.api.spells.*; @@ -19,6 +20,7 @@ import net.minecraft.world.entity.MobSpawnType; import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec3; +import net.neoforged.neoforge.common.NeoForge; import java.util.List; import java.util.Optional; @@ -83,7 +85,8 @@ public void onCast(Level world, int spellLevel, LivingEntity entity, CastSource vex.moveTo(entity.getEyePosition().add(new Vec3(Utils.getRandomScaled(2), 1, Utils.getRandomScaled(2)))); vex.finalizeSpawn((ServerLevel) world, world.getCurrentDifficultyAt(vex.getOnPos()), MobSpawnType.MOB_SUMMONED, null); vex.addEffect(new MobEffectInstance(MobEffectRegistry.VEX_TIMER, summonTime, 0, false, false, false)); - world.addFreshEntity(vex); + var event = NeoForge.EVENT_BUS.post(new SpellSummonEvent(entity, vex, this.spellId, spellLevel)); + world.addFreshEntity(event.getCreature()); } int effectAmplifier = spellLevel - 1; if (entity.hasEffect(MobEffectRegistry.VEX_TIMER)) diff --git a/src/main/java/io/redspace/ironsspellbooks/spells/ice/SummonPolarBearSpell.java b/src/main/java/io/redspace/ironsspellbooks/spells/ice/SummonPolarBearSpell.java index d18012455..b11e9ca6e 100644 --- a/src/main/java/io/redspace/ironsspellbooks/spells/ice/SummonPolarBearSpell.java +++ b/src/main/java/io/redspace/ironsspellbooks/spells/ice/SummonPolarBearSpell.java @@ -2,6 +2,7 @@ import io.redspace.ironsspellbooks.IronsSpellbooks; import io.redspace.ironsspellbooks.api.config.DefaultConfig; +import io.redspace.ironsspellbooks.api.events.SpellSummonEvent; import io.redspace.ironsspellbooks.api.magic.MagicData; import io.redspace.ironsspellbooks.api.registry.SchoolRegistry; import io.redspace.ironsspellbooks.api.spells.*; @@ -16,6 +17,7 @@ import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.level.Level; +import net.neoforged.neoforge.common.NeoForge; import java.util.List; import java.util.Optional; @@ -77,8 +79,8 @@ public void onCast(Level world, int spellLevel, LivingEntity entity, CastSource polarBear.getAttributes().getInstance(Attributes.ATTACK_DAMAGE).setBaseValue(getBearDamage(spellLevel, entity)); polarBear.getAttributes().getInstance(Attributes.MAX_HEALTH).setBaseValue(getBearHealth(spellLevel, entity)); polarBear.setHealth(polarBear.getMaxHealth()); - - world.addFreshEntity(polarBear); + var event = NeoForge.EVENT_BUS.post(new SpellSummonEvent(entity, polarBear, this.spellId, spellLevel)); + world.addFreshEntity(event.getCreature()); polarBear.addEffect(new MobEffectInstance(MobEffectRegistry.POLAR_BEAR_TIMER, summonTime, 0, false, false, false)); int effectAmplifier = 0;