diff --git a/src/main/java/gripe/_90/appliede/me/misc/EMCInterfaceLogic.java b/src/main/java/gripe/_90/appliede/me/misc/EMCInterfaceLogic.java index 304bba1..f000304 100644 --- a/src/main/java/gripe/_90/appliede/me/misc/EMCInterfaceLogic.java +++ b/src/main/java/gripe/_90/appliede/me/misc/EMCInterfaceLogic.java @@ -2,6 +2,7 @@ import java.util.List; import java.util.Objects; +import java.util.UUID; import org.jetbrains.annotations.Nullable; @@ -54,13 +55,14 @@ public class EMCInterfaceLogic implements IActionHost, IGridTickable, IUpgradeab private final MEStorage localInvHandler; private final GenericStack[] plannedWork; private final IActionSource source = IActionSource.ofMachine(this); + private final UUID ownerUUID; private final LazyOptional storageHolder; @Nullable private WrappedEMCStorage emcStorage; - private boolean hasConfig = false; + private boolean hasConfig; public EMCInterfaceLogic(IManagedGridNode node, EMCInterfaceLogicHost host, Item is) { this(node, host, is, 9); @@ -84,6 +86,9 @@ public EMCInterfaceLogic(IManagedGridNode node, EMCInterfaceLogicHost host, Item storage.useRegisteredCapacities(); storageHolder = LazyOptional.of(() -> storage).lazyMap(GenericStackItemStorage::new); + + var gridNode = mainNode.getNode(); + ownerUUID = gridNode != null ? gridNode.getOwningPlayerProfileId() : null; } public ConfigInventory getConfig() { @@ -116,17 +121,15 @@ private boolean storageFilter(AEKey what) { return true; } - var uuid = node.getOwningPlayerProfileId(); - - if (uuid == null) { + if (ownerUUID == null) { return false; } var knowledge = grid.getService(KnowledgeService.class); - return knowledge.knowsItem(item) + return knowledge.getKnownItems().contains(item) || (isUpgradedWith(AppliedE.LEARNING_CARD.get()) && IEMCProxy.INSTANCE.hasValue(item.toStack()) - && knowledge.getProviderFor(uuid) != null); + && knowledge.getProviderFor(ownerUUID) != null); } public void readFromNBT(CompoundTag tag) { diff --git a/src/main/java/gripe/_90/appliede/me/service/EMCStorage.java b/src/main/java/gripe/_90/appliede/me/service/EMCStorage.java index 4bfb5b2..68708d9 100644 --- a/src/main/java/gripe/_90/appliede/me/service/EMCStorage.java +++ b/src/main/java/gripe/_90/appliede/me/service/EMCStorage.java @@ -157,7 +157,7 @@ public long insertItem( return 0; } - if (!mayLearn && !service.knowsItem(what) || !IEMCProxy.INSTANCE.hasValue(what.toStack())) { + if (!mayLearn && !service.getKnownItems().contains(what) || !IEMCProxy.INSTANCE.hasValue(what.toStack())) { return 0; } @@ -232,7 +232,7 @@ public long extractItem(AEItemKey what, long amount, Actionable mode, IActionSou return 0; } - if (amount <= 0 || !service.knowsItem(what)) { + if (amount <= 0 || !service.getKnownItems().contains(what)) { return 0; } diff --git a/src/main/java/gripe/_90/appliede/me/service/KnowledgeService.java b/src/main/java/gripe/_90/appliede/me/service/KnowledgeService.java index 1ef145c..3cad431 100644 --- a/src/main/java/gripe/_90/appliede/me/service/KnowledgeService.java +++ b/src/main/java/gripe/_90/appliede/me/service/KnowledgeService.java @@ -49,15 +49,20 @@ public class KnowledgeService implements IGridService, IGridServiceProvider { private final TeamProjectEHandler.Proxy tpeHandler = new TeamProjectEHandler.Proxy(); private final IGrid grid; + private Set knownItemCache; public KnowledgeService(IGrid grid) { this.grid = grid; - MinecraftForge.EVENT_BUS.addListener((PlayerKnowledgeChangeEvent event) -> updatePatterns()); + MinecraftForge.EVENT_BUS.addListener((PlayerKnowledgeChangeEvent event) -> { + knownItemCache = null; + updatePatterns(); + }); } @Override public void addNode(IGridNode gridNode, @Nullable CompoundTag savedData) { if (gridNode.getOwner() instanceof EMCModulePart module) { + knownItemCache = null; moduleNodes.add(module.getMainNode()); var uuid = gridNode.getOwningPlayerProfileId(); @@ -72,6 +77,7 @@ public void addNode(IGridNode gridNode, @Nullable CompoundTag savedData) { @Override public void removeNode(IGridNode gridNode) { if (gridNode.getOwner() instanceof EMCModulePart module) { + knownItemCache = null; moduleNodes.remove(module.getMainNode()); providers.clear(); tpeHandler.clear(); @@ -137,11 +143,15 @@ public MEStorage getStorage(IManagedGridNode node) { } public Set getKnownItems() { - return getProviders().stream() - .flatMap(provider -> provider.getKnowledge().stream()) - .map(item -> AEItemKey.of(item.createStack())) - .filter(Objects::nonNull) - .collect(Collectors.toUnmodifiableSet()); + if (knownItemCache == null) { + knownItemCache = getProviders().stream() + .flatMap(provider -> provider.getKnowledge().stream()) + .map(item -> AEItemKey.of(item.createStack())) + .filter(Objects::nonNull) + .collect(Collectors.toUnmodifiableSet()); + } + + return knownItemCache; } public List getPatterns(IManagedGridNode node) { @@ -188,10 +198,6 @@ BigInteger getEmc() { .reduce(BigInteger.ZERO, BigInteger::add); } - public boolean knowsItem(AEItemKey item) { - return getProviders().stream().anyMatch(provider -> provider.hasKnowledge(item.toStack())); - } - public boolean isTrackingPlayer(Player player) { var uuid = player.getUUID(); return providers.containsKey(uuid) || tpeHandler.isPlayerInTrackedTeam(uuid);