| 1 | package net.minecraft.entity;
|
| 2 |
|
| 3 | import net.minecraft.block.Block;
|
| 4 | import net.minecraft.block.BlockRedstoneDiode;
|
| 5 | import net.minecraft.entity.player.EntityPlayer;
|
| 6 | import net.minecraft.nbt.NBTTagCompound;
|
| 7 | import net.minecraft.util.AxisAlignedBB;
|
| 8 | import net.minecraft.util.BlockPos;
|
| 9 | import net.minecraft.util.DamageSource;
|
| 10 | import net.minecraft.util.EnumFacing;
|
| 11 | import net.minecraft.world.World;
|
| 12 | import org.apache.commons.lang3.Validate;
|
| 13 |
|
| 14 | public abstract class EntityHanging extends Entity
|
| 15 | {
|
| 16 | private int tickCounter1;
|
| 17 | protected BlockPos hangingPosition;
|
| 18 |
|
| 19 | /** The direction the entity is facing */
|
| 20 | public EnumFacing facingDirection;
|
| 21 |
|
| 22 | public EntityHanging(World worldIn)
|
| 23 | {
|
| 24 | super(worldIn);
|
| 25 | this.setSize(0.5F, 0.5F);
|
| 26 | }
|
| 27 |
|
| 28 | public EntityHanging(World worldIn, BlockPos hangingPositionIn)
|
| 29 | {
|
| 30 | this(worldIn);
|
| 31 | this.hangingPosition = hangingPositionIn;
|
| 32 | }
|
| 33 |
|
| 34 | protected void entityInit()
|
| 35 | {
|
| 36 | }
|
| 37 |
|
| 38 | /**
|
| 39 | * Updates facing and bounding box based on it
|
| 40 | */
|
| 41 | protected void updateFacingWithBoundingBox(EnumFacing facingDirectionIn)
|
| 42 | {
|
| 43 | Validate.notNull(facingDirectionIn);
|
| 44 | Validate.isTrue(facingDirectionIn.getAxis().isHorizontal());
|
| 45 | this.facingDirection = facingDirectionIn;
|
| 46 | this.prevRotationYaw = this.rotationYaw = (float)(this.facingDirection.getHorizontalIndex() * 90);
|
| 47 | this.updateBoundingBox();
|
| 48 | }
|
| 49 |
|
| 50 | /**
|
| 51 | * Updates the entity bounding box based on current facing
|
| 52 | */
|
| 53 | private void updateBoundingBox()
|
| 54 | {
|
| 55 | if (this.facingDirection != null)
|
| 56 | {
|
| 57 | double d0 = (double)this.hangingPosition.getX() + 0.5D;
|
| 58 | double d1 = (double)this.hangingPosition.getY() + 0.5D;
|
| 59 | double d2 = (double)this.hangingPosition.getZ() + 0.5D;
|
| 60 | double d3 = 0.46875D;
|
| 61 | double d4 = this.func_174858_a(this.getWidthPixels());
|
| 62 | double d5 = this.func_174858_a(this.getHeightPixels());
|
| 63 | d0 = d0 - (double)this.facingDirection.getFrontOffsetX() * 0.46875D;
|
| 64 | d2 = d2 - (double)this.facingDirection.getFrontOffsetZ() * 0.46875D;
|
| 65 | d1 = d1 + d5;
|
| 66 | EnumFacing enumfacing = this.facingDirection.rotateYCCW();
|
| 67 | d0 = d0 + d4 * (double)enumfacing.getFrontOffsetX();
|
| 68 | d2 = d2 + d4 * (double)enumfacing.getFrontOffsetZ();
|
| 69 | this.posX = d0;
|
| 70 | this.posY = d1;
|
| 71 | this.posZ = d2;
|
| 72 | double d6 = (double)this.getWidthPixels();
|
| 73 | double d7 = (double)this.getHeightPixels();
|
| 74 | double d8 = (double)this.getWidthPixels();
|
| 75 |
|
| 76 | if (this.facingDirection.getAxis() == EnumFacing.Axis.Z)
|
| 77 | {
|
| 78 | d8 = 1.0D;
|
| 79 | }
|
| 80 | else
|
| 81 | {
|
| 82 | d6 = 1.0D;
|
| 83 | }
|
| 84 |
|
| 85 | d6 = d6 / 32.0D;
|
| 86 | d7 = d7 / 32.0D;
|
| 87 | d8 = d8 / 32.0D;
|
| 88 | this.setEntityBoundingBox(new AxisAlignedBB(d0 - d6, d1 - d7, d2 - d8, d0 + d6, d1 + d7, d2 + d8));
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | private double func_174858_a(int p_174858_1_)
|
| 93 | {
|
| 94 | return p_174858_1_ % 32 == 0 ? 0.5D : 0.0D;
|
| 95 | }
|
| 96 |
|
| 97 | /**
|
| 98 | * Called to update the entity's position/logic.
|
| 99 | */
|
| 100 | public void onUpdate()
|
| 101 | {
|
| 102 | this.prevPosX = this.posX;
|
| 103 | this.prevPosY = this.posY;
|
| 104 | this.prevPosZ = this.posZ;
|
| 105 |
|
| 106 | if (this.tickCounter1++ == 100 && !this.worldObj.isRemote)
|
| 107 | {
|
| 108 | this.tickCounter1 = 0;
|
| 109 |
|
| 110 | if (!this.isDead && !this.onValidSurface())
|
| 111 | {
|
| 112 | this.setDead();
|
| 113 | this.onBroken((Entity)null);
|
| 114 | }
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | /**
|
| 119 | * checks to make sure painting can be placed there
|
| 120 | */
|
| 121 | public boolean onValidSurface()
|
| 122 | {
|
| 123 | if (!this.worldObj.getCollidingBoundingBoxes(this, this.getEntityBoundingBox()).isEmpty())
|
| 124 | {
|
| 125 | return false;
|
| 126 | }
|
| 127 | else
|
| 128 | {
|
| 129 | int i = Math.max(1, this.getWidthPixels() / 16);
|
| 130 | int j = Math.max(1, this.getHeightPixels() / 16);
|
| 131 | BlockPos blockpos = this.hangingPosition.offset(this.facingDirection.getOpposite());
|
| 132 | EnumFacing enumfacing = this.facingDirection.rotateYCCW();
|
| 133 |
|
| 134 | for (int k = 0; k < i; ++k)
|
| 135 | {
|
| 136 | for (int l = 0; l < j; ++l)
|
| 137 | {
|
| 138 | BlockPos blockpos1 = blockpos.offset(enumfacing, k).up(l);
|
| 139 | Block block = this.worldObj.getBlockState(blockpos1).getBlock();
|
| 140 |
|
| 141 | if (!block.getMaterial().isSolid() && !BlockRedstoneDiode.isRedstoneRepeaterBlockID(block))
|
| 142 | {
|
| 143 | return false;
|
| 144 | }
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 | for (Entity entity : this.worldObj.getEntitiesWithinAABBExcludingEntity(this, this.getEntityBoundingBox()))
|
| 149 | {
|
| 150 | if (entity instanceof EntityHanging)
|
| 151 | {
|
| 152 | return false;
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | return true;
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | /**
|
| 161 | * Returns true if other Entities should be prevented from moving through this Entity.
|
| 162 | */
|
| 163 | public boolean canBeCollidedWith()
|
| 164 | {
|
| 165 | return true;
|
| 166 | }
|
| 167 |
|
| 168 | /**
|
| 169 | * Called when a player attacks an entity. If this returns true the attack will not happen.
|
| 170 | */
|
| 171 | public boolean hitByEntity(Entity entityIn)
|
| 172 | {
|
| 173 | return entityIn instanceof EntityPlayer ? this.attackEntityFrom(DamageSource.causePlayerDamage((EntityPlayer)entityIn), 0.0F) : false;
|
| 174 | }
|
| 175 |
|
| 176 | public EnumFacing getHorizontalFacing()
|
| 177 | {
|
| 178 | return this.facingDirection;
|
| 179 | }
|
| 180 |
|
| 181 | /**
|
| 182 | * Called when the entity is attacked.
|
| 183 | */
|
| 184 | public boolean attackEntityFrom(DamageSource source, float amount)
|
| 185 | {
|
| 186 | if (this.isEntityInvulnerable(source))
|
| 187 | {
|
| 188 | return false;
|
| 189 | }
|
| 190 | else
|
| 191 | {
|
| 192 | if (!this.isDead && !this.worldObj.isRemote)
|
| 193 | {
|
| 194 | this.setDead();
|
| 195 | this.setBeenAttacked();
|
| 196 | this.onBroken(source.getEntity());
|
| 197 | }
|
| 198 |
|
| 199 | return true;
|
| 200 | }
|
| 201 | }
|
| 202 |
|
| 203 | /**
|
| 204 | * Tries to moves the entity by the passed in displacement. Args: x, y, z
|
| 205 | */
|
| 206 | public void moveEntity(double x, double y, double z)
|
| 207 | {
|
| 208 | if (!this.worldObj.isRemote && !this.isDead && x * x + y * y + z * z > 0.0D)
|
| 209 | {
|
| 210 | this.setDead();
|
| 211 | this.onBroken((Entity)null);
|
| 212 | }
|
| 213 | }
|
| 214 |
|
| 215 | /**
|
| 216 | * Adds to the current velocity of the entity. Args: x, y, z
|
| 217 | */
|
| 218 | public void addVelocity(double x, double y, double z)
|
| 219 | {
|
| 220 | if (!this.worldObj.isRemote && !this.isDead && x * x + y * y + z * z > 0.0D)
|
| 221 | {
|
| 222 | this.setDead();
|
| 223 | this.onBroken((Entity)null);
|
| 224 | }
|
| 225 | }
|
| 226 |
|
| 227 | /**
|
| 228 | * (abstract) Protected helper method to write subclass entity data to NBT.
|
| 229 | */
|
| 230 | public void writeEntityToNBT(NBTTagCompound tagCompound)
|
| 231 | {
|
| 232 | tagCompound.setByte("Facing", (byte)this.facingDirection.getHorizontalIndex());
|
| 233 | tagCompound.setInteger("TileX", this.getHangingPosition().getX());
|
| 234 | tagCompound.setInteger("TileY", this.getHangingPosition().getY());
|
| 235 | tagCompound.setInteger("TileZ", this.getHangingPosition().getZ());
|
| 236 | }
|
| 237 |
|
| 238 | /**
|
| 239 | * (abstract) Protected helper method to read subclass entity data from NBT.
|
| 240 | */
|
| 241 | public void readEntityFromNBT(NBTTagCompound tagCompund)
|
| 242 | {
|
| 243 | this.hangingPosition = new BlockPos(tagCompund.getInteger("TileX"), tagCompund.getInteger("TileY"), tagCompund.getInteger("TileZ"));
|
| 244 | EnumFacing enumfacing;
|
| 245 |
|
| 246 | if (tagCompund.hasKey("Direction", 99))
|
| 247 | {
|
| 248 | enumfacing = EnumFacing.getHorizontal(tagCompund.getByte("Direction"));
|
| 249 | this.hangingPosition = this.hangingPosition.offset(enumfacing);
|
| 250 | }
|
| 251 | else if (tagCompund.hasKey("Facing", 99))
|
| 252 | {
|
| 253 | enumfacing = EnumFacing.getHorizontal(tagCompund.getByte("Facing"));
|
| 254 | }
|
| 255 | else
|
| 256 | {
|
| 257 | enumfacing = EnumFacing.getHorizontal(tagCompund.getByte("Dir"));
|
| 258 | }
|
| 259 |
|
| 260 | this.updateFacingWithBoundingBox(enumfacing);
|
| 261 | }
|
| 262 |
|
| 263 | public abstract int getWidthPixels();
|
| 264 |
|
| 265 | public abstract int getHeightPixels();
|
| 266 |
|
| 267 | /**
|
| 268 | * Called when this entity is broken. Entity parameter may be null.
|
| 269 | */
|
| 270 | public abstract void onBroken(Entity brokenEntity);
|
| 271 |
|
| 272 | protected boolean shouldSetPosAfterLoading()
|
| 273 | {
|
| 274 | return false;
|
| 275 | }
|
| 276 |
|
| 277 | /**
|
| 278 | * Sets the x,y,z of the entity from the given parameters. Also seems to set up a bounding box.
|
| 279 | */
|
| 280 | public void setPosition(double x, double y, double z)
|
| 281 | {
|
| 282 | this.posX = x;
|
| 283 | this.posY = y;
|
| 284 | this.posZ = z;
|
| 285 | BlockPos blockpos = this.hangingPosition;
|
| 286 | this.hangingPosition = new BlockPos(x, y, z);
|
| 287 |
|
| 288 | if (!this.hangingPosition.equals(blockpos))
|
| 289 | {
|
| 290 | this.updateBoundingBox();
|
| 291 | this.isAirBorne = true;
|
| 292 | }
|
| 293 | }
|
| 294 |
|
| 295 | public BlockPos getHangingPosition()
|
| 296 | {
|
| 297 | return this.hangingPosition;
|
| 298 | }
|
| 299 | }
|
| 300 |
|