EntitySheep .java

net.minecraft.entity.passive.EntitySheep (class): members: getHeadRotationAngleX, getHeadRotationPointY, getRandomSheepColor, inventoryCrafting, getDyeRgb.

net.minecraft.entity.passiveclass390 linescommonextends EntityAnimal13.3 KB

API Surface (27)

boolean canInteractWith(EntityPlayer playerIn)
float[] getDyeRgb(EnumDyeColor dyeColor)
void updateAITasks()
void onLivingUpdate()
void applyEntityAttributes()
void entityInit()
void dropFewItems(boolean wasRecentlyHit, int lootingModifier)
Item getDropItem()
void handleStatusUpdate(byte id)
float getHeadRotationPointY(float p_70894_1_)
float getHeadRotationAngleX(float p_70890_1_)
boolean interact(EntityPlayer player)
void writeEntityToNBT(NBTTagCompound tagCompound)
void readEntityFromNBT(NBTTagCompound tagCompund)
String getLivingSound()
String getHurtSound()
String getDeathSound()
void playStepSound(BlockPos pos, Block blockIn)
EnumDyeColor getFleeceColor()
void setFleeceColor(EnumDyeColor color)
boolean getSheared()
void setSheared(boolean sheared)
EnumDyeColor getRandomSheepColor(Random random)
EntitySheep createChild(EntityAgeable ageable)
void eatGrassBonus()
IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, IEntityLivingData livingdata)
float getEyeHeight()

Fields & Constants (4)

DYE_TO_RGB
inventoryCrafting
sheepTimer
entityAIEatGrass

String literals (5)

"mob.sheep.shear"
"Sheared"
"Color"
"mob.sheep.say"
"mob.sheep.step"

Full Source

EntitySheep.java390 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.entity.passive;
2
3
import com.google.common.collect.Maps;
4
import java.util.Map;
5
import java.util.Random;
6
import net.minecraft.block.Block;
7
import net.minecraft.entity.EntityAgeable;
8
import net.minecraft.entity.IEntityLivingData;
9
import net.minecraft.entity.SharedMonsterAttributes;
10
import net.minecraft.entity.ai.EntityAIEatGrass;
11
import net.minecraft.entity.ai.EntityAIFollowParent;
12
import net.minecraft.entity.ai.EntityAILookIdle;
13
import net.minecraft.entity.ai.EntityAIMate;
14
import net.minecraft.entity.ai.EntityAIPanic;
15
import net.minecraft.entity.ai.EntityAISwimming;
16
import net.minecraft.entity.ai.EntityAITempt;
17
import net.minecraft.entity.ai.EntityAIWander;
18
import net.minecraft.entity.ai.EntityAIWatchClosest;
19
import net.minecraft.entity.item.EntityItem;
20
import net.minecraft.entity.player.EntityPlayer;
21
import net.minecraft.init.Blocks;
22
import net.minecraft.init.Items;
23
import net.minecraft.inventory.Container;
24
import net.minecraft.inventory.InventoryCrafting;
25
import net.minecraft.item.EnumDyeColor;
26
import net.minecraft.item.Item;
27
import net.minecraft.item.ItemStack;
28
import net.minecraft.item.crafting.CraftingManager;
29
import net.minecraft.nbt.NBTTagCompound;
30
import net.minecraft.pathfinding.PathNavigateGround;
31
import net.minecraft.util.BlockPos;
32
import net.minecraft.util.MathHelper;
33
import net.minecraft.world.DifficultyInstance;
34
import net.minecraft.world.World;
35
36
public class EntitySheep extends EntityAnimal
37
{
38
    /**
39
     * Internal crafting inventory used to check the result of mixing dyes corresponding to the fleece color when
40
     * breeding sheep.
41
     */
42
    private final InventoryCrafting inventoryCrafting = new InventoryCrafting(new Container()
43
    {
44
        public boolean canInteractWith(EntityPlayer playerIn)
45
        {
46
            return false;
47
        }
48
    }, 2, 1);
49
    private static final Map<EnumDyeColor, float[]> DYE_TO_RGB = Maps.newEnumMap(EnumDyeColor.class);
50
51
    /**
52
     * Used to control movement as well as wool regrowth. Set to 40 on handleHealthUpdate and counts down with each
53
     * tick.
54
     */
55
    private int sheepTimer;
56
    private EntityAIEatGrass entityAIEatGrass = new EntityAIEatGrass(this);
57
58
    public static float[] getDyeRgb(EnumDyeColor dyeColor)
59
    {
60
        return (float[])DYE_TO_RGB.get(dyeColor);
61
    }
62
63
    public EntitySheep(World worldIn)
64
    {
65
        super(worldIn);
66
        this.setSize(0.9F, 1.3F);
67
        ((PathNavigateGround)this.getNavigator()).setAvoidsWater(true);
68
        this.tasks.addTask(0, new EntityAISwimming(this));
69
        this.tasks.addTask(1, new EntityAIPanic(this, 1.25D));
70
        this.tasks.addTask(2, new EntityAIMate(this, 1.0D));
71
        this.tasks.addTask(3, new EntityAITempt(this, 1.1D, Items.wheat, false));
72
        this.tasks.addTask(4, new EntityAIFollowParent(this, 1.1D));
73
        this.tasks.addTask(5, this.entityAIEatGrass);
74
        this.tasks.addTask(6, new EntityAIWander(this, 1.0D));
75
        this.tasks.addTask(7, new EntityAIWatchClosest(this, EntityPlayer.class, 6.0F));
76
        this.tasks.addTask(8, new EntityAILookIdle(this));
77
        this.inventoryCrafting.setInventorySlotContents(0, new ItemStack(Items.dye, 1, 0));
78
        this.inventoryCrafting.setInventorySlotContents(1, new ItemStack(Items.dye, 1, 0));
79
    }
80
81
    protected void updateAITasks()
82
    {
83
        this.sheepTimer = this.entityAIEatGrass.getEatingGrassTimer();
84
        super.updateAITasks();
85
    }
86
87
    /**
88
     * Called frequently so the entity can update its state every tick as required. For example, zombies and skeletons
89
     * use this to react to sunlight and start to burn.
90
     */
91
    public void onLivingUpdate()
92
    {
93
        if (this.worldObj.isRemote)
94
        {
95
            this.sheepTimer = Math.max(0, this.sheepTimer - 1);
96
        }
97
98
        super.onLivingUpdate();
99
    }
100
101
    protected void applyEntityAttributes()
102
    {
103
        super.applyEntityAttributes();
104
        this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(8.0D);
105
        this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(0.23000000417232513D);
106
    }
107
108
    protected void entityInit()
109
    {
110
        super.entityInit();
111
        this.dataWatcher.addObject(16, new Byte((byte)0));
112
    }
113
114
    /**
115
     * Drop 0-2 items of this living's type
116
     *  
117
     * @param wasRecentlyHit true if this this entity was recently hit by appropriate entity (generally only if player
118
     * or tameable)
119
     * @param lootingModifier level of enchanment to be applied to this drop
120
     */
121
    protected void dropFewItems(boolean wasRecentlyHit, int lootingModifier)
122
    {
123
        if (!this.getSheared())
124
        {
125
            this.entityDropItem(new ItemStack(Item.getItemFromBlock(Blocks.wool), 1, this.getFleeceColor().getMetadata()), 0.0F);
126
        }
127
128
        int i = this.rand.nextInt(2) + 1 + this.rand.nextInt(1 + lootingModifier);
129
130
        for (int j = 0; j < i; ++j)
131
        {
132
            if (this.isBurning())
133
            {
134
                this.dropItem(Items.cooked_mutton, 1);
135
            }
136
            else
137
            {
138
                this.dropItem(Items.mutton, 1);
139
            }
140
        }
141
    }
142
143
    protected Item getDropItem()
144
    {
145
        return Item.getItemFromBlock(Blocks.wool);
146
    }
147
148
    public void handleStatusUpdate(byte id)
149
    {
150
        if (id == 10)
151
        {
152
            this.sheepTimer = 40;
153
        }
154
        else
155
        {
156
            super.handleStatusUpdate(id);
157
        }
158
    }
159
160
    public float getHeadRotationPointY(float p_70894_1_)
161
    {
162
        return this.sheepTimer <= 0 ? 0.0F : (this.sheepTimer >= 4 && this.sheepTimer <= 36 ? 1.0F : (this.sheepTimer < 4 ? ((float)this.sheepTimer - p_70894_1_) / 4.0F : -((float)(this.sheepTimer - 40) - p_70894_1_) / 4.0F));
163
    }
164
165
    public float getHeadRotationAngleX(float p_70890_1_)
166
    {
167
        if (this.sheepTimer > 4 && this.sheepTimer <= 36)
168
        {
169
            float f = ((float)(this.sheepTimer - 4) - p_70890_1_) / 32.0F;
170
            return ((float)Math.PI / 5F) + ((float)Math.PI * 7F / 100F) * MathHelper.sin(f * 28.7F);
171
        }
172
        else
173
        {
174
            return this.sheepTimer > 0 ? ((float)Math.PI / 5F) : this.rotationPitch / (180F / (float)Math.PI);
175
        }
176
    }
177
178
    /**
179
     * Called when a player interacts with a mob. e.g. gets milk from a cow, gets into the saddle on a pig.
180
     */
181
    public boolean interact(EntityPlayer player)
182
    {
183
        ItemStack itemstack = player.inventory.getCurrentItem();
184
185
        if (itemstack != null && itemstack.getItem() == Items.shears && !this.getSheared() && !this.isChild())
186
        {
187
            if (!this.worldObj.isRemote)
188
            {
189
                this.setSheared(true);
190
                int i = 1 + this.rand.nextInt(3);
191
192
                for (int j = 0; j < i; ++j)
193
                {
194
                    EntityItem entityitem = this.entityDropItem(new ItemStack(Item.getItemFromBlock(Blocks.wool), 1, this.getFleeceColor().getMetadata()), 1.0F);
195
                    entityitem.motionY += (double)(this.rand.nextFloat() * 0.05F);
196
                    entityitem.motionX += (double)((this.rand.nextFloat() - this.rand.nextFloat()) * 0.1F);
197
                    entityitem.motionZ += (double)((this.rand.nextFloat() - this.rand.nextFloat()) * 0.1F);
198
                }
199
            }
200
201
            itemstack.damageItem(1, player);
202
            this.playSound("mob.sheep.shear", 1.0F, 1.0F);
203
        }
204
205
        return super.interact(player);
206
    }
207
208
    /**
209
     * (abstract) Protected helper method to write subclass entity data to NBT.
210
     */
211
    public void writeEntityToNBT(NBTTagCompound tagCompound)
212
    {
213
        super.writeEntityToNBT(tagCompound);
214
        tagCompound.setBoolean("Sheared", this.getSheared());
215
        tagCompound.setByte("Color", (byte)this.getFleeceColor().getMetadata());
216
    }
217
218
    /**
219
     * (abstract) Protected helper method to read subclass entity data from NBT.
220
     */
221
    public void readEntityFromNBT(NBTTagCompound tagCompund)
222
    {
223
        super.readEntityFromNBT(tagCompund);
224
        this.setSheared(tagCompund.getBoolean("Sheared"));
225
        this.setFleeceColor(EnumDyeColor.byMetadata(tagCompund.getByte("Color")));
226
    }
227
228
    /**
229
     * Returns the sound this mob makes while it's alive.
230
     */
231
    protected String getLivingSound()
232
    {
233
        return "mob.sheep.say";
234
    }
235
236
    /**
237
     * Returns the sound this mob makes when it is hurt.
238
     */
239
    protected String getHurtSound()
240
    {
241
        return "mob.sheep.say";
242
    }
243
244
    /**
245
     * Returns the sound this mob makes on death.
246
     */
247
    protected String getDeathSound()
248
    {
249
        return "mob.sheep.say";
250
    }
251
252
    protected void playStepSound(BlockPos pos, Block blockIn)
253
    {
254
        this.playSound("mob.sheep.step", 0.15F, 1.0F);
255
    }
256
257
    /**
258
     * Gets the wool color of this sheep.
259
     */
260
    public EnumDyeColor getFleeceColor()
261
    {
262
        return EnumDyeColor.byMetadata(this.dataWatcher.getWatchableObjectByte(16) & 15);
263
    }
264
265
    /**
266
     * Sets the wool color of this sheep
267
     */
268
    public void setFleeceColor(EnumDyeColor color)
269
    {
270
        byte b0 = this.dataWatcher.getWatchableObjectByte(16);
271
        this.dataWatcher.updateObject(16, Byte.valueOf((byte)(b0 & 240 | color.getMetadata() & 15)));
272
    }
273
274
    /**
275
     * returns true if a sheeps wool has been sheared
276
     */
277
    public boolean getSheared()
278
    {
279
        return (this.dataWatcher.getWatchableObjectByte(16) & 16) != 0;
280
    }
281
282
    /**
283
     * make a sheep sheared if set to true
284
     */
285
    public void setSheared(boolean sheared)
286
    {
287
        byte b0 = this.dataWatcher.getWatchableObjectByte(16);
288
289
        if (sheared)
290
        {
291
            this.dataWatcher.updateObject(16, Byte.valueOf((byte)(b0 | 16)));
292
        }
293
        else
294
        {
295
            this.dataWatcher.updateObject(16, Byte.valueOf((byte)(b0 & -17)));
296
        }
297
    }
298
299
    /**
300
     * Chooses a "vanilla" sheep color based on the provided random.
301
     */
302
    public static EnumDyeColor getRandomSheepColor(Random random)
303
    {
304
        int i = random.nextInt(100);
305
        return i < 5 ? EnumDyeColor.BLACK : (i < 10 ? EnumDyeColor.GRAY : (i < 15 ? EnumDyeColor.SILVER : (i < 18 ? EnumDyeColor.BROWN : (random.nextInt(500) == 0 ? EnumDyeColor.PINK : EnumDyeColor.WHITE))));
306
    }
307
308
    public EntitySheep createChild(EntityAgeable ageable)
309
    {
310
        EntitySheep entitysheep = (EntitySheep)ageable;
311
        EntitySheep entitysheep1 = new EntitySheep(this.worldObj);
312
        entitysheep1.setFleeceColor(this.getDyeColorMixFromParents(this, entitysheep));
313
        return entitysheep1;
314
    }
315
316
    /**
317
     * This function applies the benefits of growing back wool and faster growing up to the acting entity. (This
318
     * function is used in the AIEatGrass)
319
     */
320
    public void eatGrassBonus()
321
    {
322
        this.setSheared(false);
323
324
        if (this.isChild())
325
        {
326
            this.addGrowth(60);
327
        }
328
    }
329
330
    /**
331
     * Called only once on an entity when first time spawned, via egg, mob spawner, natural spawning etc, but not called
332
     * when entity is reloaded from nbt. Mainly used for initializing attributes and inventory
333
     */
334
    public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, IEntityLivingData livingdata)
335
    {
336
        livingdata = super.onInitialSpawn(difficulty, livingdata);
337
        this.setFleeceColor(getRandomSheepColor(this.worldObj.rand));
338
        return livingdata;
339
    }
340
341
    /**
342
     * Attempts to mix both parent sheep to come up with a mixed dye color.
343
     */
344
    private EnumDyeColor getDyeColorMixFromParents(EntityAnimal father, EntityAnimal mother)
345
    {
346
        int i = ((EntitySheep)father).getFleeceColor().getDyeDamage();
347
        int j = ((EntitySheep)mother).getFleeceColor().getDyeDamage();
348
        this.inventoryCrafting.getStackInSlot(0).setItemDamage(i);
349
        this.inventoryCrafting.getStackInSlot(1).setItemDamage(j);
350
        ItemStack itemstack = CraftingManager.getInstance().findMatchingRecipe(this.inventoryCrafting, ((EntitySheep)father).worldObj);
351
        int k;
352
353
        if (itemstack != null && itemstack.getItem() == Items.dye)
354
        {
355
            k = itemstack.getMetadata();
356
        }
357
        else
358
        {
359
            k = this.worldObj.rand.nextBoolean() ? i : j;
360
        }
361
362
        return EnumDyeColor.byDyeDamage(k);
363
    }
