TheOnlyTails

RubyBarrelBlock

Sep 27th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. package com.theonlytails.ruby.blocks;
  2.  
  3. import com.theonlytails.ruby.init.TileEntitiesRegistry;
  4. import com.theonlytails.ruby.tileentity.RubyBarrelTileEntity;
  5. import net.minecraft.block.AbstractBlock;
  6. import net.minecraft.block.Block;
  7. import net.minecraft.block.BlockState;
  8. import net.minecraft.block.SoundType;
  9. import net.minecraft.block.material.Material;
  10. import net.minecraft.block.material.MaterialColor;
  11. import net.minecraft.entity.player.PlayerEntity;
  12. import net.minecraft.entity.player.ServerPlayerEntity;
  13. import net.minecraft.inventory.InventoryHelper;
  14. import net.minecraft.state.BooleanProperty;
  15. import net.minecraft.state.StateContainer;
  16. import net.minecraft.state.properties.BlockStateProperties;
  17. import net.minecraft.stats.Stats;
  18. import net.minecraft.tileentity.TileEntity;
  19. import net.minecraft.util.ActionResultType;
  20. import net.minecraft.util.Hand;
  21. import net.minecraft.util.math.BlockPos;
  22. import net.minecraft.util.math.BlockRayTraceResult;
  23. import net.minecraft.world.IBlockReader;
  24. import net.minecraft.world.World;
  25. import net.minecraftforge.fml.network.NetworkHooks;
  26. import org.jetbrains.annotations.NotNull;
  27. import org.jetbrains.annotations.Nullable;
  28.  
  29. @SuppressWarnings("deprecation")
  30. public class RubyBarrelBlock extends Block {
  31. public static final BooleanProperty OPEN_PROPERTY = BlockStateProperties.OPEN;
  32.  
  33. public RubyBarrelBlock() {
  34. super(AbstractBlock.Properties
  35. .create(Material.IRON, MaterialColor.IRON)
  36. .hardnessAndResistance(3.5f)
  37. .sound(SoundType.METAL));
  38.  
  39. this.setDefaultState(this.getStateContainer().getBaseState().with(OPEN_PROPERTY, false));
  40. }
  41.  
  42. @Override
  43. public boolean hasTileEntity(BlockState state) {
  44. return true;
  45. }
  46.  
  47. @Nullable
  48. @Override
  49. public TileEntity createTileEntity(BlockState state, IBlockReader world) {
  50. return TileEntitiesRegistry.RUBY_BARREL.get().create();
  51. }
  52.  
  53. @Override
  54. public @NotNull ActionResultType onBlockActivated(@NotNull BlockState state,
  55. @NotNull World worldIn,
  56. @NotNull BlockPos pos,
  57. @NotNull PlayerEntity player,
  58. @NotNull Hand handIn,
  59. @NotNull BlockRayTraceResult hit) {
  60. if (!worldIn.isRemote) {
  61. TileEntity tileentity = worldIn.getTileEntity(pos);
  62.  
  63. if (tileentity instanceof RubyBarrelTileEntity) {
  64. NetworkHooks.openGui(
  65. ((ServerPlayerEntity) player), (RubyBarrelTileEntity) tileentity, pos);
  66. player.addStat(Stats.OPEN_BARREL);
  67. }
  68.  
  69. return ActionResultType.SUCCESS;
  70.  
  71. }
  72.  
  73. return ActionResultType.SUCCESS;
  74. }
  75.  
  76. @Override
  77. public void onReplaced(@NotNull BlockState state, @NotNull World worldIn, @NotNull BlockPos pos, @NotNull BlockState newState, boolean isMoving) {
  78. if (state.getBlock() != newState.getBlock()) {
  79. TileEntity tileEntity = worldIn.getTileEntity(pos);
  80.  
  81. if (tileEntity instanceof RubyBarrelTileEntity) {
  82. InventoryHelper.dropItems(worldIn,
  83. pos,
  84. ((RubyBarrelTileEntity) tileEntity).getItems()
  85. );
  86. }
  87. }
  88. }
  89.  
  90. @Override
  91. protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
  92. builder.add(OPEN_PROPERTY);
  93. }
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment