Skip to content

Commit

Permalink
Add proper aggressive caching of network-wide known EMC items
Browse files Browse the repository at this point in the history
  • Loading branch information
62832 committed Jun 5, 2024
1 parent 86022ea commit 61edf50
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
15 changes: 9 additions & 6 deletions src/main/java/gripe/_90/appliede/me/misc/EMCInterfaceLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import java.util.Objects;
import java.util.UUID;

import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -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<IItemHandler> 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);
Expand All @@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/gripe/_90/appliede/me/service/EMCStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
26 changes: 16 additions & 10 deletions src/main/java/gripe/_90/appliede/me/service/KnowledgeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,20 @@ public class KnowledgeService implements IGridService, IGridServiceProvider {
private final TeamProjectEHandler.Proxy tpeHandler = new TeamProjectEHandler.Proxy();

private final IGrid grid;
private Set<AEItemKey> 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();

Expand All @@ -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();
Expand Down Expand Up @@ -137,11 +143,15 @@ public MEStorage getStorage(IManagedGridNode node) {
}

public Set<AEItemKey> 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<IPatternDetails> getPatterns(IManagedGridNode node) {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 61edf50

Please sign in to comment.