| 1 | package net.minecraft.entity;
|
| 2 |
|
| 3 | import net.minecraft.entity.player.EntityPlayer;
|
| 4 | import net.minecraft.init.Items;
|
| 5 | import net.minecraft.item.ItemStack;
|
| 6 | import net.minecraft.nbt.NBTTagCompound;
|
| 7 | import net.minecraft.util.EnumParticleTypes;
|
| 8 | import net.minecraft.util.MathHelper;
|
| 9 | import net.minecraft.world.World;
|
| 10 |
|
| 11 | public abstract class EntityAgeable extends EntityCreature
|
| 12 | {
|
| 13 | protected int growingAge;
|
| 14 | protected int field_175502_b;
|
| 15 | protected int field_175503_c;
|
| 16 | private float ageWidth = -1.0F;
|
| 17 | private float ageHeight;
|
| 18 |
|
| 19 | public EntityAgeable(World worldIn)
|
| 20 | {
|
| 21 | super(worldIn);
|
| 22 | }
|
| 23 |
|
| 24 | public abstract EntityAgeable createChild(EntityAgeable ageable);
|
| 25 |
|
| 26 | /**
|
| 27 | * Called when a player interacts with a mob. e.g. gets milk from a cow, gets into the saddle on a pig.
|
| 28 | */
|
| 29 | public boolean interact(EntityPlayer player)
|
| 30 | {
|
| 31 | ItemStack itemstack = player.inventory.getCurrentItem();
|
| 32 |
|
| 33 | if (itemstack != null && itemstack.getItem() == Items.spawn_egg)
|
| 34 | {
|
| 35 | if (!this.worldObj.isRemote)
|
| 36 | {
|
| 37 | Class <? extends Entity > oclass = EntityList.getClassFromID(itemstack.getMetadata());
|
| 38 |
|
| 39 | if (oclass != null && this.getClass() == oclass)
|
| 40 | {
|
| 41 | EntityAgeable entityageable = this.createChild(this);
|
| 42 |
|
| 43 | if (entityageable != null)
|
| 44 | {
|
| 45 | entityageable.setGrowingAge(-24000);
|
| 46 | entityageable.setLocationAndAngles(this.posX, this.posY, this.posZ, 0.0F, 0.0F);
|
| 47 | this.worldObj.spawnEntityInWorld(entityageable);
|
| 48 |
|
| 49 | if (itemstack.hasDisplayName())
|
| 50 | {
|
| 51 | entityageable.setCustomNameTag(itemstack.getDisplayName());
|
| 52 | }
|
| 53 |
|
| 54 | if (!player.capabilities.isCreativeMode)
|
| 55 | {
|
| 56 | --itemstack.stackSize;
|
| 57 |
|
| 58 | if (itemstack.stackSize <= 0)
|
| 59 | {
|
| 60 | player.inventory.setInventorySlotContents(player.inventory.currentItem, (ItemStack)null);
|
| 61 | }
|
| 62 | }
|
| 63 | }
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | return true;
|
| 68 | }
|
| 69 | else
|
| 70 | {
|
| 71 | return false;
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | protected void entityInit()
|
| 76 | {
|
| 77 | super.entityInit();
|
| 78 | this.dataWatcher.addObject(12, Byte.valueOf((byte)0));
|
| 79 | }
|
| 80 |
|
| 81 | /**
|
| 82 | * The age value may be negative or positive or zero. If it's negative, it get's incremented on each tick, if it's
|
| 83 | * positive, it get's decremented each tick. Don't confuse this with EntityLiving.getAge. With a negative value the
|
| 84 | * Entity is considered a child.
|
| 85 | */
|
| 86 | public int getGrowingAge()
|
| 87 | {
|
| 88 | return this.worldObj.isRemote ? this.dataWatcher.getWatchableObjectByte(12) : this.growingAge;
|
| 89 | }
|
| 90 |
|
| 91 | public void func_175501_a(int p_175501_1_, boolean p_175501_2_)
|
| 92 | {
|
| 93 | int i = this.getGrowingAge();
|
| 94 | int j = i;
|
| 95 | i = i + p_175501_1_ * 20;
|
| 96 |
|
| 97 | if (i > 0)
|
| 98 | {
|
| 99 | i = 0;
|
| 100 |
|
| 101 | if (j < 0)
|
| 102 | {
|
| 103 | this.onGrowingAdult();
|
| 104 | }
|
| 105 | }
|
| 106 |
|
| 107 | int k = i - j;
|
| 108 | this.setGrowingAge(i);
|
| 109 |
|
| 110 | if (p_175501_2_)
|
| 111 | {
|
| 112 | this.field_175502_b += k;
|
| 113 |
|
| 114 | if (this.field_175503_c == 0)
|
| 115 | {
|
| 116 | this.field_175503_c = 40;
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | if (this.getGrowingAge() == 0)
|
| 121 | {
|
| 122 | this.setGrowingAge(this.field_175502_b);
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 126 | /**
|
| 127 | * "Adds the value of the parameter times 20 to the age of this entity. If the entity is an adult (if the entity's
|
| 128 | * age is greater than 0), it will have no effect."
|
| 129 | */
|
| 130 | public void addGrowth(int growth)
|
| 131 | {
|
| 132 | this.func_175501_a(growth, false);
|
| 133 | }
|
| 134 |
|
| 135 | /**
|
| 136 | * The age value may be negative or positive or zero. If it's negative, it get's incremented on each tick, if it's
|
| 137 | * positive, it get's decremented each tick. With a negative value the Entity is considered a child.
|
| 138 | */
|
| 139 | public void setGrowingAge(int age)
|
| 140 | {
|
| 141 | this.dataWatcher.updateObject(12, Byte.valueOf((byte)MathHelper.clamp_int(age, -1, 1)));
|
| 142 | this.growingAge = age;
|
| 143 | this.setScaleForAge(this.isChild());
|
| 144 | }
|
| 145 |
|
| 146 | /**
|
| 147 | * (abstract) Protected helper method to write subclass entity data to NBT.
|
| 148 | */
|
| 149 | public void writeEntityToNBT(NBTTagCompound tagCompound)
|
| 150 | {
|
| 151 | super.writeEntityToNBT(tagCompound);
|
| 152 | tagCompound.setInteger("Age", this.getGrowingAge());
|
| 153 | tagCompound.setInteger("ForcedAge", this.field_175502_b);
|
| 154 | }
|
| 155 |
|
| 156 | /**
|
| 157 | * (abstract) Protected helper method to read subclass entity data from NBT.
|
| 158 | */
|
| 159 | public void readEntityFromNBT(NBTTagCompound tagCompund)
|
| 160 | {
|
| 161 | super.readEntityFromNBT(tagCompund);
|
| 162 | this.setGrowingAge(tagCompund.getInteger("Age"));
|
| 163 | this.field_175502_b = tagCompund.getInteger("ForcedAge");
|
| 164 | }
|
| 165 |
|
| 166 | /**
|
| 167 | * Called frequently so the entity can update its state every tick as required. For example, zombies and skeletons
|
| 168 | * use this to react to sunlight and start to burn.
|
| 169 | */
|
| 170 | public void onLivingUpdate()
|
| 171 | {
|
| 172 | super.onLivingUpdate();
|
| 173 |
|
| 174 | if (this.worldObj.isRemote)
|
| 175 | {
|
| 176 | if (this.field_175503_c > 0)
|
| 177 | {
|
| 178 | if (this.field_175503_c % 4 == 0)
|
| 179 | {
|
| 180 | this.worldObj.spawnParticle(EnumParticleTypes.VILLAGER_HAPPY, 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, 0.0D, 0.0D, 0.0D, new int[0]);
|
| 181 | }
|
| 182 |
|
| 183 | --this.field_175503_c;
|
| 184 | }
|
| 185 |
|
| 186 | this.setScaleForAge(this.isChild());
|
| 187 | }
|
| 188 | else
|
| 189 | {
|
| 190 | int i = this.getGrowingAge();
|
| 191 |
|
| 192 | if (i < 0)
|
| 193 | {
|
| 194 | ++i;
|
| 195 | this.setGrowingAge(i);
|
| 196 |
|
| 197 | if (i == 0)
|
| 198 | {
|
| 199 | this.onGrowingAdult();
|
| 200 | }
|
| 201 | }
|
| 202 | else if (i > 0)
|
| 203 | {
|
| 204 | --i;
|
| 205 | this.setGrowingAge(i);
|
| 206 | }
|
| 207 | }
|
| 208 | }
|
| 209 |
|
| 210 | /**
|
| 211 | * This is called when Entity's growing age timer reaches 0 (negative values are considered as a child, positive as
|
| 212 | * an adult)
|
| 213 | */
|
| 214 | protected void onGrowingAdult()
|
| 215 | {
|
| 216 | }
|
| 217 |
|
| 218 | /**
|
| 219 | * If Animal, checks if the age timer is negative
|
| 220 | */
|
| 221 | public boolean isChild()
|
| 222 | {
|
| 223 | return this.getGrowingAge() < 0;
|
| 224 | }
|
| 225 |
|
| 226 | /**
|
| 227 | * "Sets the scale for an ageable entity according to the boolean parameter, which says if it's a child."
|
| 228 | */
|
| 229 | public void setScaleForAge(boolean p_98054_1_)
|
| 230 | {
|
| 231 | this.setScale(p_98054_1_ ? 0.5F : 1.0F);
|
| 232 | }
|
| 233 |
|
| 234 | /**
|
| 235 | * Sets the width and height of the entity. Args: width, height
|
| 236 | */
|
| 237 | protected final void setSize(float width, float height)
|
| 238 | {
|
| 239 | boolean flag = this.ageWidth > 0.0F;
|
| 240 | this.ageWidth = width;
|
| 241 | this.ageHeight = height;
|
| 242 |
|
| 243 | if (!flag)
|
| 244 | {
|
| 245 | this.setScale(1.0F);
|
| 246 | }
|
| 247 | }
|
| 248 |
|
| 249 | protected final void setScale(float scale)
|
| 250 | {
|
| 251 | super.setSize(this.ageWidth * scale, this.ageHeight * scale);
|
| 252 | }
|
| 253 | }
|
| 254 |
|