TheOnlyTails

RubyBarrelBlock

Sep 29th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.27 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.*;
  6. import net.minecraft.block.material.Material;
  7. import net.minecraft.block.material.MaterialColor;
  8. import net.minecraft.entity.player.PlayerEntity;
  9. import net.minecraft.inventory.InventoryHelper;
  10. import net.minecraft.inventory.container.Container;
  11. import net.minecraft.state.BooleanProperty;
  12. import net.minecraft.state.StateContainer;
  13. import net.minecraft.state.properties.BlockStateProperties;
  14. import net.minecraft.stats.Stats;
  15. import net.minecraft.tileentity.TileEntity;
  16. import net.minecraft.util.ActionResultType;
  17. import net.minecraft.util.Hand;
  18. import net.minecraft.util.math.BlockPos;
  19. import net.minecraft.util.math.BlockRayTraceResult;
  20. import net.minecraft.world.IBlockReader;
  21. import net.minecraft.world.World;
  22. import net.minecraft.world.server.ServerWorld;
  23. import org.jetbrains.annotations.NotNull;
  24. import org.jetbrains.annotations.Nullable;
  25.  
  26. import java.util.Random;
  27.  
  28. @SuppressWarnings("deprecation")
  29. public class RubyBarrelBlock extends ContainerBlock {
  30.  
  31.     public static final BooleanProperty OPEN = 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, false));
  40.     }
  41.  
  42.  
  43.     @Override
  44.     public void tick(@NotNull BlockState state, ServerWorld worldIn, @NotNull BlockPos pos, @NotNull Random rand) {
  45.         TileEntity tileentity = worldIn.getTileEntity(pos);
  46.         if (tileentity instanceof RubyBarrelTileEntity) {
  47.             ((RubyBarrelTileEntity) tileentity).barrelTick();
  48.         }
  49.     }
  50.  
  51.     @Override
  52.     public @NotNull ActionResultType onBlockActivated(@NotNull BlockState state,
  53.                                                       @NotNull World worldIn,
  54.                                                       @NotNull BlockPos pos,
  55.                                                       @NotNull PlayerEntity player,
  56.                                                       @NotNull Hand handIn,
  57.                                                       @NotNull BlockRayTraceResult hit) {
  58.         if (!worldIn.isRemote) {
  59.             TileEntity tileentity = worldIn.getTileEntity(pos);
  60.  
  61.             if (tileentity instanceof RubyBarrelTileEntity) {
  62.                 player.openContainer(((RubyBarrelTileEntity) tileentity));
  63.                 player.addStat(Stats.OPEN_BARREL);
  64.             }
  65.         }
  66.  
  67.         return ActionResultType.SUCCESS;
  68.     }
  69.  
  70.     @Override
  71.     public void onReplaced(@NotNull BlockState state, @NotNull World worldIn, @NotNull BlockPos pos, @NotNull BlockState newState, boolean isMoving) {
  72.         if (state.getBlock() != newState.getBlock()) {
  73.             TileEntity tileEntity = worldIn.getTileEntity(pos);
  74.  
  75.             if (tileEntity instanceof RubyBarrelTileEntity) {
  76.                 InventoryHelper.dropItems(worldIn,
  77.                         pos,
  78.                         ((RubyBarrelTileEntity) tileEntity).getItems());
  79.  
  80.                 worldIn.updateComparatorOutputLevel(pos, this);
  81.             }
  82.  
  83.             super.onReplaced(state, worldIn, pos, newState, isMoving);
  84.         }
  85.     }
  86.  
  87.     @Override
  88.     public @NotNull BlockRenderType getRenderType(@NotNull BlockState state) {
  89.         return BlockRenderType.MODEL;
  90.     }
  91.  
  92.     @Override
  93.     public boolean hasComparatorInputOverride(@NotNull BlockState state) {
  94.         return true;
  95.     }
  96.  
  97.     @Override
  98.     public int getComparatorInputOverride(@NotNull BlockState blockState, World worldIn, @NotNull BlockPos pos) {
  99.         return Container.calcRedstone(worldIn.getTileEntity(pos));
  100.     }
  101.  
  102.     @Override
  103.     protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
  104.         builder.add(OPEN);
  105.     }
  106.  
  107.     @Nullable
  108.     @Override
  109.     public TileEntity createNewTileEntity(@NotNull IBlockReader worldIn) {
  110.         return TileEntitiesRegistry.RUBY_BARREL.get().create();
  111.     }
  112.  
  113.     @Override
  114.     public boolean hasTileEntity(BlockState state) {
  115.         return true;
  116.     }
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment