Advertisement
shane020482

Face Rotation

May 27th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. package shane.mod.objects;
  2.  
  3. import net.minecraft.block.BlockHorizontal;
  4. import net.minecraft.block.SoundType;
  5. import net.minecraft.block.material.Material;
  6. import net.minecraft.block.properties.IProperty;
  7. import net.minecraft.block.properties.PropertyDirection;
  8. import net.minecraft.block.state.BlockStateContainer;
  9. import net.minecraft.block.state.IBlockState;
  10. import net.minecraft.creativetab.CreativeTabs;
  11. import net.minecraft.entity.EntityLivingBase;
  12. import net.minecraft.item.ItemStack;
  13. import net.minecraft.util.BlockRenderLayer;
  14. import net.minecraft.util.EnumFacing;
  15. import net.minecraft.util.Mirror;
  16. import net.minecraft.util.Rotation;
  17. import net.minecraft.util.math.BlockPos;
  18. import net.minecraft.world.World;
  19. import shane.mod.objects.blocks.blocksbase.BlockBase;
  20.  
  21. public class yourblock extends BlockBase
  22.  
  23.  
  24. {
  25. public static final PropertyDirection FACING = BlockHorizontal.FACING;
  26.  
  27. public yourblock(String name, Material material, float hardness, float resistance, String tool, int level,
  28. CreativeTabs tab, SoundType sound, float value) {
  29. super(name, material, hardness, resistance, tool, level, tab, sound, value);
  30.  
  31.  
  32. this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
  33.  
  34. }
  35.  
  36.  
  37. @Override
  38. public BlockRenderLayer getBlockLayer()
  39. {
  40. return BlockRenderLayer.CUTOUT_MIPPED;
  41. }
  42.  
  43. @Override
  44. public boolean isOpaqueCube(IBlockState state)
  45. {
  46. return false;
  47. }
  48.  
  49. @Override
  50. public boolean isFullBlock(IBlockState state)
  51. {
  52. return false;
  53. }
  54.  
  55. public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
  56. {
  57. this.setDefaultFacing(worldIn, pos, state);
  58. }
  59.  
  60. private void setDefaultFacing(World worldIn, BlockPos pos, IBlockState state)
  61. {
  62. if (!worldIn.isRemote)
  63. {
  64. IBlockState iblockstate = worldIn.getBlockState(pos.north());
  65. IBlockState iblockstate1 = worldIn.getBlockState(pos.south());
  66. IBlockState iblockstate2 = worldIn.getBlockState(pos.west());
  67. IBlockState iblockstate3 = worldIn.getBlockState(pos.east());
  68. EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
  69.  
  70. if (enumfacing == EnumFacing.NORTH && iblockstate.isFullBlock() && !iblockstate1.isFullBlock())
  71. {
  72. enumfacing = EnumFacing.SOUTH;
  73. }
  74. else if (enumfacing == EnumFacing.SOUTH && iblockstate1.isFullBlock() && !iblockstate.isFullBlock())
  75. {
  76. enumfacing = EnumFacing.NORTH;
  77. }
  78. else if (enumfacing == EnumFacing.WEST && iblockstate2.isFullBlock() && !iblockstate3.isFullBlock())
  79. {
  80. enumfacing = EnumFacing.EAST;
  81. }
  82. else if (enumfacing == EnumFacing.EAST && iblockstate3.isFullBlock() && !iblockstate2.isFullBlock())
  83. {
  84. enumfacing = EnumFacing.WEST;
  85. }
  86.  
  87. worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
  88. }
  89. }
  90.  
  91.  
  92.  
  93.  
  94. public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
  95. {
  96. return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
  97. }
  98.  
  99.  
  100. public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
  101. {
  102. worldIn.setBlockState(pos, state.withProperty(FACING, placer.getHorizontalFacing().getOpposite()), 2);
  103.  
  104.  
  105. }
  106.  
  107.  
  108. public IBlockState getStateFromMeta(int meta)
  109. {
  110. EnumFacing enumfacing = EnumFacing.getFront(meta);
  111.  
  112. if (enumfacing.getAxis() == EnumFacing.Axis.Y)
  113. {
  114. enumfacing = EnumFacing.NORTH;
  115. }
  116.  
  117. return this.getDefaultState().withProperty(FACING, enumfacing);
  118. }
  119.  
  120.  
  121. public int getMetaFromState(IBlockState state)
  122. {
  123. return ((EnumFacing)state.getValue(FACING)).getIndex();
  124. }
  125.  
  126.  
  127. public IBlockState withRotation(IBlockState state, Rotation rot)
  128. {
  129. return state.withProperty(FACING, rot.rotate((EnumFacing)state.getValue(FACING)));
  130. }
  131.  
  132.  
  133. public IBlockState withMirror(IBlockState state, Mirror mirrorIn)
  134. {
  135. return state.withRotation(mirrorIn.toRotation((EnumFacing)state.getValue(FACING)));
  136. }
  137.  
  138. protected BlockStateContainer createBlockState()
  139. {
  140. return new BlockStateContainer(this, new IProperty[] {FACING});
  141. }
  142.  
  143.  
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement