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 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a4aea95
Fix that having multiple extract-only storage busses extracting from …
hiroscho Nov 12, 2024
7f354ad
Fix extract-only storage bus with filter and extract-only storage bus…
hiroscho Nov 12, 2024
6a0943e
refine
hiroscho Nov 14, 2024
06521bf
Merge branch 'master' into fix/same-network-multiple-extract-only-sto…
hiroscho Nov 14, 2024
d2f4e47
Merge branch 'master' into fix/same-network-multiple-extract-only-sto…
Dream-Master Nov 16, 2024
b9b53ad
Merge branch 'master' into fix/same-network-multiple-extract-only-sto…
hiroscho Nov 17, 2024
2854f41
extract changes to StorageBusInventoryHandler
hiroscho Nov 17, 2024
76742a8
Merge branch 'master' into fix/same-network-multiple-extract-only-sto…
Dream-Master Nov 18, 2024
2df603c
Merge branch 'master' into fix/same-network-multiple-extract-only-sto…
MCTBL Nov 19, 2024
f5ae6cd
Merge branch 'master' into fix/same-network-multiple-extract-only-sto…
Dream-Master Dec 9, 2024
503cf8d
Merge branch 'master' into fix/same-network-multiple-extract-only-sto…
Dream-Master Dec 9, 2024
c38ece9
Merge branch 'master' into fix/same-network-multiple-extract-only-sto…
Dream-Master Dec 18, 2024
4221d0c
Merge branch 'master' into fix/same-network-multiple-extract-only-sto…
Dream-Master Dec 21, 2024
08ba722
Merge branch 'master' into fix/same-network-multiple-extract-only-sto…
Dream-Master Dec 21, 2024
01be57e
Merge branch 'master' into fix/same-network-multiple-extract-only-sto…
Dream-Master Dec 21, 2024
dbac463
Merge branch 'master' into fix/same-network-multiple-extract-only-sto…
Dream-Master Dec 23, 2024
b0bb992
Merge branch 'master' into fix/same-network-multiple-extract-only-sto…
Dream-Master Dec 26, 2024
c963c97
Merge branch 'master' into fix/same-network-multiple-extract-only-sto…
serenibyss Dec 27, 2024
a1a5c00
Merge branch 'master' into fix/same-network-multiple-extract-only-sto…
serenibyss Dec 29, 2024
fbf461e
Merge branch 'master' into fix/same-network-multiple-extract-only-sto…
Dream-Master Dec 29, 2024
b29081e
Merge branch 'master' into fix/same-network-multiple-extract-only-sto…
Dream-Master Dec 30, 2024
024b9c5
Merge branch 'master' into fix/same-network-multiple-extract-only-sto…
serenibyss Dec 30, 2024
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
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this cache getting invalidated?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every time the iteration number changes the cache is replaced with a new one

}

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