Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make resource list interface immutable #681

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.refinedmods.refinedstorage.common.autocrafting;

import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage.api.resource.list.ResourceList;
import com.refinedmods.refinedstorage.api.resource.list.ResourceListImpl;
import com.refinedmods.refinedstorage.api.resource.list.MutableResourceList;
import com.refinedmods.refinedstorage.api.resource.list.MutableResourceListImpl;
import com.refinedmods.refinedstorage.common.support.resource.ResourceCodecs;

import java.util.ArrayList;
Expand Down Expand Up @@ -35,13 +35,13 @@ public record ProcessingPatternState(
);

List<ResourceAmount> getFlatInputs() {
final ResourceList list = ResourceListImpl.orderPreserving();
final MutableResourceList list = MutableResourceListImpl.orderPreserving();
inputs.forEach(input -> input.map(Input::input).ifPresent(list::add));
return new ArrayList<>(list.copyState());
}

List<ResourceAmount> getFlatOutputs() {
final ResourceList list = ResourceListImpl.orderPreserving();
final MutableResourceList list = MutableResourceListImpl.orderPreserving();
outputs.forEach(output -> output.ifPresent(list::add));
return new ArrayList<>(list.copyState());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.refinedmods.refinedstorage.api.grid.view.GridResource;
import com.refinedmods.refinedstorage.api.grid.view.GridView;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.api.resource.list.MutableResourceList;
import com.refinedmods.refinedstorage.api.resource.list.ResourceList;
import com.refinedmods.refinedstorage.common.content.Menus;
import com.refinedmods.refinedstorage.common.grid.view.ItemGridResource;
Expand Down Expand Up @@ -128,13 +129,13 @@ public void clear(final boolean toPlayerInventory) {

@API(status = API.Status.INTERNAL)
public ResourceList getAvailableListForRecipeTransfer() {
final ResourceList available = getView().copyBackingList();
final MutableResourceList available = getView().copyBackingList();
addContainerToList(craftingGrid.getCraftingMatrix(), available);
addContainerToList(gridPlayer.getInventory(), available);
return available;
}

private void addContainerToList(final Container container, final ResourceList available) {
private void addContainerToList(final Container container, final MutableResourceList available) {
for (int i = 0; i < container.getContainerSize(); ++i) {
final ItemStack stack = container.getItem(i);
if (stack.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.refinedmods.refinedstorage.api.core.Action;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.api.resource.list.ResourceList;
import com.refinedmods.refinedstorage.api.resource.list.ResourceListImpl;
import com.refinedmods.refinedstorage.api.resource.list.MutableResourceList;
import com.refinedmods.refinedstorage.api.resource.list.MutableResourceListImpl;
import com.refinedmods.refinedstorage.api.storage.root.RootStorage;
import com.refinedmods.refinedstorage.common.api.storage.PlayerActor;
import com.refinedmods.refinedstorage.common.support.RecipeMatrixContainer;
Expand All @@ -15,8 +15,8 @@
class SnapshotCraftingGridRefillContext implements CraftingGridRefillContext {
private final PlayerActor playerActor;
private final CraftingGridBlockEntity blockEntity;
private final ResourceList available = ResourceListImpl.create();
private final ResourceList used = ResourceListImpl.create();
private final MutableResourceList available = MutableResourceListImpl.create();
private final MutableResourceList used = MutableResourceListImpl.create();

SnapshotCraftingGridRefillContext(
final Player player,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import com.refinedmods.refinedstorage.api.network.Network;
import com.refinedmods.refinedstorage.api.network.storage.StorageNetworkComponent;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.api.resource.list.MutableResourceList;
import com.refinedmods.refinedstorage.api.resource.list.MutableResourceListImpl;
import com.refinedmods.refinedstorage.api.resource.list.ResourceList;
import com.refinedmods.refinedstorage.api.resource.list.ResourceListImpl;
import com.refinedmods.refinedstorage.common.support.resource.ItemResource;

import java.util.Comparator;
Expand All @@ -25,19 +26,20 @@ public static Comparator<ResourceKey> create(@Nullable final Network network, fi
public static <T> Comparator<T> create(@Nullable final Network network,
final Inventory playerInventory,
final Function<T, ResourceKey> resourceExtractor) {
final ResourceList available = ResourceListImpl.create();
final MutableResourceList available = MutableResourceListImpl.create();
addNetworkItemsIntoList(network, available);
addPlayerInventoryItemsIntoList(playerInventory, available);
return sortByHighestAvailableFirst(available, resourceExtractor);
}

private static void addNetworkItemsIntoList(@Nullable final Network network, final ResourceList list) {
private static void addNetworkItemsIntoList(@Nullable final Network network, final MutableResourceList list) {
if (network != null) {
network.getComponent(StorageNetworkComponent.class).getAll().forEach(list::add);
}
}

private static void addPlayerInventoryItemsIntoList(final Inventory playerInventory, final ResourceList list) {
private static void addPlayerInventoryItemsIntoList(final Inventory playerInventory,
final MutableResourceList list) {
for (int i = 0; i < playerInventory.getContainerSize(); ++i) {
final ItemStack playerInventoryStack = playerInventory.getItem(i);
if (playerInventoryStack.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.refinedmods.refinedstorage.api.network.impl.storage.StorageNetworkComponentImpl;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.api.resource.list.ResourceListImpl;
import com.refinedmods.refinedstorage.api.resource.list.MutableResourceListImpl;
import com.refinedmods.refinedstorage.common.api.storage.root.FuzzyRootStorage;
import com.refinedmods.refinedstorage.common.api.support.resource.list.FuzzyResourceList;
import com.refinedmods.refinedstorage.common.support.resource.list.FuzzyResourceListImpl;
Expand All @@ -13,10 +13,10 @@ public class PlatformStorageNetworkComponent extends StorageNetworkComponentImpl
private final FuzzyResourceList fuzzyResourceList;

public PlatformStorageNetworkComponent() {
this(new FuzzyResourceListImpl(ResourceListImpl.create()));
this(new FuzzyResourceListImpl(MutableResourceListImpl.create()));
}

private PlatformStorageNetworkComponent(final FuzzyResourceList fuzzyResourceList) {
private PlatformStorageNetworkComponent(final FuzzyResourceListImpl fuzzyResourceList) {
super(fuzzyResourceList);
this.fuzzyResourceList = fuzzyResourceList;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.refinedmods.refinedstorage.common.support.resource.list;

import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.api.resource.list.AbstractProxyResourceList;
import com.refinedmods.refinedstorage.api.resource.list.ResourceList;
import com.refinedmods.refinedstorage.api.resource.list.AbstractProxyMutableResourceList;
import com.refinedmods.refinedstorage.api.resource.list.MutableResourceList;
import com.refinedmods.refinedstorage.common.api.support.resource.FuzzyModeNormalizer;
import com.refinedmods.refinedstorage.common.api.support.resource.list.FuzzyResourceList;

Expand All @@ -14,10 +14,10 @@
import java.util.Optional;
import java.util.Set;

public class FuzzyResourceListImpl extends AbstractProxyResourceList implements FuzzyResourceList {
public class FuzzyResourceListImpl extends AbstractProxyMutableResourceList implements FuzzyResourceList {
private final Map<ResourceKey, Set<ResourceKey>> normalizedFuzzyMap = new HashMap<>();

public FuzzyResourceListImpl(final ResourceList delegate) {
public FuzzyResourceListImpl(final MutableResourceList delegate) {
super(delegate);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.refinedmods.refinedstorage.common.support.resource.list;

import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.api.resource.list.ResourceListImpl;
import com.refinedmods.refinedstorage.api.resource.list.MutableResourceListImpl;
import com.refinedmods.refinedstorage.common.SetupMinecraft;
import com.refinedmods.refinedstorage.common.api.support.resource.list.FuzzyResourceList;
import com.refinedmods.refinedstorage.common.support.resource.ItemResource;

import java.util.Collection;
Expand All @@ -28,11 +27,11 @@ class FuzzyResourceListImplTest {
private static final ItemResource DUMMY_D = new ItemResource(Items.GLASS, DataComponentPatch.EMPTY);
private static final ItemResource DUMMY_E = new ItemResource(Items.DARK_OAK_DOOR, DataComponentPatch.EMPTY);

FuzzyResourceList sut;
FuzzyResourceListImpl sut;

@BeforeEach
void setUp() {
sut = new FuzzyResourceListImpl(ResourceListImpl.create());
sut = new FuzzyResourceListImpl(MutableResourceListImpl.create());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.refinedmods.refinedstorage.api.grid.view;

import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.api.resource.list.ResourceList;
import com.refinedmods.refinedstorage.api.resource.list.MutableResourceList;
import com.refinedmods.refinedstorage.api.storage.tracked.TrackedResource;

import java.util.List;
Expand Down Expand Up @@ -100,7 +100,7 @@ public interface GridView {
/**
* @return a copy of the backing list
*/
ResourceList copyBackingList();
MutableResourceList copyBackingList();

/**
* Clears the backing list, view list and tracked resources index.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.refinedmods.refinedstorage.api.grid.view;

import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.api.resource.list.ResourceList;
import com.refinedmods.refinedstorage.api.resource.list.ResourceListImpl;
import com.refinedmods.refinedstorage.api.resource.list.MutableResourceList;
import com.refinedmods.refinedstorage.api.resource.list.MutableResourceListImpl;
import com.refinedmods.refinedstorage.api.storage.tracked.TrackedResource;

import java.util.HashMap;
Expand All @@ -16,7 +16,7 @@
@API(status = API.Status.STABLE, since = "2.0.0-milestone.2.4")
public class GridViewBuilderImpl implements GridViewBuilder {
private final GridResourceFactory resourceFactory;
private final ResourceList backingList = ResourceListImpl.create();
private final MutableResourceList backingList = MutableResourceListImpl.create();
private final Set<ResourceKey> craftableResources = new HashSet<>();
private final Map<ResourceKey, TrackedResource> trackedResources = new HashMap<>();
private final GridSortingType identitySortingType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.refinedmods.refinedstorage.api.core.CoreValidations;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.api.resource.list.ResourceList;
import com.refinedmods.refinedstorage.api.resource.list.MutableResourceList;
import com.refinedmods.refinedstorage.api.storage.tracked.TrackedResource;

import java.util.ArrayList;
Expand All @@ -24,7 +24,7 @@
public class GridViewImpl implements GridView {
private static final Logger LOGGER = LoggerFactory.getLogger(GridViewImpl.class);

private final ResourceList backingList;
private final MutableResourceList backingList;
private final Comparator<GridResource> identitySort;
private final GridResourceFactory resourceFactory;
private final Map<ResourceKey, TrackedResource> trackedResources = new HashMap<>();
Expand All @@ -47,7 +47,7 @@ public class GridViewImpl implements GridView {
* @param craftableResources resources which are craftable and must stay in the view list
*/
public GridViewImpl(final GridResourceFactory resourceFactory,
final ResourceList backingList,
final MutableResourceList backingList,
final Map<ResourceKey, TrackedResource> initialTrackedResources,
final Set<ResourceKey> craftableResources,
final GridSortingType identitySortingType,
Expand Down Expand Up @@ -156,7 +156,7 @@ public void onChange(final ResourceKey resource,
final long amount,
@Nullable final TrackedResource trackedResource) {
final boolean wasAvailable = backingList.contains(resource);
final ResourceList.OperationResult operationResult = updateBackingList(resource, amount);
final MutableResourceList.OperationResult operationResult = updateBackingList(resource, amount);
updateOrRemoveTrackedResource(resource, trackedResource);
final GridResource gridResource = viewList.index.get(resource);
if (gridResource != null) {
Expand All @@ -172,7 +172,7 @@ public void onChange(final ResourceKey resource,
}
}

private ResourceList.OperationResult updateBackingList(final ResourceKey resource, final long amount) {
private MutableResourceList.OperationResult updateBackingList(final ResourceKey resource, final long amount) {
if (amount < 0) {
return backingList.remove(resource, Math.abs(amount)).orElseThrow(RuntimeException::new);
} else {
Expand Down Expand Up @@ -204,7 +204,7 @@ private void reinsertIntoViewList(final ResourceKey resource, final GridResource
}

private void handleChangeForExistingResource(final ResourceKey resource,
final ResourceList.OperationResult operationResult,
final MutableResourceList.OperationResult operationResult,
final GridResource gridResource) {
final boolean noLongerAvailable = !operationResult.available();
final boolean canBeSorted = !preventSorting;
Expand Down Expand Up @@ -283,7 +283,7 @@ public List<GridResource> getViewList() {
}

@Override
public ResourceList copyBackingList() {
public MutableResourceList copyBackingList() {
return backingList.copy();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.refinedmods.refinedstorage.api.grid.view.GridResourceImpl;
import com.refinedmods.refinedstorage.api.grid.view.GridView;
import com.refinedmods.refinedstorage.api.grid.view.GridViewImpl;
import com.refinedmods.refinedstorage.api.resource.list.ResourceListImpl;
import com.refinedmods.refinedstorage.api.resource.list.MutableResourceListImpl;
import com.refinedmods.refinedstorage.api.storage.tracked.TrackedResource;
import com.refinedmods.refinedstorage.query.lexer.LexerTokenMappings;
import com.refinedmods.refinedstorage.query.parser.ParserOperatorMappings;
Expand Down Expand Up @@ -35,7 +35,7 @@ class GridQueryParserImplTest {

private final GridView view = new GridViewImpl(
(resource, craftable) -> Optional.of(new GridResourceImpl(resource)),
ResourceListImpl.create(),
MutableResourceListImpl.create(),
new HashMap<>(),
new HashSet<>(),
v -> Comparator.comparing(GridResource::getName),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import com.refinedmods.refinedstorage.api.network.storage.StorageNetworkComponent;
import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.api.resource.list.MutableResourceList;
import com.refinedmods.refinedstorage.api.resource.list.MutableResourceListImpl;
import com.refinedmods.refinedstorage.api.resource.list.ResourceList;
import com.refinedmods.refinedstorage.api.resource.list.ResourceListImpl;
import com.refinedmods.refinedstorage.api.storage.EmptyActor;
import com.refinedmods.refinedstorage.common.api.support.network.AbstractNetworkNodeContainerBlockEntity;
import com.refinedmods.refinedstorage.common.api.support.resource.ResourceContainer;
Expand Down Expand Up @@ -238,7 +239,7 @@ private static Runnable resourceContainerContainsExactly(final ResourceContainer
final ResourceAmount... expected) {
final ResourceList expectedList = toResourceList(expected);
return () -> {
final ResourceList given = ResourceListImpl.create();
final MutableResourceList given = MutableResourceListImpl.create();
for (int i = 0; i < container.size(); i++) {
final ResourceAmount item = container.get(i);
if (item != null) {
Expand All @@ -255,7 +256,7 @@ public static Runnable containerContainsExactly(final GameTestHelper helper,
final var containerBlockEntity = requireBlockEntity(helper, pos, BaseContainerBlockEntity.class);
final ResourceList expectedList = toResourceList(expected);
return () -> {
final ResourceList given = ResourceListImpl.create();
final MutableResourceList given = MutableResourceListImpl.create();
for (int i = 0; i < containerBlockEntity.getContainerSize(); i++) {
final ItemStack itemStack = containerBlockEntity.getItem(i);
if (!itemStack.isEmpty()) {
Expand All @@ -281,7 +282,7 @@ private static ResourceList toResourceList(final ResourceAmount... resources) {
}

private static ResourceList toResourceList(final Collection<ResourceAmount> resources) {
final ResourceList list = ResourceListImpl.create();
final MutableResourceList list = MutableResourceListImpl.create();
for (final ResourceAmount resource : resources) {
list.add(resource);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import com.refinedmods.refinedstorage.api.network.security.SecurityNetworkComponent;
import com.refinedmods.refinedstorage.api.network.security.SecurityPolicy;
import com.refinedmods.refinedstorage.api.network.storage.StorageNetworkComponent;
import com.refinedmods.refinedstorage.api.resource.list.ResourceListImpl;
import com.refinedmods.refinedstorage.api.resource.list.MutableResourceListImpl;
import com.refinedmods.refinedstorage.network.test.fake.FakePermissions;

public final class NetworkTestFixtures {
Expand All @@ -33,7 +33,7 @@ public final class NetworkTestFixtures {
);
NETWORK_COMPONENT_MAP_FACTORY.addFactory(
StorageNetworkComponent.class,
network -> new StorageNetworkComponentImpl(ResourceListImpl.orderPreserving())
network -> new StorageNetworkComponentImpl(MutableResourceListImpl.orderPreserving())
);
NETWORK_COMPONENT_MAP_FACTORY.addFactory(
SecurityNetworkComponent.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.api.resource.filter.Filter;
import com.refinedmods.refinedstorage.api.resource.filter.FilterMode;
import com.refinedmods.refinedstorage.api.resource.list.ResourceList;
import com.refinedmods.refinedstorage.api.resource.list.MutableResourceList;
import com.refinedmods.refinedstorage.api.resource.list.listenable.ResourceListListener;
import com.refinedmods.refinedstorage.api.storage.AccessMode;
import com.refinedmods.refinedstorage.api.storage.Actor;
Expand Down Expand Up @@ -159,7 +159,7 @@ public long getStored() {
}

@Override
public void onChanged(final ResourceList.OperationResult change) {
public void onChanged(final MutableResourceList.OperationResult change) {
if (delegate != null && delegate.contains(delegate)) {
return;
}
Expand Down
Loading
Loading