package com.yolp900.itsjustacharm.common.blocks.base;
import com.google.common.collect.Lists;
import com.yolp900.itsjustacharm.ItsJustaCharm;
import com.yolp900.itsjustacharm.api.IJCConstants;
import com.yolp900.itsjustacharm.common.blocks.ModBlocks;
import com.yolp900.itsjustacharm.common.integration.waila.IWailaStackProvider;
import com.yolp900.itsjustacharm.reference.LibLocations;
import mcp.mobius.waila.api.IWailaConfigHandler;
import mcp.mobius.waila.api.IWailaDataAccessor;
import net.minecraft.block.*;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.renderer.block.statemap.StateMap;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.common.IShearable;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public abstract class ModBlockLeaves extends BlockLeaves implements IModBlock, IShearable, IWailaStackProvider {
public ModBlockLeaves(IJCConstants.Blocks block) {
this.setUnlocalizedName(block.getUnlocalizedName());
this.setRegistryName(block.getRegistryName());
this.setHardness(0.2F);
this.setSoundType(SoundType.PLANT);
this.setCreativeTab(ItsJustaCharm.creativeTab);
ModBlocks.modBlocks.add(this);
}
@Override
public void registerBlock() {
ItemBlock itemBlock = new ItemBlock(this);
itemBlock.setRegistryName(getRegistryName());
itemBlock.setUnlocalizedName(getUnlocalizedName());
registerBlock(this);
registerItemBlock(itemBlock);
}
protected void registerBlock(Block block) {
GameRegistry.register(block);
}
protected void registerItemBlock(ItemBlock itemBlock) {
GameRegistry.register(itemBlock);
}
@Override
@SideOnly(Side.CLIENT)
public void registerRender() {
Item item = Item.getItemFromBlock(this);
if (item == null || !(item instanceof ItemBlock)) {
return;
}
ItemBlock itemBlock = (ItemBlock)item;
registerRender(this);
registerRender(itemBlock);
}
@SideOnly(Side.CLIENT)
protected void registerRender(Block block) {
ModelLoader.setCustomStateMapper(block, new StateMap.Builder().ignore(CHECK_DECAY, DECAYABLE).build());
}
@SideOnly(Side.CLIENT)
protected void registerRender(ItemBlock itemBlock) {
ResourceLocation registryName = itemBlock.getRegistryName();
String domain = registryName.getResourceDomain();
String path = LibLocations.ITEMBLOCK_MODEL_FOLDER_PREFIX + registryName.getResourcePath();
ResourceLocation location = new ResourceLocation(domain, path);
ModelLoader.setCustomModelResourceLocation(itemBlock, 0, new ModelResourceLocation(location.toString()));
}
@Override
@Nonnull
@Deprecated
public IBlockState getStateFromMeta(int meta) {
return this.getDefaultState().withProperty(DECAYABLE, (meta & 4) == 0).withProperty(CHECK_DECAY, (meta & 8) > 0);
}
@Override
public int getMetaFromState(IBlockState state) {
int i = 0;
if (!state.getValue(DECAYABLE))
{
i |= 4;
}
if (state.getValue(CHECK_DECAY))
{
i |= 8;
}
return i;
}
@Override
@Nonnull
public BlockPlanks.EnumType getWoodType(int meta) {
throw new NotImplementedException();
}
@Override
public List<ItemStack> onSheared(ItemStack item, IBlockAccess world, BlockPos pos, int fortune) {
return Lists.newArrayList(new ItemStack(this, 1, 0));
}
@Override
public void breakBlock(@Nonnull World worldIn, @Nonnull BlockPos pos, @Nonnull IBlockState state) {
int k = pos.getX();
int l = pos.getY();
int i1 = pos.getZ();
if (worldIn.isAreaLoaded(new BlockPos(k - 2, l - 2, i1 - 2), new BlockPos(k + 2, l + 2, i1 + 2))) {
for (int j1 = -1; j1 <= 1; ++j1) {
for (int k1 = -1; k1 <= 1; ++k1) {
for (int l1 = -1; l1 <= 1; ++l1) {
BlockPos blockpos = pos.add(j1, k1, l1);
IBlockState iblockstate = worldIn.getBlockState(blockpos);
if (iblockstate.getBlock().isLeaves(iblockstate, worldIn, blockpos)) {
iblockstate.getBlock().beginLeavesDecay(iblockstate, worldIn, blockpos);
}
}
}
}
}
}
protected abstract ItemStack getStackDropped(IBlockState state, Random rand, int fortune);
@Override
@Nonnull
protected BlockStateContainer createBlockState() {
return new BlockStateContainer(this, CHECK_DECAY, DECAYABLE);
}
@Override
@Nonnull
public List<ItemStack> getDrops(IBlockAccess world, @Nonnull BlockPos pos, @Nonnull IBlockState state, int fortune) {
List<ItemStack> ret = new ArrayList<ItemStack>();
Random rand = world instanceof World ? ((World)world).rand : new Random();
int chance = this.getSaplingDropChance(state);
if (fortune > 0) {
chance -= 2 << fortune;
if (chance < 10) {
chance = 10;
}
}
ItemStack droppedStack = getStackDropped(state, rand, fortune);
if (rand.nextInt(chance) == 0 && droppedStack != null) {
ret.add(droppedStack);
}
chance = 200;
if (fortune > 0) {
chance -= 10 << fortune;
if (chance < 40) {
chance = 40;
}
}
this.captureDrops(true);
if (world instanceof World) {
this.dropApple((World)world, pos, state, chance);
}
ret.addAll(this.captureDrops(false));
return ret;
}
@Override
public boolean isOpaqueCube(IBlockState state) {
return Blocks.LEAVES.isOpaqueCube(state);
}
@Override
public ItemStack getWailaStack(IWailaDataAccessor accessor, IWailaConfigHandler config) {
return new ItemStack(this);
}
@Override
public boolean shouldSideBeRendered(@Nonnull IBlockState blockState, @Nonnull IBlockAccess blockAccess, @Nonnull BlockPos pos, @Nonnull EnumFacing side) {
this.leavesFancy = !Blocks.LEAVES.isOpaqueCube(blockState);
return super.shouldSideBeRendered(blockState, blockAccess, pos, side);
}
@Nonnull
@SideOnly (Side.CLIENT)
@Override
public BlockRenderLayer getBlockLayer() {
return Blocks.LEAVES.getBlockLayer();
}
}