Advertisement
Vaerys_Dawn

HoneyTank.java

Dec 11th, 2020 (edited)
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.83 KB | None | 0 0
  1. package com.resourcefulbees.resourcefulbees.block;
  2.  
  3. import com.resourcefulbees.resourcefulbees.registry.ModTileEntityTypes;
  4. import com.resourcefulbees.resourcefulbees.tileentity.HoneyTankTileEntity;
  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.entity.player.PlayerEntity;
  11. import net.minecraft.fluid.FluidState;
  12. import net.minecraft.fluid.Fluids;
  13. import net.minecraft.item.*;
  14. import net.minecraft.state.BooleanProperty;
  15. import net.minecraft.state.IntegerProperty;
  16. import net.minecraft.state.StateContainer;
  17. import net.minecraft.state.properties.BlockStateProperties;
  18. import net.minecraft.tileentity.TileEntity;
  19. import net.minecraft.util.ActionResultType;
  20. import net.minecraft.util.Direction;
  21. import net.minecraft.util.Hand;
  22. import net.minecraft.util.math.BlockPos;
  23. import net.minecraft.util.math.BlockRayTraceResult;
  24. import net.minecraft.util.math.shapes.ISelectionContext;
  25. import net.minecraft.util.math.shapes.VoxelShape;
  26. import net.minecraft.world.IBlockReader;
  27. import net.minecraft.world.IWorld;
  28. import net.minecraft.world.World;
  29. import net.minecraftforge.fluids.FluidUtil;
  30. import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
  31. import org.jetbrains.annotations.Nullable;
  32.  
  33. import javax.annotation.Nonnull;
  34.  
  35. public class HoneyTank extends Block {
  36.  
  37.     protected static final VoxelShape VOXEL_SHAPE = Block.makeCuboidShape(1.0D, 0.0D, 1.0D, 15.0D, 16.0D, 15.0D);
  38.  
  39.     public static final IntegerProperty LEVEL = IntegerProperty.create("level", 0, 14);
  40.     public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
  41.  
  42.     public static final AbstractBlock.Properties PROPERTIES = Block.Properties.create(Material.GLASS)
  43.             .sound(SoundType.GLASS)
  44.             .nonOpaque();
  45.  
  46.     public final int maxHoneyAmount;
  47.  
  48.     public HoneyTank(AbstractBlock.Properties properties, int maxHoneyAmount) {
  49.         super(properties);
  50.         this.maxHoneyAmount = maxHoneyAmount;
  51.         BlockState defaultState = this.stateContainer.getBaseState()
  52.                 .with(LEVEL, 0)
  53.                 .with(WATERLOGGED, false);
  54.         this.setDefaultState(defaultState);
  55.     }
  56.  
  57.     @Nullable
  58.     @Override
  59.     public TileEntity createTileEntity(BlockState state, IBlockReader world) {
  60.         return new HoneyTankTileEntity(ModTileEntityTypes.HONEY_TANK_TILE_ENTITY.get(), maxHoneyAmount);
  61.     }
  62.  
  63.     @Override
  64.     public boolean hasTileEntity(BlockState state) {
  65.         return true;
  66.     }
  67.  
  68.     @Nonnull
  69.     @Override
  70.     public ActionResultType onUse(@Nonnull BlockState state, World world, @Nonnull BlockPos pos, @Nonnull PlayerEntity player, @Nonnull Hand hand, @Nonnull BlockRayTraceResult blockRayTraceResult) {
  71.         if (world.isRemote) {
  72.             ItemStack heldItem = player.getHeldItem(hand);
  73.             boolean usingHoney = heldItem.getItem() instanceof HoneyBottleItem;
  74.             boolean usingBottle = heldItem.getItem() instanceof GlassBottleItem;
  75.             boolean usingBucket = heldItem.getItem() instanceof BucketItem;
  76.             TileEntity tileEntity = world.getTileEntity(pos);
  77.  
  78.             if (tileEntity instanceof HoneyTankTileEntity) {
  79.                 HoneyTankTileEntity tank = (HoneyTankTileEntity) tileEntity;
  80.                 if (usingBucket) {
  81.                     tileEntity.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY)
  82.                             .ifPresent(iFluidHandler -> FluidUtil.interactWithFluidHandler(player, hand, world, pos, null));
  83.                 } else if (usingBottle) {
  84.                     tank.fillBottle(player, hand, world, pos, null);
  85.                 } else if (usingHoney) {
  86.                     tank.emptyBottle(player, hand, world, pos, null);
  87.                 }
  88.                 updateBlockState(world, pos);
  89.             }
  90.         }
  91.         return ActionResultType.SUCCESS;
  92.     }
  93.  
  94.     private void updateBlockState(World world, BlockPos pos) {
  95.         BlockState state = world.getBlockState(pos);
  96.         if (state.getBlock() instanceof HoneyTank) {
  97.             world.setBlockState(pos, state.with(LEVEL, getLevel(world, pos)));
  98.         }
  99.     }
  100.  
  101.  
  102.     @Override
  103.     public FluidState getFluidState(BlockState state) {
  104.         return state.get(WATERLOGGED) ? Fluids.WATER.getStillFluidState(false) : Fluids.EMPTY.getDefaultState();
  105.     }
  106.  
  107.     @Override
  108.     public BlockState getStateForPlacement(BlockItemUseContext context) {
  109.         FluidState fluidState = context.getWorld().getFluidState(context.getPos());
  110.         return this.getDefaultState();
  111.     }
  112.  
  113.     @Override
  114.     protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
  115.         builder.add(WATERLOGGED, LEVEL);
  116.     }
  117.  
  118.     @Override
  119.     public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
  120.         return VOXEL_SHAPE;
  121.     }
  122.  
  123.     @Override
  124.     public BlockState updatePostPlacement(BlockState stateIn, Direction facing, BlockState facingState, IWorld world, BlockPos currentPos, BlockPos facingPos) {
  125.         if (stateIn.get(WATERLOGGED)) {
  126.             world.getPendingFluidTicks().scheduleTick(currentPos, Fluids.WATER, Fluids.WATER.getTickRate(world));
  127.         }
  128.         return stateIn.with(LEVEL, getLevel(world, currentPos));
  129.     }
  130.  
  131.     private int getLevel(IWorld world, BlockPos currentPos) {
  132.         TileEntity tileEntity = world.getTileEntity(currentPos);
  133.         if (tileEntity instanceof HoneyTankTileEntity) {
  134.             HoneyTankTileEntity tank = (HoneyTankTileEntity) tileEntity;
  135.             float fillPercentage = ((float) tank.fluidTank.getFluidAmount()) / ((float) tank.fluidTank.getTankCapacity(0));
  136.             return (int) (fillPercentage * 14);
  137.         }
  138.         return 0;
  139.     }
  140. }
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement