Skip to content

Commit

Permalink
Make Cell Workbench respect slot-level filters when copying filters t…
Browse files Browse the repository at this point in the history
…o cells
  • Loading branch information
shartte committed Mar 13, 2024
1 parent fc8c1b4 commit 89f53aa
Showing 6 changed files with 112 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -158,7 +158,10 @@ private void configChanged() {

public static void copy(GenericStackInv from, GenericStackInv to) {
for (int i = 0; i < Math.min(from.size(), to.size()); ++i) {
to.setStack(i, from.getStack(i));
var fromStack = from.getStack(i);
if (fromStack == null || to.isAllowedIn(i, fromStack.what())) {
to.setStack(i, fromStack);
}
}
for (int i = from.size(); i < to.size(); i++) {
to.setStack(i, null);
Original file line number Diff line number Diff line change
@@ -61,7 +61,14 @@ public class GenericStackInv implements MEStorage, GenericInternalInventory {
private Component description = Component.empty();

public enum Mode {
/**
* When in types mode, the config inventory will ignore all amounts and always return amount 1 for stacks in the
* inventory.
*/
CONFIG_TYPES,
/**
* When in stack mode, the config inventory will respect amounts and drop stacks with amounts of 0 or less.
*/
CONFIG_STACKS,
STORAGE
}
2 changes: 1 addition & 1 deletion src/main/java/appeng/menu/AEBaseMenu.java
Original file line number Diff line number Diff line change
@@ -549,7 +549,7 @@ public void setFilter(int slotIndex, ItemStack item) {
return;
}

if (s instanceof FakeSlot) {
if (s instanceof FakeSlot fakeSlot && !fakeSlot.canSetFilterTo(item)) {
s.set(item);
}
}
4 changes: 2 additions & 2 deletions src/main/java/appeng/menu/slot/CellPartitionSlot.java
Original file line number Diff line number Diff line change
@@ -18,9 +18,9 @@ public class CellPartitionSlot extends FakeSlot implements IOptionalSlot {
private final IPartitionSlotHost host;
private final int slot;

public CellPartitionSlot(InternalInventory inv, IPartitionSlotHost containerBus, int invSlot) {
public CellPartitionSlot(InternalInventory inv, IPartitionSlotHost host, int invSlot) {
super(inv, invSlot);
this.host = containerBus;
this.host = host;
this.slot = invSlot;
}

4 changes: 4 additions & 0 deletions src/main/java/appeng/menu/slot/FakeSlot.java
Original file line number Diff line number Diff line change
@@ -50,6 +50,10 @@ public boolean mayPlace(ItemStack stack) {

@Override
public void set(ItemStack is) {
if (!canSetFilterTo(is)) {
return;
}

if (!is.isEmpty()) {
is = is.copy();
}
94 changes: 94 additions & 0 deletions src/main/java/appeng/util/ConfigInventory.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package appeng.util;

import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;

import org.jetbrains.annotations.Nullable;

@@ -48,6 +51,97 @@ protected ConfigInventory(Set<AEKeyType> supportedKeyTypes, @Nullable AEKeySlotF
setFilter(slotFilter);
}

public final static class Builder {
private final Mode mode;
private final int size;
private Set<AEKeyType> supportedKeyTypes = AEKeyTypes.getAll();
@Nullable
private AEKeySlotFilter slotFilter;
@Nullable
private Runnable changeListener;
private boolean allowOverstacking;

private Builder(Mode mode, int size) {
this.mode = mode;
this.size = size;
}

public Builder supportedKeyType(AEKeyType keyType) {
this.supportedKeyTypes = Set.of(keyType);
return this;
}

public Builder supportedKeyTypes(AEKeyType keyType, AEKeyType... moreKeyTypes) {
if (moreKeyTypes.length == 0) {
return supportedKeyType(keyType);
}
this.supportedKeyTypes = new HashSet<>(1 + moreKeyTypes.length);
this.supportedKeyTypes.add(keyType);
Collections.addAll(this.supportedKeyTypes, moreKeyTypes);
return this;
}

/**
* Set a filter that limits what can be inserted to certain slots.
*/
public Builder slotFilter(AEKeySlotFilter slotFilter) {
this.slotFilter = slotFilter;
return this;
}

/**
* Set a filter that applies to all slots at once.
*/
public Builder slotFilter(Predicate<AEKey> filter) {
this.slotFilter = (slot, what) -> filter.apply(what);
return this;
}

public Builder changeListener(Runnable changeListener) {
this.changeListener = changeListener;
return this;
}

/**
* Allow slots to exceed the natural stack size limits of items.
*/
public Builder allowOverstacking(boolean enable) {
this.allowOverstacking = enable;
return this;
}

public ConfigInventory build() {
return new ConfigInventory(
supportedKeyTypes,
slotFilter,
mode,
size,
changeListener,
allowOverstacking);
}
}

/**
* @param size The number of slots in this inventory.
*/
public static Builder configTypes(int size) {
return new Builder(Mode.CONFIG_TYPES, size);
}

/**
* @param size The number of slots in this inventory.
*/
public static Builder configStacks(int size) {
return new Builder(Mode.CONFIG_STACKS, size);
}

/**
* @param size The number of slots in this inventory.
*/
public static Builder storage(int size) {
return new Builder(Mode.STORAGE, size);
}

/**
* When in types mode, the config inventory will ignore all amounts and always return amount 1 for stacks in the
* inventory.

0 comments on commit 89f53aa

Please sign in to comment.