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

Fix/same network multiple extract only storage buses #612

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/main/java/appeng/me/cache/NetworkMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public boolean validForPass(final int i) {

@Nullable
@SuppressWarnings("unchecked")
private IMEInventoryHandler<T> getHandler() {
public IMEInventoryHandler<T> getHandler() {
switch (this.myChannel) {
case ITEMS -> {
return (IMEInventoryHandler<T>) this.myGridCache.getItemInventoryHandler();
Expand Down
68 changes: 62 additions & 6 deletions src/main/java/appeng/me/storage/MEInventoryHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

package appeng.me.storage;

import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.function.Predicate;

import javax.annotation.Nonnull;
Expand All @@ -24,11 +28,14 @@
import appeng.api.storage.StorageChannel;
import appeng.api.storage.data.IAEStack;
import appeng.api.storage.data.IItemList;
import appeng.me.cache.NetworkMonitor;
import appeng.util.prioitylist.DefaultPriorityList;
import appeng.util.prioitylist.IPartitionList;

public class MEInventoryHandler<T extends IAEStack<T>> implements IMEInventoryHandler<T> {

private static final ThreadLocal<Map<Integer, Map<NetworkInventoryHandler<?>, IItemList<?>>>> networkItemsForIteration = new ThreadLocal<>();

private final IMEInventoryHandler<T> internal;
private int myPriority;
private IncludeExclude myWhitelist;
Expand Down Expand Up @@ -124,7 +131,7 @@ public IItemList<T> getAvailableItems(final IItemList<T> out, int iteration) {
if (this.isExtractFilterActive() && !this.myExtractPartitionList.isEmpty()) {
return this.filterAvailableItems(out, iteration);
} else {
return this.internal.getAvailableItems(out, iteration);
return this.getAvailableItems(out, iteration, e -> true);
}
}

Expand All @@ -133,17 +140,66 @@ public boolean isVisible() {
}

protected IItemList<T> filterAvailableItems(IItemList<T> out, int iteration) {
final IItemList<T> allAvailableItems = this.internal
.getAvailableItems((IItemList<T>) this.internal.getChannel().createList(), iteration);
Predicate<T> filterCondition = this.getExtractFilterCondition();
for (T item : allAvailableItems) {
if (filterCondition.test(item)) {
out.add(item);
getAvailableItems(out, iteration, filterCondition);
return out;
}

private IItemList<T> getAvailableItems(IItemList<T> out, int iteration, Predicate<T> filterCondition) {
final IItemList<T> allAvailableItems = this.getAllAvailableItems(iteration);
Iterator<T> it = allAvailableItems.iterator();
while (it.hasNext()) {
T items = it.next();
if (filterCondition.test(items)) {
out.add(items);
// have to remove the item otherwise it could be counted double
it.remove();
}
}
return out;
}

private IItemList<T> getAllAvailableItems(int iteration) {
NetworkInventoryHandler<T> networkInventoryHandler = getNetworkInventoryHandler();
if (networkInventoryHandler == null) {
return this.internal.getAvailableItems((IItemList<T>) this.internal.getChannel().createList(), iteration);
}

Map<Integer, Map<NetworkInventoryHandler<?>, IItemList<?>>> s = networkItemsForIteration.get();
if (s != null && !s.containsKey(iteration)) {
s = null;
}
if (s == null) {
s = Collections.singletonMap(iteration, new IdentityHashMap<>());
networkItemsForIteration.set(s);
}
Map<NetworkInventoryHandler<?>, IItemList<?>> networkInventoryItems = s.get(iteration);
if (!networkInventoryItems.containsKey(networkInventoryHandler)) {
IItemList<T> allAvailableItems = this.internal
.getAvailableItems((IItemList<T>) this.internal.getChannel().createList(), iteration);
networkInventoryItems.put(networkInventoryHandler, allAvailableItems);
}

return (IItemList<T>) networkInventoryItems.get(networkInventoryHandler);
}

private NetworkInventoryHandler<T> getNetworkInventoryHandler() {
return (NetworkInventoryHandler<T>) findNetworkInventoryHandler(this.getInternal());
}

// should give back the NetworkInventoryHandler for storage buses, everything else can be null
private NetworkInventoryHandler<?> findNetworkInventoryHandler(IMEInventory<?> inventory) {
if (inventory instanceof MEPassThrough<?>passThrough) {
return findNetworkInventoryHandler(passThrough.getInternal());
} else if (inventory instanceof NetworkMonitor<?>networkMonitor) {
return findNetworkInventoryHandler(networkMonitor.getHandler());
} else if (inventory instanceof NetworkInventoryHandler<?>networkInventoryHandler) {
return networkInventoryHandler;
} else {
return null;
}
}

@Override
public T getAvailableItem(@Nonnull T request, int iteration) {
if (!this.hasReadAccess && !isVisible()) {
Expand Down