package micdoodle8.mods.galacticraft.core.blocks;
import micdoodle8.mods.galacticraft.core.GalacticraftCore;
import micdoodle8.mods.galacticraft.core.items.IShiftDescription;
import micdoodle8.mods.galacticraft.core.tile.TileEntityParaChest;
import micdoodle8.mods.galacticraft.core.util.EnumSortCategoryBlock;
import micdoodle8.mods.galacticraft.core.util.GCCoreUtil;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.passive.EntityOcelot;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.EnumDyeColor;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import java.util.Iterator;
import java.util.Random;
public class BlockParaChest extends BlockContainer implements ITileEntityProvider, IShiftDescription, ISortableBlock
{
public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
public static final PropertyEnum<EnumDyeColor> COLOR = PropertyEnum.create("color", EnumDyeColor.class);
public BlockParaChest(String assetName)
{
super(Material.wood);
this.setHardness(3.0F);
this.setStepSound(Block.soundTypeWood);
this.setBlockBounds(0.0625F, 0.0F, 0.0625F, 0.9375F, 0.875F, 0.9375F);
this.setUnlocalizedName(assetName);
this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
}
@Override
public int getRenderType()
{
return 3;
}
@Override
public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
{
return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing());
}
@Override
public CreativeTabs getCreativeTabToDisplayOn()
{
return GalacticraftCore.galacticraftBlocksTab;
}
@Override
public boolean isOpaqueCube()
{
return false;
}
@Override
public boolean isFullCube()
{
return false;
}
@Override
public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
{
super.onBlockAdded(worldIn, pos, state);
}
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
{
if (worldIn.isRemote)
{
return true;
}
else
{
IInventory iinventory = this.getInventory(worldIn, pos);
if (iinventory != null && playerIn instanceof EntityPlayerMP)
{
playerIn.openGui(GalacticraftCore.instance, -1, worldIn, pos.getX(), pos.getY(), pos.getZ());
}
return true;
}
}
@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
{
super.onBlockPlacedBy(worldIn, pos, state, placer, stack);
}
@Override
public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
{
TileEntityParaChest tileentitychest = (TileEntityParaChest) worldIn.getTileEntity(pos);
if (tileentitychest != null)
{
tileentitychest.updateContainingBlockInfo();
}
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
{
TileEntityParaChest tileentitychest = (TileEntityParaChest) worldIn.getTileEntity(pos);
if (tileentitychest != null)
{
Random syncRandom = GCCoreUtil.getRandom(pos);
for (int j1 = 0; j1 < tileentitychest.getSizeInventory(); ++j1)
{
ItemStack itemstack = tileentitychest.getStackInSlot(j1);
if (itemstack != null)
{
float f = syncRandom.nextFloat() * 0.8F + 0.1F;
float f1 = syncRandom.nextFloat() * 0.8F + 0.1F;
EntityItem entityitem;
for (float f2 = syncRandom.nextFloat() * 0.8F + 0.1F; itemstack.stackSize > 0; worldIn.spawnEntityInWorld(entityitem))
{
int k1 = syncRandom.nextInt(21) + 10;
if (k1 > itemstack.stackSize)
{
k1 = itemstack.stackSize;
}
itemstack.stackSize -= k1;
entityitem = new EntityItem(worldIn, pos.getX() + f, pos.getY() + f1, pos.getZ() + f2, new ItemStack(itemstack.getItem(), k1, itemstack.getItemDamage()));
float f3 = 0.05F;
entityitem.motionX = (float) syncRandom.nextGaussian() * f3;
entityitem.motionY = (float) syncRandom.nextGaussian() * f3 + 0.2F;
entityitem.motionZ = (float) syncRandom.nextGaussian() * f3;
if (itemstack.hasTagCompound())
{
entityitem.getEntityItem().setTagCompound((NBTTagCompound) itemstack.getTagCompound().copy());
}
}
}
}
worldIn.updateComparatorOutputLevel(pos, null);
}
super.breakBlock(worldIn, pos, state);
}
public IInventory getInventory(World par1World, BlockPos pos)
{
Object object = par1World.getTileEntity(pos);
if (object == null)
{
return null;
}
else if (par1World.isSideSolid(pos.offset(EnumFacing.UP), EnumFacing.DOWN))
{
return null;
}
else if (BlockParaChest.isOcelotBlockingChest(par1World, pos))
{
return null;
}
else
{
return (IInventory) object;
}
}
public static boolean isOcelotBlockingChest(World par0World, BlockPos pos)
{
Iterator<?> iterator = par0World.getEntitiesWithinAABB(EntityOcelot.class, AxisAlignedBB.fromBounds(pos.getX(), pos.getY() + 1, pos.getZ(), pos.getX() + 1, pos.getY() + 2, pos.getZ() + 1)).iterator();
EntityOcelot entityocelot;
do
{
if (!iterator.hasNext())
{
return false;
}
entityocelot = (EntityOcelot) iterator.next();
}
while (!entityocelot.isSitting());
return true;
}
@Override
public TileEntity createNewTileEntity(World par1World, int meta)
{
return new TileEntityParaChest();
}
@Override
public String getShiftDescription(int meta)
{
return GCCoreUtil.translate(this.getUnlocalizedName() + ".description");
}
@Override
public boolean showDescription(int meta)
{
return true;
}
@Override
public IBlockState getStateFromMeta(int meta)
{
EnumFacing enumfacing = EnumFacing.getFront(meta);
if (enumfacing.getAxis() == EnumFacing.Axis.Y)
{
enumfacing = EnumFacing.NORTH;
}
return this.getDefaultState().withProperty(FACING, enumfacing);
}
@Override
public int getMetaFromState(IBlockState state)
{
return (state.getValue(FACING)).getIndex();
}
@Override
protected BlockState createBlockState()
{
return new BlockState(this, COLOR, FACING);
}
@Override
public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos)
{
TileEntity tile = worldIn.getTileEntity(pos);
if (!(tile instanceof TileEntityParaChest))
{
return state;
}
TileEntityParaChest chest = (TileEntityParaChest) tile;
return state.withProperty(COLOR, chest.color);
}
@Override
public EnumSortCategoryBlock getCategory(int meta)
{
return EnumSortCategoryBlock.GENERAL;
}
}