| 1 | package net.minecraft.entity.item;
|
| 2 |
|
| 3 | import net.minecraft.block.BlockRailBase;
|
| 4 | import net.minecraft.block.state.IBlockState;
|
| 5 | import net.minecraft.entity.Entity;
|
| 6 | import net.minecraft.entity.projectile.EntityArrow;
|
| 7 | import net.minecraft.init.Blocks;
|
| 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.world.Explosion;
|
| 14 | import net.minecraft.world.World;
|
| 15 |
|
| 16 | public class EntityMinecartTNT extends EntityMinecart
|
| 17 | {
|
| 18 | private int minecartTNTFuse = -1;
|
| 19 |
|
| 20 | public EntityMinecartTNT(World worldIn)
|
| 21 | {
|
| 22 | super(worldIn);
|
| 23 | }
|
| 24 |
|
| 25 | public EntityMinecartTNT(World worldIn, double x, double y, double z)
|
| 26 | {
|
| 27 | super(worldIn, x, y, z);
|
| 28 | }
|
| 29 |
|
| 30 | public EntityMinecart.EnumMinecartType getMinecartType()
|
| 31 | {
|
| 32 | return EntityMinecart.EnumMinecartType.TNT;
|
| 33 | }
|
| 34 |
|
| 35 | public IBlockState getDefaultDisplayTile()
|
| 36 | {
|
| 37 | return Blocks.tnt.getDefaultState();
|
| 38 | }
|
| 39 |
|
| 40 | /**
|
| 41 | * Called to update the entity's position/logic.
|
| 42 | */
|
| 43 | public void onUpdate()
|
| 44 | {
|
| 45 | super.onUpdate();
|
| 46 |
|
| 47 | if (this.minecartTNTFuse > 0)
|
| 48 | {
|
| 49 | --this.minecartTNTFuse;
|
| 50 | this.worldObj.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, this.posX, this.posY + 0.5D, this.posZ, 0.0D, 0.0D, 0.0D, new int[0]);
|
| 51 | }
|
| 52 | else if (this.minecartTNTFuse == 0)
|
| 53 | {
|
| 54 | this.explodeCart(this.motionX * this.motionX + this.motionZ * this.motionZ);
|
| 55 | }
|
| 56 |
|
| 57 | if (this.isCollidedHorizontally)
|
| 58 | {
|
| 59 | double d0 = this.motionX * this.motionX + this.motionZ * this.motionZ;
|
| 60 |
|
| 61 | if (d0 >= 0.009999999776482582D)
|
| 62 | {
|
| 63 | this.explodeCart(d0);
|
| 64 | }
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | /**
|
| 69 | * Called when the entity is attacked.
|
| 70 | */
|
| 71 | public boolean attackEntityFrom(DamageSource source, float amount)
|
| 72 | {
|
| 73 | Entity entity = source.getSourceOfDamage();
|
| 74 |
|
| 75 | if (entity instanceof EntityArrow)
|
| 76 | {
|
| 77 | EntityArrow entityarrow = (EntityArrow)entity;
|
| 78 |
|
| 79 | if (entityarrow.isBurning())
|
| 80 | {
|
| 81 | this.explodeCart(entityarrow.motionX * entityarrow.motionX + entityarrow.motionY * entityarrow.motionY + entityarrow.motionZ * entityarrow.motionZ);
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | return super.attackEntityFrom(source, amount);
|
| 86 | }
|
| 87 |
|
| 88 | public void killMinecart(DamageSource source)
|
| 89 | {
|
| 90 | super.killMinecart(source);
|
| 91 | double d0 = this.motionX * this.motionX + this.motionZ * this.motionZ;
|
| 92 |
|
| 93 | if (!source.isExplosion() && this.worldObj.getGameRules().getBoolean("doEntityDrops"))
|
| 94 | {
|
| 95 | this.entityDropItem(new ItemStack(Blocks.tnt, 1), 0.0F);
|
| 96 | }
|
| 97 |
|
| 98 | if (source.isFireDamage() || source.isExplosion() || d0 >= 0.009999999776482582D)
|
| 99 | {
|
| 100 | this.explodeCart(d0);
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | /**
|
| 105 | * Makes the minecart explode.
|
| 106 | */
|
| 107 | protected void explodeCart(double p_94103_1_)
|
| 108 | {
|
| 109 | if (!this.worldObj.isRemote)
|
| 110 | {
|
| 111 | double d0 = Math.sqrt(p_94103_1_);
|
| 112 |
|
| 113 | if (d0 > 5.0D)
|
| 114 | {
|
| 115 | d0 = 5.0D;
|
| 116 | }
|
| 117 |
|
| 118 | this.worldObj.createExplosion(this, this.posX, this.posY, this.posZ, (float)(4.0D + this.rand.nextDouble() * 1.5D * d0), true);
|
| 119 | this.setDead();
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | public void fall(float distance, float damageMultiplier)
|
| 124 | {
|
| 125 | if (distance >= 3.0F)
|
| 126 | {
|
| 127 | float f = distance / 10.0F;
|
| 128 | this.explodeCart((double)(f * f));
|
| 129 | }
|
| 130 |
|
| 131 | super.fall(distance, damageMultiplier);
|
| 132 | }
|
| 133 |
|
| 134 | /**
|
| 135 | * Called every tick the minecart is on an activator rail. Args: x, y, z, is the rail receiving power
|
| 136 | */
|
| 137 | public void onActivatorRailPass(int x, int y, int z, boolean receivingPower)
|
| 138 | {
|
| 139 | if (receivingPower && this.minecartTNTFuse < 0)
|
| 140 | {
|
| 141 | this.ignite();
|
| 142 | }
|
| 143 | }
|
| 144 |
|
| 145 | public void handleStatusUpdate(byte id)
|
| 146 | {
|
| 147 | if (id == 10)
|
| 148 | {
|
| 149 | this.ignite();
|
| 150 | }
|
| 151 | else
|
| 152 | {
|
| 153 | super.handleStatusUpdate(id);
|
| 154 | }
|
| 155 | }
|
| 156 |
|
| 157 | /**
|
| 158 | * Ignites this TNT cart.
|
| 159 | */
|
| 160 | public void ignite()
|
| 161 | {
|
| 162 | this.minecartTNTFuse = 80;
|
| 163 |
|
| 164 | if (!this.worldObj.isRemote)
|
| 165 | {
|
| 166 | this.worldObj.setEntityState(this, (byte)10);
|
| 167 |
|
| 168 | if (!this.isSilent())
|
| 169 | {
|
| 170 | this.worldObj.playSoundAtEntity(this, "game.tnt.primed", 1.0F, 1.0F);
|
| 171 | }
|
| 172 | }
|
| 173 | }
|
| 174 |
|
| 175 | /**
|
| 176 | * Gets the remaining fuse time in ticks.
|
| 177 | */
|
| 178 | public int getFuseTicks()
|
| 179 | {
|
| 180 | return this.minecartTNTFuse;
|
| 181 | }
|
| 182 |
|
| 183 | /**
|
| 184 | * Returns true if the TNT minecart is ignited.
|
| 185 | */
|
| 186 | public boolean isIgnited()
|
| 187 | {
|
| 188 | return this.minecartTNTFuse > -1;
|
| 189 | }
|
| 190 |
|
| 191 | /**
|
| 192 | * Explosion resistance of a block relative to this entity
|
| 193 | */
|
| 194 | public float getExplosionResistance(Explosion explosionIn, World worldIn, BlockPos pos, IBlockState blockStateIn)
|
| 195 | {
|
| 196 | return !this.isIgnited() || !BlockRailBase.isRailBlock(blockStateIn) && !BlockRailBase.isRailBlock(worldIn, pos.up()) ? super.getExplosionResistance(explosionIn, worldIn, pos, blockStateIn) : 0.0F;
|
| 197 | }
|
| 198 |
|
| 199 | public boolean verifyExplosion(Explosion explosionIn, World worldIn, BlockPos pos, IBlockState blockStateIn, float p_174816_5_)
|
| 200 | {
|
| 201 | return !this.isIgnited() || !BlockRailBase.isRailBlock(blockStateIn) && !BlockRailBase.isRailBlock(worldIn, pos.up()) ? super.verifyExplosion(explosionIn, worldIn, pos, blockStateIn, p_174816_5_) : false;
|
| 202 | }
|
| 203 |
|
| 204 | /**
|
| 205 | * (abstract) Protected helper method to read subclass entity data from NBT.
|
| 206 | */
|
| 207 | protected void readEntityFromNBT(NBTTagCompound tagCompund)
|
| 208 | {
|
| 209 | super.readEntityFromNBT(tagCompund);
|
| 210 |
|
| 211 | if (tagCompund.hasKey("TNTFuse", 99))
|
| 212 | {
|
| 213 | this.minecartTNTFuse = tagCompund.getInteger("TNTFuse");
|
| 214 | }
|
| 215 | }
|
| 216 |
|
| 217 | /**
|
| 218 | * (abstract) Protected helper method to write subclass entity data to NBT.
|
| 219 | */
|
| 220 | protected void writeEntityToNBT(NBTTagCompound tagCompound)
|
| 221 | {
|
| 222 | super.writeEntityToNBT(tagCompound);
|
| 223 | tagCompound.setInteger("TNTFuse", this.minecartTNTFuse);
|
| 224 | }
|
| 225 | }
|
| 226 |
|