From 159a87059907571cd401bf400c243a4473ab98a4 Mon Sep 17 00:00:00 2001 From: Sollace Date: Wed, 13 Sep 2023 14:33:01 +0100 Subject: [PATCH] Fixed passive entities not attacking the player when holding items with want it need it --- .../unicopia/EquinePredicates.java | 10 ++-------- .../unicopia/entity/ai/WantItTakeItGoal.java | 13 +++++++++---- .../unicopia/entity/mob/SpellbookEntity.java | 2 +- .../enchantment/WantItNeedItEnchantment.java | 19 +++++++++++++++++++ 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/EquinePredicates.java b/src/main/java/com/minelittlepony/unicopia/EquinePredicates.java index 85acafff..64038eb6 100644 --- a/src/main/java/com/minelittlepony/unicopia/EquinePredicates.java +++ b/src/main/java/com/minelittlepony/unicopia/EquinePredicates.java @@ -5,9 +5,8 @@ import java.util.function.Predicate; import com.minelittlepony.unicopia.ability.magic.Caster; import com.minelittlepony.unicopia.entity.Equine; import com.minelittlepony.unicopia.entity.MagicImmune; -import com.minelittlepony.unicopia.item.enchantment.UEnchantments; +import com.minelittlepony.unicopia.item.enchantment.WantItNeedItEnchantment; -import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.entity.*; import net.minecraft.entity.decoration.AbstractDecorationEntity; import net.minecraft.entity.player.PlayerEntity; @@ -34,12 +33,7 @@ public interface EquinePredicates { Predicate EXCEPT_MAGIC_IMMUNE = IS_MAGIC_IMMUNE.negate(); Predicate VALID_LIVING_AND_NOT_MAGIC_IMMUNE = EntityPredicates.VALID_LIVING_ENTITY.and(EXCEPT_MAGIC_IMMUNE); - Predicate HAS_WANT_IT_NEED_IT = e -> { - return EnchantmentHelper.getEquipmentLevel(UEnchantments.WANT_IT_NEED_IT, e) > 0 - || EnchantmentHelper.getLevel(UEnchantments.WANT_IT_NEED_IT, e.getOffHandStack()) > 0 - || EnchantmentHelper.getLevel(UEnchantments.WANT_IT_NEED_IT, e.getMainHandStack()) > 0; - }; - + Predicate LIVING_HAS_WANT_IT_NEED_IT = e -> WantItNeedItEnchantment.getLevel(e) > 0; Predicate VALID_FOR_DISGUISE = EntityPredicates.EXCEPT_SPECTATOR.and(e -> !(e instanceof LightningEntity || e instanceof AbstractDecorationEntity)); static Predicate ofRace(Race race) { diff --git a/src/main/java/com/minelittlepony/unicopia/entity/ai/WantItTakeItGoal.java b/src/main/java/com/minelittlepony/unicopia/entity/ai/WantItTakeItGoal.java index 3fdb9cf2..d2fad6d3 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/ai/WantItTakeItGoal.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/ai/WantItTakeItGoal.java @@ -3,6 +3,7 @@ package com.minelittlepony.unicopia.entity.ai; import com.minelittlepony.unicopia.AwaitTickQueue; import com.minelittlepony.unicopia.EquinePredicates; import com.minelittlepony.unicopia.item.enchantment.UEnchantments; +import com.minelittlepony.unicopia.item.enchantment.WantItNeedItEnchantment; import com.minelittlepony.unicopia.particle.FollowingParticleEffect; import com.minelittlepony.unicopia.particle.ParticleUtils; import com.minelittlepony.unicopia.particle.UParticles; @@ -14,13 +15,14 @@ import net.minecraft.entity.LivingEntity; import net.minecraft.entity.ai.TargetPredicate; import net.minecraft.entity.mob.MobEntity; import net.minecraft.item.ItemStack; +import net.minecraft.predicate.entity.EntityPredicates; import net.minecraft.util.Hand; public class WantItTakeItGoal extends BreakHeartGoal { - private final TargetPredicate predicate = TargetPredicate.createAttackable() + private final TargetPredicate predicate = TargetPredicate.createNonAttackable() .setBaseMaxDistance(64) - .setPredicate(EquinePredicates.HAS_WANT_IT_NEED_IT); + .setPredicate(EquinePredicates.LIVING_HAS_WANT_IT_NEED_IT.and(LivingEntity::canTakeDamage).and(EntityPredicates.EXCEPT_CREATIVE_OR_SPECTATOR)); protected int cooldown; @@ -30,8 +32,11 @@ public class WantItTakeItGoal extends BreakHeartGoal { @Override protected boolean canTarget(Entity e) { - return (!e.isRemoved() && e instanceof ItemEntity && EnchantmentHelper.getLevel(UEnchantments.WANT_IT_NEED_IT, ((ItemEntity)e).getStack()) > 0) - || (e instanceof LivingEntity && predicate.test(mob, (LivingEntity)e)); + return e != null && !e.isRemoved() && ( + (e instanceof LivingEntity l && predicate.test(mob, l) + || (e instanceof ItemEntity i && WantItNeedItEnchantment.getLevel(i) > 0) + ) + ); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/entity/mob/SpellbookEntity.java b/src/main/java/com/minelittlepony/unicopia/entity/mob/SpellbookEntity.java index e9aa4286..eb97f159 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/mob/SpellbookEntity.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/mob/SpellbookEntity.java @@ -65,7 +65,7 @@ public class SpellbookEntity extends MobEntity implements MagicImmune { if (player instanceof ServerPlayerEntity recipient && player.currentScreenHandler instanceof SpellbookScreenHandler book && getUuid().equals(book.entityId)) { - Channel.SERVER_SPELLBOOK_UPDATE.sendToPlayer(new MsgSpellbookStateChanged<>(book.syncId, state), recipient); + Channel.SERVER_SPELLBOOK_UPDATE.sendToPlayer(MsgSpellbookStateChanged.create(book, state), recipient); } }); }); diff --git a/src/main/java/com/minelittlepony/unicopia/item/enchantment/WantItNeedItEnchantment.java b/src/main/java/com/minelittlepony/unicopia/item/enchantment/WantItNeedItEnchantment.java index b206ff4e..6aed3a5e 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/enchantment/WantItNeedItEnchantment.java +++ b/src/main/java/com/minelittlepony/unicopia/item/enchantment/WantItNeedItEnchantment.java @@ -7,6 +7,9 @@ import com.minelittlepony.unicopia.particle.ParticleUtils; import com.minelittlepony.unicopia.particle.UParticles; import net.minecraft.enchantment.EnchantmentHelper; +import net.minecraft.entity.Entity; +import net.minecraft.entity.ItemEntity; +import net.minecraft.entity.LivingEntity; import net.minecraft.item.ItemStack; public class WantItNeedItEnchantment extends SimpleEnchantment { @@ -27,4 +30,20 @@ public class WantItNeedItEnchantment extends SimpleEnchantment { int oldLevel = EnchantmentHelper.getLevel(UEnchantments.WANT_IT_NEED_IT, oldStack); return newLevel > oldLevel; } + + public static int getLevel(Entity entity) { + return entity instanceof LivingEntity l ? getLevel(l) + : entity instanceof ItemEntity i ? getLevel(i) + : 0; + } + + public static int getLevel(ItemEntity entity) { + return EnchantmentHelper.getLevel(UEnchantments.WANT_IT_NEED_IT, entity.getStack()); + } + + public static int getLevel(LivingEntity entity) { + return EnchantmentHelper.getEquipmentLevel(UEnchantments.WANT_IT_NEED_IT, entity) + + EnchantmentHelper.getLevel(UEnchantments.WANT_IT_NEED_IT, entity.getOffHandStack()) + + EnchantmentHelper.getLevel(UEnchantments.WANT_IT_NEED_IT, entity.getMainHandStack()); + } }