Fixed passive entities not attacking the player when holding items with want it need it

This commit is contained in:
Sollace 2023-09-13 14:33:01 +01:00
parent 7187ad61ee
commit 159a870599
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
4 changed files with 31 additions and 13 deletions

View file

@ -5,9 +5,8 @@ import java.util.function.Predicate;
import com.minelittlepony.unicopia.ability.magic.Caster; import com.minelittlepony.unicopia.ability.magic.Caster;
import com.minelittlepony.unicopia.entity.Equine; import com.minelittlepony.unicopia.entity.Equine;
import com.minelittlepony.unicopia.entity.MagicImmune; 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.*;
import net.minecraft.entity.decoration.AbstractDecorationEntity; import net.minecraft.entity.decoration.AbstractDecorationEntity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
@ -34,12 +33,7 @@ public interface EquinePredicates {
Predicate<Entity> EXCEPT_MAGIC_IMMUNE = IS_MAGIC_IMMUNE.negate(); Predicate<Entity> EXCEPT_MAGIC_IMMUNE = IS_MAGIC_IMMUNE.negate();
Predicate<Entity> VALID_LIVING_AND_NOT_MAGIC_IMMUNE = EntityPredicates.VALID_LIVING_ENTITY.and(EXCEPT_MAGIC_IMMUNE); Predicate<Entity> VALID_LIVING_AND_NOT_MAGIC_IMMUNE = EntityPredicates.VALID_LIVING_ENTITY.and(EXCEPT_MAGIC_IMMUNE);
Predicate<LivingEntity> HAS_WANT_IT_NEED_IT = e -> { Predicate<LivingEntity> LIVING_HAS_WANT_IT_NEED_IT = e -> WantItNeedItEnchantment.getLevel(e) > 0;
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<Entity> VALID_FOR_DISGUISE = EntityPredicates.EXCEPT_SPECTATOR.and(e -> !(e instanceof LightningEntity || e instanceof AbstractDecorationEntity)); Predicate<Entity> VALID_FOR_DISGUISE = EntityPredicates.EXCEPT_SPECTATOR.and(e -> !(e instanceof LightningEntity || e instanceof AbstractDecorationEntity));
static Predicate<Entity> ofRace(Race race) { static Predicate<Entity> ofRace(Race race) {

View file

@ -3,6 +3,7 @@ package com.minelittlepony.unicopia.entity.ai;
import com.minelittlepony.unicopia.AwaitTickQueue; import com.minelittlepony.unicopia.AwaitTickQueue;
import com.minelittlepony.unicopia.EquinePredicates; import com.minelittlepony.unicopia.EquinePredicates;
import com.minelittlepony.unicopia.item.enchantment.UEnchantments; 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.FollowingParticleEffect;
import com.minelittlepony.unicopia.particle.ParticleUtils; import com.minelittlepony.unicopia.particle.ParticleUtils;
import com.minelittlepony.unicopia.particle.UParticles; 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.ai.TargetPredicate;
import net.minecraft.entity.mob.MobEntity; import net.minecraft.entity.mob.MobEntity;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.predicate.entity.EntityPredicates;
import net.minecraft.util.Hand; import net.minecraft.util.Hand;
public class WantItTakeItGoal extends BreakHeartGoal { public class WantItTakeItGoal extends BreakHeartGoal {
private final TargetPredicate predicate = TargetPredicate.createAttackable() private final TargetPredicate predicate = TargetPredicate.createNonAttackable()
.setBaseMaxDistance(64) .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; protected int cooldown;
@ -30,8 +32,11 @@ public class WantItTakeItGoal extends BreakHeartGoal {
@Override @Override
protected boolean canTarget(Entity e) { protected boolean canTarget(Entity e) {
return (!e.isRemoved() && e instanceof ItemEntity && EnchantmentHelper.getLevel(UEnchantments.WANT_IT_NEED_IT, ((ItemEntity)e).getStack()) > 0) return e != null && !e.isRemoved() && (
|| (e instanceof LivingEntity && predicate.test(mob, (LivingEntity)e)); (e instanceof LivingEntity l && predicate.test(mob, l)
|| (e instanceof ItemEntity i && WantItNeedItEnchantment.getLevel(i) > 0)
)
);
} }
@Override @Override

View file

@ -65,7 +65,7 @@ public class SpellbookEntity extends MobEntity implements MagicImmune {
if (player instanceof ServerPlayerEntity recipient if (player instanceof ServerPlayerEntity recipient
&& player.currentScreenHandler instanceof SpellbookScreenHandler book && player.currentScreenHandler instanceof SpellbookScreenHandler book
&& getUuid().equals(book.entityId)) { && 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);
} }
}); });
}); });

View file

@ -7,6 +7,9 @@ import com.minelittlepony.unicopia.particle.ParticleUtils;
import com.minelittlepony.unicopia.particle.UParticles; import com.minelittlepony.unicopia.particle.UParticles;
import net.minecraft.enchantment.EnchantmentHelper; 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; import net.minecraft.item.ItemStack;
public class WantItNeedItEnchantment extends SimpleEnchantment { public class WantItNeedItEnchantment extends SimpleEnchantment {
@ -27,4 +30,20 @@ public class WantItNeedItEnchantment extends SimpleEnchantment {
int oldLevel = EnchantmentHelper.getLevel(UEnchantments.WANT_IT_NEED_IT, oldStack); int oldLevel = EnchantmentHelper.getLevel(UEnchantments.WANT_IT_NEED_IT, oldStack);
return newLevel > oldLevel; 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());
}
} }