Search in sources :

Example 1 with NearestAttackableTargetGoal

use of net.minecraft.entity.ai.goal.NearestAttackableTargetGoal in project Werewolves by TeamLapen.

the class ModEntityEventHandler method makeWerewolfFriendly.

/**
 * copy from {@link de.teamlapen.vampirism.entity.ModEntityEventHandler#makeVampireFriendly(String, MobEntity, Class, Class, int, BiFunction, Predicate)}
 */
public static <T extends MobEntity, S extends LivingEntity, Q extends NearestAttackableTargetGoal<S>> void makeWerewolfFriendly(String name, T entity, Class<Q> targetClass, Class<S> targetEntityClass, int attackPriority, BiFunction<T, Predicate<LivingEntity>, Q> replacement, Predicate<EntityType<? extends T>> typeCheck) {
    try {
        Goal target = null;
        for (PrioritizedGoal t : entity.targetSelector.availableGoals) {
            Goal g = t.getGoal();
            if (targetClass.equals(g.getClass()) && t.getPriority() == attackPriority && targetEntityClass.equals(((NearestAttackableTargetGoal<?>) g).targetType)) {
                target = g;
                break;
            }
        }
        if (target != null) {
            entity.targetSelector.removeGoal(target);
            EntityType<? extends T> type = (EntityType<? extends T>) entity.getType();
            if (typeCheck.test(type)) {
                Predicate<LivingEntity> newPredicate = nonWerewolfCheck;
                if (((Q) target).targetConditions.selector != null) {
                    newPredicate = newPredicate.and(((NearestAttackableTargetGoal<?>) target).targetConditions.selector);
                }
                entity.targetSelector.addGoal(attackPriority, replacement.apply(entity, newPredicate));
            }
        } else {
            if (entityAIReplacementWarnMap.getOrDefault(name, true)) {
                LOGGER.warn("Could not replace {} attack target task for {}", name, entity.getType().getDescription());
                entityAIReplacementWarnMap.put(name, false);
            }
        }
    } catch (Exception e) {
        LOGGER.error("Could not replace " + name + " attack target task for " + entity.getType().getDescription(), e);
    }
}
Also used : EntityType(net.minecraft.entity.EntityType) LivingEntity(net.minecraft.entity.LivingEntity) PrioritizedGoal(net.minecraft.entity.ai.goal.PrioritizedGoal) Goal(net.minecraft.entity.ai.goal.Goal) NearestAttackableTargetGoal(net.minecraft.entity.ai.goal.NearestAttackableTargetGoal) PrioritizedGoal(net.minecraft.entity.ai.goal.PrioritizedGoal) NearestAttackableTargetGoal(net.minecraft.entity.ai.goal.NearestAttackableTargetGoal)

Aggregations

EntityType (net.minecraft.entity.EntityType)1 LivingEntity (net.minecraft.entity.LivingEntity)1 Goal (net.minecraft.entity.ai.goal.Goal)1 NearestAttackableTargetGoal (net.minecraft.entity.ai.goal.NearestAttackableTargetGoal)1 PrioritizedGoal (net.minecraft.entity.ai.goal.PrioritizedGoal)1