Advertisement
TheOnlyTails

RubyBarrelBlock

Oct 1st, 2020
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.57 KB | None | 0 0
  1. package com.theonlytails.ruby.blocks;
  2.  
  3. import com.theonlytails.ruby.init.TileEntityTypesRegistry;
  4. import com.theonlytails.ruby.tileentity.TileRubyBarrel;
  5. import net.minecraft.block.Block;
  6. import net.minecraft.block.BlockState;
  7. import net.minecraft.block.SoundType;
  8. import net.minecraft.block.material.Material;
  9. import net.minecraft.block.material.MaterialColor;
  10. import net.minecraft.entity.player.PlayerEntity;
  11. import net.minecraft.inventory.InventoryHelper;
  12. import net.minecraft.inventory.container.INamedContainerProvider;
  13. import net.minecraft.state.BooleanProperty;
  14. import net.minecraft.state.StateContainer;
  15. import net.minecraft.state.properties.BlockStateProperties;
  16. import net.minecraft.stats.Stats;
  17. import net.minecraft.tileentity.TileEntity;
  18. import net.minecraft.util.ActionResultType;
  19. import net.minecraft.util.Hand;
  20. import net.minecraft.util.SoundEvents;
  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.common.ToolType;
  26. import net.minecraftforge.items.ItemHandlerHelper;
  27. import org.jetbrains.annotations.Nullable;
  28.  
  29. import java.util.stream.IntStream;
  30.  
  31. @SuppressWarnings("deprecation")
  32. public class RubyBarrelBlock extends Block {
  33.     public static final BooleanProperty PROPERTY_OPEN = BlockStateProperties.OPEN;
  34.  
  35.     public RubyBarrelBlock() {
  36.         super(Properties.create(
  37.                 Material.IRON, MaterialColor.RED)
  38.                 .hardnessAndResistance(3.5f)
  39.                 .sound(SoundType.METAL)
  40.                 .harvestTool(ToolType.PICKAXE)
  41.                 .harvestLevel(2)
  42.                 .setRequiresTool());
  43.  
  44.         this.setDefaultState(this.getStateContainer().getBaseState().with(PROPERTY_OPEN, false));
  45.     }
  46.  
  47.     @Override
  48.     public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
  49.         if (!worldIn.isRemote()) {
  50.             INamedContainerProvider tileEntity = (INamedContainerProvider) worldIn.getTileEntity(pos);
  51.  
  52.             if (tileEntity != null) {
  53.                 TileRubyBarrel rubyBarrelTile = (TileRubyBarrel) tileEntity;
  54.  
  55.                 if (rubyBarrelTile.players == 0) {
  56.                     rubyBarrelTile.playSound(SoundEvents.BLOCK_BARREL_OPEN);
  57.                     rubyBarrelTile.changeState(state, true);
  58.                 }
  59.  
  60.                 rubyBarrelTile.players++;
  61.                 player.openContainer(tileEntity);
  62.                 player.addStat(Stats.OPEN_BARREL);
  63.             }
  64.         }
  65.  
  66.         return ActionResultType.SUCCESS;
  67.     }
  68.  
  69.     @Override
  70.     public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
  71.         if (state.getBlock() != newState.getBlock()) {
  72.             TileEntity tileEntity = worldIn.getTileEntity(pos);
  73.  
  74.             if (tileEntity instanceof TileRubyBarrel) {
  75.                 TileRubyBarrel rubyBarrelTile = (TileRubyBarrel) tileEntity;
  76.  
  77.                 dropItems(rubyBarrelTile, worldIn, pos);
  78.                 worldIn.updateComparatorOutputLevel(pos, this);
  79.             }
  80.         }
  81.  
  82.         super.onReplaced(state, worldIn, pos, newState, isMoving);
  83.     }
  84.  
  85.     @Nullable
  86.     @Override
  87.     public TileEntity createTileEntity(BlockState state, IBlockReader world) {
  88.         return TileEntityTypesRegistry.RUBY_BARREL.get().create();
  89.     }
  90.  
  91.     @Override
  92.     public boolean hasTileEntity(BlockState state) {
  93.         return true;
  94.     }
  95.  
  96.     @Override
  97.     public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) {
  98.         TileEntity rubyBarrel = worldIn.getTileEntity(pos);
  99.         return rubyBarrel instanceof TileRubyBarrel ?
  100.                 ItemHandlerHelper.calcRedstoneFromInventory(((TileRubyBarrel) rubyBarrel).itemHandler) : 0;
  101.     }
  102.  
  103.     @Override
  104.     public boolean hasComparatorInputOverride(BlockState state) {
  105.         return true;
  106.     }
  107.  
  108.     private static void dropItems(TileRubyBarrel rubyBarrel, World world, BlockPos pos) {
  109.         IntStream.range(0, rubyBarrel.itemHandler.getSlots())
  110.                 .mapToObj(rubyBarrel.itemHandler::getStackInSlot)
  111.                 .filter(stack -> !stack.isEmpty())
  112.                 .forEach(stack ->
  113.                         InventoryHelper.spawnItemStack(
  114.                                 world, pos.getX(), pos.getY(), pos.getZ(), stack));
  115.     }
  116.  
  117.     @Override
  118.     protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
  119.         builder.add(PROPERTY_OPEN);
  120.     }
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement