| 1 | package net.minecraft.entity.passive;
|
| 2 |
|
| 3 | import net.minecraft.block.Block;
|
| 4 | import net.minecraft.entity.EntityAgeable;
|
| 5 | import net.minecraft.entity.SharedMonsterAttributes;
|
| 6 | import net.minecraft.entity.ai.EntityAIControlledByPlayer;
|
| 7 | import net.minecraft.entity.ai.EntityAIFollowParent;
|
| 8 | import net.minecraft.entity.ai.EntityAILookIdle;
|
| 9 | import net.minecraft.entity.ai.EntityAIMate;
|
| 10 | import net.minecraft.entity.ai.EntityAIPanic;
|
| 11 | import net.minecraft.entity.ai.EntityAISwimming;
|
| 12 | import net.minecraft.entity.ai.EntityAITempt;
|
| 13 | import net.minecraft.entity.ai.EntityAIWander;
|
| 14 | import net.minecraft.entity.ai.EntityAIWatchClosest;
|
| 15 | import net.minecraft.entity.effect.EntityLightningBolt;
|
| 16 | import net.minecraft.entity.monster.EntityPigZombie;
|
| 17 | import net.minecraft.entity.player.EntityPlayer;
|
| 18 | import net.minecraft.init.Items;
|
| 19 | import net.minecraft.item.Item;
|
| 20 | import net.minecraft.item.ItemStack;
|
| 21 | import net.minecraft.nbt.NBTTagCompound;
|
| 22 | import net.minecraft.pathfinding.PathNavigateGround;
|
| 23 | import net.minecraft.stats.AchievementList;
|
| 24 | import net.minecraft.util.BlockPos;
|
| 25 | import net.minecraft.world.World;
|
| 26 |
|
| 27 | public class EntityPig extends EntityAnimal
|
| 28 | {
|
| 29 | /** AI task for player control. */
|
| 30 | private final EntityAIControlledByPlayer aiControlledByPlayer;
|
| 31 |
|
| 32 | public EntityPig(World worldIn)
|
| 33 | {
|
| 34 | super(worldIn);
|
| 35 | this.setSize(0.9F, 0.9F);
|
| 36 | ((PathNavigateGround)this.getNavigator()).setAvoidsWater(true);
|
| 37 | this.tasks.addTask(0, new EntityAISwimming(this));
|
| 38 | this.tasks.addTask(1, new EntityAIPanic(this, 1.25D));
|
| 39 | this.tasks.addTask(2, this.aiControlledByPlayer = new EntityAIControlledByPlayer(this, 0.3F));
|
| 40 | this.tasks.addTask(3, new EntityAIMate(this, 1.0D));
|
| 41 | this.tasks.addTask(4, new EntityAITempt(this, 1.2D, Items.carrot_on_a_stick, false));
|
| 42 | this.tasks.addTask(4, new EntityAITempt(this, 1.2D, Items.carrot, false));
|
| 43 | this.tasks.addTask(5, new EntityAIFollowParent(this, 1.1D));
|
| 44 | this.tasks.addTask(6, new EntityAIWander(this, 1.0D));
|
| 45 | this.tasks.addTask(7, new EntityAIWatchClosest(this, EntityPlayer.class, 6.0F));
|
| 46 | this.tasks.addTask(8, new EntityAILookIdle(this));
|
| 47 | }
|
| 48 |
|
| 49 | protected void applyEntityAttributes()
|
| 50 | {
|
| 51 | super.applyEntityAttributes();
|
| 52 | this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(10.0D);
|
| 53 | this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(0.25D);
|
| 54 | }
|
| 55 |
|
| 56 | /**
|
| 57 | * returns true if all the conditions for steering the entity are met. For pigs, this is true if it is being ridden
|
| 58 | * by a player and the player is holding a carrot-on-a-stick
|
| 59 | */
|
| 60 | public boolean canBeSteered()
|
| 61 | {
|
| 62 | ItemStack itemstack = ((EntityPlayer)this.riddenByEntity).getHeldItem();
|
| 63 | return itemstack != null && itemstack.getItem() == Items.carrot_on_a_stick;
|
| 64 | }
|
| 65 |
|
| 66 | protected void entityInit()
|
| 67 | {
|
| 68 | super.entityInit();
|
| 69 | this.dataWatcher.addObject(16, Byte.valueOf((byte)0));
|
| 70 | }
|
| 71 |
|
| 72 | /**
|
| 73 | * (abstract) Protected helper method to write subclass entity data to NBT.
|
| 74 | */
|
| 75 | public void writeEntityToNBT(NBTTagCompound tagCompound)
|
| 76 | {
|
| 77 | super.writeEntityToNBT(tagCompound);
|
| 78 | tagCompound.setBoolean("Saddle", this.getSaddled());
|
| 79 | }
|
| 80 |
|
| 81 | /**
|
| 82 | * (abstract) Protected helper method to read subclass entity data from NBT.
|
| 83 | */
|
| 84 | public void readEntityFromNBT(NBTTagCompound tagCompund)
|
| 85 | {
|
| 86 | super.readEntityFromNBT(tagCompund);
|
| 87 | this.setSaddled(tagCompund.getBoolean("Saddle"));
|
| 88 | }
|
| 89 |
|
| 90 | /**
|
| 91 | * Returns the sound this mob makes while it's alive.
|
| 92 | */
|
| 93 | protected String getLivingSound()
|
| 94 | {
|
| 95 | return "mob.pig.say";
|
| 96 | }
|
| 97 |
|
| 98 | /**
|
| 99 | * Returns the sound this mob makes when it is hurt.
|
| 100 | */
|
| 101 | protected String getHurtSound()
|
| 102 | {
|
| 103 | return "mob.pig.say";
|
| 104 | }
|
| 105 |
|
| 106 | /**
|
| 107 | * Returns the sound this mob makes on death.
|
| 108 | */
|
| 109 | protected String getDeathSound()
|
| 110 | {
|
| 111 | return "mob.pig.death";
|
| 112 | }
|
| 113 |
|
| 114 | protected void playStepSound(BlockPos pos, Block blockIn)
|
| 115 | {
|
| 116 | this.playSound("mob.pig.step", 0.15F, 1.0F);
|
| 117 | }
|
| 118 |
|
| 119 | /**
|
| 120 | * Called when a player interacts with a mob. e.g. gets milk from a cow, gets into the saddle on a pig.
|
| 121 | */
|
| 122 | public boolean interact(EntityPlayer player)
|
| 123 | {
|
| 124 | if (super.interact(player))
|
| 125 | {
|
| 126 | return true;
|
| 127 | }
|
| 128 | else if (!this.getSaddled() || this.worldObj.isRemote || this.riddenByEntity != null && this.riddenByEntity != player)
|
| 129 | {
|
| 130 | return false;
|
| 131 | }
|
| 132 | else
|
| 133 | {
|
| 134 | player.mountEntity(this);
|
| 135 | return true;
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | protected Item getDropItem()
|
| 140 | {
|
| 141 | return this.isBurning() ? Items.cooked_porkchop : Items.porkchop;
|
| 142 | }
|
| 143 |
|
| 144 | /**
|
| 145 | * Drop 0-2 items of this living's type
|
| 146 | *
|
| 147 | * @param wasRecentlyHit true if this this entity was recently hit by appropriate entity (generally only if player
|
| 148 | * or tameable)
|
| 149 | * @param lootingModifier level of enchanment to be applied to this drop
|
| 150 | */
|
| 151 | protected void dropFewItems(boolean wasRecentlyHit, int lootingModifier)
|
| 152 | {
|
| 153 | int i = this.rand.nextInt(3) + 1 + this.rand.nextInt(1 + lootingModifier);
|
| 154 |
|
| 155 | for (int j = 0; j < i; ++j)
|
| 156 | {
|
| 157 | if (this.isBurning())
|
| 158 | {
|
| 159 | this.dropItem(Items.cooked_porkchop, 1);
|
| 160 | }
|
| 161 | else
|
| 162 | {
|
| 163 | this.dropItem(Items.porkchop, 1);
|
| 164 | }
|
| 165 | }
|
| 166 |
|
| 167 | if (this.getSaddled())
|
| 168 | {
|
| 169 | this.dropItem(Items.saddle, 1);
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | /**
|
| 174 | * Returns true if the pig is saddled.
|
| 175 | */
|
| 176 | public boolean getSaddled()
|
| 177 | {
|
| 178 | return (this.dataWatcher.getWatchableObjectByte(16) & 1) != 0;
|
| 179 | }
|
| 180 |
|
| 181 | /**
|
| 182 | * Set or remove the saddle of the pig.
|
| 183 | */
|
| 184 | public void setSaddled(boolean saddled)
|
| 185 | {
|
| 186 | if (saddled)
|
| 187 | {
|
| 188 | this.dataWatcher.updateObject(16, Byte.valueOf((byte)1));
|
| 189 | }
|
| 190 | else
|
| 191 | {
|
| 192 | this.dataWatcher.updateObject(16, Byte.valueOf((byte)0));
|
| 193 | }
|
| 194 | }
|
| 195 |
|
| 196 | /**
|
| 197 | * Called when a lightning bolt hits the entity.
|
| 198 | */
|
| 199 | public void onStruckByLightning(EntityLightningBolt lightningBolt)
|
| 200 | {
|
| 201 | if (!this.worldObj.isRemote && !this.isDead)
|
| 202 | {
|
| 203 | EntityPigZombie entitypigzombie = new EntityPigZombie(this.worldObj);
|
| 204 | entitypigzombie.setCurrentItemOrArmor(0, new ItemStack(Items.golden_sword));
|
| 205 | entitypigzombie.setLocationAndAngles(this.posX, this.posY, this.posZ, this.rotationYaw, this.rotationPitch);
|
| 206 | entitypigzombie.setNoAI(this.isAIDisabled());
|
| 207 |
|
| 208 | if (this.hasCustomName())
|
| 209 | {
|
| 210 | entitypigzombie.setCustomNameTag(this.getCustomNameTag());
|
| 211 | entitypigzombie.setAlwaysRenderNameTag(this.getAlwaysRenderNameTag());
|
| 212 | }
|
| 213 |
|
| 214 | this.worldObj.spawnEntityInWorld(entitypigzombie);
|
| 215 | this.setDead();
|
| 216 | }
|
| 217 | }
|
| 218 |
|
| 219 | public void fall(float distance, float damageMultiplier)
|
| 220 | {
|
| 221 | super.fall(distance, damageMultiplier);
|
| 222 |
|
| 223 | if (distance > 5.0F && this.riddenByEntity instanceof EntityPlayer)
|
| 224 | {
|
| 225 | ((EntityPlayer)this.riddenByEntity).triggerAchievement(AchievementList.flyPig);
|
| 226 | }
|
| 227 | }
|
| 228 |
|
| 229 | public EntityPig createChild(EntityAgeable ageable)
|
| 230 | {
|
| 231 | return new EntityPig(this.worldObj);
|
| 232 | }
|
| 233 |
|
| 234 | /**
|
| 235 | * Checks if the parameter is an item which this animal can be fed to breed it (wheat, carrots or seeds depending on
|
| 236 | * the animal type)
|
| 237 | */
|
| 238 | public boolean isBreedingItem(ItemStack stack)
|
| 239 | {
|
| 240 | return stack != null && stack.getItem() == Items.carrot;
|
| 241 | }
|
| 242 |
|
| 243 | /**
|
| 244 | * Return the AI task for player control.
|
| 245 | */
|
| 246 | public EntityAIControlledByPlayer getAIControlledByPlayer()
|
| 247 | {
|
| 248 | return this.aiControlledByPlayer;
|
| 249 | }
|
| 250 | }
|
| 251 |
|