Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.resourcefulbees.resourcefulbees.block;
- import com.resourcefulbees.resourcefulbees.registry.ModTileEntityTypes;
- import com.resourcefulbees.resourcefulbees.tileentity.HoneyTankTileEntity;
- import net.minecraft.block.AbstractBlock;
- import net.minecraft.block.Block;
- import net.minecraft.block.BlockState;
- import net.minecraft.block.SoundType;
- import net.minecraft.block.material.Material;
- import net.minecraft.entity.player.PlayerEntity;
- import net.minecraft.fluid.FluidState;
- import net.minecraft.fluid.Fluids;
- import net.minecraft.item.*;
- import net.minecraft.state.BooleanProperty;
- import net.minecraft.state.IntegerProperty;
- import net.minecraft.state.StateContainer;
- import net.minecraft.state.properties.BlockStateProperties;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.ActionResultType;
- import net.minecraft.util.Direction;
- import net.minecraft.util.Hand;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.util.math.BlockRayTraceResult;
- import net.minecraft.util.math.shapes.ISelectionContext;
- import net.minecraft.util.math.shapes.VoxelShape;
- import net.minecraft.world.IBlockReader;
- import net.minecraft.world.IWorld;
- import net.minecraft.world.World;
- import net.minecraftforge.fluids.FluidUtil;
- import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
- import org.jetbrains.annotations.Nullable;
- import javax.annotation.Nonnull;
- public class HoneyTank extends Block {
- protected static final VoxelShape VOXEL_SHAPE = Block.makeCuboidShape(1.0D, 0.0D, 1.0D, 15.0D, 16.0D, 15.0D);
- public static final IntegerProperty LEVEL = IntegerProperty.create("level", 0, 14);
- public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
- public static final AbstractBlock.Properties PROPERTIES = Block.Properties.create(Material.GLASS)
- .sound(SoundType.GLASS)
- .nonOpaque();
- public final int maxHoneyAmount;
- public HoneyTank(AbstractBlock.Properties properties, int maxHoneyAmount) {
- super(properties);
- this.maxHoneyAmount = maxHoneyAmount;
- BlockState defaultState = this.stateContainer.getBaseState()
- .with(LEVEL, 0)
- .with(WATERLOGGED, false);
- this.setDefaultState(defaultState);
- }
- @Nullable
- @Override
- public TileEntity createTileEntity(BlockState state, IBlockReader world) {
- return new HoneyTankTileEntity(ModTileEntityTypes.HONEY_TANK_TILE_ENTITY.get(), maxHoneyAmount);
- }
- @Override
- public boolean hasTileEntity(BlockState state) {
- return true;
- }
- @Nonnull
- @Override
- public ActionResultType onUse(@Nonnull BlockState state, World world, @Nonnull BlockPos pos, @Nonnull PlayerEntity player, @Nonnull Hand hand, @Nonnull BlockRayTraceResult blockRayTraceResult) {
- if (world.isRemote) {
- ItemStack heldItem = player.getHeldItem(hand);
- boolean usingHoney = heldItem.getItem() instanceof HoneyBottleItem;
- boolean usingBottle = heldItem.getItem() instanceof GlassBottleItem;
- boolean usingBucket = heldItem.getItem() instanceof BucketItem;
- TileEntity tileEntity = world.getTileEntity(pos);
- if (tileEntity instanceof HoneyTankTileEntity) {
- HoneyTankTileEntity tank = (HoneyTankTileEntity) tileEntity;
- if (usingBucket) {
- tileEntity.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY)
- .ifPresent(iFluidHandler -> FluidUtil.interactWithFluidHandler(player, hand, world, pos, null));
- } else if (usingBottle) {
- tank.fillBottle(player, hand, world, pos, null);
- } else if (usingHoney) {
- tank.emptyBottle(player, hand, world, pos, null);
- }
- updateBlockState(world, pos);
- }
- }
- return ActionResultType.SUCCESS;
- }
- private void updateBlockState(World world, BlockPos pos) {
- BlockState state = world.getBlockState(pos);
- if (state.getBlock() instanceof HoneyTank) {
- world.setBlockState(pos, state.with(LEVEL, getLevel(world, pos)));
- }
- }
- @Override
- public FluidState getFluidState(BlockState state) {
- return state.get(WATERLOGGED) ? Fluids.WATER.getStillFluidState(false) : Fluids.EMPTY.getDefaultState();
- }
- @Override
- public BlockState getStateForPlacement(BlockItemUseContext context) {
- FluidState fluidState = context.getWorld().getFluidState(context.getPos());
- return this.getDefaultState();
- }
- @Override
- protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
- builder.add(WATERLOGGED, LEVEL);
- }
- @Override
- public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
- return VOXEL_SHAPE;
- }
- @Override
- public BlockState updatePostPlacement(BlockState stateIn, Direction facing, BlockState facingState, IWorld world, BlockPos currentPos, BlockPos facingPos) {
- if (stateIn.get(WATERLOGGED)) {
- world.getPendingFluidTicks().scheduleTick(currentPos, Fluids.WATER, Fluids.WATER.getTickRate(world));
- }
- return stateIn.with(LEVEL, getLevel(world, currentPos));
- }
- private int getLevel(IWorld world, BlockPos currentPos) {
- TileEntity tileEntity = world.getTileEntity(currentPos);
- if (tileEntity instanceof HoneyTankTileEntity) {
- HoneyTankTileEntity tank = (HoneyTankTileEntity) tileEntity;
- float fillPercentage = ((float) tank.fluidTank.getFluidAmount()) / ((float) tank.fluidTank.getTankCapacity(0));
- return (int) (fillPercentage * 14);
- }
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement