package pneumaticCraft.common.block;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.Entity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;
import pneumaticCraft.PneumaticCraft;
import pneumaticCraft.common.tileentity.TileEntityElevatorBase;
import pneumaticCraft.common.tileentity.TileEntityElevatorFrame;
public class BlockElevatorFrame extends BlockPneumaticCraftModeled{
public BlockElevatorFrame(Material par2Material){
super(par2Material);
}
@Override
public void onBlockAdded(World world, int x, int y, int z){
super.onBlockAdded(world, x, y, z);
TileEntityElevatorBase elevatorBase = getElevatorTE(world, x, y, z);
if(elevatorBase != null) {
elevatorBase.updateMaxElevatorHeight();
}
}
@Override
protected Class<? extends TileEntity> getTileEntityClass(){
return TileEntityElevatorFrame.class;
}
@Override
public MovingObjectPosition collisionRayTrace(World world, int x, int y, int z, Vec3 origin, Vec3 direction){
if(world.isRemote){
ItemStack playerStack = PneumaticCraft.proxy.getPlayer().getCurrentEquippedItem();
if(playerStack != null && playerStack.getItem() == Item.getItemFromBlock(this)){
return super.collisionRayTrace(world, x, y, z, origin, direction);
}
}
boolean isColliding = false;
setBlockBounds(0, 0, 0, 2 / 16F, 1, 2 / 16F);
if(super.collisionRayTrace(world, x, y, z, origin, direction) != null) isColliding = true;
setBlockBounds(0, 0, 14 / 16F, 2 / 16F, 1, 1);
if(super.collisionRayTrace(world, x, y, z, origin, direction) != null) isColliding = true;
setBlockBounds(14 / 16F, 0, 14 / 16F, 1, 1, 1);
if(super.collisionRayTrace(world, x, y, z, origin, direction) != null) isColliding = true;
setBlockBounds(14 / 16F, 0, 0, 1, 1, 2 / 16F);
if(super.collisionRayTrace(world, x, y, z, origin, direction) != null) isColliding = true;
setBlockBounds(0, 0, 0, 1, 1, 1);
return isColliding ? super.collisionRayTrace(world, x, y, z, origin, direction) : null;
}
@Override
public void onEntityCollidedWithBlock(World world, int x, int y, int z, Entity entity){
// if(world.getBlockMetadata(x, y, z) > 0) return;
TileEntityElevatorBase te = getElevatorTE(world, x, y, z);
if(te != null && te.extension - te.oldExtension > 0F) {// when the
// block's
// attached to an
// elevator and
// the elevator
// is going up.
/* float blockHeight = getElevatorBlockHeight(world, x, y, z);
if(entity.posY - y > 0 && entity.posY - y < 1 && blockHeight > entity.posY - y) {
entity.posY = y + blockHeight;
}*/
if(getElevatorBlockHeight(world, x, y, z) > 0F) {
//System.out.println("extDif: " + (te.extension - te.oldExtension));
// entity.posY += te.extension - te.oldExtension;
entity.setPosition(entity.posX, entity.posY + te.extension - te.oldExtension + 0.05F, entity.posZ);
}
/* float blockHeight = getElevatorBlockHeight(world, x, y, z);
if(blockHeight > 0F && entity.posY < blockHeight + y) {
//System.out.println("extDif: " + (te.extension - te.oldExtension));
// entity.posY += te.extension - te.oldExtension;
entity.moveEntity(0, te.extension - te.oldExtension + 0.05F, 0);
}
*/
}
}
/**
* Returns a bounding box from the pool of bounding boxes (this means this
* box can change after the pool has been cleared to be reused)
*/
@Override
public AxisAlignedBB getCollisionBoundingBoxFromPool(World par1World, int par2, int par3, int par4){
float blockHeight = getElevatorBlockHeight(par1World, par2, par3, par4);
if(blockHeight > 0F) {
// this.setBlockBounds(0, 0, 0, 1, blockHeight, 1);
// return super.getCollisionBoundingBoxFromPool(par1World, par2,
// par3, par4);
return AxisAlignedBB.getBoundingBox(par2, par3, par4, par2 + 1, par3 + blockHeight, par4 + 1);
} else {
return null;
}
// return null;
}
public TileEntityElevatorBase getElevatorTE(World world, int x, int y, int z){
int i = 0;
while(true) {
i--;
if(world.getBlock(x, y + i, z) == Blockss.elevatorBase) break;
if(world.getBlock(x, y + i, z) != Blockss.elevatorFrame || y <= 0) return null;
}
return (TileEntityElevatorBase)world.getTileEntity(x, y + i, z);
}
private float getElevatorBlockHeight(World world, int x, int y, int z){
TileEntityElevatorBase te = getElevatorTE(world, x, y, z);
if(te == null) return 0F;
float blockHeight = te.extension - (y - te.yCoord) + 1;
// System.out.println("blockHeight (" + x + ", " + y + ", " + z + "): " + blockHeight);
// + blockHeight);
if(blockHeight < 0F) return 0F;
if(blockHeight > 1F) return 1F;
return blockHeight;
}
@Override
public void onNeighborBlockChange(World world, int x, int y, int z, Block block){
int blockMeta = world.getBlockMetadata(x, y, z);
if(blockMeta == 0 && world.getBlock(x, y - 1, z) == Blockss.elevatorBase) {
world.setBlockMetadataWithNotify(x, y, z, 1, 2);
} else if(blockMeta == 1) {
world.setBlockMetadataWithNotify(x, y, z, 0, 2);
}
}
@Override
public void breakBlock(World world, int x, int y, int z, Block block, int meta){
TileEntityElevatorBase elevatorBase = getElevatorTE(world, x, y, z);
if(elevatorBase != null) {
elevatorBase.updateMaxElevatorHeight();
}
super.breakBlock(world, x, y, z, block, meta);
}
}