Skip to content

Commit

Permalink
Merge remote-tracking branch 'hiroscho/fix/same-network-multiple-extr…
Browse files Browse the repository at this point in the history
…act-only-storage-bus' into dev
  • Loading branch information
Dream-Master committed Jan 3, 2025
2 parents 2e4c8ba + 024b9c5 commit 08c2792
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 3 deletions.
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
2 changes: 1 addition & 1 deletion src/main/java/appeng/me/storage/MEInventoryHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class MEInventoryHandler<T extends IAEStack<T>> implements IMEInventoryHa
private IPartitionList<T> myExtractPartitionList;

private AccessRestriction cachedAccessRestriction;
private boolean hasReadAccess;
protected boolean hasReadAccess;
protected boolean hasWriteAccess;
protected boolean isSticky;
protected boolean isExtractFilterActive;
Expand Down
100 changes: 100 additions & 0 deletions src/main/java/appeng/me/storage/StorageBusInventoryHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
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 appeng.api.storage.IMEInventory;
import appeng.api.storage.StorageChannel;
import appeng.api.storage.data.IAEStack;
import appeng.api.storage.data.IItemList;
import appeng.me.cache.NetworkMonitor;

public class StorageBusInventoryHandler<T extends IAEStack<T>> extends MEInventoryHandler<T> {

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

public StorageBusInventoryHandler(IMEInventory<T> i, StorageChannel channel) {
super(i, channel);
}

@Override
public IItemList<T> getAvailableItems(final IItemList<T> out, int iteration) {
if (!this.hasReadAccess && !isVisible()) {
return out;
}

if (this.isExtractFilterActive() && !this.getExtractPartitionList().isEmpty()) {
return this.filterAvailableItems(out, iteration);
} else {
return this.getAvailableItems(out, iteration, e -> true);
}
}

@Override
protected IItemList<T> filterAvailableItems(IItemList<T> out, int iteration) {
Predicate<T> filterCondition = this.getExtractFilterCondition();
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.getInternal()
.getAvailableItems((IItemList<T>) this.getInternal().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.getInternal()
.getAvailableItems((IItemList<T>) this.getInternal().getChannel().createList(), iteration);
networkInventoryItems.put(networkInventoryHandler, allAvailableItems);
}

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

/**
* Find the NetworkInventoryHandler for this storage bus
*/
private NetworkInventoryHandler<T> getNetworkInventoryHandler() {
return (NetworkInventoryHandler<T>) findNetworkInventoryHandler(this.getInternal());
}

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;
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/appeng/parts/misc/PartStorageBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import appeng.me.GridAccessException;
import appeng.me.storage.MEInventoryHandler;
import appeng.me.storage.MEMonitorIInventory;
import appeng.me.storage.StorageBusInventoryHandler;
import appeng.parts.automation.PartUpgradeable;
import appeng.tile.inventory.AppEngInternalAEInventory;
import appeng.tile.inventory.InvOperation;
Expand Down Expand Up @@ -480,7 +481,7 @@ public MEInventoryHandler<IAEItemStack> getInternalHandler() {
if (inv != null) {
this.checkInterfaceVsStorageBus(target, this.getSide().getOpposite());

this.handler = new MEInventoryHandler<IAEItemStack>(inv, StorageChannel.ITEMS);
this.handler = new StorageBusInventoryHandler<>(inv, StorageChannel.ITEMS);

AccessRestriction currentAccess = (AccessRestriction) this.getConfigManager()
.getSetting(Settings.ACCESS);
Expand Down

0 comments on commit 08c2792

Please sign in to comment.