Change the stored attacker to a Caster<?>

This commit is contained in:
Sollace 2022-12-19 23:23:08 +01:00
parent 4110c66493
commit 8629cba3e9
5 changed files with 22 additions and 19 deletions

View file

@ -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<T extends Entity> 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.

View file

@ -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<ItemEntity> {
owner.getDataTracker().startTracking(ITEM_RACE, Race.REGISTRY.getId(Race.HUMAN).toString());
}
@Nullable
@Override
public Entity getAttacker() {
public Caster<?> getAttacker() {
return null;
}

View file

@ -54,7 +54,7 @@ public abstract class Living<T extends LivingEntity> implements Equine<T>, Caste
private Runnable landEvent;
@Nullable
private Entity attacker;
private Caster<?> attacker;
private int invinsibilityTicks;
@ -175,21 +175,19 @@ public abstract class Living<T extends LivingEntity> implements Equine<T>, Caste
@Nullable
@Override
public final Entity getAttacker() {
public final Caster<?> getAttacker() {
return attacker;
}
@Override
public Optional<Boolean> 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;
}

View file

@ -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<Text> 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<Entity>)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()));
});
}
}

View file

@ -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;
}