EntityAINearestAttackableTarget .java

net.minecraft.entity.ai.EntityAINearestAttackableTarget (class): members: theNearestAttackableTargetSorter, targetEntitySelector, targetChance, Sorter.

net.minecraft.entity.aiclass137 linescommonextends EntityLivingBase>4.7 KB

API Surface (4)

boolean apply(T p_apply_1_)
boolean shouldExecute()
void startExecuting()
int compare(Entity p_compare_1_, Entity p_compare_2_)

Fields & Constants (5)

targetClass
targetChance
theNearestAttackableTargetSorter
targetEntitySelector
targetEntity

Code Graph: Dependencies (6)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

EntityAINearestAttackableTarget.java137 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.entity.ai;
2
3
import com.google.common.base.Predicate;
4
import com.google.common.base.Predicates;
5
import java.util.Collections;
6
import java.util.Comparator;
7
import java.util.List;
8
import net.minecraft.entity.Entity;
9
import net.minecraft.entity.EntityCreature;
10
import net.minecraft.entity.EntityLivingBase;
11
import net.minecraft.entity.player.EntityPlayer;
12
import net.minecraft.util.EntitySelectors;
13
14
public class EntityAINearestAttackableTarget<T extends EntityLivingBase> extends EntityAITarget
15
{
16
    protected final Class<T> targetClass;
17
    private final int targetChance;
18
19
    /** Instance of EntityAINearestAttackableTargetSorter. */
20
    protected final EntityAINearestAttackableTarget.Sorter theNearestAttackableTargetSorter;
21
    protected Predicate <? super T > targetEntitySelector;
22
    protected EntityLivingBase targetEntity;
23
24
    public EntityAINearestAttackableTarget(EntityCreature creature, Class<T> classTarget, boolean checkSight)
25
    {
26
        this(creature, classTarget, checkSight, false);
27
    }
28
29
    public EntityAINearestAttackableTarget(EntityCreature creature, Class<T> classTarget, boolean checkSight, boolean onlyNearby)
30
    {
31
        this(creature, classTarget, 10, checkSight, onlyNearby, (Predicate <? super T >)null);
32
    }
33
34
    public EntityAINearestAttackableTarget(EntityCreature creature, Class<T> classTarget, int chance, boolean checkSight, boolean onlyNearby, final Predicate <? super T > targetSelector)
35
    {
36
        super(creature, checkSight, onlyNearby);
37
        this.targetClass = classTarget;
38
        this.targetChance = chance;
39
        this.theNearestAttackableTargetSorter = new EntityAINearestAttackableTarget.Sorter(creature);
40
        this.setMutexBits(1);
41
        this.targetEntitySelector = new Predicate<T>()
42
        {
43
            public boolean apply(T p_apply_1_)
44
            {
45
                if (targetSelector != null && !targetSelector.apply(p_apply_1_))
46
                {
47
                    return false;
48
                }
49
                else
50
                {
51
                    if (p_apply_1_ instanceof EntityPlayer)
52
                    {
53
                        double d0 = EntityAINearestAttackableTarget.this.getTargetDistance();
54
55
                        if (p_apply_1_.isSneaking())
56
                        {
57
                            d0 *= 0.800000011920929D;
58
                        }
59
60
                        if (p_apply_1_.isInvisible())
61
                        {
62
                            float f = ((EntityPlayer)p_apply_1_).getArmorVisibility();
63
64
                            if (f < 0.1F)
65
                            {
66
                                f = 0.1F;
67
                            }
68
69
                            d0 *= (double)(0.7F * f);
70
                        }
71
72
                        if ((double)p_apply_1_.getDistanceToEntity(EntityAINearestAttackableTarget.this.taskOwner) > d0)
73
                        {
74
                            return false;
75
                        }
76
                    }
77
78
                    return EntityAINearestAttackableTarget.this.isSuitableTarget(p_apply_1_, false);
79
                }
80
            }
81
        };
82
    }
83
84
    /**
85
     * Returns whether the EntityAIBase should begin execution.
86
     */
87
    public boolean shouldExecute()
88
    {
89
        if (this.targetChance > 0 && this.taskOwner.getRNG().nextInt(this.targetChance) != 0)
90
        {
91
            return false;
92
        }
93
        else
94
        {
95
            double d0 = this.getTargetDistance();
96
            List<T> list = this.taskOwner.worldObj.<T>getEntitiesWithinAABB(this.targetClass, this.taskOwner.getEntityBoundingBox().expand(d0, 4.0D, d0), Predicates.<T> and (this.targetEntitySelector, EntitySelectors.NOT_SPECTATING));
97
            Collections.sort(list, this.theNearestAttackableTargetSorter);
98
99
            if (list.isEmpty())
100
            {
101
                return false;
102
            }
103
            else
104
            {
105
                this.targetEntity = (EntityLivingBase)list.get(0);
106
                return true;
107
            }
108
        }
109
    }
110
111
    /**
112
     * Execute a one shot task or start executing a continuous task
113
     */
114
    public void startExecuting()
115
    {
116
        this.taskOwner.setAttackTarget(this.targetEntity);
117
        super.startExecuting();
118
    }
119
120
    public static class Sorter implements Comparator<Entity>
121
    {
122
        private final Entity theEntity;
123
124
        public Sorter(Entity theEntityIn)
125
        {
126
            this.theEntity = theEntityIn;
127
        }
128
129
        public int compare(Entity p_compare_1_, Entity p_compare_2_)
130
        {
131
            double d0 = this.theEntity.getDistanceSqToEntity(p_compare_1_);
132
            double d1 = this.theEntity.getDistanceSqToEntity(p_compare_2_);
133
            return d0 < d1 ? -1 : (d0 > d1 ? 1 : 0);
134
        }
135
    }
136
}
137

Deobfuscated MCP 1.8.9 source of EntityAINearestAttackableTarget from Marcelektro/MavenMCP-1.8.9 (net/minecraft/entity/ai/EntityAINearestAttackableTarget.java), stored verbatim.