package com.pam.harvestcraft.blocks.blocks; import com.pam.harvestcraft.HarvestCraft; import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.block.properties.IProperty; import net.minecraft.block.properties.PropertyInteger; import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Items; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.stats.StatList; import net.minecraft.util.BlockRenderLayer; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import javax.annotation.Nullable; import java.util.Random; public class BlockPamCake extends Block { public int CUSTOMBITES; public static final PropertyInteger BITES = PropertyInteger.create("bites", 0, 6); protected static final AxisAlignedBB[] CAKE_AABB = new AxisAlignedBB[] {new AxisAlignedBB(0.0625D, 0.0D, 0.0625D, 0.9375D, 0.5D, 0.9375D), new AxisAlignedBB(0.1875D, 0.0D, 0.0625D, 0.9375D, 0.5D, 0.9375D), new AxisAlignedBB(0.3125D, 0.0D, 0.0625D, 0.9375D, 0.5D, 0.9375D), new AxisAlignedBB(0.4375D, 0.0D, 0.0625D, 0.9375D, 0.5D, 0.9375D), new AxisAlignedBB(0.5625D, 0.0D, 0.0625D, 0.9375D, 0.5D, 0.9375D), new AxisAlignedBB(0.6875D, 0.0D, 0.0625D, 0.9375D, 0.5D, 0.9375D), new AxisAlignedBB(0.8125D, 0.0D, 0.0625D, 0.9375D, 0.5D, 0.9375D)}; public BlockPamCake(int CUSTOMBITES) { super(Material.CAKE); this.setDefaultState(this.blockState.getBaseState().withProperty(BITES, Integer.valueOf(0))); this.setTickRandomly(true); this.CUSTOMBITES = CUSTOMBITES; setCreativeTab(HarvestCraft.modTab); } public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) { return CAKE_AABB[((Integer)state.getValue(BITES)).intValue()]; } @SideOnly(Side.CLIENT) public AxisAlignedBB getSelectedBoundingBox(IBlockState state, World worldIn, BlockPos pos) { return state.getCollisionBoundingBox(worldIn, pos); } public boolean isFullCube(IBlockState state) { return false; } /** * Used to determine ambient occlusion and culling when rebuilding chunks for render */ public boolean isOpaqueCube(IBlockState state) { return false; } public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, @Nullable ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) { this.eatCake(worldIn, pos, state, playerIn); return true; } private void eatCake(World worldIn, BlockPos pos, IBlockState state, EntityPlayer player) { if (player.canEat(false)) { player.addStat(StatList.CAKE_SLICES_EATEN); player.getFoodStats().addStats(2, 0.1F); int i = ((Integer)state.getValue(BITES)).intValue(); if (i < CUSTOMBITES) { worldIn.setBlockState(pos, state.withProperty(BITES, Integer.valueOf(i + 1)), 3); } else { worldIn.setBlockToAir(pos); } } } public boolean canPlaceBlockAt(World worldIn, BlockPos pos) { return super.canPlaceBlockAt(worldIn, pos) ? this.canBlockStay(worldIn, pos) : false; } /** * Called when a neighboring block was changed and marks that this state should perform any checks during a neighbor * change. Cases may include when redstone power is updated, cactus blocks popping off due to a neighboring solid * block, etc. */ public void neighborChanged(IBlockState state, World worldIn, BlockPos pos, Block blockIn) { if (!this.canBlockStay(worldIn, pos)) { worldIn.setBlockToAir(pos); } } private boolean canBlockStay(World worldIn, BlockPos pos) { return worldIn.getBlockState(pos.down()).getMaterial().isSolid(); } /** * Returns the quantity of items to drop on block destruction. */ public int quantityDropped(Random random) { return 0; } /** * Get the Item that this Block should drop when harvested. */ @Nullable public Item getItemDropped(IBlockState state, Random rand, int fortune) { return null; } public ItemStack getItem(World worldIn, BlockPos pos, IBlockState state) { return new ItemStack(Items.CAKE); } /** * Convert the given metadata into a BlockState for this Block */ public IBlockState getStateFromMeta(int meta) { return this.getDefaultState().withProperty(BITES, Integer.valueOf(meta)); } @SideOnly(Side.CLIENT) public BlockRenderLayer getBlockLayer() { return BlockRenderLayer.CUTOUT; } /** * Convert the BlockState into the correct metadata value */ public int getMetaFromState(IBlockState state) { return ((Integer)state.getValue(BITES)).intValue(); } protected BlockStateContainer createBlockState() { return new BlockStateContainer(this, new IProperty[] {BITES}); } public int getComparatorInputOverride(IBlockState blockState, World worldIn, BlockPos pos) { return (7 - ((Integer)blockState.getValue(BITES)).intValue()) * 2; } public boolean hasComparatorInputOverride(IBlockState state) { return true; } }