/*******************************************************************************
* AbyssalCraft
* Copyright (c) 2012 - 2017 Shinoow.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Contributors:
* Shinoow - implementation
******************************************************************************/
package com.shinoow.abyssalcraft.common.blocks;
import java.util.Random;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.internal.FMLNetworkHandler;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import com.shinoow.abyssalcraft.AbyssalCraft;
import com.shinoow.abyssalcraft.api.block.ACBlocks;
import com.shinoow.abyssalcraft.common.blocks.tile.TileEntityMaterializer;
import com.shinoow.abyssalcraft.lib.ACLib;
import com.shinoow.abyssalcraft.lib.ACTabs;
public class BlockMaterializer extends BlockContainer {
public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
private final Random rand = new Random();
private static boolean keepInventory;
public BlockMaterializer() {
super(Material.ROCK);
setDefaultState(blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
setHarvestLevel("pickaxe", 8);
setHardness(100);
setResistance(Float.MAX_VALUE);
setSoundType(SoundType.STONE);
setCreativeTab(ACTabs.tabDecoration);
}
@Override
public Item getItemDropped(IBlockState state, Random par1Random, int par3)
{
return Item.getItemFromBlock(ACBlocks.materializer);
}
@Override
public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
{
setDefaultFacing(worldIn, pos, state);
}
private void setDefaultFacing(World worldIn, BlockPos pos, IBlockState state)
{
if (!worldIn.isRemote)
{
IBlockState block = worldIn.getBlockState(pos.north());
IBlockState block1 = worldIn.getBlockState(pos.south());
IBlockState block2 = worldIn.getBlockState(pos.west());
IBlockState block3 = worldIn.getBlockState(pos.east());
EnumFacing enumfacing = state.getValue(FACING);
if (enumfacing == EnumFacing.NORTH && block.isFullBlock() && !block1.isFullBlock())
enumfacing = EnumFacing.SOUTH;
else if (enumfacing == EnumFacing.SOUTH && block1.isFullBlock() && !block.isFullBlock())
enumfacing = EnumFacing.NORTH;
else if (enumfacing == EnumFacing.WEST && block2.isFullBlock() && !block3.isFullBlock())
enumfacing = EnumFacing.EAST;
else if (enumfacing == EnumFacing.EAST && block3.isFullBlock() && !block2.isFullBlock())
enumfacing = EnumFacing.WEST;
worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
}
}
// }
@Override
public boolean onBlockActivated(World par1World, BlockPos pos, IBlockState state, EntityPlayer par5EntityPlayer, EnumHand hand, EnumFacing side, float par7, float par8, float par9) {
if(!par1World.isRemote)
FMLNetworkHandler.openGui(par5EntityPlayer, AbyssalCraft.instance, ACLib.materializerGuiID, par1World, pos.getX(), pos.getY(), pos.getZ());
return true;
}
// public static void updateMaterializerBlockState(boolean par0, World par1World, int par2, int par3, int par4) {
// TileEntity tileentity = par1World.getTileEntity(par2, par3, par4);
// keepInventory = true;
//
// par1World.setBlock(par2, par3, par4, AbyssalCraft.materializer);
//
// keepInventory = false;
//
// if (tileentity != null){
// tileentity.validate();
// par1World.setTileEntity(par2, par3, par4, tileentity);
// }
// }
/**
* Returns a new instance of a block's tile entity class. Called on placing the block.
*/
@Override
public TileEntity createNewTileEntity(World par1World, int par2)
{
return new TileEntityMaterializer();
}
/**
* Called when the block is placed in the world.
*/
@Override
public void onBlockPlacedBy(World par1World, BlockPos pos, IBlockState state, EntityLivingBase par5EntityLivingBase, ItemStack par6ItemStack) {
par1World.setBlockState(pos, state.withProperty(FACING, par5EntityLivingBase.getHorizontalFacing().getOpposite()), 2);
if (par6ItemStack.hasDisplayName())
((TileEntityMaterializer)par1World.getTileEntity(pos)).func_145951_a(par6ItemStack.getDisplayName());
}
@Override
public void breakBlock(World par1World, BlockPos pos, IBlockState state) {
if (!keepInventory){
TileEntityMaterializer tileentitymaterializer = (TileEntityMaterializer)par1World.getTileEntity(pos);
if (tileentitymaterializer != null){
InventoryHelper.dropInventoryItems(par1World, pos, tileentitymaterializer);
par1World.updateComparatorOutputLevel(pos, this);
}
}
super.breakBlock(par1World, pos, state);
}
@Override
public boolean hasComparatorInputOverride(IBlockState state)
{
return true;
}
@Override
public int getComparatorInputOverride(IBlockState state, World par1World, BlockPos pos)
{
return Container.calcRedstone(par1World.getTileEntity(pos));
}
@Override
@SideOnly(Side.CLIENT)
public ItemStack getItem(World par1World, BlockPos pos, IBlockState state)
{
return new ItemStack(ACBlocks.materializer);
}
@Override
public EnumBlockRenderType getRenderType(IBlockState state)
{
return EnumBlockRenderType.MODEL;
}
// /**
// * Possibly modify the given BlockState before rendering it on an Entity (Minecarts, Endermen, ...)
// */
// @Override
// @SideOnly(Side.CLIENT)
// public IBlockState getStateForEntityRender(IBlockState state)
// {
// return getDefaultState().withProperty(FACING, EnumFacing.SOUTH);
// }
/**
* Convert the given metadata into a BlockState for this Block
*/
@Override
public IBlockState getStateFromMeta(int meta)
{
EnumFacing enumfacing = EnumFacing.getFront(meta);
if (enumfacing.getAxis() == EnumFacing.Axis.Y)
enumfacing = EnumFacing.NORTH;
return getDefaultState().withProperty(FACING, enumfacing);
}
/**
* Convert the BlockState into the correct metadata value
*/
@Override
public int getMetaFromState(IBlockState state)
{
return state.getValue(FACING).getIndex();
}
@Override
protected BlockStateContainer createBlockState()
{
return new BlockStateContainer.Builder(this).add(FACING).build();
}
}