Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static final RegistryObject<TileEntityType<?>> HONEY_TANK_TILE_ENTITY = TILE_ENTITY_TYPES.register("honey_tank", () -> TileEntityType.Builder
- .create(() -> new HoneyTankTileEntity(4000), ModBlocks.PURPUR_HONEY_TANK.get(), ModBlocks.NETHER_HONEY_TANK.get(), ModBlocks.WOODEN_HONEY_TANK.get())
- .build(null));
- public static final RegistryObject<Block> PURPUR_HONEY_TANK = BLOCKS.register("purpur_honey_tank", () -> new HoneyTank(HoneyTank.PROPERTIES, 64000, HoneyTank.TankType.PURPUR));
- public static final RegistryObject<Block> NETHER_HONEY_TANK = BLOCKS.register("nether_honey_tank", () -> new HoneyTank(HoneyTank.PROPERTIES, 16000, HoneyTank.TankType.NETHER));
- public static final RegistryObject<Block> WOODEN_HONEY_TANK = BLOCKS.register("wooden_honey_tank", () -> new HoneyTank(HoneyTank.PROPERTIES, 4000, HoneyTank.TankType.WOODEN));
- package com.resourcefulbees.resourcefulbees.block;
- 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(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(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));
- }
- if (!world.isRemote()) {
- stateIn.with(LEVEL, getLevel(world, currentPos));
- }
- return stateIn;
- }
- 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;
- }
- }
- package com.resourcefulbees.resourcefulbees.tileentity;
- import com.resourcefulbees.resourcefulbees.lib.ModConstants;
- import com.resourcefulbees.resourcefulbees.registry.ModFluids;
- import com.resourcefulbees.resourcefulbees.registry.ModTileEntityTypes;
- import com.resourcefulbees.resourcefulbees.utils.BeeInfoUtils;
- import net.minecraft.entity.player.PlayerEntity;
- import net.minecraft.item.ItemStack;
- import net.minecraft.item.Items;
- import net.minecraft.tileentity.ITickableTileEntity;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.Direction;
- import net.minecraft.util.Hand;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.world.World;
- import net.minecraftforge.common.capabilities.Capability;
- import net.minecraftforge.common.util.LazyOptional;
- import net.minecraftforge.fluids.FluidStack;
- import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
- import net.minecraftforge.fluids.capability.IFluidHandler;
- import net.minecraftforge.fluids.capability.templates.FluidTank;
- import org.apache.logging.log4j.LogManager;
- import org.apache.logging.log4j.Logger;
- import javax.annotation.Nonnull;
- import javax.annotation.Nullable;
- import java.util.function.Predicate;
- public class HoneyTankTileEntity extends TileEntity implements ITickableTileEntity {
- public static final Logger LOGGER = LogManager.getLogger();
- public final FluidTank fluidTank;
- private final LazyOptional<IFluidHandler> fluidOptional;
- public static final FluidStack HONEY_BOTTLE_FLUID_STACK = new FluidStack(ModFluids.HONEY_STILL.get(), ModConstants.HONEY_PER_BOTTLE);
- public HoneyTankTileEntity(int maxHoneyAmount) {
- super(ModTileEntityTypes.HONEY_TANK_TILE_ENTITY.get());
- fluidTank = new FluidTank(maxHoneyAmount, honeyFluidPredicate());
- fluidOptional = LazyOptional.of(() -> fluidTank);
- }
- private static Predicate<FluidStack> honeyFluidPredicate() {
- return fluidStack -> fluidStack.getFluid().isIn(BeeInfoUtils.getFluidTag("forge:honey"));
- }
- @Nonnull
- @Override
- public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
- if (cap.equals(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY)) return fluidOptional.cast();
- return super.getCapability(cap, side);
- }
- @Override
- public void tick() {
- }
- public void fillBottle(PlayerEntity player, Hand hand, World world, BlockPos pos, @Nullable Direction side) {
- if (!fluidTank.getFluid().isFluidEqual(HONEY_BOTTLE_FLUID_STACK) || fluidTank.isEmpty()) {
- return;
- }
- if (fluidTank.getFluidAmount() >= ModConstants.HONEY_PER_BOTTLE) {
- fluidTank.drain(HONEY_BOTTLE_FLUID_STACK, IFluidHandler.FluidAction.EXECUTE);
- ItemStack stack = player.getHeldItem(hand);
- if (stack.getCount() > 1) {
- stack.setCount(stack.getCount() - 1);
- player.addItemStackToInventory(new ItemStack(Items.HONEY_BOTTLE, 1));
- } else {
- player.setHeldItem(hand, new ItemStack(Items.HONEY_BOTTLE, 1));
- }
- }
- }
- public void emptyBottle(PlayerEntity player, Hand hand, World world, BlockPos pos, @Nullable Direction side) {
- if (!fluidTank.getFluid().isFluidEqual(HONEY_BOTTLE_FLUID_STACK) && !fluidTank.isEmpty()) {
- return;
- }
- if (fluidTank.getFluidAmount() + ModConstants.HONEY_PER_BOTTLE <= fluidTank.getTankCapacity(0)) {
- fluidTank.fill(HONEY_BOTTLE_FLUID_STACK, IFluidHandler.FluidAction.EXECUTE);
- ItemStack stack = player.getHeldItem(hand);
- if (stack.getCount() > 1) {
- stack.setCount(stack.getCount() - 1);
- player.addItemStackToInventory(new ItemStack(Items.GLASS_BOTTLE, 1));
- } else {
- player.setHeldItem(hand, new ItemStack(Items.GLASS_BOTTLE, 1));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment