EntityAIWatchClosest .java

net.minecraft.entity.ai.EntityAIWatchClosest (class): members: maxDistanceForPlayer, closestEntity, watchedClass, theWatcher, chance, lookTime, updateTask.

net.minecraft.entity.aiclass100 linescommonextends EntityAIBase3.2 KB

API Surface (5)

boolean shouldExecute()
boolean continueExecuting()
void startExecuting()
void resetTask()
void updateTask()

Fields & Constants (6)

theWatcher
closestEntity
maxDistanceForPlayer
lookTime
chance
watchedClass

Code Graph: Dependencies (4)

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

Full Source

EntityAIWatchClosest.java100 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.entity.ai;
2
3
import net.minecraft.entity.Entity;
4
import net.minecraft.entity.EntityLiving;
5
import net.minecraft.entity.player.EntityPlayer;
6
7
public class EntityAIWatchClosest extends EntityAIBase
8
{
9
    protected EntityLiving theWatcher;
10
11
    /** The closest entity which is being watched by this one. */
12
    protected Entity closestEntity;
13
14
    /** This is the Maximum distance that the AI will look for the Entity */
15
    protected float maxDistanceForPlayer;
16
    private int lookTime;
17
    private float chance;
18
    protected Class <? extends Entity > watchedClass;
19
20
    public EntityAIWatchClosest(EntityLiving entitylivingIn, Class <? extends Entity > watchTargetClass, float maxDistance)
21
    {
22
        this.theWatcher = entitylivingIn;
23
        this.watchedClass = watchTargetClass;
24
        this.maxDistanceForPlayer = maxDistance;
25
        this.chance = 0.02F;
26
        this.setMutexBits(2);
27
    }
28
29
    public EntityAIWatchClosest(EntityLiving entitylivingIn, Class <? extends Entity > watchTargetClass, float maxDistance, float chanceIn)
30
    {
31
        this.theWatcher = entitylivingIn;
32
        this.watchedClass = watchTargetClass;
33
        this.maxDistanceForPlayer = maxDistance;
34
        this.chance = chanceIn;
35
        this.setMutexBits(2);
36
    }
37
38
    /**
39
     * Returns whether the EntityAIBase should begin execution.
40
     */
41
    public boolean shouldExecute()
42
    {
43
        if (this.theWatcher.getRNG().nextFloat() >= this.chance)
44
        {
45
            return false;
46
        }
47
        else
48
        {
49
            if (this.theWatcher.getAttackTarget() != null)
50
            {
51
                this.closestEntity = this.theWatcher.getAttackTarget();
52
            }
53
54
            if (this.watchedClass == EntityPlayer.class)
55
            {
56
                this.closestEntity = this.theWatcher.worldObj.getClosestPlayerToEntity(this.theWatcher, (double)this.maxDistanceForPlayer);
57
            }
58
            else
59
            {
60
                this.closestEntity = this.theWatcher.worldObj.findNearestEntityWithinAABB(this.watchedClass, this.theWatcher.getEntityBoundingBox().expand((double)this.maxDistanceForPlayer, 3.0D, (double)this.maxDistanceForPlayer), this.theWatcher);
61
            }
62
63
            return this.closestEntity != null;
64
        }
65
    }
66
67
    /**
68
     * Returns whether an in-progress EntityAIBase should continue executing
69
     */
70
    public boolean continueExecuting()
71
    {
72
        return !this.closestEntity.isEntityAlive() ? false : (this.theWatcher.getDistanceSqToEntity(this.closestEntity) > (double)(this.maxDistanceForPlayer * this.maxDistanceForPlayer) ? false : this.lookTime > 0);
73
    }
74
75
    /**
76
     * Execute a one shot task or start executing a continuous task
77
     */
78
    public void startExecuting()
79
    {
80
        this.lookTime = 40 + this.theWatcher.getRNG().nextInt(40);
81
    }
82
83
    /**
84
     * Resets the task
85
     */
86
    public void resetTask()
87
    {
88
        this.closestEntity = null;
89
    }
90
91
    /**
92
     * Updates the task
93
     */
94
    public void updateTask()
95
    {
96
        this.theWatcher.getLookHelper().setLookPosition(this.closestEntity.posX, this.closestEntity.posY + (double)this.closestEntity.getEyeHeight(), this.closestEntity.posZ, 10.0F, (float)this.theWatcher.getVerticalFaceSpeed());
97
        --this.lookTime;
98
    }
99
}
100

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