diff --git a/src/main/java/appeng/api/behaviors/FluidContainerItemStrategy.java b/src/main/java/appeng/api/behaviors/FluidContainerItemStrategy.java index ffb3b54466b..55f89dfbe87 100644 --- a/src/main/java/appeng/api/behaviors/FluidContainerItemStrategy.java +++ b/src/main/java/appeng/api/behaviors/FluidContainerItemStrategy.java @@ -1,11 +1,14 @@ package appeng.api.behaviors; +import org.jetbrains.annotations.Nullable; + import net.fabricmc.fabric.api.transfer.v1.context.ContainerItemContext; import net.fabricmc.fabric.api.transfer.v1.fluid.FluidStorage; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant; import net.fabricmc.fabric.api.transfer.v1.item.PlayerInventoryStorage; +import net.fabricmc.fabric.api.transfer.v1.storage.Storage; +import net.fabricmc.fabric.api.transfer.v1.storage.StorageUtil; import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction; -import org.jetbrains.annotations.Nullable; - import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.item.ItemStack; @@ -16,45 +19,27 @@ import appeng.util.GenericContainerHelper; import appeng.util.fluid.FluidSoundHelper; -class FluidContainerItemStrategy - implements ContainerItemStrategy { +class FluidContainerItemStrategy implements ContainerItemStrategy> { @Override public @Nullable GenericStack getContainedStack(ItemStack stack) { return GenericContainerHelper.getContainedFluidStack(stack); } @Override - public @Nullable Context findCarriedContext(Player player, AbstractContainerMenu menu) { - var fluidCapability = ContainerItemContext.ofPlayerCursor(player, menu).find(FluidStorage.ITEM); - if (fluidCapability != null) { - return new CarriedContext(player, menu); - } - return null; + public @Nullable Storage findCarriedContext(Player player, AbstractContainerMenu menu) { + return ContainerItemContext.ofPlayerCursor(player, menu).find(FluidStorage.ITEM); } @Override - public @Nullable Context findPlayerSlotContext(Player player, int slot) { + public @Nullable Storage findPlayerSlotContext(Player player, int slot) { var playerInv = PlayerInventoryStorage.of(player.getInventory()); - var fluidCapability = ContainerItemContext.ofPlayerSlot(player, playerInv.getSlot(slot)) - .find(FluidStorage.ITEM); - if (fluidCapability != null) { - return new PlayerInvContext(player, slot); - } - - return null; + return ContainerItemContext.ofPlayerSlot(player, playerInv.getSlots().get(slot)).find(FluidStorage.ITEM); } @Override - public long extract(Context context, AEFluidKey what, long amount, Actionable mode) { + public long extract(Storage context, AEFluidKey what, long amount, Actionable mode) { try (var tx = Transaction.openOuter()) { - var stack = context.getStack(); - var copy = stack.copyWithCount(1); - var fluidCapability = ContainerItemContext.withConstant(copy).find(FluidStorage.ITEM); - if (fluidCapability == null) { - return 0; - } - - var extracted = fluidCapability.extract(what.toVariant(), amount, tx); + var extracted = context.extract(what.toVariant(), amount, tx); if (mode == Actionable.MODULATE) { tx.commit(); } @@ -63,20 +48,13 @@ public long extract(Context context, AEFluidKey what, long amount, Actionable mo } @Override - public long insert(Context context, AEFluidKey what, long amount, Actionable mode) { + public long insert(Storage context, AEFluidKey what, long amount, Actionable mode) { try (var tx = Transaction.openOuter()) { - var stack = context.getStack(); - var copy = stack.copyWithCount(1); - var fluidCapability = ContainerItemContext.withConstant(copy).find(FluidStorage.ITEM); - if (fluidCapability == null) { - return 0; - } - - var filled = fluidCapability.insert(what.toVariant(), amount, tx); + var inserted = context.insert(what.toVariant(), amount, tx); if (mode == Actionable.MODULATE) { tx.commit(); } - return filled; + return inserted; } } @@ -91,51 +69,11 @@ public void playEmptySound(Player player, AEFluidKey what) { } @Override - public @Nullable GenericStack getExtractableContent(Context context) { - return getContainedStack(context.getStack()); - } - - interface Context { - ItemStack getStack(); - - void setStack(ItemStack stack); - - void addOverflow(ItemStack stack); - } - - private record CarriedContext(Player player, AbstractContainerMenu menu) implements Context { - @Override - public ItemStack getStack() { - return menu.getCarried(); - } - - @Override - public void setStack(ItemStack stack) { - menu.setCarried(stack); - } - - public void addOverflow(ItemStack stack) { - if (menu.getCarried().isEmpty()) { - menu.setCarried(stack); - } else { - player.getInventory().placeItemBackInInventory(stack); - } - } - } - - private record PlayerInvContext(Player player, int slot) implements Context { - @Override - public ItemStack getStack() { - return player.getInventory().getItem(slot); - } - - @Override - public void setStack(ItemStack stack) { - player.getInventory().setItem(slot, stack); - } - - public void addOverflow(ItemStack stack) { - player.getInventory().placeItemBackInInventory(stack); + public @Nullable GenericStack getExtractableContent(Storage context) { + var resourceAmount = StorageUtil.findExtractableContent(context, null); + if (resourceAmount == null) { + return null; } + return new GenericStack(AEFluidKey.of(resourceAmount.resource().getFluid()), resourceAmount.amount()); } -} \ No newline at end of file +}