| 1 | package net.minecraft.entity.passive;
|
| 2 |
|
| 3 | import net.minecraft.block.Block;
|
| 4 | import net.minecraft.entity.EntityAgeable;
|
| 5 | import net.minecraft.entity.player.EntityPlayer;
|
| 6 | import net.minecraft.init.Blocks;
|
| 7 | import net.minecraft.init.Items;
|
| 8 | import net.minecraft.item.ItemStack;
|
| 9 | import net.minecraft.nbt.NBTTagCompound;
|
| 10 | import net.minecraft.util.BlockPos;
|
| 11 | import net.minecraft.util.DamageSource;
|
| 12 | import net.minecraft.util.EnumParticleTypes;
|
| 13 | import net.minecraft.util.MathHelper;
|
| 14 | import net.minecraft.world.World;
|
| 15 |
|
| 16 | public abstract class EntityAnimal extends EntityAgeable implements IAnimals
|
| 17 | {
|
| 18 | protected Block spawnableBlock = Blocks.grass;
|
| 19 | private int inLove;
|
| 20 | private EntityPlayer playerInLove;
|
| 21 |
|
| 22 | public EntityAnimal(World worldIn)
|
| 23 | {
|
| 24 | super(worldIn);
|
| 25 | }
|
| 26 |
|
| 27 | protected void updateAITasks()
|
| 28 | {
|
| 29 | if (this.getGrowingAge() != 0)
|
| 30 | {
|
| 31 | this.inLove = 0;
|
| 32 | }
|
| 33 |
|
| 34 | super.updateAITasks();
|
| 35 | }
|
| 36 |
|
| 37 | /**
|
| 38 | * Called frequently so the entity can update its state every tick as required. For example, zombies and skeletons
|
| 39 | * use this to react to sunlight and start to burn.
|
| 40 | */
|
| 41 | public void onLivingUpdate()
|
| 42 | {
|
| 43 | super.onLivingUpdate();
|
| 44 |
|
| 45 | if (this.getGrowingAge() != 0)
|
| 46 | {
|
| 47 | this.inLove = 0;
|
| 48 | }
|
| 49 |
|
| 50 | if (this.inLove > 0)
|
| 51 | {
|
| 52 | --this.inLove;
|
| 53 |
|
| 54 | if (this.inLove % 10 == 0)
|
| 55 | {
|
| 56 | double d0 = this.rand.nextGaussian() * 0.02D;
|
| 57 | double d1 = this.rand.nextGaussian() * 0.02D;
|
| 58 | double d2 = this.rand.nextGaussian() * 0.02D;
|
| 59 | this.worldObj.spawnParticle(EnumParticleTypes.HEART, this.posX + (double)(this.rand.nextFloat() * this.width * 2.0F) - (double)this.width, this.posY + 0.5D + (double)(this.rand.nextFloat() * this.height), this.posZ + (double)(this.rand.nextFloat() * this.width * 2.0F) - (double)this.width, d0, d1, d2, new int[0]);
|
| 60 | }
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | /**
|
| 65 | * Called when the entity is attacked.
|
| 66 | */
|
| 67 | public boolean attackEntityFrom(DamageSource source, float amount)
|
| 68 | {
|
| 69 | if (this.isEntityInvulnerable(source))
|
| 70 | {
|
| 71 | return false;
|
| 72 | }
|
| 73 | else
|
| 74 | {
|
| 75 | this.inLove = 0;
|
| 76 | return super.attackEntityFrom(source, amount);
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | public float getBlockPathWeight(BlockPos pos)
|
| 81 | {
|
| 82 | return this.worldObj.getBlockState(pos.down()).getBlock() == Blocks.grass ? 10.0F : this.worldObj.getLightBrightness(pos) - 0.5F;
|
| 83 | }
|
| 84 |
|
| 85 | /**
|
| 86 | * (abstract) Protected helper method to write subclass entity data to NBT.
|
| 87 | */
|
| 88 | public void writeEntityToNBT(NBTTagCompound tagCompound)
|
| 89 | {
|
| 90 | super.writeEntityToNBT(tagCompound);
|
| 91 | tagCompound.setInteger("InLove", this.inLove);
|
| 92 | }
|
| 93 |
|
| 94 | /**
|
| 95 | * (abstract) Protected helper method to read subclass entity data from NBT.
|
| 96 | */
|
| 97 | public void readEntityFromNBT(NBTTagCompound tagCompund)
|
| 98 | {
|
| 99 | super.readEntityFromNBT(tagCompund);
|
| 100 | this.inLove = tagCompund.getInteger("InLove");
|
| 101 | }
|
| 102 |
|
| 103 | /**
|
| 104 | * Checks if the entity's current position is a valid location to spawn this entity.
|
| 105 | */
|
| 106 | public boolean getCanSpawnHere()
|
| 107 | {
|
| 108 | int i = MathHelper.floor_double(this.posX);
|
| 109 | int j = MathHelper.floor_double(this.getEntityBoundingBox().minY);
|
| 110 | int k = MathHelper.floor_double(this.posZ);
|
| 111 | BlockPos blockpos = new BlockPos(i, j, k);
|
| 112 | return this.worldObj.getBlockState(blockpos.down()).getBlock() == this.spawnableBlock && this.worldObj.getLight(blockpos) > 8 && super.getCanSpawnHere();
|
| 113 | }
|
| 114 |
|
| 115 | /**
|
| 116 | * Get number of ticks, at least during which the living entity will be silent.
|
| 117 | */
|
| 118 | public int getTalkInterval()
|
| 119 | {
|
| 120 | return 120;
|
| 121 | }
|
| 122 |
|
| 123 | /**
|
| 124 | * Determines if an entity can be despawned, used on idle far away entities
|
| 125 | */
|
| 126 | protected boolean canDespawn()
|
| 127 | {
|
| 128 | return false;
|
| 129 | }
|
| 130 |
|
| 131 | /**
|
| 132 | * Get the experience points the entity currently has.
|
| 133 | */
|
| 134 | protected int getExperiencePoints(EntityPlayer player)
|
| 135 | {
|
| 136 | return 1 + this.worldObj.rand.nextInt(3);
|
| 137 | }
|
| 138 |
|
| 139 | /**
|
| 140 | * Checks if the parameter is an item which this animal can be fed to breed it (wheat, carrots or seeds depending on
|
| 141 | * the animal type)
|
| 142 | */
|
| 143 | public boolean isBreedingItem(ItemStack stack)
|
| 144 | {
|
| 145 | return stack == null ? false : stack.getItem() == Items.wheat;
|
| 146 | }
|
| 147 |
|
| 148 | /**
|
| 149 | * Called when a player interacts with a mob. e.g. gets milk from a cow, gets into the saddle on a pig.
|
| 150 | */
|
| 151 | public boolean interact(EntityPlayer player)
|
| 152 | {
|
| 153 | ItemStack itemstack = player.inventory.getCurrentItem();
|
| 154 |
|
| 155 | if (itemstack != null)
|
| 156 | {
|
| 157 | if (this.isBreedingItem(itemstack) && this.getGrowingAge() == 0 && this.inLove <= 0)
|
| 158 | {
|
| 159 | this.consumeItemFromStack(player, itemstack);
|
| 160 | this.setInLove(player);
|
| 161 | return true;
|
| 162 | }
|
| 163 |
|
| 164 | if (this.isChild() && this.isBreedingItem(itemstack))
|
| 165 | {
|
| 166 | this.consumeItemFromStack(player, itemstack);
|
| 167 | this.func_175501_a((int)((float)(-this.getGrowingAge() / 20) * 0.1F), true);
|
| 168 | return true;
|
| 169 | }
|
| 170 | }
|
| 171 |
|
| 172 | return super.interact(player);
|
| 173 | }
|
| 174 |
|
| 175 | /**
|
| 176 | * Decreases ItemStack size by one
|
| 177 | */
|
| 178 | protected void consumeItemFromStack(EntityPlayer player, ItemStack stack)
|
| 179 | {
|
| 180 | if (!player.capabilities.isCreativeMode)
|
| 181 | {
|
| 182 | --stack.stackSize;
|
| 183 |
|
| 184 | if (stack.stackSize <= 0)
|
| 185 | {
|
| 186 | player.inventory.setInventorySlotContents(player.inventory.currentItem, (ItemStack)null);
|
| 187 | }
|
| 188 | }
|
| 189 | }
|
| 190 |
|
| 191 | public void setInLove(EntityPlayer player)
|
| 192 | {
|
| 193 | this.inLove = 600;
|
| 194 | this.playerInLove = player;
|
| 195 | this.worldObj.setEntityState(this, (byte)18);
|
| 196 | }
|
| 197 |
|
| 198 | public EntityPlayer getPlayerInLove()
|
| 199 | {
|
| 200 | return this.playerInLove;
|
| 201 | }
|
| 202 |
|
| 203 | /**
|
| 204 | * Returns if the entity is currently in 'love mode'.
|
| 205 | */
|
| 206 | public boolean isInLove()
|
| 207 | {
|
| 208 | return this.inLove > 0;
|
| 209 | }
|
| 210 |
|
| 211 | public void resetInLove()
|
| 212 | {
|
| 213 | this.inLove = 0;
|
| 214 | }
|
| 215 |
|
| 216 | /**
|
| 217 | * Returns true if the mob is currently able to mate with the specified mob.
|
| 218 | */
|
| 219 | public boolean canMateWith(EntityAnimal otherAnimal)
|
| 220 | {
|
| 221 | return otherAnimal == this ? false : (otherAnimal.getClass() != this.getClass() ? false : this.isInLove() && otherAnimal.isInLove());
|
| 222 | }
|
| 223 |
|
| 224 | public void handleStatusUpdate(byte id)
|
| 225 | {
|
| 226 | if (id == 18)
|
| 227 | {
|
| 228 | for (int i = 0; i < 7; ++i)
|
| 229 | {
|
| 230 | double d0 = this.rand.nextGaussian() * 0.02D;
|
| 231 | double d1 = this.rand.nextGaussian() * 0.02D;
|
| 232 | double d2 = this.rand.nextGaussian() * 0.02D;
|
| 233 | this.worldObj.spawnParticle(EnumParticleTypes.HEART, this.posX + (double)(this.rand.nextFloat() * this.width * 2.0F) - (double)this.width, this.posY + 0.5D + (double)(this.rand.nextFloat() * this.height), this.posZ + (double)(this.rand.nextFloat() * this.width * 2.0F) - (double)this.width, d0, d1, d2, new int[0]);
|
| 234 | }
|
| 235 | }
|
| 236 | else
|
| 237 | {
|
| 238 | super.handleStatusUpdate(id);
|
| 239 | }
|
| 240 | }
|
| 241 | }
|
| 242 |
|