Skip to content

Commit

Permalink
Merge pull request #619 from refinedmods/feat/GH-616/iface-bucket-ext…
Browse files Browse the repository at this point in the history
…ract

feat: ability to extract resources other than item from the interface
  • Loading branch information
raoulvdberge authored Jul 13, 2024
2 parents 450fe6c + 16bf161 commit f461e49
Show file tree
Hide file tree
Showing 28 changed files with 446 additions and 149 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- Ability to extract fluids from the Interface using an empty bucket or other empty fluid container.

## [2.0.0-milestone.4.4] - 2024-07-10

## [2.0.0-milestone.4.3] - 2024-07-06
Expand Down
8 changes: 8 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.sonarqube.gradle.SonarExtension

plugins {
id("refinedarchitect.root")
id("refinedarchitect.base")
Expand All @@ -10,3 +12,9 @@ refinedarchitect {
subprojects {
group = "com.refinedmods.refinedstorage"
}

project.extensions.getByType<SonarExtension>().apply {
properties {
property("sonar.coverage.exclusions", "refinedstorage-platform-neoforge/**/*,refinedstorage-platform-fabric/**/*,refinedstorage-platform-common/**/*,refinedstorage-platform-api/**/*")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.refinedmods.refinedstorage.platform.api.support.network.bounditem.NetworkBoundItemHelper;
import com.refinedmods.refinedstorage.platform.api.support.registry.PlatformRegistry;
import com.refinedmods.refinedstorage.platform.api.support.resource.RecipeModIngredientConverter;
import com.refinedmods.refinedstorage.platform.api.support.resource.ResourceContainerInsertStrategy;
import com.refinedmods.refinedstorage.platform.api.support.resource.ResourceFactory;
import com.refinedmods.refinedstorage.platform.api.support.resource.ResourceRendering;
import com.refinedmods.refinedstorage.platform.api.support.resource.ResourceType;
Expand Down Expand Up @@ -132,6 +133,10 @@ GridInsertionStrategy createGridInsertionStrategy(AbstractContainerMenu containe

void addGridInsertionStrategyFactory(GridInsertionStrategyFactory insertionStrategyFactory);

void addResourceContainerInsertStrategy(ResourceContainerInsertStrategy strategy);

Collection<ResourceContainerInsertStrategy> getResourceContainerInsertStrategies();

void addAlternativeGridInsertionHint(GridInsertionHint hint);

GridInsertionHints getGridInsertionHints();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.refinedmods.refinedstorage.platform.api.support.network.bounditem.NetworkBoundItemHelper;
import com.refinedmods.refinedstorage.platform.api.support.registry.PlatformRegistry;
import com.refinedmods.refinedstorage.platform.api.support.resource.RecipeModIngredientConverter;
import com.refinedmods.refinedstorage.platform.api.support.resource.ResourceContainerInsertStrategy;
import com.refinedmods.refinedstorage.platform.api.support.resource.ResourceFactory;
import com.refinedmods.refinedstorage.platform.api.support.resource.ResourceRendering;
import com.refinedmods.refinedstorage.platform.api.support.resource.ResourceType;
Expand Down Expand Up @@ -230,6 +231,16 @@ public void addGridInsertionStrategyFactory(final GridInsertionStrategyFactory i
ensureLoaded().addGridInsertionStrategyFactory(insertionStrategyFactory);
}

@Override
public void addResourceContainerInsertStrategy(final ResourceContainerInsertStrategy strategy) {
ensureLoaded().addResourceContainerInsertStrategy(strategy);
}

@Override
public Collection<ResourceContainerInsertStrategy> getResourceContainerInsertStrategies() {
return ensureLoaded().getResourceContainerInsertStrategies();
}

@Override
public void addAlternativeGridInsertionHint(final GridInsertionHint hint) {
ensureLoaded().addAlternativeGridInsertionHint(hint);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.refinedmods.refinedstorage.platform.api.support.resource;

import net.minecraft.world.item.ItemStack;
import org.apiguardian.api.API;

@API(status = API.Status.STABLE, since = "2.0.0-milestone.4.5")
public record FluidOperationResult(ItemStack container, PlatformResourceKey fluid, long amount) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.refinedmods.refinedstorage.platform.api.support.resource;

import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;

import java.util.Optional;

import net.minecraft.world.item.ItemStack;
import org.apiguardian.api.API;

/**
* Defines how a resource can be extracted from a resource slot into a container.
* For fluids, this maps to how a fluid is inserted into a fluid container like a bucket.
*/
@API(status = API.Status.STABLE, since = "2.0.0-milestone.4.5")
public interface ResourceContainerInsertStrategy {
/**
* @param container the container to insert the resource into
* @param resourceAmount the resource to insert into the container
* @return the result of the insertion, if any. If no result is present, the next insertion strategy will be tried.
*/
Optional<InsertResult> insert(ItemStack container, ResourceAmount resourceAmount);

Optional<ConversionInfo> getConversionInfo(ResourceKey resource);

record InsertResult(ItemStack container, long inserted) {
}

record ConversionInfo(ItemStack from, ItemStack to) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
import com.refinedmods.refinedstorage.platform.common.support.energy.EnergyLootItemFunction;
import com.refinedmods.refinedstorage.platform.common.support.network.BaseNetworkNodeContainerBlockEntity;
import com.refinedmods.refinedstorage.platform.common.support.network.component.PlatformStorageNetworkComponent;
import com.refinedmods.refinedstorage.platform.common.support.resource.FluidResourceContainerInsertStrategy;
import com.refinedmods.refinedstorage.platform.common.support.resource.FluidResourceFactory;
import com.refinedmods.refinedstorage.platform.common.support.resource.ResourceContainerData;
import com.refinedmods.refinedstorage.platform.common.support.resource.ResourceTypes;
Expand Down Expand Up @@ -218,6 +219,7 @@ protected final void initializePlatformApi() {
registerStorageTypes();
registerResourceTypes();
registerAdditionalResourceFactories();
registerResourceContainerInsertStrategies();
registerDestructorStrategyFactories();
registerConstructorStrategyFactories();
registerStorageMonitorInsertionStrategies();
Expand Down Expand Up @@ -254,6 +256,10 @@ private void registerAdditionalResourceFactories() {
PlatformApi.INSTANCE.addResourceFactory(new FluidResourceFactory());
}

private void registerResourceContainerInsertStrategies() {
PlatformApi.INSTANCE.addResourceContainerInsertStrategy(new FluidResourceContainerInsertStrategy());
}

private void registerDestructorStrategyFactories() {
PlatformApi.INSTANCE.addDestructorStrategyFactory(new BlockBreakDestructorStrategyFactory());
PlatformApi.INSTANCE.addDestructorStrategyFactory(new FluidBreakDestructorStrategyFactory());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.refinedmods.refinedstorage.api.core.Action;
import com.refinedmods.refinedstorage.api.grid.view.GridResourceFactory;
import com.refinedmods.refinedstorage.api.network.energy.EnergyStorage;
import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage.platform.api.grid.strategy.GridInsertionStrategyFactory;
import com.refinedmods.refinedstorage.platform.api.support.resource.FluidOperationResult;
import com.refinedmods.refinedstorage.platform.common.support.containermenu.MenuOpener;
import com.refinedmods.refinedstorage.platform.common.support.containermenu.TransferManager;
import com.refinedmods.refinedstorage.platform.common.support.render.FluidRenderer;
Expand Down Expand Up @@ -63,9 +65,11 @@ public interface Platform {

FluidRenderer getFluidRenderer();

Optional<ContainedFluid> getContainedFluid(ItemStack stack);
Optional<FluidOperationResult> drainContainer(ItemStack container);

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

Optional<ItemStack> getFilledBucket(FluidResource fluidResource);

TransferManager createTransferManager(AbstractContainerMenu containerMenu);

Expand Down Expand Up @@ -111,7 +115,4 @@ List<ClientTooltipComponent> processTooltipComponents(
<T extends CustomPacketPayload> void sendPacketToServer(T packet);

<T extends CustomPacketPayload> void sendPacketToClient(ServerPlayer player, T packet);

record ContainedFluid(ItemStack remainderContainer, FluidResource fluid, long amount) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.refinedmods.refinedstorage.platform.api.support.network.bounditem.NetworkBoundItemHelper;
import com.refinedmods.refinedstorage.platform.api.support.registry.PlatformRegistry;
import com.refinedmods.refinedstorage.platform.api.support.resource.RecipeModIngredientConverter;
import com.refinedmods.refinedstorage.platform.api.support.resource.ResourceContainerInsertStrategy;
import com.refinedmods.refinedstorage.platform.api.support.resource.ResourceFactory;
import com.refinedmods.refinedstorage.platform.api.support.resource.ResourceRendering;
import com.refinedmods.refinedstorage.platform.api.support.resource.ResourceType;
Expand Down Expand Up @@ -85,6 +86,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -169,6 +171,7 @@ public class PlatformApiImpl implements PlatformApi {
private final PlatformRegistry<SlotReferenceFactory> slotReferenceFactoryRegistry = new PlatformRegistryImpl<>();
private final CompositeSlotReferenceProvider slotReferenceProvider = new CompositeSlotReferenceProvider();
private final PlatformRegistry<PlatformPermission> permissionRegistry = new PlatformRegistryImpl<>();
private final List<ResourceContainerInsertStrategy> resourceExtractStrategies = new ArrayList<>();

public PlatformApiImpl() {
gridSynchronizerRegistry.register(createIdentifier("off"), NoopGridSynchronizer.INSTANCE);
Expand Down Expand Up @@ -372,6 +375,16 @@ public void addGridInsertionStrategyFactory(final GridInsertionStrategyFactory i
gridInsertionStrategyFactories.add(insertionStrategyFactory);
}

@Override
public void addResourceContainerInsertStrategy(final ResourceContainerInsertStrategy strategy) {
resourceExtractStrategies.add(strategy);
}

@Override
public Collection<ResourceContainerInsertStrategy> getResourceContainerInsertStrategies() {
return Collections.unmodifiableList(resourceExtractStrategies);
}

@Override
public void addAlternativeGridInsertionHint(final GridInsertionHint hint) {
gridInsertionHints.addAlternativeHint(hint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.refinedmods.refinedstorage.api.core.Action;
import com.refinedmods.refinedstorage.api.grid.view.GridResourceFactory;
import com.refinedmods.refinedstorage.api.network.energy.EnergyStorage;
import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage.platform.api.grid.strategy.GridInsertionStrategyFactory;
import com.refinedmods.refinedstorage.platform.api.support.resource.FluidOperationResult;
import com.refinedmods.refinedstorage.platform.common.support.containermenu.MenuOpener;
import com.refinedmods.refinedstorage.platform.common.support.containermenu.TransferManager;
import com.refinedmods.refinedstorage.platform.common.support.render.FluidRenderer;
Expand Down Expand Up @@ -100,13 +102,19 @@ public FluidRenderer getFluidRenderer() {
}

@Override
public Optional<ContainedFluid> getContainedFluid(final ItemStack stack) {
return ensureLoaded().getContainedFluid(stack);
public Optional<FluidOperationResult> drainContainer(final ItemStack container) {
return ensureLoaded().drainContainer(container);
}

@Override
public Optional<ItemStack> convertToBucket(final FluidResource fluidResource) {
return ensureLoaded().convertToBucket(fluidResource);
public Optional<FluidOperationResult> fillContainer(final ItemStack container,
final ResourceAmount resourceAmount) {
return ensureLoaded().fillContainer(container, resourceAmount);
}

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

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.refinedmods.refinedstorage.platform.common.grid.screen.hint;

import com.refinedmods.refinedstorage.platform.api.grid.GridInsertionHint;
import com.refinedmods.refinedstorage.platform.api.support.resource.FluidOperationResult;
import com.refinedmods.refinedstorage.platform.common.Platform;
import com.refinedmods.refinedstorage.platform.common.support.resource.FluidResource;
import com.refinedmods.refinedstorage.platform.common.support.resource.FluidResourceRendering;
import com.refinedmods.refinedstorage.platform.common.support.tooltip.MouseWithIconClientTooltipComponent;
import com.refinedmods.refinedstorage.platform.common.support.tooltip.MouseClientTooltipComponent;

import java.util.Optional;

Expand All @@ -13,18 +15,13 @@
public class FluidGridInsertionHint implements GridInsertionHint {
@Override
public Optional<ClientTooltipComponent> getHint(final ItemStack carried) {
return Platform.INSTANCE.getContainedFluid(carried).map(this::createComponent);
return Platform.INSTANCE.drainContainer(carried).map(this::createComponent);
}

private MouseWithIconClientTooltipComponent createComponent(final Platform.ContainedFluid result) {
return new MouseWithIconClientTooltipComponent(
MouseWithIconClientTooltipComponent.Type.RIGHT,
(graphics, x, y) -> Platform.INSTANCE.getFluidRenderer().render(
graphics.pose(),
x,
y,
result.fluid()
),
private ClientTooltipComponent createComponent(final FluidOperationResult result) {
return MouseClientTooltipComponent.fluid(
MouseClientTooltipComponent.Type.RIGHT,
(FluidResource) result.fluid(),
result.amount() == Platform.INSTANCE.getBucketAmount()
? null
: FluidResourceRendering.format(result.amount())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.refinedmods.refinedstorage.platform.api.grid.GridInsertionHint;
import com.refinedmods.refinedstorage.platform.api.support.AmountFormatting;
import com.refinedmods.refinedstorage.platform.common.support.tooltip.MouseWithIconClientTooltipComponent;
import com.refinedmods.refinedstorage.platform.common.support.tooltip.MouseClientTooltipComponent;

import java.util.Optional;

Expand All @@ -12,9 +12,9 @@
public class ItemGridInsertionHint implements GridInsertionHint {
@Override
public Optional<ClientTooltipComponent> getHint(final ItemStack carried) {
return Optional.of(new MouseWithIconClientTooltipComponent(
MouseWithIconClientTooltipComponent.Type.LEFT,
(graphics, x, y) -> graphics.renderItem(carried, x, y),
return Optional.of(MouseClientTooltipComponent.item(
MouseClientTooltipComponent.Type.LEFT,
carried,
carried.getCount() == 1 ? null : AmountFormatting.format(carried.getCount())
));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.refinedmods.refinedstorage.platform.common.grid.screen.hint;

import com.refinedmods.refinedstorage.platform.api.grid.GridInsertionHint;
import com.refinedmods.refinedstorage.platform.common.support.tooltip.MouseWithIconClientTooltipComponent;
import com.refinedmods.refinedstorage.platform.common.support.tooltip.MouseClientTooltipComponent;

import java.util.Optional;

Expand All @@ -11,9 +11,9 @@
public class SingleItemGridInsertionHint implements GridInsertionHint {
@Override
public Optional<ClientTooltipComponent> getHint(final ItemStack carried) {
return Optional.of(new MouseWithIconClientTooltipComponent(
MouseWithIconClientTooltipComponent.Type.RIGHT,
(graphics, x, y) -> graphics.renderItem(carried, x, y),
return Optional.of(MouseClientTooltipComponent.item(
MouseClientTooltipComponent.Type.RIGHT,
carried,
null
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.refinedmods.refinedstorage.platform.common.Platform;
import com.refinedmods.refinedstorage.platform.common.support.resource.FluidResource;
import com.refinedmods.refinedstorage.platform.common.support.resource.FluidResourceRendering;
import com.refinedmods.refinedstorage.platform.common.support.tooltip.MouseWithIconClientTooltipComponent;
import com.refinedmods.refinedstorage.platform.common.support.tooltip.MouseClientTooltipComponent;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -52,13 +52,11 @@ public int getRegistryId() {

@Override
public List<ClientTooltipComponent> getExtractionHints() {
return Platform.INSTANCE.convertToBucket(fluidResource).map(
bucket -> (ClientTooltipComponent) new MouseWithIconClientTooltipComponent(
MouseWithIconClientTooltipComponent.Type.LEFT,
(graphics, x, y) -> graphics.renderItem(bucket, x, y),
null
)
).stream().toList();
return Platform.INSTANCE.getFilledBucket(fluidResource).map(bucket -> MouseClientTooltipComponent.item(
MouseClientTooltipComponent.Type.LEFT,
bucket,
null
)).stream().toList();
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.refinedmods.refinedstorage.platform.api.support.AmountFormatting;
import com.refinedmods.refinedstorage.platform.api.support.resource.PlatformResourceKey;
import com.refinedmods.refinedstorage.platform.common.support.resource.ItemResource;
import com.refinedmods.refinedstorage.platform.common.support.tooltip.MouseWithIconClientTooltipComponent;
import com.refinedmods.refinedstorage.platform.common.support.tooltip.MouseClientTooltipComponent;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -71,14 +71,14 @@ public List<ClientTooltipComponent> getExtractionHints() {
final long extractableAmount = Math.min(getAmount(), itemStack.getMaxStackSize());
final long halfExtractionAmount = extractableAmount == 1 ? 1 : extractableAmount / 2;
return List.of(
new MouseWithIconClientTooltipComponent(
MouseWithIconClientTooltipComponent.Type.LEFT,
this::render,
MouseClientTooltipComponent.itemWithDecorations(
MouseClientTooltipComponent.Type.LEFT,
itemStack,
extractableAmount == 1 ? null : AmountFormatting.format(extractableAmount)
),
new MouseWithIconClientTooltipComponent(
MouseWithIconClientTooltipComponent.Type.RIGHT,
this::render,
MouseClientTooltipComponent.itemWithDecorations(
MouseClientTooltipComponent.Type.RIGHT,
itemStack,
halfExtractionAmount == 1 ? null : AmountFormatting.format(halfExtractionAmount)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public long insert(final ResourceKey resource, final long amount, final Action a
if (amount != Platform.INSTANCE.getBucketAmount()) {
return 0;
}
return Platform.INSTANCE.convertToBucket(fluidResource).map(
return Platform.INSTANCE.getFilledBucket(fluidResource).map(
filledBucketStack -> insert(filledBucketStack, amount, action, actor)
).orElse(0L);
}
Expand Down
Loading

0 comments on commit f461e49

Please sign in to comment.