| 1 | package net.minecraft.entity.item;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.util.List;
|
| 5 | import net.minecraft.block.Block;
|
| 6 | import net.minecraft.block.BlockAnvil;
|
| 7 | import net.minecraft.block.BlockFalling;
|
| 8 | import net.minecraft.block.ITileEntityProvider;
|
| 9 | import net.minecraft.block.material.Material;
|
| 10 | import net.minecraft.block.state.IBlockState;
|
| 11 | import net.minecraft.crash.CrashReportCategory;
|
| 12 | import net.minecraft.entity.Entity;
|
| 13 | import net.minecraft.init.Blocks;
|
| 14 | import net.minecraft.item.ItemStack;
|
| 15 | import net.minecraft.nbt.NBTBase;
|
| 16 | import net.minecraft.nbt.NBTTagCompound;
|
| 17 | import net.minecraft.tileentity.TileEntity;
|
| 18 | import net.minecraft.util.BlockPos;
|
| 19 | import net.minecraft.util.DamageSource;
|
| 20 | import net.minecraft.util.EnumFacing;
|
| 21 | import net.minecraft.util.MathHelper;
|
| 22 | import net.minecraft.util.ResourceLocation;
|
| 23 | import net.minecraft.world.World;
|
| 24 |
|
| 25 | public class EntityFallingBlock extends Entity
|
| 26 | {
|
| 27 | private IBlockState fallTile;
|
| 28 | public int fallTime;
|
| 29 | public boolean shouldDropItem = true;
|
| 30 | private boolean canSetAsBlock;
|
| 31 | private boolean hurtEntities;
|
| 32 | private int fallHurtMax = 40;
|
| 33 | private float fallHurtAmount = 2.0F;
|
| 34 | public NBTTagCompound tileEntityData;
|
| 35 |
|
| 36 | public EntityFallingBlock(World worldIn)
|
| 37 | {
|
| 38 | super(worldIn);
|
| 39 | }
|
| 40 |
|
| 41 | public EntityFallingBlock(World worldIn, double x, double y, double z, IBlockState fallingBlockState)
|
| 42 | {
|
| 43 | super(worldIn);
|
| 44 | this.fallTile = fallingBlockState;
|
| 45 | this.preventEntitySpawning = true;
|
| 46 | this.setSize(0.98F, 0.98F);
|
| 47 | this.setPosition(x, y, z);
|
| 48 | this.motionX = 0.0D;
|
| 49 | this.motionY = 0.0D;
|
| 50 | this.motionZ = 0.0D;
|
| 51 | this.prevPosX = x;
|
| 52 | this.prevPosY = y;
|
| 53 | this.prevPosZ = z;
|
| 54 | }
|
| 55 |
|
| 56 | /**
|
| 57 | * returns if this entity triggers Block.onEntityWalking on the blocks they walk on. used for spiders and wolves to
|
| 58 | * prevent them from trampling crops
|
| 59 | */
|
| 60 | protected boolean canTriggerWalking()
|
| 61 | {
|
| 62 | return false;
|
| 63 | }
|
| 64 |
|
| 65 | protected void entityInit()
|
| 66 | {
|
| 67 | }
|
| 68 |
|
| 69 | /**
|
| 70 | * Returns true if other Entities should be prevented from moving through this Entity.
|
| 71 | */
|
| 72 | public boolean canBeCollidedWith()
|
| 73 | {
|
| 74 | return !this.isDead;
|
| 75 | }
|
| 76 |
|
| 77 | /**
|
| 78 | * Called to update the entity's position/logic.
|
| 79 | */
|
| 80 | public void onUpdate()
|
| 81 | {
|
| 82 | Block block = this.fallTile.getBlock();
|
| 83 |
|
| 84 | if (block.getMaterial() == Material.air)
|
| 85 | {
|
| 86 | this.setDead();
|
| 87 | }
|
| 88 | else
|
| 89 | {
|
| 90 | this.prevPosX = this.posX;
|
| 91 | this.prevPosY = this.posY;
|
| 92 | this.prevPosZ = this.posZ;
|
| 93 |
|
| 94 | if (this.fallTime++ == 0)
|
| 95 | {
|
| 96 | BlockPos blockpos = new BlockPos(this);
|
| 97 |
|
| 98 | if (this.worldObj.getBlockState(blockpos).getBlock() == block)
|
| 99 | {
|
| 100 | this.worldObj.setBlockToAir(blockpos);
|
| 101 | }
|
| 102 | else if (!this.worldObj.isRemote)
|
| 103 | {
|
| 104 | this.setDead();
|
| 105 | return;
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 | this.motionY -= 0.03999999910593033D;
|
| 110 | this.moveEntity(this.motionX, this.motionY, this.motionZ);
|
| 111 | this.motionX *= 0.9800000190734863D;
|
| 112 | this.motionY *= 0.9800000190734863D;
|
| 113 | this.motionZ *= 0.9800000190734863D;
|
| 114 |
|
| 115 | if (!this.worldObj.isRemote)
|
| 116 | {
|
| 117 | BlockPos blockpos1 = new BlockPos(this);
|
| 118 |
|
| 119 | if (this.onGround)
|
| 120 | {
|
| 121 | this.motionX *= 0.699999988079071D;
|
| 122 | this.motionZ *= 0.699999988079071D;
|
| 123 | this.motionY *= -0.5D;
|
| 124 |
|
| 125 | if (this.worldObj.getBlockState(blockpos1).getBlock() != Blocks.piston_extension)
|
| 126 | {
|
| 127 | this.setDead();
|
| 128 |
|
| 129 | if (!this.canSetAsBlock)
|
| 130 | {
|
| 131 | if (this.worldObj.canBlockBePlaced(block, blockpos1, true, EnumFacing.UP, (Entity)null, (ItemStack)null) && !BlockFalling.canFallInto(this.worldObj, blockpos1.down()) && this.worldObj.setBlockState(blockpos1, this.fallTile, 3))
|
| 132 | {
|
| 133 | if (block instanceof BlockFalling)
|
| 134 | {
|
| 135 | ((BlockFalling)block).onEndFalling(this.worldObj, blockpos1);
|
| 136 | }
|
| 137 |
|
| 138 | if (this.tileEntityData != null && block instanceof ITileEntityProvider)
|
| 139 | {
|
| 140 | TileEntity tileentity = this.worldObj.getTileEntity(blockpos1);
|
| 141 |
|
| 142 | if (tileentity != null)
|
| 143 | {
|
| 144 | NBTTagCompound nbttagcompound = new NBTTagCompound();
|
| 145 | tileentity.writeToNBT(nbttagcompound);
|
| 146 |
|
| 147 | for (String s : this.tileEntityData.getKeySet())
|
| 148 | {
|
| 149 | NBTBase nbtbase = this.tileEntityData.getTag(s);
|
| 150 |
|
| 151 | if (!s.equals("x") && !s.equals("y") && !s.equals("z"))
|
| 152 | {
|
| 153 | nbttagcompound.setTag(s, nbtbase.copy());
|
| 154 | }
|
| 155 | }
|
| 156 |
|
| 157 | tileentity.readFromNBT(nbttagcompound);
|
| 158 | tileentity.markDirty();
|
| 159 | }
|
| 160 | }
|
| 161 | }
|
| 162 | else if (this.shouldDropItem && this.worldObj.getGameRules().getBoolean("doEntityDrops"))
|
| 163 | {
|
| 164 | this.entityDropItem(new ItemStack(block, 1, block.damageDropped(this.fallTile)), 0.0F);
|
| 165 | }
|
| 166 | }
|
| 167 | }
|
| 168 | }
|
| 169 | else if (this.fallTime > 100 && !this.worldObj.isRemote && (blockpos1.getY() < 1 || blockpos1.getY() > 256) || this.fallTime > 600)
|
| 170 | {
|
| 171 | if (this.shouldDropItem && this.worldObj.getGameRules().getBoolean("doEntityDrops"))
|
| 172 | {
|
| 173 | this.entityDropItem(new ItemStack(block, 1, block.damageDropped(this.fallTile)), 0.0F);
|
| 174 | }
|
| 175 |
|
| 176 | this.setDead();
|
| 177 | }
|
| 178 | }
|
| 179 | }
|
| 180 | }
|
| 181 |
|
| 182 | public void fall(float distance, float damageMultiplier)
|
| 183 | {
|
| 184 | Block block = this.fallTile.getBlock();
|
| 185 |
|
| 186 | if (this.hurtEntities)
|
| 187 | {
|
| 188 | int i = MathHelper.ceiling_float_int(distance - 1.0F);
|
| 189 |
|
| 190 | if (i > 0)
|
| 191 | {
|
| 192 | List<Entity> list = Lists.newArrayList(this.worldObj.getEntitiesWithinAABBExcludingEntity(this, this.getEntityBoundingBox()));
|
| 193 | boolean flag = block == Blocks.anvil;
|
| 194 | DamageSource damagesource = flag ? DamageSource.anvil : DamageSource.fallingBlock;
|
| 195 |
|
| 196 | for (Entity entity : list)
|
| 197 | {
|
| 198 | entity.attackEntityFrom(damagesource, (float)Math.min(MathHelper.floor_float((float)i * this.fallHurtAmount), this.fallHurtMax));
|
| 199 | }
|
| 200 |
|
| 201 | if (flag && (double)this.rand.nextFloat() < 0.05000000074505806D + (double)i * 0.05D)
|
| 202 | {
|
| 203 | int j = ((Integer)this.fallTile.getValue(BlockAnvil.DAMAGE)).intValue();
|
| 204 | ++j;
|
| 205 |
|
| 206 | if (j > 2)
|
| 207 | {
|
| 208 | this.canSetAsBlock = true;
|
| 209 | }
|
| 210 | else
|
| 211 | {
|
| 212 | this.fallTile = this.fallTile.withProperty(BlockAnvil.DAMAGE, Integer.valueOf(j));
|
| 213 | }
|
| 214 | }
|
| 215 | }
|
| 216 | }
|
| 217 | }
|
| 218 |
|
| 219 | /**
|
| 220 | * (abstract) Protected helper method to write subclass entity data to NBT.
|
| 221 | */
|
| 222 | protected void writeEntityToNBT(NBTTagCompound tagCompound)
|
| 223 | {
|
| 224 | Block block = this.fallTile != null ? this.fallTile.getBlock() : Blocks.air;
|
| 225 | ResourceLocation resourcelocation = (ResourceLocation)Block.blockRegistry.getNameForObject(block);
|
| 226 | tagCompound.setString("Block", resourcelocation == null ? "" : resourcelocation.toString());
|
| 227 | tagCompound.setByte("Data", (byte)block.getMetaFromState(this.fallTile));
|
| 228 | tagCompound.setByte("Time", (byte)this.fallTime);
|
| 229 | tagCompound.setBoolean("DropItem", this.shouldDropItem);
|
| 230 | tagCompound.setBoolean("HurtEntities", this.hurtEntities);
|
| 231 | tagCompound.setFloat("FallHurtAmount", this.fallHurtAmount);
|
| 232 | tagCompound.setInteger("FallHurtMax", this.fallHurtMax);
|
| 233 |
|
| 234 | if (this.tileEntityData != null)
|
| 235 | {
|
| 236 | tagCompound.setTag("TileEntityData", this.tileEntityData);
|
| 237 | }
|
| 238 | }
|
| 239 |
|
| 240 | /**
|
| 241 | * (abstract) Protected helper method to read subclass entity data from NBT.
|
| 242 | */
|
| 243 | protected void readEntityFromNBT(NBTTagCompound tagCompund)
|
| 244 | {
|
| 245 | int i = tagCompund.getByte("Data") & 255;
|
| 246 |
|
| 247 | if (tagCompund.hasKey("Block", 8))
|
| 248 | {
|
| 249 | this.fallTile = Block.getBlockFromName(tagCompund.getString("Block")).getStateFromMeta(i);
|
| 250 | }
|
| 251 | else if (tagCompund.hasKey("TileID", 99))
|
| 252 | {
|
| 253 | this.fallTile = Block.getBlockById(tagCompund.getInteger("TileID")).getStateFromMeta(i);
|
| 254 | }
|
| 255 | else
|
| 256 | {
|
| 257 | this.fallTile = Block.getBlockById(tagCompund.getByte("Tile") & 255).getStateFromMeta(i);
|
| 258 | }
|
| 259 |
|
| 260 | this.fallTime = tagCompund.getByte("Time") & 255;
|
| 261 | Block block = this.fallTile.getBlock();
|
| 262 |
|
| 263 | if (tagCompund.hasKey("HurtEntities", 99))
|
| 264 | {
|
| 265 | this.hurtEntities = tagCompund.getBoolean("HurtEntities");
|
| 266 | this.fallHurtAmount = tagCompund.getFloat("FallHurtAmount");
|
| 267 | this.fallHurtMax = tagCompund.getInteger("FallHurtMax");
|
| 268 | }
|
| 269 | else if (block == Blocks.anvil)
|
| 270 | {
|
| 271 | this.hurtEntities = true;
|
| 272 | }
|
| 273 |
|
| 274 | if (tagCompund.hasKey("DropItem", 99))
|
| 275 | {
|
| 276 | this.shouldDropItem = tagCompund.getBoolean("DropItem");
|
| 277 | }
|
| 278 |
|
| 279 | if (tagCompund.hasKey("TileEntityData", 10))
|
| 280 | {
|
| 281 | this.tileEntityData = tagCompund.getCompoundTag("TileEntityData");
|
| 282 | }
|
| 283 |
|
| 284 | if (block == null || block.getMaterial() == Material.air)
|
| 285 | {
|
| 286 | this.fallTile = Blocks.sand.getDefaultState();
|
| 287 | }
|
| 288 | }
|
| 289 |
|
| 290 | public World getWorldObj()
|
| 291 | {
|
| 292 | return this.worldObj;
|
| 293 | }
|
| 294 |
|
| 295 | public void setHurtEntities(boolean p_145806_1_)
|
| 296 | {
|
| 297 | this.hurtEntities = p_145806_1_;
|
| 298 | }
|
| 299 |
|
| 300 | /**
|
| 301 | * Return whether this entity should be rendered as on fire.
|
| 302 | */
|
| 303 | public boolean canRenderOnFire()
|
| 304 | {
|
| 305 | return false;
|
| 306 | }
|
| 307 |
|
| 308 | public void addEntityCrashInfo(CrashReportCategory category)
|
| 309 | {
|
| 310 | super.addEntityCrashInfo(category);
|
| 311 |
|
| 312 | if (this.fallTile != null)
|
| 313 | {
|
| 314 | Block block = this.fallTile.getBlock();
|
| 315 | category.addCrashSection("Immitating block ID", Integer.valueOf(Block.getIdFromBlock(block)));
|
| 316 | category.addCrashSection("Immitating block data", Integer.valueOf(block.getMetaFromState(this.fallTile)));
|
| 317 | }
|
| 318 | }
|
| 319 |
|
| 320 | public IBlockState getBlock()
|
| 321 | {
|
| 322 | return this.fallTile;
|
| 323 | }
|
| 324 | }
|
| 325 |
|