364
365
    public float getEyeHeight()
366
    {
367
        return 0.95F * this.height;
368
    }
369
370
    static
371
    {
372
        DYE_TO_RGB.put(EnumDyeColor.WHITE, new float[] {1.0F, 1.0F, 1.0F});
373
        DYE_TO_RGB.put(EnumDyeColor.ORANGE, new float[] {0.85F, 0.5F, 0.2F});
374
        DYE_TO_RGB.put(EnumDyeColor.MAGENTA, new float[] {0.7F, 0.3F, 0.85F});
375
        DYE_TO_RGB.put(EnumDyeColor.LIGHT_BLUE, new float[] {0.4F, 0.6F, 0.85F});
376
        DYE_TO_RGB.put(EnumDyeColor.YELLOW, new float[] {0.9F, 0.9F, 0.2F});
377
        DYE_TO_RGB.put(EnumDyeColor.LIME, new float[] {0.5F, 0.8F, 0.1F});
378
        DYE_TO_RGB.put(EnumDyeColor.PINK, new float[] {0.95F, 0.5F, 0.65F});
379
        DYE_TO_RGB.put(EnumDyeColor.GRAY, new float[] {0.3F, 0.3F, 0.3F});
380
        DYE_TO_RGB.put(EnumDyeColor.SILVER, new float[] {0.6F, 0.6F, 0.6F});
381
        DYE_TO_RGB.put(EnumDyeColor.CYAN, new float[] {0.3F, 0.5F, 0.6F});
382
        DYE_TO_RGB.put(EnumDyeColor.PURPLE, new float[] {0.5F, 0.25F, 0.7F});
383
        DYE_TO_RGB.put(EnumDyeColor.BLUE, new float[] {0.2F, 0.3F, 0.7F});
384
        DYE_TO_RGB.put(EnumDyeColor.BROWN, new float[] {0.4F, 0.3F, 0.2F});
385
        DYE_TO_RGB.put(EnumDyeColor.GREEN, new float[] {0.4F, 0.5F, 0.2F});
386
        DYE_TO_RGB.put(EnumDyeColor.RED, new float[] {0.6F, 0.2F, 0.2F});
387
        DYE_TO_RGB.put(EnumDyeColor.BLACK, new float[] {0.1F, 0.1F, 0.1F});
388
    }
389
}
390

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