From b76374fb58d1ebe626beb046c2252f4773b54460 Mon Sep 17 00:00:00 2001 From: Electroblob77 <35599699+Electroblob77@users.noreply.github.com> Date: Tue, 30 Jun 2020 14:06:27 +0100 Subject: [PATCH] Make lightning bolt count as player damage and support damage modifiers --- .../wizardry/spell/LightningBolt.java | 91 ++++++++++++------- .../ebwizardry/spells/lightning_bolt.json | 3 +- 2 files changed, 62 insertions(+), 32 deletions(-) diff --git a/src/main/java/electroblob/wizardry/spell/LightningBolt.java b/src/main/java/electroblob/wizardry/spell/LightningBolt.java index 24b45aba..79610e61 100644 --- a/src/main/java/electroblob/wizardry/spell/LightningBolt.java +++ b/src/main/java/electroblob/wizardry/spell/LightningBolt.java @@ -2,26 +2,40 @@ package electroblob.wizardry.spell; import electroblob.wizardry.Wizardry; import electroblob.wizardry.item.SpellActions; +import electroblob.wizardry.registry.Spells; +import electroblob.wizardry.util.EntityUtils; +import electroblob.wizardry.util.MagicDamage; +import electroblob.wizardry.util.MagicDamage.DamageType; import electroblob.wizardry.util.SpellModifiers; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.effect.EntityLightningBolt; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.DamageSource; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; +import net.minecraftforge.event.entity.EntityStruckByLightningEvent; +import net.minecraftforge.event.entity.living.LivingAttackEvent; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.common.eventhandler.EventPriority; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; -//@Mod.EventBusSubscriber +@Mod.EventBusSubscriber public class LightningBolt extends SpellRay { - /** The NBT key used to store the UUID of the player that summoned the lightning bolt. Used for achievements. */ - public static final String NBT_KEY = "summoningPlayer"; + /** The NBT key used to store the UUID of the entity that summoned the lightning bolt. */ + public static final String SUMMONER_NBT_KEY = "summoner"; + /** The NBT key used to store the damage modifier for the lightning bolt. */ + public static final String DAMAGE_MODIFIER_NBT_KEY = "damageModifier"; + /** An NBT key used to cancel vanilla's implementation of lightning damage; since there's no way of extracting the + * lightning bolt entity from {@code LivingAttackEvent} this has to be done using NBT. */ + public static final String IMMUNE_TO_LIGHTNING_NBT_KEY = "immuneToLightning"; public LightningBolt(){ super("lightning_bolt", SpellActions.POINT, false); this.ignoreLivingEntities(true); + addProperties(DAMAGE); } @Override public boolean requiresPacket(){ return false; } @@ -41,17 +55,14 @@ public class LightningBolt extends SpellRay { // Bit of a hack but it works fine! boolean doFireTick = world.getGameRules().getBoolean("doFireTick"); if(doFireTick && !Wizardry.settings.playerBlockDamage) world.getGameRules().setOrCreateGameRule("doFireTick", "false"); - EntityLightningBolt entitylightning = new EntityLightningBolt(world, pos.getX(), pos.getY(), - pos.getZ(), false); - world.addWeatherEffect(entitylightning); + + EntityLightningBolt lightning = new EntityLightningBolt(world, pos.getX(), pos.getY(), pos.getZ(), false); + if(caster != null) lightning.getEntityData().setUniqueId(SUMMONER_NBT_KEY, caster.getUniqueID()); + lightning.getEntityData().setFloat(DAMAGE_MODIFIER_NBT_KEY, modifiers.get(SpellModifiers.POTENCY)); + world.addWeatherEffect(lightning); + // Reset doFireTick to true if it was true before if(doFireTick && !Wizardry.settings.playerBlockDamage) world.getGameRules().setOrCreateGameRule("doFireTick", "true"); - - // Code for eventhandler recognition for achievements - if(caster instanceof EntityPlayer){ - NBTTagCompound entityNBT = entitylightning.getEntityData(); - entityNBT.setUniqueId(NBT_KEY, caster.getUniqueID()); - } } return true; @@ -65,22 +76,40 @@ public class LightningBolt extends SpellRay { return false; } -// @SubscribeEvent -// public static void onEntityStruckByLightningEvent(EntityStruckByLightningEvent event){ -// -// if(event.getLightning().getEntityData() != null && event.getLightning().getEntityData().hasUniqueId(NBT_KEY)){ -// -// EntityPlayer player = (EntityPlayer)WizardryUtilities.getEntityByUUID(event.getLightning().world, -// event.getLightning().getEntityData().getUniqueId("summoningPlayer")); -// -// if(event.getEntity() instanceof EntityCreeper){ -// WizardryAdvancementTriggers.charge_creeper.triggerFor(player); -// } -// -// if(event.getEntity() instanceof EntityPig){ -// WizardryAdvancementTriggers.frankenstein.triggerFor(player); -// } -// } -// } + @SubscribeEvent(priority = EventPriority.LOW) + public static void onEntityStruckByLightningEvent(EntityStruckByLightningEvent event){ + + float damageModifier = event.getLightning().getEntityData().getFloat(DAMAGE_MODIFIER_NBT_KEY); + + Entity summoner = null; + + if(event.getLightning().getEntityData().hasUniqueId(SUMMONER_NBT_KEY)){ + + summoner = EntityUtils.getEntityByUUID(event.getLightning().world, + event.getLightning().getEntityData().getUniqueId(SUMMONER_NBT_KEY)); + + if(!(summoner instanceof EntityLivingBase)) summoner = null; + } + + if(damageModifier > 0 || summoner != null){ + + DamageSource source = summoner == null ? DamageSource.LIGHTNING_BOLT + : MagicDamage.causeIndirectMagicDamage(event.getLightning(), summoner, DamageType.SHOCK); + float damage = Spells.lightning_bolt.getProperty(DAMAGE).floatValue() * damageModifier; + + // Don't need DamageSafetyChecker here because this isn't an attack event + EntityUtils.attackEntityWithoutKnockback(event.getEntity(), source, damage); + event.getEntity().getEntityData().setBoolean(IMMUNE_TO_LIGHTNING_NBT_KEY, true); + } + } + + @SubscribeEvent(priority = EventPriority.HIGHEST) + public static void onLivingAttackEvent(LivingAttackEvent event){ + if(event.getEntity().getEntityData().hasKey(IMMUNE_TO_LIGHTNING_NBT_KEY) + && event.getSource() == DamageSource.LIGHTNING_BOLT){ + event.setCanceled(true); + event.getEntity().getEntityData().removeTag(IMMUNE_TO_LIGHTNING_NBT_KEY); + } + } } diff --git a/src/main/resources/assets/ebwizardry/spells/lightning_bolt.json b/src/main/resources/assets/ebwizardry/spells/lightning_bolt.json index 716e6bd9..2d906b6b 100644 --- a/src/main/resources/assets/ebwizardry/spells/lightning_bolt.json +++ b/src/main/resources/assets/ebwizardry/spells/lightning_bolt.json @@ -17,6 +17,7 @@ "chargeup": 10, "cooldown": 80, "base_properties": { - "range": 80 + "range": 80, + "damage": 5 } } \ No newline at end of file