blob: 7c684c41a9fec5fb5fd4afe7f748d390925c37a4 [file] [log] [blame] [raw]
package mekanism.common.block;
import java.util.Random;
import mekanism.api.gas.IGasItem;
import mekanism.common.Mekanism;
import mekanism.common.MekanismBlocks;
import mekanism.common.base.ISustainedInventory;
import mekanism.common.base.ITierItem;
import mekanism.common.security.ISecurityItem;
import mekanism.common.security.ISecurityTile;
import mekanism.common.security.ISecurityTile.SecurityMode;
import mekanism.common.tile.TileEntityBasicBlock;
import mekanism.common.tile.TileEntityGasTank;
import mekanism.common.util.MekanismUtils;
import mekanism.common.util.SecurityUtils;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.MathHelper;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;
import buildcraft.api.tools.IToolWrench;
public class BlockGasTank extends BlockContainer
{
public BlockGasTank()
{
super(Material.iron);
setBlockBounds(0.2F, 0.0F, 0.2F, 0.8F, 1.0F, 0.8F);
setHardness(3.5F);
setResistance(8F);
setCreativeTab(Mekanism.tabMekanism);
}
/*
@Override
@SideOnly(Side.CLIENT)
public void registerBlockIcons(IIconRegister register)
{
blockIcon = register.registerIcon("mekanism:SteelCasing");
}
*/
@Override
public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
{
TileEntityBasicBlock tileEntity = (TileEntityBasicBlock)world.getTileEntity(pos);
int side = MathHelper.floor_double((double)(placer.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
int change = 3;
switch(side)
{
case 0: change = 2; break;
case 1: change = 5; break;
case 2: change = 3; break;
case 3: change = 4; break;
}
tileEntity.setFacing((short)change);
tileEntity.redstone = world.isBlockIndirectlyGettingPowered(pos) > 0;
}
@Override
public void onNeighborBlockChange(World world, BlockPos pos, IBlockState state, Block neighborBlock)
{
if(!world.isRemote)
{
TileEntity tileEntity = world.getTileEntity(pos);
if(tileEntity instanceof TileEntityBasicBlock)
{
((TileEntityBasicBlock)tileEntity).onNeighborChange(neighborBlock);
}
}
}
@Override
public float getBlockHardness(World world, BlockPos pos)
{
TileEntity tile = world.getTileEntity(pos);
if(tile instanceof ISecurityTile)
{
return SecurityUtils.getSecurity((ISecurityTile)tile) == SecurityMode.PUBLIC ? blockHardness : -1;
}
return blockHardness;
}
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer entityplayer, EnumFacing side, float hitX, float hitY, float hitZ)
{
if(world.isRemote)
{
return true;
}
TileEntityGasTank tileEntity = (TileEntityGasTank)world.getTileEntity(pos);
if(entityplayer.getCurrentEquippedItem() != null)
{
Item tool = entityplayer.getCurrentEquippedItem().getItem();
if(MekanismUtils.hasUsableWrench(entityplayer, pos))
{
if(SecurityUtils.canAccess(entityplayer, tileEntity))
{
if(entityplayer.isSneaking())
{
dismantleBlock(world, pos, false);
return true;
}
if(MekanismUtils.isBCWrench(tool))
{
((IToolWrench)tool).wrenchUsed(entityplayer, pos);
}
int change = tileEntity.facing.rotateY().ordinal();
tileEntity.setFacing((short)change);
world.notifyNeighborsOfStateChange(pos, this);
}
else {
SecurityUtils.displayNoAccess(entityplayer);
}
return true;
}
}
if(tileEntity != null)
{
if(!entityplayer.isSneaking())
{
if(SecurityUtils.canAccess(entityplayer, tileEntity))
{
entityplayer.openGui(Mekanism.instance, 10, world, pos.getX(), pos.getY(), pos.getZ());
}
else {
SecurityUtils.displayNoAccess(entityplayer);
}
return true;
}
}
return false;
}
@Override
public boolean removedByPlayer(World world, BlockPos pos, EntityPlayer player, boolean willHarvest)
{
if(!player.capabilities.isCreativeMode && !world.isRemote && willHarvest)
{
float motion = 0.7F;
double motionX = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
double motionY = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
double motionZ = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
EntityItem entityItem = new EntityItem(world, pos.getX() + motionX, pos.getY() + motionY, pos.getZ() + motionZ, getPickBlock(null, world, pos, player));
world.spawnEntityInWorld(entityItem);
}
return world.setBlockToAir(pos);
}
public ItemStack dismantleBlock(World world, BlockPos pos, boolean returnBlock)
{
ItemStack itemStack = getPickBlock(null, world, pos, null);
world.setBlockToAir(pos);
if(!returnBlock)
{
float motion = 0.7F;
double motionX = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
double motionY = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
double motionZ = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
EntityItem entityItem = new EntityItem(world, pos.getX() + motionX, pos.getY() + motionY, pos.getZ() + motionZ, itemStack);
world.spawnEntityInWorld(entityItem);
}
return itemStack;
}
@Override
public int quantityDropped(Random random)
{
return 0;
}
@Override
public Item getItemDropped(IBlockState state, Random random, int fortune)
{
return null;
}
@Override
public boolean isOpaqueCube()
{
return false;
}
@Override
public int getRenderType()
{
return 3;
}
@Override
public TileEntity createNewTileEntity(World world, int meta)
{
return new TileEntityGasTank();
}
@Override
public ItemStack getPickBlock(MovingObjectPosition target, World world, BlockPos pos, EntityPlayer player)
{
TileEntityGasTank tileEntity = (TileEntityGasTank)world.getTileEntity(pos);
ItemStack itemStack = new ItemStack(MekanismBlocks.GasTank);
if(itemStack.hasTagCompound())
{
itemStack.setTagCompound(new NBTTagCompound());
}
if(tileEntity instanceof ISecurityTile)
{
ISecurityItem securityItem = (ISecurityItem)itemStack.getItem();
if(securityItem.hasSecurity(itemStack))
{
securityItem.setOwner(itemStack, ((ISecurityTile)tileEntity).getSecurity().getOwner());
securityItem.setSecurity(itemStack, ((ISecurityTile)tileEntity).getSecurity().getMode());
}
}
ITierItem tierItem = (ITierItem)itemStack.getItem();
tierItem.setBaseTier(itemStack, tileEntity.tier.getBaseTier());
IGasItem storageTank = (IGasItem)itemStack.getItem();
storageTank.setGas(itemStack, tileEntity.gasTank.getGas());
ISustainedInventory inventory = (ISustainedInventory)itemStack.getItem();
inventory.setInventory(tileEntity.getInventory(), itemStack);
return itemStack;
}
@Override
public boolean hasComparatorInputOverride()
{
return true;
}
@Override
public int getComparatorInputOverride(World world, BlockPos pos)
{
TileEntityGasTank tileEntity = (TileEntityGasTank)world.getTileEntity(pos);
return tileEntity.getRedstoneLevel();
}
@Override
public EnumFacing[] getValidRotations(World world, BlockPos pos)
{
TileEntity tile = world.getTileEntity(pos);
EnumFacing[] valid = new EnumFacing[6];
if(tile instanceof TileEntityBasicBlock)
{
TileEntityBasicBlock basicTile = (TileEntityBasicBlock)tile;
for(EnumFacing dir : EnumFacing.VALUES)
{
if(basicTile.canSetFacing(dir.ordinal()))
{
valid[dir.ordinal()] = dir;
}
}
}
return valid;
}
@Override
public boolean rotateBlock(World world, BlockPos pos, EnumFacing axis)
{
TileEntity tile = world.getTileEntity(pos);
if(tile instanceof TileEntityBasicBlock)
{
TileEntityBasicBlock basicTile = (TileEntityBasicBlock)tile;
if(basicTile.canSetFacing(axis.ordinal()))
{
basicTile.setFacing((short)axis.ordinal());
return true;
}
}
return false;
}
}