package com.yolp900.itsjustacharm.common.blocks.base;
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.Block;
import net.minecraft.block.BlockSapling;
import net.minecraft.block.SoundType;
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.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import javax.annotation.Nonnull;
import java.util.List;
import java.util.Random;
public abstract class ModBlockSapling extends BlockSapling implements IModBlock, IWailaStackProvider {
public ModBlockSapling(IJCConstants.Blocks block) {
this.setUnlocalizedName(block.getUnlocalizedName());
this.setRegistryName(block.getRegistryName());
this.setHardness(0.0F);
this.setSoundType(SoundType.PLANT);
this.setCreativeTab(ItsJustaCharm.creativeTab);
ModBlocks.modBlocks.add(this);
this.setDefaultState(this.blockState.getBaseState().withProperty(STAGE, 0));
}
@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(TYPE).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
public abstract void generateTree(@Nonnull World worldIn, @Nonnull BlockPos pos, @Nonnull IBlockState state, @Nonnull Random rand);
@Override
public void getSubBlocks(@Nonnull Item item, CreativeTabs tab, @Nonnull List<ItemStack> list) {
list.add(new ItemStack(item, 1, 0));
}
@Override
@Nonnull
public IBlockState getStateFromMeta(int meta) {
return this.getDefaultState().withProperty(STAGE, (meta & 8) >> 3);
}
public int getMetaFromState(IBlockState state) {
int i = 0;
i = i | state.getValue(STAGE) << 3;
return i;
}
@Override
public ItemStack getWailaStack(IWailaDataAccessor accessor, IWailaConfigHandler config) {
return new ItemStack(this);
}
}