Guest User

ModBlock

a guest
Oct 30th, 2020
11
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class ModBlock extends ContainerBlock {
  2.  
  3. public static final DirectionProperty PROPERTY_FACING = BlockStateProperties.HORIZONTAL_FACING;
  4. public static final BooleanProperty PROPERTY_OPEN = BlockStateProperties.OPEN;
  5.  
  6. public ModBlock () {
  7. super(Block.Properties.create(Material.WOOD).hardnessAndResistance(2.5F).sound(SoundType.WOOD));
  8.  
  9. setDefaultState(stateContainer.getBaseState().with(PROPERTY_FACING, Direction.NORTH).with(PROPERTY_OPEN, false));
  10. }
  11.  
  12. @Override
  13. public void tick(BlockState state, ServerWorld worldIn, BlockPos pos, Random rand) {
  14. TileEntity tileentity = worldIn.getTileEntity(pos);
  15. if (tileentity instanceof ModTileEntity) {
  16. ((ModTileEntity) tileentity).modTETick();
  17. }
  18. }
  19.  
  20. @Override
  21. public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
  22. if (!worldIn.isRemote) {
  23. TileEntity tileentity = worldIn.getTileEntity(pos);
  24. if(tileentity instanceof ModTileEntity) {
  25. NetworkHooks.openGui((ServerPlayerEntity) player, ((INamedContainerProvider) tileentity), t -> t.writeBlockPos(pos));
  26. player.addStat(Stats.OPEN_BARREL);
  27. }
  28. }
  29. return ActionResultType.SUCCESS;
  30. }
  31.  
  32. @Override
  33. public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
  34. if (state.getBlock() != newState.getBlock()) {
  35. TileEntity tileentity = worldIn.getTileEntity(pos);
  36. if (tileentity instanceof IInventory) {
  37. InventoryHelper.dropInventoryItems(worldIn, pos, (IInventory) tileentity);
  38. worldIn.updateComparatorOutputLevel(pos, this);
  39. }
  40.  
  41. super.onReplaced(state, worldIn, pos, newState, isMoving);
  42. }
  43. }
  44.  
  45. @Override
  46. public BlockRenderType getRenderType(BlockState state) {
  47. return BlockRenderType.MODEL;
  48. }
  49.  
  50. @Override
  51. public void onBlockPlacedBy(World worldIn, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) {
  52. if (stack.hasDisplayName()) {
  53. TileEntity tileentity = worldIn.getTileEntity(pos);
  54. if (tileentity instanceof ModTileEntity) {
  55. ((ModTileEntity) tileentity).setCustomName(stack.getDisplayName());
  56. }
  57. }
  58. }
  59.  
  60. @Override
  61. public boolean hasComparatorInputOverride(BlockState state) {
  62. return true;
  63. }
  64.  
  65. @Override
  66. public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) {
  67. return Container.calcRedstone(worldIn.getTileEntity(pos));
  68. }
  69.  
  70. @Override
  71. public BlockState rotate(BlockState state, Rotation rot) {
  72. return state.with(PROPERTY_FACING, rot.rotate(state.get(PROPERTY_FACING)));
  73. }
  74.  
  75. @Override
  76. public BlockState mirror(BlockState state, Mirror mirrorIn) {
  77. return state.rotate(mirrorIn.toRotation(state.get(PROPERTY_FACING)));
  78. }
  79.  
  80. @Override
  81. protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
  82. builder.add(PROPERTY_FACING, PROPERTY_OPEN);
  83. }
  84.  
  85. @Override
  86. public BlockState getStateForPlacement(BlockItemUseContext context) {
  87. return getDefaultState().with(PROPERTY_FACING, context.getPlacementHorizontalFacing().getOpposite());
  88. }
  89.  
  90. @Override
  91. public boolean hasTileEntity(BlockState state) {
  92. return true;
  93. }
  94.  
  95. @Nullable
  96. @Override
  97. public ModTileEntity createNewTileEntity(IBlockReader worldIn) {
  98. return new ModTileEntity();
  99. }
  100. }
RAW Paste Data