From ae8068ccb40aa540bc6e4abdbfbb91bc715407ce Mon Sep 17 00:00:00 2001 From: Sollace Date: Thu, 15 Feb 2024 14:17:02 +0000 Subject: [PATCH] Fix build --- .../unicopia/entity/Living.java | 6 ++-- .../unicopia/entity/behaviour/Guest.java | 28 +++++++++++++++++-- .../entity/mob/CrystalShardsEntity.java | 2 +- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/entity/Living.java b/src/main/java/com/minelittlepony/unicopia/entity/Living.java index 0615b9da..463a5136 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/Living.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/Living.java @@ -483,8 +483,8 @@ 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)) { + if (Guest.of(source.getAttacker()).hostIs(this) + || Guest.of(source.getSource()).hostIs(this)) { var type = source.getTypeRegistryEntry(); return Optional.of(entity.damage( type.matchesKey(DamageTypes.FIREBALL) ? entity.getDamageSources().create(DamageTypes.UNATTRIBUTED_FIREBALL) : @@ -492,7 +492,7 @@ public abstract class Living implements Equine, Caste new DamageSource(type, entity, entity), amount)); } - if (entity instanceof Guest guest && guest.getHost() instanceof Living l) { + if (Guest.of(entity).getHost() instanceof Living l) { l.asEntity().damage(source, amount); } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/behaviour/Guest.java b/src/main/java/com/minelittlepony/unicopia/entity/behaviour/Guest.java index 2fd67cd2..7d432c2d 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/behaviour/Guest.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/behaviour/Guest.java @@ -1,15 +1,37 @@ package com.minelittlepony.unicopia.entity.behaviour; +import org.jetbrains.annotations.Nullable; + import com.minelittlepony.unicopia.ability.magic.Caster; import net.minecraft.entity.Entity; public interface Guest { - void setHost(Caster host); + Guest NULL = new Guest() { + @Override + public void setHost(@Nullable Caster host) { } + @Nullable + @Override + public Caster getHost() { + return null; + } + }; + + void setHost(@Nullable Caster host); + + @Nullable Caster getHost(); - static boolean hasHost(Entity entity) { - return ((Guest)entity).getHost() != null; + static Guest of(@Nullable Entity entity) { + return entity == null ? NULL : (Guest)entity; + } + + default boolean hasHost() { + return getHost() != null; + } + + default boolean hostIs(Caster self) { + return getHost() == self; } } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/mob/CrystalShardsEntity.java b/src/main/java/com/minelittlepony/unicopia/entity/mob/CrystalShardsEntity.java index 7a87feed..12ff12ee 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/mob/CrystalShardsEntity.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/mob/CrystalShardsEntity.java @@ -173,7 +173,7 @@ public class CrystalShardsEntity extends StationaryObjectEntity { } } - if (!Guest.hasHost(this)) { + if (!Guest.of(this).hasHost()) { if (isDead() || isInvalid(getWorld(), getBlockPos(), getAttachmentFace())) { kill(); ParticleUtils.spawnParticles(ParticleTypes.CLOUD, this, 10);