11package knightminer .simplytea .item ;
22
3+ import org .jetbrains .annotations .Nullable ;
4+
35import knightminer .simplytea .core .Config ;
46import knightminer .simplytea .core .Registration ;
7+ import knightminer .simplytea .fluid .FluidTeapotWrapper ;
58import net .minecraft .core .BlockPos ;
69import net .minecraft .core .Direction ;
10+ import net .minecraft .nbt .CompoundTag ;
11+ import net .minecraft .sounds .SoundEvent ;
712import net .minecraft .sounds .SoundEvents ;
8- import net .minecraft .tags .FluidTags ;
913import net .minecraft .world .InteractionHand ;
1014import net .minecraft .world .InteractionResult ;
1115import net .minecraft .world .InteractionResultHolder ;
1216import net .minecraft .world .entity .LivingEntity ;
1317import net .minecraft .world .entity .animal .Cow ;
1418import net .minecraft .world .entity .player .Player ;
15- import net .minecraft .world .item .Item ;
1619import net .minecraft .world .item .ItemStack ;
1720import net .minecraft .world .item .ItemUtils ;
1821import net .minecraft .world .level .ClipContext ;
1922import net .minecraft .world .level .Level ;
2023import net .minecraft .world .level .block .BucketPickup ;
2124import net .minecraft .world .level .block .state .BlockState ;
22- import net .minecraft .world .level .material .Fluid ;
23- import net .minecraft .world .level .material .Fluids ;
2425import net .minecraft .world .phys .BlockHitResult ;
2526import net .minecraft .world .phys .HitResult .Type ;
27+ import net .minecraftforge .common .capabilities .ICapabilityProvider ;
28+ import net .minecraftforge .fluids .FluidActionResult ;
29+ import net .minecraftforge .fluids .FluidUtil ;
30+ import net .minecraftforge .fluids .capability .IFluidHandler ;
31+
32+ import java .util .Optional ;
2633
2734public class TeapotItem extends TooltipItem {
2835 public TeapotItem (Properties props ) {
@@ -33,40 +40,44 @@ public TeapotItem(Properties props) {
3340 public InteractionResultHolder <ItemStack > use (Level world , Player player , InteractionHand hand ) {
3441 ItemStack stack = player .getItemInHand (hand );
3542 BlockHitResult rayTrace = getPlayerPOVHitResult (world , player , ClipContext .Fluid .SOURCE_ONLY );
36- if (rayTrace .getType () == Type .BLOCK ) {
37- BlockPos pos = rayTrace .getBlockPos ();
38- BlockState state = world .getBlockState (pos );
39-
40- // we use name for lookup to prevent default fluid conflicts
41- Fluid fluid = state .getFluidState ().getType ();
42- if (fluid != Fluids .EMPTY ) {
43- // try for water or milk using the config lists
44- Item item = null ;
45- if (fluid .is (FluidTags .WATER )) {
46- item = Registration .teapot_water ;
47- } // TODO: milk when mods make a standard
48-
49- // if either one is found, update the stack
50- if (item != null ) {
51- // water is considered infinite unless disabled in the config
52- if (!Config .SERVER .teapot .infiniteWater ()) {
53- Direction side = rayTrace .getDirection ();
54- // unable to modify the block, fail
55- if (!world .mayInteract (player , pos ) || !player .mayUseItemAt (pos .relative (side ), side , stack ) || !(state .getBlock () instanceof BucketPickup )) {
56- return new InteractionResultHolder <>(InteractionResult .FAIL , stack );
57- }
58- ((BucketPickup )state .getBlock ()).pickupBlock (world , pos , state );
59- }
43+ if (rayTrace .getType () != Type .BLOCK ) {
44+ return InteractionResultHolder .pass (stack );
45+ }
46+
47+ BlockPos pos = rayTrace .getBlockPos ();
48+ Direction side = rayTrace .getDirection ();
49+ BlockState state = world .getBlockState (pos );
6050
61- stack = ItemUtils .createFilledResult (stack , player , new ItemStack (item ));
51+ if (!world .mayInteract (player , pos ) || !player .mayUseItemAt (pos .relative (side ), side , stack )) {
52+ return InteractionResultHolder .fail (stack );
53+ }
6254
63- // TODO: fluid sound based on fluid
64- player .playSound (SoundEvents .BUCKET_FILL , 1.0f , 1.0f );
65- return new InteractionResultHolder <>(InteractionResult .SUCCESS , stack );
55+ ItemStack filledStack = ItemStack .EMPTY ;
56+ if (state .getBlock () instanceof BucketPickup bucketPickup ) {
57+ // special case for infinite water
58+ if (FluidTeapotWrapper .isWater (state .getFluidState ().getType ()) && Config .SERVER .teapot .infiniteWater ()) {
59+ filledStack = new ItemStack (Registration .teapot_water );
60+ Optional <SoundEvent > sound = bucketPickup .getPickupSound (state );
61+ if (sound .isPresent ()) {
62+ player .playSound (sound .get (), 1.0f , 1.0f );
63+ }
64+ }
65+
66+ // use teapot like a bucket to get fluid, should work in most cases
67+ if (filledStack .isEmpty ()) {
68+ FluidActionResult actionResult = FluidUtil .tryPickUpFluid (stack , player , world , pos , side );
69+ if (actionResult .isSuccess ()) {
70+ filledStack = actionResult .getResult ();
6671 }
6772 }
6873 }
69- return new InteractionResultHolder <>(InteractionResult .FAIL , stack );
74+
75+ if (!filledStack .isEmpty ()) {
76+ ItemStack filledResult = ItemUtils .createFilledResult (stack , player , filledStack );
77+ return InteractionResultHolder .sidedSuccess (filledResult , world .isClientSide ());
78+ }
79+
80+ return InteractionResultHolder .fail (stack );
7081 }
7182
7283 @ Override
@@ -82,4 +93,10 @@ public InteractionResult interactLivingEntity(ItemStack stack, Player player, Li
8293 }
8394 return InteractionResult .PASS ;
8495 }
85- }
96+
97+ @ Nullable
98+ @ Override
99+ public ICapabilityProvider initCapabilities (ItemStack stack , @ Nullable CompoundTag nbt ) {
100+ return new FluidTeapotWrapper (stack );
101+ }
102+ }
0 commit comments