Skip to content

Commit

Permalink
Merge pull request #491 from refinedmods/fix/GH-480/cme
Browse files Browse the repository at this point in the history
GridView CME fix
  • Loading branch information
raoulvdberge authored Mar 11, 2024
2 parents 303cb25 + b8a0f04 commit e58e472
Show file tree
Hide file tree
Showing 32 changed files with 153 additions and 143 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- Fixed losing disk when using Wrench dismantling on the Portable Grid.
- Fixed losing energy when using Wrench dismantling on the Portable Grid and the Controller.
- Fixed changing side buttons not working on Forge.
- Fixed External Storage not displaying empty allowlist warning.
- Fixed incrementing starting from 1 in amount screens not having an intended off-by-one.

## [2.0.0-milestone.3.3] - 2024-02-17

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@

import org.apiguardian.api.API;

/**
* Transforms resources into {@link GridResource}s.
*/
@FunctionalInterface
@API(status = API.Status.STABLE, since = "2.0.0-milestone.2.6")
public interface GridResourceFactory {
/**
* Transforms a {@link ResourceAmount} into a {@link GridResource}.
* It's important to keep the {@link ResourceAmount} instance around to
* get updated resource amounts from the {@link GridView} backing list.
*
* @param resourceAmount the resource amount from the backing list
* @return the grid resource, if applicable
*/
Optional<GridResource> apply(ResourceAmount resourceAmount);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.refinedmods.refinedstorage2.api.resource.ResourceKey;
import com.refinedmods.refinedstorage2.api.resource.list.ResourceList;
import com.refinedmods.refinedstorage2.api.resource.list.ResourceListImpl;
import com.refinedmods.refinedstorage2.api.resource.list.ResourceListOperationResult;
import com.refinedmods.refinedstorage2.api.storage.tracked.TrackedResource;

import java.util.ArrayList;
Expand Down Expand Up @@ -120,7 +119,7 @@ public void sort() {
public void onChange(final ResourceKey resource,
final long amount,
@Nullable final TrackedResource trackedResource) {
final ResourceListOperationResult operationResult = updateBackingList(resource, amount);
final ResourceList.OperationResult operationResult = updateBackingList(resource, amount);

updateOrRemoveTrackedResource(resource, trackedResource);

Expand All @@ -138,7 +137,7 @@ public void onChange(final ResourceKey resource,
}
}

private ResourceListOperationResult updateBackingList(final ResourceKey resource, final long amount) {
private ResourceList.OperationResult updateBackingList(final ResourceKey resource, final long amount) {
if (amount < 0) {
return backingList.remove(resource, Math.abs(amount)).orElseThrow(RuntimeException::new);
} else {
Expand All @@ -156,7 +155,7 @@ private void updateOrRemoveTrackedResource(final ResourceKey resource,
}

private void reinsertZeroedResourceIntoViewList(final ResourceKey resource,
final ResourceListOperationResult operationResult,
final ResourceList.OperationResult operationResult,
final GridResource oldGridResource) {
LOGGER.debug("{} was zeroed, unzeroing", resource);
final GridResource newResource = resourceFactory.apply(operationResult.resourceAmount()).orElseThrow();
Expand All @@ -169,7 +168,7 @@ private void reinsertZeroedResourceIntoViewList(final ResourceKey resource,
}

private void handleChangeForExistingResource(final ResourceKey resource,
final ResourceListOperationResult operationResult,
final ResourceList.OperationResult operationResult,
final GridResource gridResource) {
final boolean noLongerAvailable = !operationResult.available();
final boolean canBeSorted = !preventSorting;
Expand Down Expand Up @@ -198,7 +197,7 @@ private void updateExistingResourceInViewList(final ResourceKey resource,
}

private void handleChangeForNewResource(final ResourceKey resource,
final ResourceListOperationResult operationResult) {
final ResourceList.OperationResult operationResult) {
final GridResource gridResource = resourceFactory.apply(operationResult.resourceAmount()).orElseThrow();
if (filter.test(gridResource)) {
LOGGER.debug("Filter allowed, actually adding {}", resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
import org.apiguardian.api.API;

/**
* Represents a {@link Container} that can hold any resource type.
* Represents a {@link Container} that can hold any {@link ResourceType}.
*/
@API(status = API.Status.STABLE, since = "2.0.0-milestone.2.13")
public interface ResourceContainer {
ResourceContainerType getType();

void setListener(@Nullable Runnable listener);

void change(int index, ItemStack stack, boolean tryAlternatives);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.refinedmods.refinedstorage2.platform.api.PlatformApi;
import com.refinedmods.refinedstorage2.platform.api.storage.channel.FuzzyStorageChannel;
import com.refinedmods.refinedstorage2.platform.api.support.resource.FuzzyModeNormalizer;
import com.refinedmods.refinedstorage2.platform.api.support.resource.ResourceContainerType;
import com.refinedmods.refinedstorage2.platform.common.support.FilterWithFuzzyMode;
import com.refinedmods.refinedstorage2.platform.common.support.resource.ResourceContainerImpl;

Expand All @@ -22,7 +21,6 @@ public class ExportedResourcesContainer extends ResourceContainerImpl implements
ExportedResourcesContainer(final int size, final FilterWithFuzzyMode filter) {
super(
size,
ResourceContainerType.CONTAINER,
InterfaceBlockEntity::getTransferQuota,
PlatformApi.INSTANCE.getItemResourceFactory(),
PlatformApi.INSTANCE.getAlternativeResourceFactories()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.refinedmods.refinedstorage2.platform.api.PlatformApi;
import com.refinedmods.refinedstorage2.platform.api.support.resource.PlatformResourceKey;
import com.refinedmods.refinedstorage2.platform.api.support.resource.ResourceContainer;
import com.refinedmods.refinedstorage2.platform.api.support.resource.ResourceContainerType;
import com.refinedmods.refinedstorage2.platform.common.Platform;
import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities;
import com.refinedmods.refinedstorage2.platform.common.content.ContentNames;
Expand Down Expand Up @@ -62,7 +61,6 @@ public InterfaceBlockEntity(final BlockPos pos, final BlockState state) {
static ResourceContainer createFilterContainer() {
return new ResourceContainerImpl(
EXPORT_SLOTS,
ResourceContainerType.FILTER_WITH_AMOUNT,
InterfaceBlockEntity::getTransferQuota,
PlatformApi.INSTANCE.getItemResourceFactory(),
PlatformApi.INSTANCE.getAlternativeResourceFactories()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ClientProperty;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.PropertyTypes;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ResourceSlot;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ResourceSlotType;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ServerProperty;

import net.minecraft.network.FriendlyByteBuf;
Expand Down Expand Up @@ -81,7 +82,8 @@ private Slot createExportConfigSlot(
index,
createTranslation("gui", "interface.filter_help"),
x,
EXPORT_CONFIG_SLOT_Y
EXPORT_CONFIG_SLOT_Y,
ResourceSlotType.FILTER_WITH_AMOUNT
);
}

Expand All @@ -97,7 +99,8 @@ private Slot addExportedResourceSlot(
index,
Component.empty(),
x,
EXPORT_OUTPUT_SLOT_Y
EXPORT_OUTPUT_SLOT_Y,
ResourceSlotType.CONTAINER
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.refinedmods.refinedstorage2.platform.common.storage.StorageAccessor;
import com.refinedmods.refinedstorage2.platform.common.storage.StorageConfigurationContainer;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ResourceSlot;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ResourceSlotType;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ValidatedSlot;
import com.refinedmods.refinedstorage2.platform.common.support.resource.ResourceContainerImpl;

Expand Down Expand Up @@ -83,7 +84,8 @@ private Slot createFilterSlot(final ResourceContainer resourceContainer, final i
i,
createTranslation("gui", "storage.filter_help"),
x,
FILTER_SLOT_Y
FILTER_SLOT_Y,
ResourceSlotType.FILTER
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.refinedmods.refinedstorage2.platform.common.storage.AbstractStorageContainerMenu;
import com.refinedmods.refinedstorage2.platform.common.storage.StorageConfigurationContainer;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ResourceSlot;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ResourceSlotType;
import com.refinedmods.refinedstorage2.platform.common.support.resource.ResourceContainerImpl;

import net.minecraft.network.FriendlyByteBuf;
Expand Down Expand Up @@ -48,7 +49,8 @@ private Slot createFilterSlot(final ResourceContainer resourceContainer, final i
i,
createTranslation("gui", "storage.filter_help"),
x,
FILTER_SLOT_Y
FILTER_SLOT_Y,
ResourceSlotType.FILTER
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.refinedmods.refinedstorage2.platform.common.storage.StorageAccessor;
import com.refinedmods.refinedstorage2.platform.common.storage.StorageConfigurationContainer;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ResourceSlot;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ResourceSlotType;
import com.refinedmods.refinedstorage2.platform.common.support.resource.ResourceContainerImpl;

import net.minecraft.network.FriendlyByteBuf;
Expand Down Expand Up @@ -60,7 +61,8 @@ private Slot createFilterSlot(final ResourceContainer resourceContainer, final i
i,
createTranslation("gui", "storage.filter_help"),
x,
FILTER_SLOT_Y
FILTER_SLOT_Y,
ResourceSlotType.FILTER
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ClientProperty;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.PropertyTypes;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ResourceSlot;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ResourceSlotType;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ServerProperty;
import com.refinedmods.refinedstorage2.platform.common.support.resource.ResourceContainerImpl;

Expand Down Expand Up @@ -47,7 +48,7 @@ public StorageMonitorContainerMenu(final int syncId, final Inventory playerInven
}

private void addSlots(final Inventory playerInventory, final ResourceContainer resourceContainer) {
addSlot(new ResourceSlot(resourceContainer, 0, FILTER_HELP, 80, 20));
addSlot(new ResourceSlot(resourceContainer, 0, FILTER_HELP, 80, 20, ResourceSlotType.FILTER));
addPlayerInventory(playerInventory, 8, 55);
transferManager.addFilterTransfer(playerInventory);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,24 @@ private void changeAmount(final int delta) {
return;
}
getAndValidateAmount().ifPresent(oldAmount -> {
final int correctedDelta = correctDelta(oldAmount, delta);
final N newAmount = amountOperations.changeAmount(
oldAmount,
delta,
correctedDelta,
configuration.getMinAmount(),
configuration.getMaxAmount()
);
amountField.setValue(amountOperations.format(newAmount));
});
}

private int correctDelta(final N oldAmount, final int delta) {
if (oldAmount.intValue() == 1 && delta > 0) {
return delta - 1;
}
return delta;
}

@Override
public boolean mouseScrolled(final double x, final double y, final double z, final double delta) {
if (delta > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ private void addSlots(final Player player,

private Slot createFilterSlot(final ResourceContainer resourceContainer, final int i) {
final int x = FILTER_SLOT_X + (18 * i);
return new ResourceSlot(resourceContainer, i, filterHelp, x, FILTER_SLOT_Y);
return new ResourceSlot(resourceContainer, i, filterHelp, x, FILTER_SLOT_Y, ResourceSlotType.FILTER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected AbstractSingleAmountContainerMenu(final MenuType<?> type,
}

private void addSlots(final Player player, final ResourceContainer resourceContainer) {
addSlot(new ResourceSlot(resourceContainer, 0, filterHelpText, 116, 47));
addSlot(new ResourceSlot(resourceContainer, 0, filterHelpText, 116, 47, ResourceSlotType.FILTER));
addPlayerInventory(player.getInventory(), 8, 106);
transferManager.addFilterTransfer(player.getInventory());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.refinedmods.refinedstorage2.api.resource.ResourceKey;
import com.refinedmods.refinedstorage2.platform.api.support.resource.PlatformResourceKey;
import com.refinedmods.refinedstorage2.platform.api.support.resource.ResourceContainer;
import com.refinedmods.refinedstorage2.platform.api.support.resource.ResourceContainerType;
import com.refinedmods.refinedstorage2.platform.api.support.resource.ResourceFactory;
import com.refinedmods.refinedstorage2.platform.common.Platform;

Expand All @@ -28,31 +27,35 @@ public class ResourceSlot extends Slot {

private final ResourceContainer resourceContainer;
private final Component helpText;
private final ResourceSlotType type;
@Nullable
private ResourceAmount cachedResource;

public ResourceSlot(final ResourceContainer resourceContainer,
final int index,
final Component helpText,
final int x,
final int y) {
this(resourceContainer, new SimpleContainer(resourceContainer.size()), index, helpText, x, y);
final int y,
final ResourceSlotType type) {
this(resourceContainer, new SimpleContainer(resourceContainer.size()), index, helpText, x, y, type);
}

public ResourceSlot(final ResourceContainer resourceContainer,
final Container resourceContainerAsContainer,
final int index,
final Component helpText,
final int x,
final int y) {
final int y,
final ResourceSlotType type) {
super(resourceContainerAsContainer, index, x, y);
this.resourceContainer = resourceContainer;
this.helpText = helpText;
this.cachedResource = resourceContainer.get(index);
this.type = type;
}

public ResourceSlot forAmountScreen(final int newX, final int newY) {
return new ResourceSlot(resourceContainer, container, index, helpText, newX, newY) {
return new ResourceSlot(resourceContainer, container, index, helpText, newX, newY, type) {
@Override
public boolean canModifyAmount() {
return false;
Expand All @@ -71,21 +74,21 @@ public boolean isDisabled() {
}

public boolean shouldRenderAmount() {
return resourceContainer.getType() == ResourceContainerType.FILTER_WITH_AMOUNT
|| resourceContainer.getType() == ResourceContainerType.CONTAINER;
return type == ResourceSlotType.FILTER_WITH_AMOUNT
|| type == ResourceSlotType.CONTAINER;
}

public boolean isFilter() {
return resourceContainer.getType() == ResourceContainerType.FILTER
|| resourceContainer.getType() == ResourceContainerType.FILTER_WITH_AMOUNT;
return type == ResourceSlotType.FILTER
|| type == ResourceSlotType.FILTER_WITH_AMOUNT;
}

public boolean canModifyAmount() {
return resourceContainer.getType() == ResourceContainerType.FILTER_WITH_AMOUNT;
return type == ResourceSlotType.FILTER_WITH_AMOUNT;
}

public boolean supportsItemSlotInteractions() {
return resourceContainer.getType() == ResourceContainerType.CONTAINER;
return type == ResourceSlotType.CONTAINER;
}

public boolean isDisabled() {
Expand Down Expand Up @@ -144,7 +147,7 @@ public boolean changeIfEmpty(final ItemStack stack) {
}

public void changeAmount(final long amount) {
if (resourceContainer.getType() != ResourceContainerType.FILTER_WITH_AMOUNT) {
if (type != ResourceSlotType.FILTER_WITH_AMOUNT) {
return;
}
resourceContainer.setAmount(getContainerSlot(), amount);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.refinedmods.refinedstorage2.platform.common.support.containermenu;

public enum ResourceSlotType {
FILTER,
FILTER_WITH_AMOUNT,
CONTAINER
}
Loading

0 comments on commit e58e472

Please sign in to comment.