Skip to content

Instantly share code, notes, and snippets.

@StillManic
Created June 30, 2015 21:20
Show Gist options
  • Save StillManic/86aa9dbb8340383b0607 to your computer and use it in GitHub Desktop.
Save StillManic/86aa9dbb8340383b0607 to your computer and use it in GitHub Desktop.
public static class CustomModelBlock4 extends Block
{
public static final PropertyDirection FACING = PropertyDirection.create("facing");
public static final CustomModelBlock4 instance = new CustomModelBlock4();
public static final String name = "CustomModelBlock4";
private ExtendedBlockState state = new ExtendedBlockState(this, new IProperty[] {FACING}, new IUnlistedProperty[]{OBJModel.OBJProperty.instance});
private CustomModelBlock4()
{
super(Material.iron);
this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
setCreativeTab(CreativeTabs.tabBlock);
setUnlocalizedName(name);
}
@Override
public IBlockState onBlockPlaced(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
{
return this.getDefaultState().withProperty(FACING, getFacingFromEntity(world, pos, placer));
}
@Override
public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
{
world.setBlockState(pos, state.withProperty(FACING, getFacingFromEntity(world, pos, placer)), 2);
// }
}
public static EnumFacing getFacing(int meta)
{
return EnumFacing.getFront(meta & 7);
}
@Override
@SideOnly(Side.CLIENT)
public IBlockState getStateForEntityRender(IBlockState state)
{
return this.getDefaultState().withProperty(FACING, EnumFacing.NORTH);
}
@Override
public IBlockState getStateFromMeta(int meta)
{
return this.getDefaultState().withProperty(FACING, getFacing(meta));
}
@Override
public int getMetaFromState(IBlockState state)
{
return ((EnumFacing) state.getValue(FACING)).getIndex();
}
@Override
public BlockState createBlockState()
{
this.state = new ExtendedBlockState(this, new IProperty[] {FACING}, new IUnlistedProperty[] {OBJModel.OBJProperty.instance});
return state;
}
@Override
public boolean isOpaqueCube() { return false; }
@Override
public boolean isFullCube() { return false; }
@Override
public boolean isVisuallyOpaque() { return false; }
@Override
public IBlockState getExtendedState(IBlockState state, IBlockAccess world, BlockPos pos)
{
FMLLog.info("CustomModelBlock4: getExtendedState", new Object[0]);
try
{
IModel model = ModelLoaderRegistry.getModel(new ResourceLocation(MODID.toLowerCase() + ":" + "block/eye.obj"));
if ()
if (((OBJModel) model).getModelState() != null && ((OBJModel) model).getModelState() instanceof OBJModel.OBJState)
{
FMLLog.info("CustomModelBlock4: Model state passed", new Object[0]);
OBJModel.OBJState retState = new OBJModel.OBJState(null, (OBJModel) model);
return ((IExtendedBlockState) this.state.getBaseState()).withProperty(OBJModel.OBJProperty.instance, retState).withProperty(FACING, state.getValue(FACING));
}
}
catch (IOException e)
{
FMLLog.info("CustomModelBlock4: failed to load block/eye.obj", new Object[0]);
}
return state;
}
// private void setElementTransforms(TRSRTransformation input)
// {
// try
// {
// IModel model = ModelLoaderRegistry.getModel(new ResourceLocation(MODID.toLowerCase(), "block/eye.obj"));
// TRSRTransformation transform = input == null ? TRSRTransformation.identity() : input;
// for (Map.Entry<String, OBJModel.Element> e : ((OBJModel) model).getMatLib().getElements().entrySet())
// {
// OBJModel.Element element = e.getValue();
// element.setTransform(transform);
// }
// }
// catch (IOException e)
// {
// FMLLog.info("CustomModelBlock4: failed to find build/eye.obj", new Object[0]);
// }
// }
public static EnumFacing getFacingFromEntity(World worldIn, BlockPos clickedBlock, EntityLivingBase entityIn)
{
if (MathHelper.abs((float)entityIn.posX - (float)clickedBlock.getX()) < 2.0F && MathHelper.abs((float)entityIn.posZ - (float)clickedBlock.getZ()) < 2.0F)
{
double d0 = entityIn.posY + (double)entityIn.getEyeHeight();
if (d0 - (double)clickedBlock.getY() > 2.0D)
{
return EnumFacing.UP;
}
if ((double)clickedBlock.getY() - d0 > 0.0D)
{
return EnumFacing.DOWN;
}
}
return entityIn.getHorizontalFacing().getOpposite();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment