From 8629cba3e9b6d7a78925ab02c9e59c8b39a1ece8 Mon Sep 17 00:00:00 2001 From: Sollace Date: Mon, 19 Dec 2022 23:23:08 +0100 Subject: [PATCH] Change the stored attacker to a Caster --- .../com/minelittlepony/unicopia/entity/Equine.java | 4 +++- .../com/minelittlepony/unicopia/entity/ItemImpl.java | 6 +++++- .../com/minelittlepony/unicopia/entity/Living.java | 12 +++++------- .../unicopia/mixin/MixinDamageSource.java | 8 +++----- .../unicopia/util/MagicalDamageSource.java | 11 ++++++----- 5 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/entity/Equine.java b/src/main/java/com/minelittlepony/unicopia/entity/Equine.java index 62a68e5f..6113be7e 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/Equine.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/Equine.java @@ -6,6 +6,7 @@ import org.jetbrains.annotations.Nullable; import com.minelittlepony.unicopia.EntityConvertable; import com.minelittlepony.unicopia.Race; +import com.minelittlepony.unicopia.ability.magic.Caster; import com.minelittlepony.unicopia.projectile.ProjectileImpactListener; import com.minelittlepony.unicopia.util.NbtSerialisable; import com.minelittlepony.unicopia.util.Tickable; @@ -24,7 +25,8 @@ public interface Equine extends NbtSerialisable, Tickable, Pro /** * Gets the last magical entity to attack us. */ - Entity getAttacker(); + @Nullable + Caster getAttacker(); /** * Returns true if this player is fully invisible. diff --git a/src/main/java/com/minelittlepony/unicopia/entity/ItemImpl.java b/src/main/java/com/minelittlepony/unicopia/entity/ItemImpl.java index bc326d5d..70e1da58 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/ItemImpl.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/ItemImpl.java @@ -2,7 +2,10 @@ package com.minelittlepony.unicopia.entity; import java.util.List; +import org.jetbrains.annotations.Nullable; + import com.minelittlepony.unicopia.*; +import com.minelittlepony.unicopia.ability.magic.Caster; import com.minelittlepony.unicopia.item.enchantment.UEnchantments; import com.minelittlepony.unicopia.util.VecHelper; @@ -39,8 +42,9 @@ public class ItemImpl implements Equine { owner.getDataTracker().startTracking(ITEM_RACE, Race.REGISTRY.getId(Race.HUMAN).toString()); } + @Nullable @Override - public Entity getAttacker() { + public Caster getAttacker() { return null; } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/Living.java b/src/main/java/com/minelittlepony/unicopia/entity/Living.java index 46ff7071..b90a5a87 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/Living.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/Living.java @@ -54,7 +54,7 @@ public abstract class Living implements Equine, Caste private Runnable landEvent; @Nullable - private Entity attacker; + private Caster attacker; private int invinsibilityTicks; @@ -175,21 +175,19 @@ public abstract class Living implements Equine, Caste @Nullable @Override - public final Entity getAttacker() { + public final Caster getAttacker() { return attacker; } @Override public Optional onDamage(DamageSource source, float amount) { - if (source == DamageSource.LIGHTNING_BOLT) { - if (invinsibilityTicks > 0 || tryCaptureLightning()) { - return Optional.of(false); - } + if (source == DamageSource.LIGHTNING_BOLT && (invinsibilityTicks > 0 || tryCaptureLightning())) { + return Optional.of(false); } if (source instanceof MagicalDamageSource magical) { - Entity attacker = ((MagicalDamageSource)source).getSpell(); + Caster attacker = ((MagicalDamageSource)source).getSpell(); if (attacker != null) { this.attacker = attacker; } diff --git a/src/main/java/com/minelittlepony/unicopia/mixin/MixinDamageSource.java b/src/main/java/com/minelittlepony/unicopia/mixin/MixinDamageSource.java index caa1544b..d16cd37d 100644 --- a/src/main/java/com/minelittlepony/unicopia/mixin/MixinDamageSource.java +++ b/src/main/java/com/minelittlepony/unicopia/mixin/MixinDamageSource.java @@ -5,7 +5,6 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -import com.minelittlepony.unicopia.Owned; import com.minelittlepony.unicopia.entity.Equine; import net.minecraft.entity.Entity; import net.minecraft.entity.LivingEntity; @@ -14,19 +13,18 @@ import net.minecraft.text.Text; @Mixin(DamageSource.class) abstract class MixinDamageSource { - @SuppressWarnings("unchecked") @Inject(method = "getDeathMessage", at = @At("RETURN"), cancellable = true) private void onGetDeathMessage(LivingEntity entity, CallbackInfoReturnable info) { Equine.of(entity).map(Equine::getAttacker).ifPresent(attacker -> { DamageSource self = (DamageSource)(Object)this; Entity prime = entity.getPrimeAdversary(); - if (prime != null && !(attacker instanceof Owned && ((Owned)attacker).isOwnedBy(prime))) { - info.setReturnValue(Text.translatable("death.attack.generic.and_also", info.getReturnValue(), attacker.getDisplayName())); + if (prime != null && !attacker.isOwnedBy(prime)) { + info.setReturnValue(Text.translatable("death.attack.generic.and_also", info.getReturnValue(), attacker.asEntity().getDisplayName())); return; } - info.setReturnValue(Text.translatable("death.attack." + self.getName() + ".player", entity.getDisplayName(), attacker.getDisplayName())); + info.setReturnValue(Text.translatable("death.attack." + self.getName() + ".player", entity.getDisplayName(), attacker.asEntity().getDisplayName())); }); } } diff --git a/src/main/java/com/minelittlepony/unicopia/util/MagicalDamageSource.java b/src/main/java/com/minelittlepony/unicopia/util/MagicalDamageSource.java index e8748c18..11823e25 100644 --- a/src/main/java/com/minelittlepony/unicopia/util/MagicalDamageSource.java +++ b/src/main/java/com/minelittlepony/unicopia/util/MagicalDamageSource.java @@ -36,18 +36,19 @@ public class MagicalDamageSource extends EntityDamageSource { } public static MagicalDamageSource create(String type, Caster caster) { - return new MagicalDamageSource(type, caster.getMaster(), caster.asEntity(), false, false); + return new MagicalDamageSource(type, caster.getMaster(), caster, false, false); } - private Entity spell; + @Nullable + private Caster spell; private boolean breakSunglasses; - protected MagicalDamageSource(String type, @Nullable Entity spell, boolean direct, boolean unblockable) { + protected MagicalDamageSource(String type, @Nullable Caster spell, boolean direct, boolean unblockable) { this(type, null, spell, direct, unblockable); } - protected MagicalDamageSource(String type, @Nullable Entity source, @Nullable Entity spell, boolean direct, boolean unblockable) { + protected MagicalDamageSource(String type, @Nullable Entity source, @Nullable Caster spell, boolean direct, boolean unblockable) { super(type, source); this.spell = spell; setUsesMagic(); @@ -69,7 +70,7 @@ public class MagicalDamageSource extends EntityDamageSource { } @Nullable - public Entity getSpell() { + public Caster getSpell() { return spell; }