|
| 1 | +package net.roboxgamer.modernutils.block.custom; |
| 2 | + |
| 3 | +import net.minecraft.core.BlockPos; |
| 4 | +import net.minecraft.core.component.DataComponents; |
| 5 | +import net.minecraft.network.chat.Component; |
| 6 | +import net.minecraft.sounds.SoundSource; |
| 7 | +import net.minecraft.world.InteractionHand; |
| 8 | +import net.minecraft.world.ItemInteractionResult; |
| 9 | +import net.minecraft.world.entity.player.Player; |
| 10 | +import net.minecraft.world.item.*; |
| 11 | +import net.minecraft.world.item.component.CustomData; |
| 12 | +import net.minecraft.world.level.Level; |
| 13 | +import net.minecraft.world.level.block.Block; |
| 14 | +import net.minecraft.world.level.block.EntityBlock; |
| 15 | +import net.minecraft.world.level.block.entity.BlockEntity; |
| 16 | +import net.minecraft.world.level.block.state.BlockState; |
| 17 | +import net.minecraft.world.level.material.Fluid; |
| 18 | +import net.minecraft.world.level.material.Fluids; |
| 19 | +import net.minecraft.world.level.storage.loot.LootParams; |
| 20 | +import net.minecraft.world.level.storage.loot.parameters.LootContextParams; |
| 21 | +import net.minecraft.world.phys.BlockHitResult; |
| 22 | +import net.neoforged.neoforge.common.SoundActions; |
| 23 | +import net.neoforged.neoforge.fluids.FluidStack; |
| 24 | +import net.neoforged.neoforge.fluids.capability.IFluidHandler; |
| 25 | +import net.neoforged.neoforge.items.ItemHandlerHelper; |
| 26 | +import net.roboxgamer.modernutils.block.entity.custom.FluidTankBlockEntity; |
| 27 | +import org.jetbrains.annotations.NotNull; |
| 28 | +import org.jetbrains.annotations.Nullable; |
| 29 | + |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +public class FluidTankBlock extends Block implements EntityBlock { |
| 34 | + public FluidTankBlock(Properties properties) { |
| 35 | + super(properties); |
| 36 | + } |
| 37 | + public static int capacity = 10000; |
| 38 | + |
| 39 | + @Override |
| 40 | + public @Nullable BlockEntity newBlockEntity(@NotNull BlockPos pos, @NotNull BlockState state) { |
| 41 | + return new FluidTankBlockEntity(pos, state, capacity); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + protected @NotNull ItemInteractionResult useItemOn(@NotNull ItemStack stack, @NotNull BlockState state, @NotNull Level level, @NotNull BlockPos pos, @NotNull Player player, @NotNull InteractionHand hand, @NotNull BlockHitResult hitResult) { |
| 46 | + if (!(level.getBlockEntity(pos) instanceof FluidTankBlockEntity tankBE)) { |
| 47 | + return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; |
| 48 | + } |
| 49 | + var tank = tankBE.getFluidHandler(); |
| 50 | + |
| 51 | + Item item = stack.getItem(); |
| 52 | + |
| 53 | + // Case 1: Player is holding a filled bucket |
| 54 | + if (item instanceof BucketItem bucketItem && !(item instanceof MobBucketItem)) { |
| 55 | + Fluid fluidInBucket = bucketItem.content; |
| 56 | + |
| 57 | + // If the bucket is filled with a fluid, try to insert it into the tank |
| 58 | + if (fluidInBucket != Fluids.EMPTY) { |
| 59 | + FluidStack fluidStack = new FluidStack(fluidInBucket, 1000); // Buckets contain 1000 mB |
| 60 | + int filledAmount = tank.fill(fluidStack, IFluidHandler.FluidAction.EXECUTE); |
| 61 | + |
| 62 | + if (filledAmount > 0) { |
| 63 | + if (!level.isClientSide) { |
| 64 | + // Replace filled bucket with an empty bucket |
| 65 | + if (!player.getAbilities().instabuild) { |
| 66 | + stack.shrink(1); |
| 67 | + ItemStack emptyBucket = new ItemStack(Items.BUCKET); |
| 68 | + if (!player.addItem(emptyBucket)) { |
| 69 | + player.drop(emptyBucket, false); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + var sound = bucketItem.content.getFluidType().getSound(player, level, pos, SoundActions.BUCKET_EMPTY); |
| 74 | + var soundSource = SoundSource.BLOCKS; |
| 75 | + if (sound != null) { |
| 76 | + level.playSound(player, pos, sound, soundSource, 1.0F, 1.0F); |
| 77 | + } |
| 78 | + return ItemInteractionResult.sidedSuccess(level.isClientSide); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // Case 2: Player is holding an empty bucket and trying to extract fluid from the tank |
| 84 | + if (stack.is(Items.BUCKET)) { |
| 85 | + FluidStack tankFluid = tank.getFluid(); |
| 86 | + if (!tankFluid.isEmpty() && tankFluid.getAmount() >= 1000) { // Check if there's enough fluid in the tank |
| 87 | + Fluid fluidToExtract = tankFluid.getFluid(); |
| 88 | + |
| 89 | + // Try to extract 1000 mB (1 bucket worth) from the tank |
| 90 | + FluidStack extractedFluid = tank.drain(1000, IFluidHandler.FluidAction.EXECUTE); |
| 91 | + if (!extractedFluid.isEmpty() && extractedFluid.getAmount() == 1000) { |
| 92 | + if (!level.isClientSide) { |
| 93 | + // Replace empty bucket with a filled bucket |
| 94 | + if (!player.getAbilities().instabuild) { |
| 95 | + stack.shrink(1); |
| 96 | + ItemStack filledBucket = new ItemStack(fluidToExtract.getBucket()); |
| 97 | + ItemHandlerHelper.giveItemToPlayer(player, filledBucket); |
| 98 | + } |
| 99 | + } |
| 100 | + var sound = fluidToExtract.getFluidType().getSound(player, level, pos, SoundActions.BUCKET_FILL); |
| 101 | + var soundSource = SoundSource.BLOCKS; |
| 102 | + if (sound != null) { |
| 103 | + level.playSound(player, pos, sound, soundSource, 1.0F, 1.0F); |
| 104 | + } |
| 105 | + return ItemInteractionResult.sidedSuccess(level.isClientSide); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + protected @NotNull List<ItemStack> getDrops(@NotNull BlockState state, LootParams.Builder params) { |
| 115 | + BlockEntity be = params.getOptionalParameter(LootContextParams.BLOCK_ENTITY); |
| 116 | + ItemStack itemStack = new ItemStack(state.getBlock()); |
| 117 | + if (be instanceof FluidTankBlockEntity fluidTankBE && fluidTankBE.getFluidHandler().getFluidAmount() != 0) { |
| 118 | + be.saveToItem(itemStack, params.getLevel().registryAccess()); |
| 119 | + } |
| 120 | + return Collections.singletonList(itemStack); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void appendHoverText(@NotNull ItemStack stack, Item.@NotNull TooltipContext context, @NotNull List<Component> tooltipComponents, @NotNull TooltipFlag tooltipFlag) { |
| 125 | + super.appendHoverText(stack, context, tooltipComponents, tooltipFlag); |
| 126 | + if (stack.has(DataComponents.BLOCK_ENTITY_DATA)) { |
| 127 | + CustomData data = stack.get(DataComponents.BLOCK_ENTITY_DATA); |
| 128 | + if (data != null) { |
| 129 | + var tag = data.copyTag().getCompound("modernutils"); |
| 130 | + var fluid = tag.getCompound("fluidTank").getCompound("Fluid"); |
| 131 | + FluidStack fluidStack = FluidStack.parseOptional(context.level().registryAccess(),fluid); |
| 132 | + tooltipComponents.add(Component.literal("Fluid: " + fluidStack.getFluid())); |
| 133 | + tooltipComponents.add(Component.literal("Amount: " + fluidStack.getAmount())); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments