From 91b8b827a696a81cedff8077a866207f06b009e5 Mon Sep 17 00:00:00 2001 From: Sollace Date: Tue, 13 Feb 2024 18:47:01 +0000 Subject: [PATCH] Damage from disguises is now passed through to their hosts --- .../unicopia/entity/Living.java | 22 ++++++++++++++++++ .../entity/behaviour/EndermanBehaviour.java | 10 ++++++++ .../entity/behaviour/EntityAppearance.java | 8 +++++-- .../unicopia/entity/behaviour/Guest.java | 9 ++++++++ .../entity/duck/LivingEntityDuck.java | 4 +++- .../unicopia/mixin/MixinLivingEntity.java | 23 +++++++++++++++++++ 6 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/entity/behaviour/Guest.java diff --git a/src/main/java/com/minelittlepony/unicopia/entity/Living.java b/src/main/java/com/minelittlepony/unicopia/entity/Living.java index f83c07d9..0615b9da 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/Living.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/Living.java @@ -18,6 +18,7 @@ import com.minelittlepony.unicopia.ability.magic.spell.Situation; import com.minelittlepony.unicopia.advancement.UCriteria; import com.minelittlepony.unicopia.compat.trinkets.TrinketsDelegate; import com.minelittlepony.unicopia.entity.behaviour.EntityAppearance; +import com.minelittlepony.unicopia.entity.behaviour.Guest; import com.minelittlepony.unicopia.entity.collision.MultiBoundingBoxEntity; import com.minelittlepony.unicopia.entity.damage.MagicalDamageSource; import com.minelittlepony.unicopia.entity.duck.LivingEntityDuck; @@ -36,6 +37,7 @@ import com.minelittlepony.unicopia.server.world.DragonBreathStore; import com.minelittlepony.unicopia.util.*; import it.unimi.dsi.fastutil.floats.Float2ObjectFunction; +import net.fabricmc.fabric.api.util.TriState; import net.minecraft.block.BlockState; import net.minecraft.enchantment.Enchantment; import net.minecraft.enchantment.EnchantmentHelper; @@ -44,6 +46,7 @@ import net.minecraft.entity.attribute.EntityAttribute; import net.minecraft.entity.attribute.EntityAttributeInstance; import net.minecraft.entity.attribute.EntityAttributeModifier; import net.minecraft.entity.damage.DamageSource; +import net.minecraft.entity.damage.DamageTypes; import net.minecraft.entity.data.*; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.projectile.ProjectileEntity; @@ -92,6 +95,8 @@ public abstract class Living implements Equine, Caste @Nullable private Caster attacker; + @Nullable + private transient Caster host; private Optional> target = Optional.empty(); @@ -478,6 +483,19 @@ public abstract class Living implements Equine, Caste public Optional onDamage(DamageSource source, float amount) { + if ((source.getAttacker() instanceof Guest guest && guest.getHost() instanceof Living l && l == this) + || (source.getSource() instanceof Guest guest && guest.getHost() instanceof Living l && l == this)) { + var type = source.getTypeRegistryEntry(); + return Optional.of(entity.damage( + type.matchesKey(DamageTypes.FIREBALL) ? entity.getDamageSources().create(DamageTypes.UNATTRIBUTED_FIREBALL) : + type.matchesKey(DamageTypes.PLAYER_EXPLOSION) ? entity.getDamageSources().create(DamageTypes.EXPLOSION) : + new DamageSource(type, entity, entity), amount)); + } + + if (entity instanceof Guest guest && guest.getHost() instanceof Living l) { + l.asEntity().damage(source, amount); + } + if (source.isIn(DamageTypeTags.IS_LIGHTNING) && (invinsibilityTicks > 0 || tryCaptureLightning())) { return Optional.of(false); } @@ -502,6 +520,10 @@ public abstract class Living implements Equine, Caste return Optional.empty(); } + public TriState canBeHurtByWater() { + return TriState.DEFAULT; + } + public Optional chooseClimbingPos() { return getSpellSlot().get(SpellPredicate.IS_DISGUISE, false) .map(AbstractDisguiseSpell::getDisguise) diff --git a/src/main/java/com/minelittlepony/unicopia/entity/behaviour/EndermanBehaviour.java b/src/main/java/com/minelittlepony/unicopia/entity/behaviour/EndermanBehaviour.java index 1e1562a6..e18f3761 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/behaviour/EndermanBehaviour.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/behaviour/EndermanBehaviour.java @@ -5,11 +5,13 @@ import com.minelittlepony.unicopia.entity.Living; import net.minecraft.entity.mob.EndermanEntity; import net.minecraft.item.BlockItem; import net.minecraft.item.ItemStack; +import net.minecraft.predicate.entity.EntityPredicates; import net.minecraft.util.Hand; public class EndermanBehaviour extends EntityBehaviour { @Override public void update(Living source, EndermanEntity entity, Disguise spell) { + entity.setInvulnerable(!EntityPredicates.EXCEPT_CREATIVE_OR_SPECTATOR.test(source.asEntity())); if (source.asEntity().isSneaking() || source.asEntity().isSprinting()) { entity.setTarget(entity); } else { @@ -22,5 +24,13 @@ public class EndermanBehaviour extends EntityBehaviour { } else { entity.setCarriedBlock(null); } + + //if (entity.hurtTime > 0) { + /* Vec3d teleportedPos = entity.getPos(); + + if (!teleportedPos.equals(source.asEntity().getPos())) { + source.asEntity().refreshPositionAfterTeleport(teleportedPos.x, teleportedPos.y, teleportedPos.z); + }*/ + //} } } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/behaviour/EntityAppearance.java b/src/main/java/com/minelittlepony/unicopia/entity/behaviour/EntityAppearance.java index 7a5d4c96..4cd359e4 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/behaviour/EntityAppearance.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/behaviour/EntityAppearance.java @@ -205,8 +205,12 @@ public class EntityAppearance implements NbtSerialisable, PlayerDimensions.Provi return; } - if (entity instanceof LivingEntity) { - ((LivingEntity) entity).getAttributeInstance(UEntityAttributes.ENTITY_GRAVITY_MODIFIER).clearModifiers(); + if (entity instanceof LivingEntity l) { + l.getAttributeInstance(UEntityAttributes.ENTITY_GRAVITY_MODIFIER).clearModifiers(); + } + + if (entity instanceof Guest guest) { + guest.setHost(source); } if (source.isClient()) { diff --git a/src/main/java/com/minelittlepony/unicopia/entity/behaviour/Guest.java b/src/main/java/com/minelittlepony/unicopia/entity/behaviour/Guest.java new file mode 100644 index 00000000..3615b589 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/entity/behaviour/Guest.java @@ -0,0 +1,9 @@ +package com.minelittlepony.unicopia.entity.behaviour; + +import com.minelittlepony.unicopia.ability.magic.Caster; + +public interface Guest { + void setHost(Caster host); + + Caster getHost(); +} diff --git a/src/main/java/com/minelittlepony/unicopia/entity/duck/LivingEntityDuck.java b/src/main/java/com/minelittlepony/unicopia/entity/duck/LivingEntityDuck.java index 850e673e..f126df85 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/duck/LivingEntityDuck.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/duck/LivingEntityDuck.java @@ -1,11 +1,13 @@ package com.minelittlepony.unicopia.entity.duck; +import com.minelittlepony.unicopia.entity.behaviour.Guest; + import net.minecraft.entity.LivingEntity; import net.minecraft.item.ItemStack; import net.minecraft.util.Hand; import net.minecraft.util.math.Vec3d; -public interface LivingEntityDuck { +public interface LivingEntityDuck extends Guest { void updateItemUsage(Hand hand, ItemStack stack, int time); boolean isJumping(); diff --git a/src/main/java/com/minelittlepony/unicopia/mixin/MixinLivingEntity.java b/src/main/java/com/minelittlepony/unicopia/mixin/MixinLivingEntity.java index 27d286c6..2fb6a58d 100644 --- a/src/main/java/com/minelittlepony/unicopia/mixin/MixinLivingEntity.java +++ b/src/main/java/com/minelittlepony/unicopia/mixin/MixinLivingEntity.java @@ -2,6 +2,7 @@ package com.minelittlepony.unicopia.mixin; import java.util.Optional; +import org.jetbrains.annotations.Nullable; import org.spongepowered.asm.mixin.*; import org.spongepowered.asm.mixin.gen.Accessor; import org.spongepowered.asm.mixin.injection.At; @@ -18,6 +19,7 @@ import com.minelittlepony.unicopia.entity.*; import com.minelittlepony.unicopia.entity.behaviour.EntityAppearance; import com.minelittlepony.unicopia.entity.duck.*; +import net.fabricmc.fabric.api.util.TriState; import net.minecraft.entity.Entity; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.attribute.DefaultAttributeContainer; @@ -39,6 +41,8 @@ abstract class MixinLivingEntity extends Entity implements LivingEntityDuck, Equ private Optional climbingPos; private Equine caster; + @Nullable + private transient Caster host; private MixinLivingEntity() { super(null, null); } @@ -58,6 +62,17 @@ abstract class MixinLivingEntity extends Entity implements LivingEntityDuck, Equ return (Living)caster; } + @Override + @Nullable + public Caster getHost() { + return host; + } + + @Override + public void setHost(Caster host) { + this.host = host; + } + @Override @Accessor("jumping") public abstract boolean isJumping(); @@ -157,6 +172,14 @@ abstract class MixinLivingEntity extends Entity implements LivingEntityDuck, Equ get().onDamage(source, amount).ifPresent(info::setReturnValue); } + @Inject(method = "hurtByWater()Z", at = @At("HEAD"), cancellable = true) + private void onCanBeHurtByWater(CallbackInfoReturnable info) { + TriState hurtByWater = get().canBeHurtByWater(); + if (hurtByWater != TriState.DEFAULT) { + info.setReturnValue(hurtByWater.get()); + } + } + @Inject(method = "writeCustomDataToNbt(Lnet/minecraft/nbt/NbtCompound;)V", at = @At("HEAD")) private void onWriteCustomDataToTag(NbtCompound tag, CallbackInfo info) { tag.put("unicopia_caster", get().toNBT());