diff --git a/CHANGELOG.md b/CHANGELOG.md index 48a4de402..446bd0d3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - Portable Grid search bar texture being positioned in the wrong way. - External Storage screen unnecessarily showing upgrade slots. - Grid setting changes not persisting after restarting Minecraft. +- Fixed not being able to extract fluids from the Grid with an empty bucket or other empty fluid container. ## [2.0.0-milestone.4.6] - 2024-08-08 diff --git a/refinedstorage-common-api/src/main/java/com/refinedmods/refinedstorage/common/api/grid/view/PlatformGridResource.java b/refinedstorage-common-api/src/main/java/com/refinedmods/refinedstorage/common/api/grid/view/PlatformGridResource.java index 40e9a03cd..0b062d6fd 100644 --- a/refinedstorage-common-api/src/main/java/com/refinedmods/refinedstorage/common/api/grid/view/PlatformGridResource.java +++ b/refinedstorage-common-api/src/main/java/com/refinedmods/refinedstorage/common/api/grid/view/PlatformGridResource.java @@ -17,10 +17,13 @@ import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent; import net.minecraft.network.chat.Component; import net.minecraft.world.inventory.tooltip.TooltipComponent; +import net.minecraft.world.item.ItemStack; import org.apiguardian.api.API; @API(status = API.Status.STABLE, since = "2.0.0-milestone.2.6") public interface PlatformGridResource extends GridResource { + boolean canExtract(ItemStack carriedStack, GridView view); + void onExtract(GridExtractMode extractMode, boolean cursor, GridExtractionStrategy extractionStrategy); @@ -42,7 +45,7 @@ void onScroll(GridScrollMode scrollMode, int getRegistryId(); - List getExtractionHints(GridView view); + List getExtractionHints(ItemStack carriedStack, GridView view); @Nullable @API(status = API.Status.INTERNAL) diff --git a/refinedstorage-common-api/src/main/java/com/refinedmods/refinedstorage/common/api/support/resource/ResourceContainerInsertStrategy.java b/refinedstorage-common-api/src/main/java/com/refinedmods/refinedstorage/common/api/support/resource/ResourceContainerInsertStrategy.java index 139039666..007d8f8c2 100644 --- a/refinedstorage-common-api/src/main/java/com/refinedmods/refinedstorage/common/api/support/resource/ResourceContainerInsertStrategy.java +++ b/refinedstorage-common-api/src/main/java/com/refinedmods/refinedstorage/common/api/support/resource/ResourceContainerInsertStrategy.java @@ -21,7 +21,7 @@ public interface ResourceContainerInsertStrategy { */ Optional insert(ItemStack container, ResourceAmount resourceAmount); - Optional getConversionInfo(ResourceKey resource); + Optional getConversionInfo(ResourceKey resource, ItemStack carriedStack); record InsertResult(ItemStack container, long inserted) { } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/Platform.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/Platform.java index d7c8baaa7..67245d029 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/Platform.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/Platform.java @@ -75,8 +75,6 @@ public interface Platform { Optional fillContainer(ItemStack container, ResourceAmount resourceAmount); - Optional getFilledBucket(FluidResource fluidResource); - TransferManager createTransferManager(AbstractContainerMenu containerMenu); long insertIntoContainer(Container container, ItemResource itemResource, long amount, Action action); diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/PlatformProxy.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/PlatformProxy.java index 51e7a2660..ac5c84766 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/PlatformProxy.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/PlatformProxy.java @@ -118,11 +118,6 @@ public Optional fillContainer(final ItemStack container, return ensureLoaded().fillContainer(container, resourceAmount); } - @Override - public Optional getFilledBucket(final FluidResource fluidResource) { - return ensureLoaded().getFilledBucket(fluidResource); - } - @Override public TransferManager createTransferManager(final AbstractContainerMenu containerMenu) { return ensureLoaded().createTransferManager(containerMenu); diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/screen/AbstractGridScreen.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/screen/AbstractGridScreen.java index d6330c212..503a2926b 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/screen/AbstractGridScreen.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/screen/AbstractGridScreen.java @@ -349,7 +349,7 @@ private void renderHoveredResourceTooltip(final GuiGraphics graphics, addDetailedTooltip(view, platformResource, processedLines); } if (!platformResource.isZeroed()) { - processedLines.addAll(platformResource.getExtractionHints(getMenu().getView())); + processedLines.addAll(platformResource.getExtractionHints(getMenu().getCarried(), getMenu().getView())); } Platform.INSTANCE.renderTooltip(graphics, processedLines, mouseX, mouseY); } @@ -419,7 +419,7 @@ public boolean mouseClicked(final double mouseX, final double mouseY, final int final ItemStack carriedStack = getMenu().getCarried(); final PlatformGridResource resource = getCurrentGridResource(); - if (resource != null && carriedStack.isEmpty()) { + if (resource != null && resource.canExtract(carriedStack, getMenu().getView())) { mouseClickedInGrid(clickedButton, resource); return true; } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/view/FluidGridResource.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/view/FluidGridResource.java index 5f8b956ba..63dae70de 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/view/FluidGridResource.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/view/FluidGridResource.java @@ -2,6 +2,7 @@ import com.refinedmods.refinedstorage.api.grid.operations.GridExtractMode; import com.refinedmods.refinedstorage.api.grid.view.GridView; +import com.refinedmods.refinedstorage.api.resource.ResourceAmount; import com.refinedmods.refinedstorage.common.Platform; import com.refinedmods.refinedstorage.common.api.RefinedStorageApi; import com.refinedmods.refinedstorage.common.api.grid.GridResourceAttributeKeys; @@ -9,6 +10,7 @@ import com.refinedmods.refinedstorage.common.api.grid.strategy.GridExtractionStrategy; import com.refinedmods.refinedstorage.common.api.grid.strategy.GridScrollingStrategy; import com.refinedmods.refinedstorage.common.api.grid.view.AbstractPlatformGridResource; +import com.refinedmods.refinedstorage.common.api.support.resource.FluidOperationResult; import com.refinedmods.refinedstorage.common.api.support.resource.ResourceRendering; import com.refinedmods.refinedstorage.common.api.support.resource.ResourceType; import com.refinedmods.refinedstorage.common.support.resource.FluidResource; @@ -25,8 +27,12 @@ import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.network.chat.Component; import net.minecraft.world.inventory.tooltip.TooltipComponent; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; public class FluidGridResource extends AbstractPlatformGridResource { + private static final ItemStack EMPTY_BUCKET = new ItemStack(Items.BUCKET); + private final int id; private final ResourceRendering rendering; @@ -52,12 +58,32 @@ public int getRegistryId() { } @Override - public List getExtractionHints(final GridView view) { - return Platform.INSTANCE.getFilledBucket(resource).map(bucket -> MouseClientTooltipComponent.item( - MouseClientTooltipComponent.Type.LEFT, - bucket, - null - )).stream().toList(); + public List getExtractionHints(final ItemStack carriedStack, final GridView view) { + return tryFillFluidContainer(carriedStack) + .filter(result -> result.amount() > 0) + .map(result -> MouseClientTooltipComponent.item( + MouseClientTooltipComponent.Type.LEFT, + result.container(), + null + )).stream().toList(); + } + + private Optional tryFillFluidContainer(final ItemStack carriedStack) { + final ResourceAmount toFill = new ResourceAmount(resource, Platform.INSTANCE.getBucketAmount()); + return carriedStack.isEmpty() + ? Platform.INSTANCE.fillContainer(EMPTY_BUCKET, toFill) + : Platform.INSTANCE.fillContainer(carriedStack, toFill); + } + + @Override + public boolean canExtract(final ItemStack carriedStack, final GridView view) { + if (carriedStack.isEmpty()) { + return true; + } + final ResourceAmount toFill = new ResourceAmount(resource, view.getAmount(resource)); + return Platform.INSTANCE.fillContainer(carriedStack, toFill) + .map(result -> result.amount() > 0) + .orElse(false); } @Override diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/view/ItemGridResource.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/view/ItemGridResource.java index dbf43035e..6abbfedfc 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/view/ItemGridResource.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/view/ItemGridResource.java @@ -67,7 +67,7 @@ public int getRegistryId() { } @Override - public List getExtractionHints(final GridView view) { + public List getExtractionHints(final ItemStack carriedStack, final GridView view) { final long amount = getAmount(view); final long extractableAmount = Math.min(amount, itemStack.getMaxStackSize()); final long halfExtractionAmount = extractableAmount == 1 ? 1 : extractableAmount / 2; @@ -85,6 +85,11 @@ public List getExtractionHints(final GridView view) { ); } + @Override + public boolean canExtract(final ItemStack carriedStack, final GridView view) { + return carriedStack.isEmpty(); + } + @Override public void onExtract(final GridExtractMode extractMode, final boolean cursor, diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/BucketPlayerInventoryInsertableStorage.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/BucketPlayerInventoryInsertableStorage.java index 48fab8ce3..5410e0773 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/BucketPlayerInventoryInsertableStorage.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/BucketPlayerInventoryInsertableStorage.java @@ -1,6 +1,7 @@ package com.refinedmods.refinedstorage.common.storage; import com.refinedmods.refinedstorage.api.core.Action; +import com.refinedmods.refinedstorage.api.resource.ResourceAmount; import com.refinedmods.refinedstorage.api.resource.ResourceKey; import com.refinedmods.refinedstorage.api.storage.Actor; import com.refinedmods.refinedstorage.api.storage.InsertableStorage; @@ -37,9 +38,10 @@ public long insert(final ResourceKey resource, final long amount, final Action a if (amount != Platform.INSTANCE.getBucketAmount()) { return 0; } - return Platform.INSTANCE.getFilledBucket(fluidResource).map( - filledBucketStack -> insert(filledBucketStack, amount, action, actor) - ).orElse(0L); + final ResourceAmount toFill = new ResourceAmount(fluidResource, amount); + return Platform.INSTANCE.fillContainer(EMPTY_BUCKET_STACK, toFill) + .map(result -> insert(result.container(), amount, action, actor)) + .orElse(0L); } private long insert(final ItemStack filledBucketStack, final long amount, final Action action, final Actor actor) { diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/AbstractBaseScreen.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/AbstractBaseScreen.java index b9379a411..743bfa0b0 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/AbstractBaseScreen.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/AbstractBaseScreen.java @@ -221,7 +221,7 @@ private List getTooltipForResource(final ResourceKey res if (resourceSlot.supportsItemSlotInteractions()) { RefinedStorageApi.INSTANCE.getResourceContainerInsertStrategies() .stream() - .flatMap(strategy -> strategy.getConversionInfo(resource).stream()) + .flatMap(strategy -> strategy.getConversionInfo(resource, getMenu().getCarried()).stream()) .map(conversionInfo -> MouseClientTooltipComponent.itemConversion( MouseClientTooltipComponent.Type.LEFT, conversionInfo.from(), diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/resource/FluidResourceContainerInsertStrategy.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/resource/FluidResourceContainerInsertStrategy.java index 06582a71c..56bd74479 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/resource/FluidResourceContainerInsertStrategy.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/resource/FluidResourceContainerInsertStrategy.java @@ -21,13 +21,14 @@ public Optional insert(final ItemStack container, final ResourceAm } @Override - public Optional getConversionInfo(final ResourceKey resource) { + public Optional getConversionInfo(final ResourceKey resource, final ItemStack carriedStack) { if (!(resource instanceof FluidResource fluidResource)) { return Optional.empty(); } - return Platform.INSTANCE.getFilledBucket(fluidResource).map(filledBucket -> new ConversionInfo( - EMPTY_BUCKET, - filledBucket - )); + final ItemStack container = carriedStack.isEmpty() ? EMPTY_BUCKET : carriedStack; + final ResourceAmount toFill = new ResourceAmount(fluidResource, Platform.INSTANCE.getBucketAmount()); + return Platform.INSTANCE.fillContainer(container, toFill) + .filter(result -> result.amount() > 0) + .map(result -> new ConversionInfo(container, result.container())); } } diff --git a/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/PlatformImpl.java b/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/PlatformImpl.java index 840267efc..421083629 100644 --- a/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/PlatformImpl.java +++ b/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/PlatformImpl.java @@ -195,9 +195,8 @@ public Optional fillContainer(final ItemStack container, } } - @Override - public Optional getFilledBucket(final FluidResource fluidResource) { - final SimpleSingleStackStorage interceptingStorage = SimpleSingleStackStorage.forEmptyBucket(); + private Optional getFilledItemStack(final FluidResource fluidResource, + final SimpleSingleStackStorage interceptingStorage) { final Storage destination = FluidStorage.ITEM.find( interceptingStorage.getStack(), ContainerItemContext.ofSingleSlot(interceptingStorage) diff --git a/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/grid/strategy/FluidGridExtractionStrategy.java b/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/grid/strategy/FluidGridExtractionStrategy.java index fab9c3b53..c03d12a42 100644 --- a/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/grid/strategy/FluidGridExtractionStrategy.java +++ b/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/grid/strategy/FluidGridExtractionStrategy.java @@ -18,9 +18,11 @@ import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant; import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant; import net.fabricmc.fabric.api.transfer.v1.item.PlayerInventoryStorage; +import net.fabricmc.fabric.api.transfer.v1.storage.StorageView; import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.inventory.AbstractContainerMenu; +import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import static com.refinedmods.refinedstorage.fabric.support.resource.VariantUtil.toFluidVariant; @@ -48,9 +50,12 @@ public boolean onExtract(final PlatformResourceKey resource, final GridExtractMode extractMode, final boolean cursor) { if (resource instanceof FluidResource fluidResource) { + final boolean containerOnCursor = isFluidContainerOnCursor(); final boolean bucketInInventory = hasBucketInInventory(); final boolean bucketInStorage = hasBucketInStorage(); - if (bucketInInventory) { + if (containerOnCursor) { + extractWithContainerOnCursor(fluidResource, extractMode); + } else if (bucketInInventory) { extractWithBucketInInventory(fluidResource, extractMode, cursor); } else if (bucketInStorage) { extractWithBucketInStorage(fluidResource, extractMode, cursor); @@ -60,6 +65,49 @@ public boolean onExtract(final PlatformResourceKey resource, return false; } + private void extractWithContainerOnCursor(final FluidResource fluidResource, final GridExtractMode mode) { + try (Transaction tx = Transaction.openOuter()) { + final ItemStack stack = extractContainerFromCursor(tx); + final SimpleSingleStackStorage interceptingStorage = SimpleSingleStackStorage.forStack(stack); + final net.fabricmc.fabric.api.transfer.v1.storage.Storage dest = FluidStorage.ITEM.find( + interceptingStorage.getStack(), + ContainerItemContext.ofSingleSlot(interceptingStorage) + ); + if (dest == null) { + return; + } + gridOperations.extract(fluidResource, mode, (resource2, amount, action, source) -> { + if (!(resource2 instanceof FluidResource fluidResource2)) { + return 0; + } + try (Transaction innerTx = tx.openNested()) { + final long inserted = dest.insert(toFluidVariant(fluidResource2), amount, innerTx); + final boolean couldInsertContainer = insertResultingContainerIntoInventory( + interceptingStorage, + true, + innerTx + ); + if (!couldInsertContainer) { + return 0; + } + if (action == Action.EXECUTE) { + innerTx.commit(); + tx.commit(); + } + return inserted; + } + }); + } + } + + private ItemStack extractContainerFromCursor(final Transaction tx) { + final StorageView view = playerCursorStorage.iterator().next(); + final ItemVariant variant = view.getResource(); + final ItemStack stack = variant.toStack((int) view.getAmount()); + playerCursorStorage.extract(variant, 1, tx); + return stack; + } + private void extractWithBucketInStorage(final FluidResource fluidResource, final GridExtractMode mode, final boolean cursor) { @@ -77,7 +125,8 @@ private void extractWithBucketInStorage(final FluidResource fluidResource, } try (Transaction tx = Transaction.openOuter()) { final long inserted = destination.insert(toFluidVariant(fluidResource2), amount, tx); - final boolean couldInsertBucket = insertResultingBucketIntoInventory(interceptingStorage, cursor, tx); + final boolean couldInsertBucket = + insertResultingContainerIntoInventory(interceptingStorage, cursor, tx); if (!couldInsertBucket) { return 0; } @@ -109,7 +158,7 @@ private void extractWithBucketInInventory(final FluidResource fluidResource, } try (Transaction innerTx = tx.openNested()) { final long inserted = dest.insert(toFluidVariant(fluidResource2), amount, innerTx); - final boolean couldInsertBucket = insertResultingBucketIntoInventory( + final boolean couldInsertBucket = insertResultingContainerIntoInventory( interceptingStorage, cursor, innerTx @@ -127,9 +176,9 @@ private void extractWithBucketInInventory(final FluidResource fluidResource, } } - private boolean insertResultingBucketIntoInventory(final SimpleSingleStackStorage interceptingStorage, - final boolean cursor, - final Transaction innerTx) { + private boolean insertResultingContainerIntoInventory(final SimpleSingleStackStorage interceptingStorage, + final boolean cursor, + final Transaction innerTx) { final net.fabricmc.fabric.api.transfer.v1.storage.Storage relevantStorage = cursor ? playerCursorStorage : playerInventoryStorage; @@ -137,6 +186,14 @@ private boolean insertResultingBucketIntoInventory(final SimpleSingleStackStorag return relevantStorage.insert(itemVariant, 1, innerTx) != 0; } + private boolean isFluidContainerOnCursor() { + final StorageView view = playerCursorStorage.iterator().next(); + final ItemVariant variant = view.getResource(); + final ItemStack stack = variant.toStack((int) view.getAmount()); + final ContainerItemContext ctx = ContainerItemContext.withConstant(stack); + return FluidStorage.ITEM.find(stack, ctx) != null; + } + private boolean hasBucketInInventory() { try (Transaction tx = Transaction.openOuter()) { return playerInventoryStorage.extract(BUCKET_ITEM_VARIANT, 1, tx) == 1; diff --git a/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/util/SimpleSingleStackStorage.java b/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/util/SimpleSingleStackStorage.java index 73b1513a7..3372127c5 100644 --- a/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/util/SimpleSingleStackStorage.java +++ b/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/util/SimpleSingleStackStorage.java @@ -12,7 +12,11 @@ public SimpleSingleStackStorage(final ItemStack stack) { } public static SimpleSingleStackStorage forEmptyBucket() { - return new SimpleSingleStackStorage(new ItemStack(Items.BUCKET)); + return forStack(new ItemStack(Items.BUCKET)); + } + + public static SimpleSingleStackStorage forStack(final ItemStack stack) { + return new SimpleSingleStackStorage(stack); } @Override diff --git a/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/PlatformImpl.java b/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/PlatformImpl.java index 1e91d7032..a533b9629 100644 --- a/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/PlatformImpl.java +++ b/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/PlatformImpl.java @@ -54,7 +54,6 @@ import net.minecraft.world.inventory.Slot; import net.minecraft.world.inventory.tooltip.TooltipComponent; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.Items; import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.item.crafting.CraftingInput; import net.minecraft.world.item.crafting.CraftingRecipe; @@ -165,19 +164,6 @@ public Optional fillContainer(final ItemStack container, }); } - @Override - public Optional getFilledBucket(final FluidResource fluidResource) { - return Optional.ofNullable( - new ItemStack(Items.BUCKET).getCapability(Capabilities.FluidHandler.ITEM) - ).map(dest -> { - dest.fill( - toFluidStack(fluidResource, FluidType.BUCKET_VOLUME), - IFluidHandler.FluidAction.EXECUTE - ); - return dest.getContainer(); - }); - } - @Override public TransferManager createTransferManager(final AbstractContainerMenu containerMenu) { return new TransferManager(containerMenu, ContainerTransferDestination::new); diff --git a/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/grid/strategy/FluidGridExtractionStrategy.java b/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/grid/strategy/FluidGridExtractionStrategy.java index f73c3eb51..c12bb4721 100644 --- a/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/grid/strategy/FluidGridExtractionStrategy.java +++ b/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/grid/strategy/FluidGridExtractionStrategy.java @@ -50,9 +50,12 @@ public boolean onExtract(final PlatformResourceKey resource, final GridExtractMode extractMode, final boolean cursor) { if (resource instanceof FluidResource fluidResource) { + final boolean containerOnCursor = isFluidContainerOnCursor(); final boolean bucketInInventory = hasBucketInInventory(); final boolean bucketInStorage = hasBucketInStorage(); - if (bucketInInventory) { + if (containerOnCursor) { + extractWithContainerOnCursor(fluidResource, extractMode); + } else if (bucketInInventory) { extract(fluidResource, extractMode, cursor, true); } else if (bucketInStorage) { extract(fluidResource, extractMode, cursor, false); @@ -67,6 +70,24 @@ private IFluidHandlerItem getFluidStorage(final ItemStack stack) { return stack.getCapability(Capabilities.FluidHandler.ITEM); } + private void extractWithContainerOnCursor(final FluidResource fluidResource, + final GridExtractMode mode) { + gridOperations.extract(fluidResource, mode, (resource, amount, action, source) -> { + if (!(resource instanceof FluidResource fluidResource2)) { + return 0; + } + final IFluidHandlerItem destination = getFluidStorage(menu.getCarried()); + if (destination == null) { + return 0; + } + final int inserted = destination.fill(toFluidStack(fluidResource2, amount), toFluidAction(action)); + if (inserted > 0 && action == Action.EXECUTE) { + menu.setCarried(destination.getContainer()); + } + return inserted; + }); + } + private void extract(final FluidResource fluidResource, final GridExtractMode mode, final boolean cursor, @@ -121,6 +142,10 @@ private boolean insertResultingBucket(final boolean cursor, final IFluidHandlerI } } + private boolean isFluidContainerOnCursor() { + return getFluidStorage(menu.getCarried()) != null; + } + private boolean hasBucketInStorage() { return itemStorage.extract(BUCKET_ITEM_RESOURCE, 1, Action.SIMULATE, EmptyActor.INSTANCE) == 1; }