Skip to content

Commit

Permalink
Merge pull request #624 from starforcraft/fix/GH-617/draggingcontr
Browse files Browse the repository at this point in the history
fix: can't take out fluids in grid if dragging an empty fluid container
  • Loading branch information
raoulvdberge authored Aug 11, 2024
2 parents 504769b + abff958 commit 68798f4
Show file tree
Hide file tree
Showing 16 changed files with 154 additions and 52 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -42,7 +45,7 @@ void onScroll(GridScrollMode scrollMode,

int getRegistryId();

List<ClientTooltipComponent> getExtractionHints(GridView view);
List<ClientTooltipComponent> getExtractionHints(ItemStack carriedStack, GridView view);

@Nullable
@API(status = API.Status.INTERNAL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface ResourceContainerInsertStrategy {
*/
Optional<InsertResult> insert(ItemStack container, ResourceAmount resourceAmount);

Optional<ConversionInfo> getConversionInfo(ResourceKey resource);
Optional<ConversionInfo> getConversionInfo(ResourceKey resource, ItemStack carriedStack);

record InsertResult(ItemStack container, long inserted) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ public interface Platform {

Optional<FluidOperationResult> fillContainer(ItemStack container, ResourceAmount resourceAmount);

Optional<ItemStack> getFilledBucket(FluidResource fluidResource);

TransferManager createTransferManager(AbstractContainerMenu containerMenu);

long insertIntoContainer(Container container, ItemResource itemResource, long amount, Action action);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,6 @@ public Optional<FluidOperationResult> fillContainer(final ItemStack container,
return ensureLoaded().fillContainer(container, resourceAmount);
}

@Override
public Optional<ItemStack> getFilledBucket(final FluidResource fluidResource) {
return ensureLoaded().getFilledBucket(fluidResource);
}

@Override
public TransferManager createTransferManager(final AbstractContainerMenu containerMenu) {
return ensureLoaded().createTransferManager(containerMenu);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

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;
import com.refinedmods.refinedstorage.common.api.grid.GridScrollMode;
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;
Expand All @@ -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<FluidResource> {
private static final ItemStack EMPTY_BUCKET = new ItemStack(Items.BUCKET);

private final int id;
private final ResourceRendering rendering;

Expand All @@ -52,12 +58,32 @@ public int getRegistryId() {
}

@Override
public List<ClientTooltipComponent> getExtractionHints(final GridView view) {
return Platform.INSTANCE.getFilledBucket(resource).map(bucket -> MouseClientTooltipComponent.item(
MouseClientTooltipComponent.Type.LEFT,
bucket,
null
)).stream().toList();
public List<ClientTooltipComponent> 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<FluidOperationResult> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public int getRegistryId() {
}

@Override
public List<ClientTooltipComponent> getExtractionHints(final GridView view) {
public List<ClientTooltipComponent> 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;
Expand All @@ -85,6 +85,11 @@ public List<ClientTooltipComponent> 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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ private List<ClientTooltipComponent> 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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ public Optional<InsertResult> insert(final ItemStack container, final ResourceAm
}

@Override
public Optional<ConversionInfo> getConversionInfo(final ResourceKey resource) {
public Optional<ConversionInfo> 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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,8 @@ public Optional<FluidOperationResult> fillContainer(final ItemStack container,
}
}

@Override
public Optional<ItemStack> getFilledBucket(final FluidResource fluidResource) {
final SimpleSingleStackStorage interceptingStorage = SimpleSingleStackStorage.forEmptyBucket();
private Optional<ItemStack> getFilledItemStack(final FluidResource fluidResource,
final SimpleSingleStackStorage interceptingStorage) {
final Storage<FluidVariant> destination = FluidStorage.ITEM.find(
interceptingStorage.getStack(),
ContainerItemContext.ofSingleSlot(interceptingStorage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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<FluidVariant> 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<ItemVariant> 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) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -127,16 +176,24 @@ 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<ItemVariant> relevantStorage = cursor
? playerCursorStorage
: playerInventoryStorage;
final ItemVariant itemVariant = ItemVariant.of(interceptingStorage.getStack());
return relevantStorage.insert(itemVariant, 1, innerTx) != 0;
}

private boolean isFluidContainerOnCursor() {
final StorageView<ItemVariant> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -165,19 +164,6 @@ public Optional<FluidOperationResult> fillContainer(final ItemStack container,
});
}

@Override
public Optional<ItemStack> 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);
Expand Down
Loading

0 comments on commit 68798f4

Please sign in to comment.