| 1 | package net.minecraft.tileentity;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.util.List;
|
| 5 | import net.minecraft.block.Block;
|
| 6 | import net.minecraft.block.state.IBlockState;
|
| 7 | import net.minecraft.entity.Entity;
|
| 8 | import net.minecraft.init.Blocks;
|
| 9 | import net.minecraft.nbt.NBTTagCompound;
|
| 10 | import net.minecraft.util.AxisAlignedBB;
|
| 11 | import net.minecraft.util.EnumFacing;
|
| 12 | import net.minecraft.util.ITickable;
|
| 13 |
|
| 14 | public class TileEntityPiston extends TileEntity implements ITickable
|
| 15 | {
|
| 16 | private IBlockState pistonState;
|
| 17 | private EnumFacing pistonFacing;
|
| 18 |
|
| 19 | /** if this piston is extending or not */
|
| 20 | private boolean extending;
|
| 21 | private boolean shouldHeadBeRendered;
|
| 22 | private float progress;
|
| 23 |
|
| 24 | /** the progress in (de)extending */
|
| 25 | private float lastProgress;
|
| 26 | private List<Entity> field_174933_k = Lists.<Entity>newArrayList();
|
| 27 |
|
| 28 | public TileEntityPiston()
|
| 29 | {
|
| 30 | }
|
| 31 |
|
| 32 | public TileEntityPiston(IBlockState pistonStateIn, EnumFacing pistonFacingIn, boolean extendingIn, boolean shouldHeadBeRenderedIn)
|
| 33 | {
|
| 34 | this.pistonState = pistonStateIn;
|
| 35 | this.pistonFacing = pistonFacingIn;
|
| 36 | this.extending = extendingIn;
|
| 37 | this.shouldHeadBeRendered = shouldHeadBeRenderedIn;
|
| 38 | }
|
| 39 |
|
| 40 | public IBlockState getPistonState()
|
| 41 | {
|
| 42 | return this.pistonState;
|
| 43 | }
|
| 44 |
|
| 45 | public int getBlockMetadata()
|
| 46 | {
|
| 47 | return 0;
|
| 48 | }
|
| 49 |
|
| 50 | /**
|
| 51 | * Returns true if a piston is extending
|
| 52 | */
|
| 53 | public boolean isExtending()
|
| 54 | {
|
| 55 | return this.extending;
|
| 56 | }
|
| 57 |
|
| 58 | public EnumFacing getFacing()
|
| 59 | {
|
| 60 | return this.pistonFacing;
|
| 61 | }
|
| 62 |
|
| 63 | public boolean shouldPistonHeadBeRendered()
|
| 64 | {
|
| 65 | return this.shouldHeadBeRendered;
|
| 66 | }
|
| 67 |
|
| 68 | /**
|
| 69 | * Get interpolated progress value (between lastProgress and progress) given the fractional time between ticks as an
|
| 70 | * argument
|
| 71 | */
|
| 72 | public float getProgress(float ticks)
|
| 73 | {
|
| 74 | if (ticks > 1.0F)
|
| 75 | {
|
| 76 | ticks = 1.0F;
|
| 77 | }
|
| 78 |
|
| 79 | return this.lastProgress + (this.progress - this.lastProgress) * ticks;
|
| 80 | }
|
| 81 |
|
| 82 | public float getOffsetX(float ticks)
|
| 83 | {
|
| 84 | return this.extending ? (this.getProgress(ticks) - 1.0F) * (float)this.pistonFacing.getFrontOffsetX() : (1.0F - this.getProgress(ticks)) * (float)this.pistonFacing.getFrontOffsetX();
|
| 85 | }
|
| 86 |
|
| 87 | public float getOffsetY(float ticks)
|
| 88 | {
|
| 89 | return this.extending ? (this.getProgress(ticks) - 1.0F) * (float)this.pistonFacing.getFrontOffsetY() : (1.0F - this.getProgress(ticks)) * (float)this.pistonFacing.getFrontOffsetY();
|
| 90 | }
|
| 91 |
|
| 92 | public float getOffsetZ(float ticks)
|
| 93 | {
|
| 94 | return this.extending ? (this.getProgress(ticks) - 1.0F) * (float)this.pistonFacing.getFrontOffsetZ() : (1.0F - this.getProgress(ticks)) * (float)this.pistonFacing.getFrontOffsetZ();
|
| 95 | }
|
| 96 |
|
| 97 | private void launchWithSlimeBlock(float p_145863_1_, float p_145863_2_)
|
| 98 | {
|
| 99 | if (this.extending)
|
| 100 | {
|
| 101 | p_145863_1_ = 1.0F - p_145863_1_;
|
| 102 | }
|
| 103 | else
|
| 104 | {
|
| 105 | --p_145863_1_;
|
| 106 | }
|
| 107 |
|
| 108 | AxisAlignedBB axisalignedbb = Blocks.piston_extension.getBoundingBox(this.worldObj, this.pos, this.pistonState, p_145863_1_, this.pistonFacing);
|
| 109 |
|
| 110 | if (axisalignedbb != null)
|
| 111 | {
|
| 112 | List<Entity> list = this.worldObj.getEntitiesWithinAABBExcludingEntity((Entity)null, axisalignedbb);
|
| 113 |
|
| 114 | if (!list.isEmpty())
|
| 115 | {
|
| 116 | this.field_174933_k.addAll(list);
|
| 117 |
|
| 118 | for (Entity entity : this.field_174933_k)
|
| 119 | {
|
| 120 | if (this.pistonState.getBlock() == Blocks.slime_block && this.extending)
|
| 121 | {
|
| 122 | switch (this.pistonFacing.getAxis())
|
| 123 | {
|
| 124 | case X:
|
| 125 | entity.motionX = (double)this.pistonFacing.getFrontOffsetX();
|
| 126 | break;
|
| 127 |
|
| 128 | case Y:
|
| 129 | entity.motionY = (double)this.pistonFacing.getFrontOffsetY();
|
| 130 | break;
|
| 131 |
|
| 132 | case Z:
|
| 133 | entity.motionZ = (double)this.pistonFacing.getFrontOffsetZ();
|
| 134 | }
|
| 135 | }
|
| 136 | else
|
| 137 | {
|
| 138 | entity.moveEntity((double)(p_145863_2_ * (float)this.pistonFacing.getFrontOffsetX()), (double)(p_145863_2_ * (float)this.pistonFacing.getFrontOffsetY()), (double)(p_145863_2_ * (float)this.pistonFacing.getFrontOffsetZ()));
|
| 139 | }
|
| 140 | }
|
| 141 |
|
| 142 | this.field_174933_k.clear();
|
| 143 | }
|
| 144 | }
|
| 145 | }
|
| 146 |
|
| 147 | /**
|
| 148 | * removes a piston's tile entity (and if the piston is moving, stops it)
|
| 149 | */
|
| 150 | public void clearPistonTileEntity()
|
| 151 | {
|
| 152 | if (this.lastProgress < 1.0F && this.worldObj != null)
|
| 153 | {
|
| 154 | this.lastProgress = this.progress = 1.0F;
|
| 155 | this.worldObj.removeTileEntity(this.pos);
|
| 156 | this.invalidate();
|
| 157 |
|
| 158 | if (this.worldObj.getBlockState(this.pos).getBlock() == Blocks.piston_extension)
|
| 159 | {
|
| 160 | this.worldObj.setBlockState(this.pos, this.pistonState, 3);
|
| 161 | this.worldObj.notifyBlockOfStateChange(this.pos, this.pistonState.getBlock());
|
| 162 | }
|
| 163 | }
|
| 164 | }
|
| 165 |
|
| 166 | /**
|
| 167 | * Like the old updateEntity(), except more generic.
|
| 168 | */
|
| 169 | public void update()
|
| 170 | {
|
| 171 | this.lastProgress = this.progress;
|
| 172 |
|
| 173 | if (this.lastProgress >= 1.0F)
|
| 174 | {
|
| 175 | this.launchWithSlimeBlock(1.0F, 0.25F);
|
| 176 | this.worldObj.removeTileEntity(this.pos);
|
| 177 | this.invalidate();
|
| 178 |
|
| 179 | if (this.worldObj.getBlockState(this.pos).getBlock() == Blocks.piston_extension)
|
| 180 | {
|
| 181 | this.worldObj.setBlockState(this.pos, this.pistonState, 3);
|
| 182 | this.worldObj.notifyBlockOfStateChange(this.pos, this.pistonState.getBlock());
|
| 183 | }
|
| 184 | }
|
| 185 | else
|
| 186 | {
|
| 187 | this.progress += 0.5F;
|
| 188 |
|
| 189 | if (this.progress >= 1.0F)
|
| 190 | {
|
| 191 | this.progress = 1.0F;
|
| 192 | }
|
| 193 |
|
| 194 | if (this.extending)
|
| 195 | {
|
| 196 | this.launchWithSlimeBlock(this.progress, this.progress - this.lastProgress + 0.0625F);
|
| 197 | }
|
| 198 | }
|
| 199 | }
|
| 200 |
|
| 201 | public void readFromNBT(NBTTagCompound compound)
|
| 202 | {
|
| 203 | super.readFromNBT(compound);
|
| 204 | this.pistonState = Block.getBlockById(compound.getInteger("blockId")).getStateFromMeta(compound.getInteger("blockData"));
|
| 205 | this.pistonFacing = EnumFacing.getFront(compound.getInteger("facing"));
|
| 206 | this.lastProgress = this.progress = compound.getFloat("progress");
|
| 207 | this.extending = compound.getBoolean("extending");
|
| 208 | }
|
| 209 |
|
| 210 | public void writeToNBT(NBTTagCompound compound)
|
| 211 | {
|
| 212 | super.writeToNBT(compound);
|
| 213 | compound.setInteger("blockId", Block.getIdFromBlock(this.pistonState.getBlock()));
|
| 214 | compound.setInteger("blockData", this.pistonState.getBlock().getMetaFromState(this.pistonState));
|
| 215 | compound.setInteger("facing", this.pistonFacing.getIndex());
|
| 216 | compound.setFloat("progress", this.lastProgress);
|
| 217 | compound.setBoolean("extending", this.extending);
|
| 218 | }
|
| 219 | }
|
| 220 |
|