Skip to content

Commit

Permalink
feat: void excess mode
Browse files Browse the repository at this point in the history
Only available in allowlist mode to avoid people trashing all their resources without realizing it
and blaming Refined Storage.

The void excess mode logic will only work when the resource was accepted through the filter,
otherwise, a next storage is used instead.

Requiring allowlist mode requires the player to think explicitly about the resources to be trashed.
  • Loading branch information
raoulvdberge committed Mar 10, 2024
1 parent f07c45c commit bbde868
Show file tree
Hide file tree
Showing 38 changed files with 326 additions and 216 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

- Void excess mode to storages.

### Fixed

- Fixed losing disk when using Wrench dismantling on the Portable Grid.
Expand Down
15 changes: 7 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ subprojects {

ext {
apis = [
':refinedstorage2-core-api',
':refinedstorage2-resource-api',
':refinedstorage2-storage-api',
':refinedstorage2-network-api',
':refinedstorage2-network',
':refinedstorage2-grid-api',
':refinedstorage2-query-parser'
':refinedstorage2-core-api',
':refinedstorage2-resource-api',
':refinedstorage2-storage-api',
':refinedstorage2-network-api',
':refinedstorage2-network',
':refinedstorage2-grid-api',
':refinedstorage2-query-parser'
]
compileApis = [':refinedstorage2-platform-api']
}
Expand All @@ -22,7 +22,6 @@ enableSonarQube("refinedmods_refinedstorage2")
sonarqube {
properties {
property "sonar.coverage.exclusions", "refinedstorage2-platform-forge/**/*,refinedstorage2-platform-fabric/**/*,refinedstorage2-platform-common/**/*,refinedstorage2-platform-api/**/*"
// TODO: REI code is duplicated as reusing it in a common module isn't possible/too difficult. Fix this.
property "sonar.cpd.exclusions", "refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/integration/recipemod/rei/*,refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/integration/recipemod/rei/*"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import com.refinedmods.refinedstorage2.api.core.CoreValidations;
import com.refinedmods.refinedstorage2.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage2.api.resource.ResourceKey;
import com.refinedmods.refinedstorage2.api.storage.AccessMode;
import com.refinedmods.refinedstorage2.api.resource.filter.FilterMode;
import com.refinedmods.refinedstorage2.api.storage.Actor;
import com.refinedmods.refinedstorage2.api.storage.Storage;
import com.refinedmods.refinedstorage2.api.storage.composite.CompositeAwareChild;
import com.refinedmods.refinedstorage2.api.storage.composite.Priority;

import java.util.Collection;
Expand All @@ -17,7 +18,7 @@
import org.apiguardian.api.API;

@API(status = API.Status.STABLE, since = "2.0.0-milestone.2.4")
public abstract class AbstractConfiguredProxyStorage<S extends Storage> implements Storage, Priority {
public abstract class AbstractConfiguredProxyStorage<S extends Storage> implements CompositeAwareChild, Priority {
@Nullable
private S delegate;
private final StorageConfiguration config;
Expand All @@ -33,21 +34,39 @@ protected AbstractConfiguredProxyStorage(final StorageConfiguration config, fina

@Override
public long extract(final ResourceKey resource, final long amount, final Action action, final Actor actor) {
if (delegate == null || config.getAccessMode() == AccessMode.INSERT || !config.isActive()) {
return 0;
}
return delegate.extract(resource, amount, action, actor);
throw new UnsupportedOperationException("Immediate extract is not allowed");
}

@Override
public long insert(final ResourceKey resource, final long amount, final Action action, final Actor actor) {
if (delegate == null
|| config.getAccessMode() == AccessMode.EXTRACT
|| !config.isActive()
|| !config.isAllowed(resource)) {
return 0;
throw new UnsupportedOperationException("Immediate insert is not allowed");
}

@Override
public Amount compositeInsert(final ResourceKey resource,
final long amount,
final Action action,
final Actor actor) {
if (delegate == null || config.isExtractOnly() || !config.isActive() || !config.isAllowed(resource)) {
return Amount.ZERO;
}
final long inserted = delegate.insert(resource, amount, action, actor);
if ((config.isVoidExcess() && config.getFilterMode() == FilterMode.ALLOW) && inserted < amount) {
return new Amount(amount, inserted);
}
return new Amount(inserted, inserted);
}

@Override
public Amount compositeExtract(final ResourceKey resource,
final long amount,
final Action action,
final Actor actor) {
if (delegate == null || config.isInsertOnly() || !config.isActive()) {
return Amount.ZERO;
}
return delegate.insert(resource, amount, action, actor);
final long extracted = delegate.extract(resource, amount, action, actor);
return new Amount(extracted, extracted);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public abstract class AbstractStorageNetworkNode extends AbstractNetworkNode imp
private final Filter filter = new Filter();
private int priority;
private AccessMode accessMode = AccessMode.INSERT_EXTRACT;
private boolean voidExcess;

@Override
public int getPriority() {
Expand Down Expand Up @@ -50,10 +51,19 @@ public void setFilterMode(final FilterMode mode) {
@Override
public void setPriority(final int priority) {
this.priority = priority;

trySortSources();
}

@Override
public boolean isVoidExcess() {
return voidExcess;
}

@Override
public void setVoidExcess(final boolean voidExcess) {
this.voidExcess = voidExcess;
}

private void trySortSources() {
if (network == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
public interface StorageConfiguration extends Priority {
AccessMode getAccessMode();

default boolean isInsertOnly() {
return getAccessMode() == AccessMode.INSERT;
}

default boolean isExtractOnly() {
return getAccessMode() == AccessMode.EXTRACT;
}

boolean isVoidExcess();

void setVoidExcess(boolean voidExcess);

void setAccessMode(AccessMode accessMode);

FilterMode getFilterMode();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.refinedmods.refinedstorage2.api.network.impl.node.externalstorage;

import com.refinedmods.refinedstorage2.api.core.Action;
import com.refinedmods.refinedstorage2.api.network.node.AbstractConfiguredProxyStorage;
import com.refinedmods.refinedstorage2.api.network.node.StorageConfiguration;
import com.refinedmods.refinedstorage2.api.resource.ResourceKey;
import com.refinedmods.refinedstorage2.api.storage.Actor;
import com.refinedmods.refinedstorage2.api.storage.composite.CompositeAwareChild;
import com.refinedmods.refinedstorage2.api.storage.composite.ConsumingStorage;
import com.refinedmods.refinedstorage2.api.storage.composite.ParentComposite;
import com.refinedmods.refinedstorage2.api.storage.external.ExternalStorage;
import com.refinedmods.refinedstorage2.api.storage.external.ExternalStorageListener;
Expand All @@ -21,7 +20,7 @@
import javax.annotation.Nullable;

public class ExposedExternalStorage extends AbstractConfiguredProxyStorage<ExternalStorage>
implements ConsumingStorage, CompositeAwareChild, TrackedStorage, ExternalStorageListener {
implements TrackedStorage, ExternalStorageListener {
private final Set<ParentComposite> parents = new HashSet<>();
private final LongSupplier clock;
@Nullable
Expand Down Expand Up @@ -63,6 +62,24 @@ public void onRemovedFromComposite(final ParentComposite parentComposite) {
}
}

@Override
public Amount compositeInsert(final ResourceKey resource,
final long amount,
final Action action,
final Actor actor) {
final Amount inserted = super.compositeInsert(resource, amount, action, actor);
return inserted.withoutNotifyingList();
}

@Override
public Amount compositeExtract(final ResourceKey resource,
final long amount,
final Action action,
final Actor actor) {
final Amount extracted = super.compositeExtract(resource, amount, action, actor);
return extracted.withoutNotifyingList();
}

@Override
public void setDelegate(final ExternalStorage newDelegate) {
super.setDelegate(newDelegate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.refinedmods.refinedstorage2.api.resource.list.ResourceListImpl;
import com.refinedmods.refinedstorage2.api.storage.Actor;
import com.refinedmods.refinedstorage2.api.storage.Storage;
import com.refinedmods.refinedstorage2.api.storage.composite.CompositeAwareChild;
import com.refinedmods.refinedstorage2.api.storage.composite.CompositeStorage;
import com.refinedmods.refinedstorage2.api.storage.composite.CompositeStorageImpl;
import com.refinedmods.refinedstorage2.api.storage.composite.ParentComposite;
Expand All @@ -16,7 +15,7 @@
import java.util.Optional;

class ExposedMultiStorage extends AbstractImmutableConfiguredProxyStorage<CompositeStorageImpl>
implements CompositeStorage, CompositeAwareChild {
implements CompositeStorage {
protected ExposedMultiStorage(final StorageConfiguration config) {
super(config, new CompositeStorageImpl(new ResourceListImpl()));
}
Expand Down
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.storage.Actor;
import com.refinedmods.refinedstorage2.api.storage.Storage;
import com.refinedmods.refinedstorage2.api.storage.composite.CompositeAwareChild;
import com.refinedmods.refinedstorage2.api.storage.composite.ParentComposite;
import com.refinedmods.refinedstorage2.api.storage.limited.LimitedStorage;
import com.refinedmods.refinedstorage2.api.storage.tracked.TrackedResource;
Expand All @@ -15,8 +14,7 @@
import java.util.Optional;
import java.util.Set;

class ExposedStorage extends AbstractConfiguredProxyStorage<Storage>
implements TrackedStorage, CompositeAwareChild {
class ExposedStorage extends AbstractConfiguredProxyStorage<Storage> implements TrackedStorage {
private final Set<ParentComposite> parents = new HashSet<>();

ExposedStorage(final StorageConfiguration config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import net.minecraft.world.level.Level;
import org.apiguardian.api.API;

// TODO: Immunity for despawning
// TODO: Tags/ore dict in recipes
@API(status = API.Status.STABLE, since = "2.0.0-milestone.1.0")
public abstract class AbstractStorageContainerItem extends Item implements StorageContainerItem {
protected final StorageContainerItemHelper helper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.refinedmods.refinedstorage2.api.grid.operations.GridOperations;
import com.refinedmods.refinedstorage2.api.grid.view.GridResource;
import com.refinedmods.refinedstorage2.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage2.api.storage.Actor;
import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannel;

Expand Down Expand Up @@ -31,7 +32,7 @@ public interface ResourceType {

double getDisplayAmount(long amount);

Optional<GridResource> toGridResource(PlatformResourceKey resource, long amount);
Optional<GridResource> toGridResource(ResourceAmount resourceAmount);

boolean isGridResourceBelonging(GridResource gridResource);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import static com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil.createTranslation;
import static java.util.Objects.requireNonNull;

// TODO: textures
public class ConfigurationCardItem extends Item {
private static final Component EMPTY_HELP = createTranslation("item", "configuration_card.empty_help");
private static final Component CONFIGURED_HELP = createTranslation("item", "configuration_card.configured_help");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.refinedmods.refinedstorage2.platform.common.constructordestructor;

import com.refinedmods.refinedstorage2.platform.common.storage.FilterModeSideButtonWidget;
import com.refinedmods.refinedstorage2.platform.common.support.AbstractFilterScreen;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.PropertyTypes;
import com.refinedmods.refinedstorage2.platform.common.support.widget.FilterModeSideButtonWidget;

import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Inventory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void setFocusSearchBar(final KeyMapping focusSearchBar) {
this.focusSearchBar = focusSearchBar;
}

@Nullable // TODO - implement on Fabric
@Nullable
public KeyMapping getClearCraftingGridMatrixToNetwork() {
return clearCraftingGridMatrixToNetwork;
}
Expand All @@ -37,7 +37,7 @@ public void setClearCraftingGridMatrixToNetwork(final KeyMapping clearCraftingGr
this.clearCraftingGridMatrixToNetwork = clearCraftingGridMatrixToNetwork;
}

@Nullable // TODO - implement on Fabric
@Nullable
public KeyMapping getClearCraftingGridMatrixToInventory() {
return clearCraftingGridMatrixToInventory;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.refinedmods.refinedstorage2.api.grid.view.GridResourceFactory;
import com.refinedmods.refinedstorage2.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage2.platform.api.support.registry.PlatformRegistry;
import com.refinedmods.refinedstorage2.platform.api.support.resource.PlatformResourceKey;
import com.refinedmods.refinedstorage2.platform.api.support.resource.ResourceType;

import java.util.Optional;
Expand All @@ -18,12 +17,9 @@ public CompositeGridResourceFactory(final PlatformRegistry<ResourceType> resourc

@Override
public Optional<GridResource> apply(final ResourceAmount resourceAmount) {
if (!(resourceAmount.getResource() instanceof PlatformResourceKey platformResource)) {
return Optional.empty();
}
return resourceTypeRegistry.getAll()
.stream()
.flatMap(type -> type.toGridResource(platformResource, resourceAmount.getAmount()).stream())
.flatMap(type -> type.toGridResource(resourceAmount).stream())
.findFirst();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.refinedmods.refinedstorage2.platform.common.importer;

import com.refinedmods.refinedstorage2.platform.common.storage.FilterModeSideButtonWidget;
import com.refinedmods.refinedstorage2.platform.common.support.AbstractFilterScreen;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.PropertyTypes;
import com.refinedmods.refinedstorage2.platform.common.support.widget.FilterModeSideButtonWidget;
import com.refinedmods.refinedstorage2.platform.common.support.widget.FuzzyModeSideButtonWidget;

import net.minecraft.network.chat.Component;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import static com.refinedmods.refinedstorage2.platform.common.networking.NetworkReceiverKey.getDimensionName;
import static com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil.createTranslation;

// TODO: better active texture.
public class NetworkCardItem extends Item {
private static final MutableComponent UNBOUND_HELP = createTranslation("item", "network_card.unbound_help");
private static final MutableComponent BOUND_HELP = createTranslation("item", "network_card.bound_help");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

import static com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil.createTranslation;

// TODO: better error texture and better active texture with animation? Reuse controller textures?
public class NetworkTransmitterBlockEntity
extends AbstractRedstoneModeNetworkNodeContainerBlockEntity<SimpleNetworkNode>
implements ExtendedMenuProvider, BlockEntityWithDrops {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ protected AbstractStorageContainerMenu(final MenuType<?> type, final int syncId)
registerProperty(new ClientProperty<>(PropertyTypes.FILTER_MODE, FilterMode.BLOCK));
registerProperty(new ClientProperty<>(PropertyTypes.FUZZY_MODE, false));
registerProperty(new ClientProperty<>(StoragePropertyTypes.ACCESS_MODE, AccessMode.INSERT_EXTRACT));
registerProperty(new ClientProperty<>(StoragePropertyTypes.VOID_EXCESS, false));
registerProperty(new ClientProperty<>(PropertyTypes.REDSTONE_MODE, RedstoneMode.IGNORE));
}

Expand Down Expand Up @@ -46,6 +47,11 @@ protected AbstractStorageContainerMenu(final MenuType<?> type,
configContainer::getAccessMode,
configContainer::setAccessMode
));
registerProperty(new ServerProperty<>(
StoragePropertyTypes.VOID_EXCESS,
configContainer::isVoidExcess,
configContainer::setVoidExcess
));
registerProperty(new ServerProperty<>(
PropertyTypes.REDSTONE_MODE,
configContainer::getRedstoneMode,
Expand All @@ -56,4 +62,9 @@ protected AbstractStorageContainerMenu(final MenuType<?> type,
boolean shouldDisplayFilterModeWarning() {
return getProperty(PropertyTypes.FILTER_MODE).getValue() == FilterMode.ALLOW && areAllResourceSlotsEmpty();
}

boolean shouldDisplayVoidExcessModeWarning() {
return Boolean.TRUE.equals(getProperty(StoragePropertyTypes.VOID_EXCESS).getValue())
&& getProperty(PropertyTypes.FILTER_MODE).getValue() != FilterMode.ALLOW;
}
}
Loading

0 comments on commit bbde868

Please sign in to comment.