Skip to content

Commit

Permalink
Merge pull request #641 from refinedmods/refactor/NO-ISSUE/immutable-…
Browse files Browse the repository at this point in the history
…resource-amount

Immutable resource amount
raoulvdberge authored Aug 3, 2024

Verified

This commit was signed with the committer’s verified signature.
gastaldi George Gastaldi
2 parents 48a5784 + a8adc84 commit e05b665
Showing 75 changed files with 916 additions and 920 deletions.
Original file line number Diff line number Diff line change
@@ -2,39 +2,40 @@

import com.refinedmods.refinedstorage.api.grid.view.GridResourceAttributeKey;
import com.refinedmods.refinedstorage.api.grid.view.GridView;
import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage.api.storage.tracked.TrackedResource;
import com.refinedmods.refinedstorage.common.api.support.resource.PlatformResourceKey;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;

import org.apiguardian.api.API;

@API(status = API.Status.STABLE, since = "2.0.0-milestone.3.0")
public abstract class AbstractPlatformGridResource implements PlatformGridResource {
protected final ResourceAmount resourceAmount;
public abstract class AbstractPlatformGridResource<T extends PlatformResourceKey> implements PlatformGridResource {
protected final T resource;
private final String name;
private final Map<GridResourceAttributeKey, Set<String>> attributes;
private boolean zeroed;

protected AbstractPlatformGridResource(final ResourceAmount resourceAmount,
protected AbstractPlatformGridResource(final T resource,
final String name,
final Map<GridResourceAttributeKey, Set<String>> attributes) {
this.resourceAmount = resourceAmount;
this.resource = resource;
this.name = name;
this.attributes = attributes;
}

@Override
public Optional<TrackedResource> getTrackedResource(final GridView view) {
return view.getTrackedResource(resourceAmount.getResource());
return view.getTrackedResource(resource);
}

@Override
public long getAmount() {
return resourceAmount.getAmount();
public long getAmount(final GridView view) {
return view.getAmount(resource);
}

@Override
@@ -57,10 +58,16 @@ public void setZeroed(final boolean zeroed) {
this.zeroed = zeroed;
}

@Nullable
@Override
public PlatformResourceKey getResourceForRecipeMods() {
return resource;
}

@Override
public String toString() {
return "AbstractPlatformGridResource{"
+ "resourceAmount=" + resourceAmount
+ "resource=" + resource
+ ", name='" + name + '\''
+ ", attributes=" + attributes
+ ", zeroed=" + zeroed
Original file line number Diff line number Diff line change
@@ -2,10 +2,12 @@

import com.refinedmods.refinedstorage.api.grid.operations.GridExtractMode;
import com.refinedmods.refinedstorage.api.grid.view.GridResource;
import com.refinedmods.refinedstorage.api.grid.view.GridView;
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.support.resource.PlatformResourceKey;
import com.refinedmods.refinedstorage.common.api.support.resource.ResourceType;

import java.util.List;
import java.util.Optional;
@@ -28,19 +30,21 @@ void onScroll(GridScrollMode scrollMode,

void render(GuiGraphics graphics, int x, int y);

String getDisplayedAmount();
String getDisplayedAmount(GridView view);

String getAmountInTooltip();
String getAmountInTooltip(GridView view);

boolean belongsToResourceType(ResourceType resourceType);

List<Component> getTooltip();

Optional<TooltipComponent> getTooltipImage();

int getRegistryId();

List<ClientTooltipComponent> getExtractionHints();
List<ClientTooltipComponent> getExtractionHints(GridView view);

@Nullable
@API(status = API.Status.INTERNAL)
PlatformResourceKey getUnderlyingResource();
PlatformResourceKey getResourceForRecipeMods();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.refinedmods.refinedstorage.common.api.storage.root;

import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.api.storage.root.RootStorage;

@@ -10,5 +9,5 @@

@API(status = API.Status.STABLE, since = "2.0.0-milestone.2.4")
public interface FuzzyRootStorage extends RootStorage {
Collection<ResourceAmount> getFuzzy(ResourceKey resource);
Collection<ResourceKey> getFuzzy(ResourceKey resource);
}
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

import com.refinedmods.refinedstorage.api.grid.operations.GridOperations;
import com.refinedmods.refinedstorage.api.grid.view.GridResource;
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.root.RootStorage;

@@ -33,9 +33,7 @@ public interface ResourceType {

double getDisplayAmount(long amount);

Optional<GridResource> toGridResource(ResourceAmount resourceAmount);

boolean isGridResourceBelonging(GridResource gridResource);
Optional<GridResource> toGridResource(ResourceKey resource);

long getInterfaceExportLimit();

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.refinedmods.refinedstorage.common.api.support.resource.list;

import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.api.resource.list.ResourceList;
import com.refinedmods.refinedstorage.common.api.support.resource.FuzzyModeNormalizer;
@@ -17,5 +16,5 @@ public interface FuzzyResourceList extends ResourceList {
* @param resource the resource, doesn't matter if it's normalized or not
* @return a list of fuzzy matched variants, or empty list if none found
*/
Collection<ResourceAmount> getFuzzy(ResourceKey resource);
Collection<ResourceKey> getFuzzy(ResourceKey resource);
}
Original file line number Diff line number Diff line change
@@ -49,14 +49,14 @@ public CraftingPatternClientTooltipComponent(final int width,
this.width = width;
this.height = height;
this.craftingPattern = craftingPattern;
final ItemResource outputResource = craftingPattern.output().getResource() instanceof ItemResource itemResource
final ItemResource outputResource = craftingPattern.output().resource() instanceof ItemResource itemResource
? itemResource
: null;
this.outputStack = outputResource != null
? outputResource.toItemStack(craftingPattern.output().getAmount())
? outputResource.toItemStack(craftingPattern.output().amount())
: null;
this.outputText = outputResource != null
? Component.literal(String.format("%dx ", craftingPattern.output().getAmount()))
? Component.literal(String.format("%dx ", craftingPattern.output().amount()))
.append(outputResource.toItemStack().getHoverName())
.withStyle(ChatFormatting.GRAY) : null;
}
Original file line number Diff line number Diff line change
@@ -244,11 +244,11 @@ private ItemStack createProcessingPattern() {
final ItemStack result = createPatternStack(PatternType.PROCESSING);
final List<Optional<ResourceAmount>> inputs = new ArrayList<>();
for (int i = 0; i < processingInput.size(); ++i) {
inputs.add(Optional.ofNullable(processingInput.get(i)).map(ResourceAmount::copy));
inputs.add(Optional.ofNullable(processingInput.get(i)));
}
final List<Optional<ResourceAmount>> outputs = new ArrayList<>();
for (int i = 0; i < processingOutput.size(); ++i) {
outputs.add(Optional.ofNullable(processingOutput.get(i)).map(ResourceAmount::copy));
outputs.add(Optional.ofNullable(processingOutput.get(i)));
}
final ProcessingPatternState patternProcessingState = new ProcessingPatternState(
inputs,
Original file line number Diff line number Diff line change
@@ -28,12 +28,12 @@ public static Optional<ItemStack> getOutput(final ItemStack stack) {
}
return RefinedStorageApi.INSTANCE.getPattern(stack, level).map(pattern -> {
if (pattern instanceof CraftingPattern craftingPattern
&& craftingPattern.output().getResource() instanceof ItemResource itemResource) {
&& craftingPattern.output().resource() instanceof ItemResource itemResource) {
return itemResource.toItemStack();
}
if (pattern instanceof ProcessingPattern processingPattern
&& processingPattern.outputs().size() == 1
&& processingPattern.outputs().getFirst().getResource() instanceof ItemResource itemResource) {
&& processingPattern.outputs().getFirst().resource() instanceof ItemResource itemResource) {
return itemResource.toItemStack();
}
return null;
Original file line number Diff line number Diff line change
@@ -60,14 +60,14 @@ private static List<Component> getOutputText(final ProcessingPatternState state)

private static Component getOutputText(final ResourceAmount resourceAmount) {
final ResourceRendering rendering = RefinedStorageApi.INSTANCE.getResourceRendering(
resourceAmount.getResource()
resourceAmount.resource()
);
final String displayAmount = rendering.getDisplayedAmount(
resourceAmount.getAmount(),
resourceAmount.amount(),
false
);
return Component.literal(String.format("%sx ", displayAmount))
.append(rendering.getDisplayName(resourceAmount.getResource()))
.append(rendering.getDisplayName(resourceAmount.resource()))
.withStyle(ChatFormatting.GRAY);
}

@@ -113,10 +113,10 @@ private void renderMatrixSlots(final int x,
graphics.blitSprite(SLOT, slotX, slotY, 18, 18);
slots.get(idx).ifPresent(resourceAmount -> {
final ResourceRendering rendering = RefinedStorageApi.INSTANCE.getResourceRendering(
resourceAmount.getResource()
resourceAmount.resource()
);
rendering.render(resourceAmount.getResource(), graphics, slotX + 1, slotY + 1);
AbstractBaseScreen.renderResourceAmount(graphics, slotX + 1, slotY + 1, resourceAmount.getAmount(),
rendering.render(resourceAmount.resource(), graphics, slotX + 1, slotY + 1);
AbstractBaseScreen.renderResourceAmount(graphics, slotX + 1, slotY + 1, resourceAmount.amount(),
rendering);
});
}
Original file line number Diff line number Diff line change
@@ -36,12 +36,12 @@ public record ProcessingPatternState(
List<ResourceAmount> getFlatInputs() {
final ResourceList list = ResourceListImpl.orderPreserving();
inputs.forEach(input -> input.ifPresent(list::add));
return new ArrayList<>(list.getAll());
return new ArrayList<>(list.copyState());
}

List<ResourceAmount> getFlatOutputs() {
final ResourceList list = ResourceListImpl.orderPreserving();
outputs.forEach(output -> output.ifPresent(list::add));
return new ArrayList<>(list.getAll());
return new ArrayList<>(list.copyState());
}
}
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@
import com.refinedmods.refinedstorage.api.network.Network;
import com.refinedmods.refinedstorage.api.network.impl.node.detector.AbstractDetectorAmountStrategy;
import com.refinedmods.refinedstorage.api.network.impl.node.detector.DetectorAmountStrategy;
import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.api.storage.root.RootStorage;
import com.refinedmods.refinedstorage.common.api.storage.root.FuzzyRootStorage;
@@ -23,7 +22,7 @@ public long getAmount(final Network network, final ResourceKey configuredResourc
}
return fuzzyRootStorage.getFuzzy(configuredResource)
.stream()
.mapToLong(ResourceAmount::getAmount)
.mapToLong(rootStorage::get)
.sum();
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.refinedmods.refinedstorage.common.exporter;

import com.refinedmods.refinedstorage.api.network.impl.node.exporter.ExporterTransferStrategyImpl;
import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.api.storage.InsertableStorage;
import com.refinedmods.refinedstorage.api.storage.root.RootStorage;
import com.refinedmods.refinedstorage.common.api.storage.root.FuzzyRootStorage;

import java.util.Collection;
import java.util.stream.Collectors;

public class FuzzyExporterTransferStrategy extends ExporterTransferStrategyImpl {
public FuzzyExporterTransferStrategy(final InsertableStorage destination, final long transferQuota) {
@@ -18,11 +16,7 @@ public FuzzyExporterTransferStrategy(final InsertableStorage destination, final
@Override
protected Collection<ResourceKey> expand(final ResourceKey resource, final RootStorage rootStorage) {
if (rootStorage instanceof FuzzyRootStorage fuzzyRootStorage) {
return fuzzyRootStorage
.getFuzzy(resource)
.stream()
.map(ResourceAmount::getResource)
.collect(Collectors.toSet());
return fuzzyRootStorage.getFuzzy(resource);
}
return super.expand(resource, rootStorage);
}
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@
import com.refinedmods.refinedstorage.common.api.grid.strategy.GridExtractionStrategy;
import com.refinedmods.refinedstorage.common.api.grid.strategy.GridInsertionStrategy;
import com.refinedmods.refinedstorage.common.api.grid.strategy.GridScrollingStrategy;
import com.refinedmods.refinedstorage.common.api.grid.view.PlatformGridResource;
import com.refinedmods.refinedstorage.common.api.storage.PlayerActor;
import com.refinedmods.refinedstorage.common.api.support.registry.PlatformRegistry;
import com.refinedmods.refinedstorage.common.api.support.resource.PlatformResourceKey;
@@ -39,7 +40,7 @@

import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.function.BiPredicate;
import javax.annotation.Nullable;

import net.minecraft.server.level.ServerPlayer;
@@ -100,10 +101,10 @@ protected AbstractGridContainerMenu(
this.active = gridData.active();

final GridViewBuilder viewBuilder = createViewBuilder();
gridData.resources().forEach(gridResource -> viewBuilder.withResource(
gridResource.resourceAmount().getResource(),
gridResource.resourceAmount().getAmount(),
gridResource.trackedResource().orElse(null)
gridData.resources().forEach(resource -> viewBuilder.withResource(
resource.resourceAmount().resource(),
resource.resourceAmount().amount(),
resource.trackedResource().orElse(null)
));

this.view = viewBuilder.build();
@@ -137,12 +138,13 @@ protected AbstractGridContainerMenu(
initStrategies((ServerPlayer) playerInventory.player);
}

private Predicate<GridResource> filterResourceType() {
return gridResource -> Platform.INSTANCE.getConfig().getGrid().getResourceType().flatMap(resourceTypeId ->
private BiPredicate<GridView, GridResource> filterResourceType() {
return (v, resource) -> resource instanceof PlatformGridResource platformResource
&& Platform.INSTANCE.getConfig().getGrid().getResourceType().flatMap(resourceTypeId ->
RefinedStorageApi.INSTANCE
.getResourceTypeRegistry()
.get(resourceTypeId)
.map(type -> type.isGridResourceBelonging(gridResource))
.map(platformResource::belongsToResourceType)
).orElse(true);
}

@@ -199,7 +201,7 @@ private boolean onSearchTextChanged(final String text) {
view.setFilterAndSort(QUERY_PARSER.parse(text).and(filterResourceType()));
return true;
} catch (GridQueryParserException e) {
view.setFilterAndSort(resource -> false);
view.setFilterAndSort((v, resource) -> false);
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.refinedmods.refinedstorage.common.grid;

import com.refinedmods.refinedstorage.api.grid.view.GridResource;
import com.refinedmods.refinedstorage.api.grid.view.GridView;
import com.refinedmods.refinedstorage.api.resource.list.ResourceList;
import com.refinedmods.refinedstorage.common.content.Menus;
import com.refinedmods.refinedstorage.common.grid.view.ItemGridResource;
@@ -14,8 +15,8 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Predicate;
import javax.annotation.Nullable;

import net.minecraft.world.Container;
@@ -35,7 +36,7 @@ public class CraftingGridContainerMenu extends AbstractGridContainerMenu {
@Nullable
private Consumer<Boolean> activenessListener;
@Nullable
private Predicate<GridResource> filterBeforeFilteringBasedOnCraftingMatrixItems;
private BiPredicate<GridView, GridResource> filterBeforeFilteringBasedOnCraftingMatrixItems;

public CraftingGridContainerMenu(final int syncId,
final Inventory playerInventory,
@@ -149,9 +150,9 @@ public void transferRecipe(final List<List<ItemResource>> recipe) {
public void filterBasedOnCraftingMatrixItems() {
final Set<ItemResource> craftingMatrixItems = getCraftingMatrixItems();
filterBeforeFilteringBasedOnCraftingMatrixItems = getView().setFilterAndSort(
gridResource -> gridResource instanceof ItemGridResource itemGridResource
&& itemGridResource.getUnderlyingResource() != null
&& craftingMatrixItems.contains((ItemResource) itemGridResource.getUnderlyingResource())
(view, gridResource) -> gridResource instanceof ItemGridResource itemGridResource
&& itemGridResource.getResourceForRecipeMods() != null
&& craftingMatrixItems.contains((ItemResource) itemGridResource.getResourceForRecipeMods())
);
}

Loading

0 comments on commit e05b665

Please sign in to comment.