33import net .minecraft .core .BlockPos ;
44import net .minecraft .core .component .DataComponents ;
55import net .minecraft .network .chat .Component ;
6+ import net .minecraft .sounds .SoundEvents ;
67import net .minecraft .sounds .SoundSource ;
78import net .minecraft .world .InteractionHand ;
89import net .minecraft .world .ItemInteractionResult ;
1920import net .minecraft .world .level .storage .loot .LootParams ;
2021import net .minecraft .world .level .storage .loot .parameters .LootContextParams ;
2122import net .minecraft .world .phys .BlockHitResult ;
23+ import net .neoforged .neoforge .capabilities .Capabilities ;
2224import net .neoforged .neoforge .common .SoundActions ;
2325import net .neoforged .neoforge .fluids .FluidStack ;
2426import net .neoforged .neoforge .fluids .capability .IFluidHandler ;
27+ import net .neoforged .neoforge .fluids .capability .IFluidHandlerItem ;
2528import net .neoforged .neoforge .items .ItemHandlerHelper ;
2629import net .roboxgamer .modernutils .block .entity .custom .FluidTankBlockEntity ;
2730import org .jetbrains .annotations .NotNull ;
@@ -42,72 +45,103 @@ public FluidTankBlock(Properties properties) {
4245 }
4346
4447 @ 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 );
48+ protected @ NotNull ItemInteractionResult useItemOn (@ NotNull ItemStack stack , @ NotNull BlockState blockState , Level level , @ NotNull BlockPos pos , @ NotNull Player player , @ NotNull InteractionHand hand , @ NotNull BlockHitResult blockHitResult ) {
49+ IFluidHandlerItem fluidHandlerItem = stack .getCapability (Capabilities .FluidHandler .ITEM );
50+ if (fluidHandlerItem != null ) {
51+ if (stack .getItem () instanceof BucketItem ) {
52+ if (!(level .getBlockEntity (pos ) instanceof FluidTankBlockEntity tankBE )) {
53+ return ItemInteractionResult .PASS_TO_DEFAULT_BLOCK_INTERACTION ;
54+ }
55+ var tank = tankBE .getFluidHandler ();
56+
57+ Item item = stack .getItem ();
58+
59+ // Case 1: Player is holding a filled bucket
60+ if (item instanceof BucketItem bucketItem && !(item instanceof MobBucketItem )) {
61+ Fluid fluidInBucket = bucketItem .content ;
62+
63+ // If the bucket is filled with a fluid, try to insert it into the tank
64+ if (fluidInBucket != Fluids .EMPTY ) {
65+ FluidStack fluidStack = new FluidStack (fluidInBucket , 1000 ); // Buckets contain 1000 mB
66+ int filledAmount = tank .fill (fluidStack , IFluidHandler .FluidAction .EXECUTE );
67+
68+ if (filledAmount > 0 ) {
69+ // Replace filled bucket with an empty bucket
70+ if (!player .getAbilities ().instabuild ) {
71+ stack .shrink (1 );
72+ ItemStack emptyBucket = new ItemStack (Items .BUCKET );
73+ if (!player .addItem (emptyBucket )) {
74+ player .drop (emptyBucket , false );
75+ }
76+ }
77+ var sound = bucketItem .content .getFluidType ().getSound (player , level , pos , SoundActions .BUCKET_EMPTY );
78+ var soundSource = SoundSource .BLOCKS ;
79+ if (sound != null ) {
80+ level .playSound (player , pos , sound , soundSource , 1.0F , 1.0F );
81+ }
82+ return ItemInteractionResult .sidedSuccess (level .isClientSide ());
83+ }
84+ }
85+ }
86+
87+ // Case 2: Player is holding an empty bucket and trying to extract fluid from the tank
88+ if (stack .is (Items .BUCKET )) {
89+ FluidStack tankFluid = tank .getFluid ();
90+ if (!tankFluid .isEmpty () && tankFluid .getAmount () >= 1000 ) { // Check if there's enough fluid in the tank
91+ Fluid fluidToExtract = tankFluid .getFluid ();
92+
93+ // Try to extract 1000 mB (1 bucket worth) from the tank
94+ FluidStack extractedFluid = tank .drain (1000 , IFluidHandler .FluidAction .EXECUTE );
95+ if (!extractedFluid .isEmpty () && extractedFluid .getAmount () == 1000 ) {
96+ // Replace empty bucket with a filled bucket
97+ if (!player .getAbilities ().instabuild ) {
98+ stack .shrink (1 );
99+ ItemStack filledBucket = new ItemStack (fluidToExtract .getBucket ());
100+ ItemHandlerHelper .giveItemToPlayer (player , filledBucket );
101+ }
102+ var sound = fluidToExtract .getFluidType ().getSound (player , level , pos , SoundActions .BUCKET_FILL );
103+ var soundSource = SoundSource .BLOCKS ;
104+ if (sound != null ) {
105+ level .playSound (player , pos , sound , soundSource , 1.0F , 1.0F );
106+ }
107+ return ItemInteractionResult .sidedSuccess (level .isClientSide ());
108+ }
109+ }
70110 }
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 );
79111 }
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 );
112+
113+ IFluidHandler cap = level . getCapability ( Capabilities . FluidHandler . BLOCK , pos , blockHitResult . getDirection ());
114+ if ( cap == null ) {
115+ return ItemInteractionResult . PASS_TO_DEFAULT_BLOCK_INTERACTION ;
116+ }
117+
118+ FluidStack itemFluid = fluidHandlerItem . getFluidInTank ( 0 );
119+ FluidStack tankFluid = cap . getFluidInTank ( 0 );
120+
121+ if ( tankFluid . getAmount () > itemFluid . getAmount ()) {
122+ FluidStack drainTest = cap .drain (fluidHandlerItem . getTankCapacity ( 0 ) , IFluidHandler .FluidAction .SIMULATE );
123+ if (drainTest . getAmount () > 0 ) {
124+ int fillAmount = fluidHandlerItem . fill ( drainTest , IFluidHandler . FluidAction . SIMULATE );
125+ if ( fillAmount > 0 ) {
126+ FluidStack drained = cap . drain ( fillAmount , IFluidHandler . FluidAction . EXECUTE );
127+ fluidHandlerItem . fill ( drained , IFluidHandler . FluidAction . EXECUTE );
128+ level . playSound ( null , pos , SoundEvents . BUCKET_FILL , SoundSource . BLOCKS , 1.0F , 1.0F );
129+ return ItemInteractionResult . sidedSuccess ( level . isClientSide () );
98130 }
99131 }
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 );
132+ } else {
133+ int fillAmount = cap .fill (itemFluid , IFluidHandler .FluidAction .SIMULATE );
134+ if (fillAmount > 0 ) {
135+ FluidStack drained = fluidHandlerItem .drain (fillAmount , IFluidHandler .FluidAction .EXECUTE );
136+ if (!drained .isEmpty ()) {
137+ cap .fill (drained , IFluidHandler .FluidAction .EXECUTE );
138+ level .playSound (null , pos , SoundEvents .BUCKET_EMPTY , SoundSource .BLOCKS , 1.0F , 1.0F );
139+ return ItemInteractionResult .sidedSuccess (level .isClientSide ());
140+ }
104141 }
105- return ItemInteractionResult .sidedSuccess (level .isClientSide );
106142 }
107143 }
108- }
109-
110- return ItemInteractionResult .PASS_TO_DEFAULT_BLOCK_INTERACTION ;
144+ return ItemInteractionResult .SUCCESS ;
111145 }
112146
113147 @ Override
0 commit comments