Guest User

Untitled

a guest
Jan 27th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.87 KB | None | 0 0
  1. package com.titanjc.titancraft.blocks;
  2.  
  3. import com.titanjc.titancraft.Constants;
  4. import com.titanjc.titancraft.TitCMain;
  5. import net.minecraft.block.Block;
  6. import net.minecraft.block.BlockHorizontal;
  7. import net.minecraft.block.ITileEntityProvider;
  8. import net.minecraft.block.material.Material;
  9. import net.minecraft.block.properties.IProperty;
  10. import net.minecraft.block.properties.PropertyBool;
  11. import net.minecraft.block.properties.PropertyEnum;
  12. import net.minecraft.block.properties.PropertyInteger;
  13. import net.minecraft.block.state.BlockStateContainer;
  14. import net.minecraft.block.state.IBlockState;
  15. import net.minecraft.entity.EntityLivingBase;
  16. import net.minecraft.tileentity.TileEntity;
  17. import net.minecraft.util.EnumFacing;
  18. import net.minecraft.util.math.BlockPos;
  19. import net.minecraft.world.IBlockAccess;
  20. import net.minecraft.world.World;
  21.  
  22. /**
  23.  * Created by Student on 1/26/2017.
  24.  */
  25. public class BlockShelf extends BlockHorizontal implements ITileEntityProvider {
  26.     private Boolean left;
  27.     private Boolean center;
  28.     private Boolean right;
  29.     private Boolean lonely;
  30.  
  31.     private static final PropertyInteger CONNECTION = PropertyInteger.create("connections", 1, 4);
  32.  
  33.     private enum Connections{
  34.         LEFT(1),
  35.         RIGHT(2),
  36.         CENTER(3),
  37.         LONELY(4);
  38.  
  39.         private final int setting;
  40.  
  41.         Connections(int setting) {
  42.             this.setting = setting;
  43.         }
  44.  
  45.         public int getSetting() {
  46.             return setting;
  47.         }
  48.     }
  49.  
  50.     public BlockShelf() {
  51.         super(Material.WOOD);
  52.         setUnlocalizedName(Constants.TitanCraftBlocks.SHELF.getUnlocalizedName());
  53.         setRegistryName(Constants.TitanCraftBlocks.SHELF.getRegistryName());
  54.         setCreativeTab(TitCMain.CREATIVE_TAB);
  55.         setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.WEST).withProperty(CONNECTION, Connections.LONELY.getSetting()));
  56.         left = right = center = lonely = false;
  57.     }
  58.  
  59.     @Override
  60.     public TileEntity createNewTileEntity(World worldIn, int meta) {
  61.         return null;
  62.     }
  63.  
  64.     @Override
  65.     public boolean isFullCube(IBlockState state) {
  66.         return false;
  67.     }
  68.  
  69.     @Override
  70.     public boolean isOpaqueCube(IBlockState state) {
  71.         return false;
  72.     }
  73.  
  74.     private boolean canConnectTo(IBlockAccess worldIn, BlockPos pos) {
  75.         IBlockState iblockstate = worldIn.getBlockState(pos);
  76.         Block block = iblockstate.getBlock();
  77.         return (block instanceof BlockShelf);
  78.     }
  79.  
  80.     @Override
  81.     public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) {
  82.         IBlockState state = super.onBlockPlaced(worldIn, pos, facing, hitX, hitY, hitZ, meta, placer);
  83.         return state.withProperty(FACING, placer.getHorizontalFacing());
  84.     }
  85.  
  86.     @Override
  87.     public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) {
  88.  
  89.         switch(state.getValue(FACING)) {
  90.             case NORTH:
  91.                 left = canConnectTo(worldIn, pos.west());
  92.                 right = canConnectTo(worldIn, pos.east());
  93.                 break;
  94.  
  95.             case EAST:
  96.                 left = canConnectTo(worldIn, pos.north());
  97.                 right = canConnectTo(worldIn, pos.south());
  98.                 break;
  99.  
  100.             case WEST:
  101.                 left = canConnectTo(worldIn, pos.south());
  102.                 right = canConnectTo(worldIn, pos.north());
  103.                 break;
  104.  
  105.             case SOUTH:
  106.                 left = canConnectTo(worldIn, pos.east());
  107.                 right = canConnectTo(worldIn, pos.west());
  108.                 break;
  109.         }
  110.         center = (left && right);
  111.         if(!center && !left && !right)
  112.             return state.withProperty(CONNECTION, Connections.LONELY.getSetting()).withProperty(FACING, EnumFacing.getHorizontal(getMetaFromState(state)));
  113.         if(center)
  114.             return state.withProperty(CONNECTION, Connections.CENTER.getSetting()).withProperty(FACING, EnumFacing.getHorizontal(getMetaFromState(state)));
  115.         if(left)
  116.             return state.withProperty(CONNECTION, Connections.LEFT.getSetting()).withProperty(FACING, EnumFacing.getHorizontal(getMetaFromState(state)));
  117.         else
  118.             return state.withProperty(CONNECTION, Connections.RIGHT.getSetting()).withProperty(FACING, EnumFacing.getHorizontal(getMetaFromState(state)));
  119.     }
  120.  
  121.     @Override
  122.     public int getMetaFromState(IBlockState state) {
  123.         return state.getValue(FACING).getHorizontalIndex();
  124.     }
  125.  
  126.     @Override
  127.     public IBlockState getStateFromMeta(int meta) {
  128.         return getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta));
  129.     }
  130.  
  131.     @Override
  132.     protected BlockStateContainer createBlockState() {
  133.         return new BlockStateContainer(this, FACING, CONNECTION);
  134.     }
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment