From 22919b5f566e7ea3c45834e4a0dfdc70b92b2ad8 Mon Sep 17 00:00:00 2001 From: "yrsegal@gmail.com" Date: Sat, 30 Apr 2022 23:18:07 -0400 Subject: [PATCH] brainsweeping fail now attributes the damage to the caster this is to prevent Murder Villager from being a cheap and easy disposal solution for all your pesky mindflayed problems --- .../misc/DamageSourceOvercast.java | 2 +- .../api/misc/EntityDamageSourceOvercast.java | 13 +++++++++++++ .../hexcasting/api/misc/HexDamageSources.java | 6 +++++- .../api/spell/casting/CastingHarness.kt | 2 +- .../hexcasting/api/spell/mishaps/Mishap.kt | 16 ++++++++++++++++ .../api/spell/mishaps/MishapAlreadyBrainswept.kt | 2 +- .../api/spell/mishaps/MishapBadBrainsweep.kt | 2 +- .../api/spell/mishaps/MishapDivideByZero.kt | 2 +- .../mixin/accessor/AccessorLivingEntity.java | 14 ++++++++++++++ Common/src/main/resources/hexplat.mixins.json | 5 ++++- 10 files changed, 57 insertions(+), 7 deletions(-) rename Common/src/main/java/at/petrak/hexcasting/{common => api}/misc/DamageSourceOvercast.java (86%) create mode 100644 Common/src/main/java/at/petrak/hexcasting/api/misc/EntityDamageSourceOvercast.java create mode 100644 Common/src/main/java/at/petrak/hexcasting/mixin/accessor/AccessorLivingEntity.java diff --git a/Common/src/main/java/at/petrak/hexcasting/common/misc/DamageSourceOvercast.java b/Common/src/main/java/at/petrak/hexcasting/api/misc/DamageSourceOvercast.java similarity index 86% rename from Common/src/main/java/at/petrak/hexcasting/common/misc/DamageSourceOvercast.java rename to Common/src/main/java/at/petrak/hexcasting/api/misc/DamageSourceOvercast.java index 70c1232d..9cd89eea 100644 --- a/Common/src/main/java/at/petrak/hexcasting/common/misc/DamageSourceOvercast.java +++ b/Common/src/main/java/at/petrak/hexcasting/api/misc/DamageSourceOvercast.java @@ -1,4 +1,4 @@ -package at.petrak.hexcasting.common.misc; +package at.petrak.hexcasting.api.misc; import net.minecraft.world.damagesource.DamageSource; diff --git a/Common/src/main/java/at/petrak/hexcasting/api/misc/EntityDamageSourceOvercast.java b/Common/src/main/java/at/petrak/hexcasting/api/misc/EntityDamageSourceOvercast.java new file mode 100644 index 00000000..659ccbff --- /dev/null +++ b/Common/src/main/java/at/petrak/hexcasting/api/misc/EntityDamageSourceOvercast.java @@ -0,0 +1,13 @@ +package at.petrak.hexcasting.api.misc; + +import net.minecraft.world.damagesource.EntityDamageSource; +import net.minecraft.world.entity.Entity; + +public class EntityDamageSourceOvercast extends EntityDamageSource { + public EntityDamageSourceOvercast(Entity entity) { + super("hexcasting.overcast", entity); + this.bypassArmor(); + this.bypassMagic(); + this.setMagic(); + } +} diff --git a/Common/src/main/java/at/petrak/hexcasting/api/misc/HexDamageSources.java b/Common/src/main/java/at/petrak/hexcasting/api/misc/HexDamageSources.java index 59043f16..a28c27f8 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/misc/HexDamageSources.java +++ b/Common/src/main/java/at/petrak/hexcasting/api/misc/HexDamageSources.java @@ -1,8 +1,12 @@ package at.petrak.hexcasting.api.misc; -import at.petrak.hexcasting.common.misc.DamageSourceOvercast; import net.minecraft.world.damagesource.DamageSource; +import net.minecraft.world.entity.Entity; public final class HexDamageSources { public static final DamageSource OVERCAST = new DamageSourceOvercast(); + + public static DamageSource overcastDamageFrom(Entity cause) { + return new EntityDamageSourceOvercast(cause); + } } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/casting/CastingHarness.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/casting/CastingHarness.kt index 58b33626..217e3436 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/casting/CastingHarness.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/casting/CastingHarness.kt @@ -306,7 +306,7 @@ class CastingHarness private constructor( HexAdvancementTriggers.OVERCAST_TRIGGER.trigger(this.ctx.caster, manaToActuallyPayFor) this.ctx.caster.awardStat(HexStatistics.MANA_OVERCASTED, manaCost - costLeft) - this.ctx.caster.hurt(HexDamageSources.OVERCAST, healthtoRemove.toFloat()) + Mishap.trulyHurt(this.ctx.caster, HexDamageSources.OVERCAST, healthtoRemove.toFloat()) costLeft -= manaToActuallyPayFor } } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/Mishap.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/Mishap.kt index 63824eb7..885bdfa2 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/Mishap.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/Mishap.kt @@ -6,12 +6,15 @@ import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.api.spell.casting.CastingContext import at.petrak.hexcasting.api.spell.math.HexPattern import at.petrak.hexcasting.common.lib.HexItems +import at.petrak.hexcasting.mixin.accessor.AccessorLivingEntity import net.minecraft.ChatFormatting import net.minecraft.Util import net.minecraft.network.chat.Component import net.minecraft.network.chat.Style import net.minecraft.network.chat.TranslatableComponent import net.minecraft.resources.ResourceLocation +import net.minecraft.world.damagesource.DamageSource +import net.minecraft.world.entity.LivingEntity import net.minecraft.world.entity.item.ItemEntity import net.minecraft.world.item.DyeColor import net.minecraft.world.item.ItemStack @@ -61,4 +64,17 @@ sealed class Mishap : Throwable() { } data class Context(val pattern: HexPattern, val action: ResourceLocation?) + + companion object { + fun trulyHurt(entity: LivingEntity, source: DamageSource, amount: Float) { + if (entity.invulnerableTime > 10) { + val lastHurt = (entity as AccessorLivingEntity).`hex$getLastHurt`() + if (lastHurt < amount) + entity.invulnerableTime = 0 + else + entity.`hex$setLastHurt`(lastHurt - amount) + } + entity.hurt(source, amount) + } + } } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapAlreadyBrainswept.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapAlreadyBrainswept.kt index 1614f61d..2965f923 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapAlreadyBrainswept.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapAlreadyBrainswept.kt @@ -14,7 +14,7 @@ class MishapAlreadyBrainswept(val villager: Villager) : Mishap() { dyeColor(DyeColor.GREEN) override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList>) { - villager.hurt(HexDamageSources.OVERCAST, villager.health) + villager.hurt(HexDamageSources.overcastDamageFrom(ctx.caster), villager.health) } override fun particleSpray(ctx: CastingContext): ParticleSpray { diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapBadBrainsweep.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapBadBrainsweep.kt index d9a54bac..737884b2 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapBadBrainsweep.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapBadBrainsweep.kt @@ -16,7 +16,7 @@ class MishapBadBrainsweep(val villager: Villager, val pos: BlockPos) : Mishap() dyeColor(DyeColor.GREEN) override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList>) { - villager.hurt(HexDamageSources.OVERCAST, villager.health) + trulyHurt(villager, HexDamageSources.overcastDamageFrom(ctx.caster), villager.health) } override fun particleSpray(ctx: CastingContext): ParticleSpray { diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapDivideByZero.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapDivideByZero.kt index 4d17e021..6d502581 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapDivideByZero.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapDivideByZero.kt @@ -17,7 +17,7 @@ class MishapDivideByZero(val operand1: Component, val operand2: Component, val s override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList>) { stack.add(SpellDatum.make(Widget.GARBAGE)) - ctx.caster.hurt(HexDamageSources.OVERCAST, ctx.caster.health / 2) + trulyHurt(ctx.caster, HexDamageSources.OVERCAST, ctx.caster.health / 2) } override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component { diff --git a/Common/src/main/java/at/petrak/hexcasting/mixin/accessor/AccessorLivingEntity.java b/Common/src/main/java/at/petrak/hexcasting/mixin/accessor/AccessorLivingEntity.java new file mode 100644 index 00000000..21608dea --- /dev/null +++ b/Common/src/main/java/at/petrak/hexcasting/mixin/accessor/AccessorLivingEntity.java @@ -0,0 +1,14 @@ +package at.petrak.hexcasting.mixin.accessor; + +import net.minecraft.world.entity.LivingEntity; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(LivingEntity.class) +public interface AccessorLivingEntity { + @Accessor("lastHurt") + float hex$getLastHurt(); + + @Accessor("lastHurt") + void hex$setLastHurt(float lastHurt); +} diff --git a/Common/src/main/resources/hexplat.mixins.json b/Common/src/main/resources/hexplat.mixins.json index 2c480007..04165a24 100644 --- a/Common/src/main/resources/hexplat.mixins.json +++ b/Common/src/main/resources/hexplat.mixins.json @@ -5,7 +5,10 @@ "refmap": "hexcasting.mixins.refmap.json", "package": "at.petrak.hexcasting.mixin", "mixins": [ - "MixinMob", "MixinVillager", "accessor.AccessorUseOnContext", + "MixinMob", + "MixinVillager", + "accessor.AccessorLivingEntity", + "accessor.AccessorUseOnContext", "accessor.CriteriaTriggersAccessor" ] }