Skip to content

Commit

Permalink
Added events to Summoning and Counterspell (#609)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
clcment446 authored Oct 10, 2024
1 parent 7eb8dbc commit 54a535d
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<K extends LivingEntity> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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 {
Expand Down Expand Up @@ -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> 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> 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;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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;

Expand Down Expand Up @@ -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<SummonedHorse>(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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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;
Expand Down Expand Up @@ -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<SummonedVex>(entity, vex, this.spellId, spellLevel));
world.addFreshEntity(event.getCreature());
}
int effectAmplifier = spellLevel - 1;
if (entity.hasEffect(MobEffectRegistry.VEX_TIMER))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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;
Expand Down Expand Up @@ -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<SummonedPolarBear>(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;
Expand Down

0 comments on commit 54a535d

Please sign in to comment.