Skip to content

Instantly share code, notes, and snippets.

/BlockPress Secret

Created March 31, 2017 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5ee21eb80fbb4f537d926abd47ca619a to your computer and use it in GitHub Desktop.
Save anonymous/5ee21eb80fbb4f537d926abd47ca619a to your computer and use it in GitHub Desktop.
package com.chocolatemod.specialblocks;
import java.util.Random;
import com.chocolatemod.init.SpecialBlocks;
import com.chocolatemod.main.Flags;
import com.chocolatemod.main.MainRegistry;
import com.chocolatemod.main.ModGuiHandler;
import com.chocolatemod.specialblocks.tileentity.TileEntityPress;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityFurnace;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public class BlockPress extends BlockContainer {
public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
public BlockPress() {
super(Material.piston);
this.setUnlocalizedName("Press");
this.setHardness(2.0F);
this.setResistance(5.0F);
this.setCreativeTab(MainRegistry.tabChocolateBlocks);
this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
}
public boolean isFullCube()
{
return false;
}
public boolean isOpaqueCube()
{
return false;
}
/**
* Sets the block's bounds for rendering it as an item
*/
/**
public void setBlockBoundsForItemRender()
{
float f = 0.0625F;
float f1 = 1.0F;
this.setBlockBounds(f*3, 0.0F, f*3, 1.0F-f*3, f1, 1.0F-f*3);
}
public AxisAlignedBB getCollisionBoundingBox(World worldIn, BlockPos pos, IBlockState state)
{
float f = 0.0625F;
float f1 = (float)(1 + (0) * 2) / 16.0F;
float f2 = 1.0F;
return new AxisAlignedBB((double)((float)pos.getX()+ f*3), (double)pos.getY(), (double)((float)pos.getZ()+ f*3), (double)((float)(pos.getX() + 1)- f*3), (double)((float)pos.getY() + f2-f*2), (double)((float)(pos.getZ() + 1)- f*3));
}
@SideOnly(Side.CLIENT)
public AxisAlignedBB getSelectedBoundingBox(World worldIn, BlockPos pos)
{
return this.getCollisionBoundingBox(worldIn, pos, worldIn.getBlockState(pos));
}
**/
public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
{
this.setDefaultFacing(worldIn, pos, state);
worldIn.setBlockState(pos.up(), SpecialBlocks.Presser.getDefaultState().withProperty(FACING, state.getValue(FACING)), Flags.SYNC);
}
private void setDefaultFacing(World worldIn, BlockPos pos, IBlockState state)
{
if (!worldIn.isRemote)
{
Block block = worldIn.getBlockState(pos.north()).getBlock();
Block block1 = worldIn.getBlockState(pos.south()).getBlock();
Block block2 = worldIn.getBlockState(pos.west()).getBlock();
Block block3 = worldIn.getBlockState(pos.east()).getBlock();
EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
if (enumfacing == EnumFacing.NORTH && block.isFullBlock() && !block1.isFullBlock())
{
enumfacing = EnumFacing.SOUTH;
}
else if (enumfacing == EnumFacing.SOUTH && block1.isFullBlock() && !block.isFullBlock())
{
enumfacing = EnumFacing.NORTH;
}
else if (enumfacing == EnumFacing.WEST && block2.isFullBlock() && !block3.isFullBlock())
{
enumfacing = EnumFacing.EAST;
}
else if (enumfacing == EnumFacing.EAST && block3.isFullBlock() && !block2.isFullBlock())
{
enumfacing = EnumFacing.WEST;
}
worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
}
}
public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
{
return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
}
@Override
public void breakBlock(World world, BlockPos pos, IBlockState blockstate) {
TileEntityPress te = (TileEntityPress) world.getTileEntity(pos);
InventoryHelper.dropInventoryItems(world, pos, te);
super.breakBlock(world, pos, blockstate);
}
@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
if (stack.hasDisplayName()) {
((TileEntityPress) worldIn.getTileEntity(pos)).setCustomName(stack.getDisplayName());
}
}
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
{
if (!worldIn.isRemote) {
playerIn.openGui(MainRegistry.modInstance, ModGuiHandler.hydraulicPress_tileEntity_gui, worldIn, pos.getX(), pos.getY(), pos.getZ());
}
return true;
}
/**
* Possibly modify the given BlockState before rendering it on an Entity (Minecarts, Endermen, ...)
*/
@SideOnly(Side.CLIENT)
public IBlockState getStateForEntityRender(IBlockState state)
{
return this.getDefaultState().withProperty(FACING, EnumFacing.SOUTH);
}
/**
* Convert the given metadata into a BlockState for this Block
*/
public IBlockState getStateFromMeta(int meta)
{
EnumFacing enumfacing = EnumFacing.getFront(meta);
if (enumfacing.getAxis() == EnumFacing.Axis.Y)
{
enumfacing = EnumFacing.NORTH;
}
return this.getDefaultState().withProperty(FACING, enumfacing);
}
/**
* Convert the BlockState into the correct metadata value
*/
public int getMetaFromState(IBlockState state)
{
return ((EnumFacing)state.getValue(FACING)).getIndex();
}
protected BlockState createBlockState()
{
return new BlockState(this, new IProperty[] {FACING});
}
@SideOnly(Side.CLIENT)
static final class SwitchEnumFacing
{
static final int[] FACING_LOOKUP = new int[EnumFacing.values().length];
private static final String __OBFID = "CL_00002111";
static
{
try
{
FACING_LOOKUP[EnumFacing.WEST.ordinal()] = 1;
}
catch (NoSuchFieldError var4)
{
;
}
try
{
FACING_LOOKUP[EnumFacing.EAST.ordinal()] = 2;
}
catch (NoSuchFieldError var3)
{
;
}
try
{
FACING_LOOKUP[EnumFacing.NORTH.ordinal()] = 3;
}
catch (NoSuchFieldError var2)
{
;
}
try
{
FACING_LOOKUP[EnumFacing.SOUTH.ordinal()] = 4;
}
catch (NoSuchFieldError var1)
{
;
}
}
}
@Override
public void onNeighborBlockChange(World world, BlockPos pos, IBlockState state, Block block)
{
if (!this.canBlockStay(world, pos))
{
world.destroyBlock(pos, true);
}
}
public boolean presserIsAbove(World world, BlockPos pos)
{
return SpecialBlocks.Presser == world.getBlockState(pos.up()).getBlock();
}
public boolean canBlockStay(World world, BlockPos pos)
{
return presserIsAbove(world, pos);
}
@Override
public int quantityDropped(Random random)
{
return 1;
}
public int getRenderType()
{
return 3;
}
@Override
public TileEntity createNewTileEntity(World worldIn, int meta) {
return new TileEntityPress();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment