Skip to content

Instantly share code, notes, and snippets.

@Asherslab
Created June 30, 2018 08:23
Show Gist options
  • Save Asherslab/f39d8dc9421499f163be66d522143410 to your computer and use it in GitHub Desktop.
Save Asherslab/f39d8dc9421499f163be66d522143410 to your computer and use it in GitHub Desktop.
package com.minecolonies.coremod.blocks.decorative;
import com.minecolonies.api.util.constant.Constants;
import com.minecolonies.api.util.constant.NbtTagConstants;
import com.minecolonies.coremod.blocks.AbstractBlockMinecoloniesStairs;
import com.minecolonies.coremod.creativetab.ModCreativeTabs;
import com.minecolonies.coremod.tileentities.TileEntityShingle;
import net.minecraft.block.BlockPlanks;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.property.ExtendedBlockState;
import net.minecraftforge.common.property.IExtendedBlockState;
import net.minecraftforge.common.property.IUnlistedProperty;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Locale;
public class BlockShingleNew extends AbstractBlockMinecoloniesStairs<BlockShingleNew> implements ITileEntityProvider
{
public static final PropertyEnum<BlockPlanks.EnumType> VARIANT = PropertyEnum.create("variant", BlockPlanks.EnumType.class);
private ExtendedBlockState state = new ExtendedBlockState(this, new IProperty[]{VARIANT}, new IUnlistedProperty[]{});
/**
* The hardness this block has.
*/
private static final float BLOCK_HARDNESS = 3F;
/**
* The resistance this block has.
*/
private static final float RESISTANCE = 1F;
/**
* Light opacity of the block.
*/
private static final int LIGHT_OPACITY = 255;
/**
* Prefix of the block.
*/
public static final String BLOCK_PREFIX = "blockshinglenew";
public BlockShingleNew(final IBlockState modelState, final String name)
{
super(modelState);
this.setDefaultState(this.blockState.getBaseState().withProperty(VARIANT, BlockPlanks.EnumType.OAK));
init(name);
}
private void init(final String name)
{
setRegistryName(name);
setUnlocalizedName(String.format("%s.%s", Constants.MOD_ID.toLowerCase(Locale.US), name));
setCreativeTab(ModCreativeTabs.MINECOLONIES);
setHardness(BLOCK_HARDNESS);
setResistance(RESISTANCE);
this.useNeighborBrightness = true;
this.setLightOpacity(LIGHT_OPACITY);
}
@Override
public void getSubBlocks(final CreativeTabs itemIn, final NonNullList<ItemStack> items)
{
for (final BlockPlanks.EnumType woodType : BlockPlanks.EnumType.values())
{
ItemStack stack = new ItemStack(Item.getItemFromBlock(this));
final NBTTagCompound compound = new NBTTagCompound();
compound.setInteger(NbtTagConstants.TAG_WOOD_TYPE, woodType.getMetadata());
stack.setTagCompound(compound);
items.add(stack);
}
}
@Nullable
@Override
public TileEntity createNewTileEntity(final World world, final int i)
{
return new TileEntityShingle();
}
@Override
public boolean onBlockActivated(
final World worldIn,
final BlockPos pos,
final IBlockState state,
final EntityPlayer playerIn,
final EnumHand hand,
final EnumFacing facing,
final float hitX,
final float hitY,
final float hitZ)
{
System.out.println("BlockState: " + state);
return super.onBlockActivated(worldIn, pos, state, playerIn, hand, facing, hitX, hitY, hitZ);
}
@Override
protected BlockStateContainer createBlockState()
{
Collection<IProperty<?>> properties = new ArrayList<>(super.createBlockState().getProperties());
properties.add(VARIANT);
return new ExtendedBlockState(this, properties.toArray(new IProperty<?>[properties.size()]), new IUnlistedProperty[]{});
}
@Override
public IBlockState getExtendedState(final IBlockState state, final IBlockAccess world, final BlockPos pos)
{
if (world.getTileEntity(pos) == null) return this.state.getBaseState();
final TileEntityShingle tileEntity = (TileEntityShingle) world.getTileEntity(pos);
System.out.println("State: " + state);
BlockPlanks.EnumType woodtype = null;
if (tileEntity != null)
{
woodtype = tileEntity.getWoodType();
System.out.println("woodType: " + woodtype);
}
if (woodtype == null)
{
woodtype = BlockPlanks.EnumType.OAK;
}
System.out.println("Woodtype: " + woodtype);
final IBlockState blockState = ((IExtendedBlockState) state).withProperty(VARIANT, woodtype);
System.out.println("BlockState: " + blockState);
return state;
}
@Override
public void onBlockPlacedBy(final World worldIn, final BlockPos pos, final IBlockState state, final EntityLivingBase placer, final ItemStack stack)
{
final NBTTagCompound compound = stack.getTagCompound();
if (compound != null)
{
final BlockPlanks.EnumType woodType = BlockPlanks.EnumType.byMetadata(compound.getInteger(NbtTagConstants.TAG_WOOD_TYPE));
TileEntityShingle tileEntity = (TileEntityShingle) worldIn.getTileEntity(pos);
if (tileEntity != null)
{
tileEntity.setWoodType(woodType);
tileEntity.markDirty();
}
}
super.onBlockPlacedBy(worldIn, pos, state, placer, stack);
}
@Override
public void getDrops(final NonNullList<ItemStack> drops, final IBlockAccess world, final BlockPos pos, final IBlockState state, final int fortune)
{
drops.add(generateItemStackFromWorldPos(world, pos, state));
}
@Override
public ItemStack getPickBlock(final IBlockState state, final RayTraceResult target, final World world, final BlockPos pos, final EntityPlayer player)
{
return generateItemStackFromWorldPos(world, pos, state);
}
private ItemStack generateItemStackFromWorldPos(IBlockAccess world, BlockPos pos, IBlockState state) {
TileEntity worldEntity = world.getTileEntity(pos);
if(worldEntity == null || !(worldEntity instanceof TileEntityShingle))
return ItemStack.EMPTY;
final TileEntityShingle tileEntity = (TileEntityShingle) worldEntity;
final NBTTagCompound compound = new NBTTagCompound();
compound.setInteger(NbtTagConstants.TAG_WOOD_TYPE, tileEntity.getWoodType().getMetadata());
final ItemStack stack = new ItemStack(Item.getItemFromBlock(state.getBlock()));
stack.setTagCompound(compound);
return stack;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment