| 1 | package net.minecraft.entity.passive;
|
| 2 |
|
| 3 | import java.util.UUID;
|
| 4 | import net.minecraft.entity.EntityLivingBase;
|
| 5 | import net.minecraft.entity.IEntityOwnable;
|
| 6 | import net.minecraft.entity.ai.EntityAISit;
|
| 7 | import net.minecraft.entity.player.EntityPlayerMP;
|
| 8 | import net.minecraft.nbt.NBTTagCompound;
|
| 9 | import net.minecraft.scoreboard.Team;
|
| 10 | import net.minecraft.server.management.PreYggdrasilConverter;
|
| 11 | import net.minecraft.util.DamageSource;
|
| 12 | import net.minecraft.util.EnumParticleTypes;
|
| 13 | import net.minecraft.world.World;
|
| 14 |
|
| 15 | public abstract class EntityTameable extends EntityAnimal implements IEntityOwnable
|
| 16 | {
|
| 17 | protected EntityAISit aiSit = new EntityAISit(this);
|
| 18 |
|
| 19 | public EntityTameable(World worldIn)
|
| 20 | {
|
| 21 | super(worldIn);
|
| 22 | this.setupTamedAI();
|
| 23 | }
|
| 24 |
|
| 25 | protected void entityInit()
|
| 26 | {
|
| 27 | super.entityInit();
|
| 28 | this.dataWatcher.addObject(16, Byte.valueOf((byte)0));
|
| 29 | this.dataWatcher.addObject(17, "");
|
| 30 | }
|
| 31 |
|
| 32 | /**
|
| 33 | * (abstract) Protected helper method to write subclass entity data to NBT.
|
| 34 | */
|
| 35 | public void writeEntityToNBT(NBTTagCompound tagCompound)
|
| 36 | {
|
| 37 | super.writeEntityToNBT(tagCompound);
|
| 38 |
|
| 39 | if (this.getOwnerId() == null)
|
| 40 | {
|
| 41 | tagCompound.setString("OwnerUUID", "");
|
| 42 | }
|
| 43 | else
|
| 44 | {
|
| 45 | tagCompound.setString("OwnerUUID", this.getOwnerId());
|
| 46 | }
|
| 47 |
|
| 48 | tagCompound.setBoolean("Sitting", this.isSitting());
|
| 49 | }
|
| 50 |
|
| 51 | /**
|
| 52 | * (abstract) Protected helper method to read subclass entity data from NBT.
|
| 53 | */
|
| 54 | public void readEntityFromNBT(NBTTagCompound tagCompund)
|
| 55 | {
|
| 56 | super.readEntityFromNBT(tagCompund);
|
| 57 | String s = "";
|
| 58 |
|
| 59 | if (tagCompund.hasKey("OwnerUUID", 8))
|
| 60 | {
|
| 61 | s = tagCompund.getString("OwnerUUID");
|
| 62 | }
|
| 63 | else
|
| 64 | {
|
| 65 | String s1 = tagCompund.getString("Owner");
|
| 66 | s = PreYggdrasilConverter.getStringUUIDFromName(s1);
|
| 67 | }
|
| 68 |
|
| 69 | if (s.length() > 0)
|
| 70 | {
|
| 71 | this.setOwnerId(s);
|
| 72 | this.setTamed(true);
|
| 73 | }
|
| 74 |
|
| 75 | this.aiSit.setSitting(tagCompund.getBoolean("Sitting"));
|
| 76 | this.setSitting(tagCompund.getBoolean("Sitting"));
|
| 77 | }
|
| 78 |
|
| 79 | /**
|
| 80 | * Play the taming effect, will either be hearts or smoke depending on status
|
| 81 | */
|
| 82 | protected void playTameEffect(boolean play)
|
| 83 | {
|
| 84 | EnumParticleTypes enumparticletypes = EnumParticleTypes.HEART;
|
| 85 |
|
| 86 | if (!play)
|
| 87 | {
|
| 88 | enumparticletypes = EnumParticleTypes.SMOKE_NORMAL;
|
| 89 | }
|
| 90 |
|
| 91 | for (int i = 0; i < 7; ++i)
|
| 92 | {
|
| 93 | double d0 = this.rand.nextGaussian() * 0.02D;
|
| 94 | double d1 = this.rand.nextGaussian() * 0.02D;
|
| 95 | double d2 = this.rand.nextGaussian() * 0.02D;
|
| 96 | this.worldObj.spawnParticle(enumparticletypes, 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]);
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | public void handleStatusUpdate(byte id)
|
| 101 | {
|
| 102 | if (id == 7)
|
| 103 | {
|
| 104 | this.playTameEffect(true);
|
| 105 | }
|
| 106 | else if (id == 6)
|
| 107 | {
|
| 108 | this.playTameEffect(false);
|
| 109 | }
|
| 110 | else
|
| 111 | {
|
| 112 | super.handleStatusUpdate(id);
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | public boolean isTamed()
|
| 117 | {
|
| 118 | return (this.dataWatcher.getWatchableObjectByte(16) & 4) != 0;
|
| 119 | }
|
| 120 |
|
| 121 | public void setTamed(boolean tamed)
|
| 122 | {
|
| 123 | byte b0 = this.dataWatcher.getWatchableObjectByte(16);
|
| 124 |
|
| 125 | if (tamed)
|
| 126 | {
|
| 127 | this.dataWatcher.updateObject(16, Byte.valueOf((byte)(b0 | 4)));
|
| 128 | }
|
| 129 | else
|
| 130 | {
|
| 131 | this.dataWatcher.updateObject(16, Byte.valueOf((byte)(b0 & -5)));
|
| 132 | }
|
| 133 |
|
| 134 | this.setupTamedAI();
|
| 135 | }
|
| 136 |
|
| 137 | protected void setupTamedAI()
|
| 138 | {
|
| 139 | }
|
| 140 |
|
| 141 | public boolean isSitting()
|
| 142 | {
|
| 143 | return (this.dataWatcher.getWatchableObjectByte(16) & 1) != 0;
|
| 144 | }
|
| 145 |
|
| 146 | public void setSitting(boolean sitting)
|
| 147 | {
|
| 148 | byte b0 = this.dataWatcher.getWatchableObjectByte(16);
|
| 149 |
|
| 150 | if (sitting)
|
| 151 | {
|
| 152 | this.dataWatcher.updateObject(16, Byte.valueOf((byte)(b0 | 1)));
|
| 153 | }
|
| 154 | else
|
| 155 | {
|
| 156 | this.dataWatcher.updateObject(16, Byte.valueOf((byte)(b0 & -2)));
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | public String getOwnerId()
|
| 161 | {
|
| 162 | return this.dataWatcher.getWatchableObjectString(17);
|
| 163 | }
|
| 164 |
|
| 165 | public void setOwnerId(String ownerUuid)
|
| 166 | {
|
| 167 | this.dataWatcher.updateObject(17, ownerUuid);
|
| 168 | }
|
| 169 |
|
| 170 | public EntityLivingBase getOwner()
|
| 171 | {
|
| 172 | try
|
| 173 | {
|
| 174 | UUID uuid = UUID.fromString(this.getOwnerId());
|
| 175 | return uuid == null ? null : this.worldObj.getPlayerEntityByUUID(uuid);
|
| 176 | }
|
| 177 | catch (IllegalArgumentException var2)
|
| 178 | {
|
| 179 | return null;
|
| 180 | }
|
| 181 | }
|
| 182 |
|
| 183 | public boolean isOwner(EntityLivingBase entityIn)
|
| 184 | {
|
| 185 | return entityIn == this.getOwner();
|
| 186 | }
|
| 187 |
|
| 188 | /**
|
| 189 | * Returns the AITask responsible of the sit logic
|
| 190 | */
|
| 191 | public EntityAISit getAISit()
|
| 192 | {
|
| 193 | return this.aiSit;
|
| 194 | }
|
| 195 |
|
| 196 | public boolean shouldAttackEntity(EntityLivingBase p_142018_1_, EntityLivingBase p_142018_2_)
|
| 197 | {
|
| 198 | return true;
|
| 199 | }
|
| 200 |
|
| 201 | public Team getTeam()
|
| 202 | {
|
| 203 | if (this.isTamed())
|
| 204 | {
|
| 205 | EntityLivingBase entitylivingbase = this.getOwner();
|
| 206 |
|
| 207 | if (entitylivingbase != null)
|
| 208 | {
|
| 209 | return entitylivingbase.getTeam();
|
| 210 | }
|
| 211 | }
|
| 212 |
|
| 213 | return super.getTeam();
|
| 214 | }
|
| 215 |
|
| 216 | public boolean isOnSameTeam(EntityLivingBase otherEntity)
|
| 217 | {
|
| 218 | if (this.isTamed())
|
| 219 | {
|
| 220 | EntityLivingBase entitylivingbase = this.getOwner();
|
| 221 |
|
| 222 | if (otherEntity == entitylivingbase)
|
| 223 | {
|
| 224 | return true;
|
| 225 | }
|
| 226 |
|
| 227 | if (entitylivingbase != null)
|
| 228 | {
|
| 229 | return entitylivingbase.isOnSameTeam(otherEntity);
|
| 230 | }
|
| 231 | }
|
| 232 |
|
| 233 | return super.isOnSameTeam(otherEntity);
|
| 234 | }
|
| 235 |
|
| 236 | /**
|
| 237 | * Called when the mob's health reaches 0.
|
| 238 | */
|
| 239 | public void onDeath(DamageSource cause)
|
| 240 | {
|
| 241 | if (!this.worldObj.isRemote && this.worldObj.getGameRules().getBoolean("showDeathMessages") && this.hasCustomName() && this.getOwner() instanceof EntityPlayerMP)
|
| 242 | {
|
| 243 | ((EntityPlayerMP)this.getOwner()).addChatMessage(this.getCombatTracker().getDeathMessage());
|
| 244 | }
|
| 245 |
|
| 246 | super.onDeath(cause);
|
| 247 | }
|
| 248 | }
|
| 249 |
|