Skip to content

Commit

Permalink
Store providers directly within grid service instead of a composite c…
Browse files Browse the repository at this point in the history
…lass
  • Loading branch information
62832 committed Apr 29, 2024
1 parent 0ee2d9b commit 87d3623
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ private boolean tryUsePlan(int slot, AEKey what, int amount) {

var knowledge = grid.getService(KnowledgeService.class);

if (!knowledge.getKnowledge().knowsItem(itemKey)) {
if (!knowledge.knowsItem(itemKey)) {
return false;
}

Expand Down

This file was deleted.

12 changes: 5 additions & 7 deletions src/main/java/gripe/_90/appliede/service/EMCStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public record EMCStorage(KnowledgeService service) implements MEStorage {
@Override
public void getAvailableStacks(KeyCounter out) {
var emc = service.getKnowledge().getEmc();
var emc = service.getEmc();
var currentTier = 1;

while (emc.divide(AppliedE.TIER_LIMIT).signum() == 1) {
Expand All @@ -40,13 +40,12 @@ public long insert(AEKey what, long amount, Actionable mode, IActionSource sourc
var bigAmount = BigInteger.valueOf(amount);
var multiplier = AppliedE.TIER_LIMIT.pow(emc.getTier() - 1);

var knowledge = service.getKnowledge();
var toInsert = bigAmount.multiply(multiplier);

var providers = new ArrayList<>(knowledge.getProviders());
var providers = new ArrayList<>(service.getProviders());
Collections.shuffle(providers);

var divisor = BigInteger.valueOf(knowledge.getProviders().size());
var divisor = BigInteger.valueOf(service.getProviders().size());
var quotient = toInsert.divide(divisor);
var remainder = toInsert.remainder(divisor).longValue();

Expand All @@ -67,17 +66,16 @@ public long extract(AEKey what, long amount, Actionable mode, IActionSource sour
return 0;
}

var knowledge = service.getKnowledge();
var extracted = 0L;
var multiplier = AppliedE.TIER_LIMIT.pow(emc.getTier() - 1);

var providers = new ArrayList<>(knowledge.getProviders());
var providers = new ArrayList<>(service.getProviders());

while (!providers.isEmpty() && extracted < amount) {
Collections.shuffle(providers);

var toExtract = BigInteger.valueOf(amount - extracted).multiply(multiplier);
var divisor = BigInteger.valueOf(knowledge.getProviders().size());
var divisor = BigInteger.valueOf(service.getProviders().size());
var quotient = toExtract.divide(divisor);
var remainder = toExtract.remainder(divisor).longValue();

Expand Down
54 changes: 45 additions & 9 deletions src/main/java/gripe/_90/appliede/service/KnowledgeService.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package gripe._90.appliede.service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.jetbrains.annotations.Nullable;

import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.MinecraftServer;
import net.minecraftforge.common.MinecraftForge;

import appeng.api.crafting.IPatternDetails;
import appeng.api.features.IPlayerRegistry;
import appeng.api.networking.IGridNode;
import appeng.api.networking.IGridService;
import appeng.api.networking.IGridServiceProvider;
Expand All @@ -23,12 +31,15 @@
import gripe._90.appliede.module.EMCModulePart;
import gripe._90.appliede.module.TransmutationPattern;

import moze_intel.projecte.api.capabilities.IKnowledgeProvider;
import moze_intel.projecte.api.event.PlayerKnowledgeChangeEvent;
import moze_intel.projecte.api.proxy.ITransmutationProxy;

public class KnowledgeService implements IGridService, IGridServiceProvider {
private final CompositeKnowledgeProvider knowledge = new CompositeKnowledgeProvider();
private final MEStorage storage = new EMCStorage(this);
private final List<EMCModulePart> modules = new ArrayList<>();
private final Map<UUID, Supplier<IKnowledgeProvider>> providers = new Object2ObjectLinkedOpenHashMap<>();
private final MEStorage storage = new EMCStorage(this);

private MinecraftServer server;

public KnowledgeService() {
Expand All @@ -44,20 +55,28 @@ public void addNode(IGridNode gridNode, @Nullable CompoundTag savedData) {

if (gridNode.getOwner() instanceof EMCModulePart module) {
modules.add(module);
knowledge.addNode(gridNode);
var uuid = gridNode.getOwningPlayerProfileId();

if (uuid != null) {
providers.put(uuid, () -> ITransmutationProxy.INSTANCE.getKnowledgeProviderFor(uuid));
}
}
}

@Override
public void removeNode(IGridNode gridNode) {
if (gridNode.getOwner() instanceof EMCModulePart module) {
modules.remove(module);
knowledge.removeNode(gridNode);
var uuid = gridNode.getOwningPlayerProfileId();

if (uuid != null) {
providers.remove(uuid);
}
}
}

public CompositeKnowledgeProvider getKnowledge() {
return knowledge;
public Set<Supplier<IKnowledgeProvider>> getProviders() {
return providers.values().stream().collect(Collectors.toUnmodifiableSet());
}

public MEStorage getStorage() {
Expand All @@ -70,7 +89,7 @@ public MEStorage getStorage(EMCModulePart module) {

public List<IPatternDetails> getPatterns() {
var patterns = new ArrayList<IPatternDetails>();
var emc = knowledge.getEmc();
var emc = getEmc();
var highestTier = 1;

while (emc.divide(AppliedE.TIER_LIMIT).signum() == 1) {
Expand All @@ -82,7 +101,7 @@ public List<IPatternDetails> getPatterns() {
patterns.add(new TransmutationPattern(null, tier));
}

var knownItems = knowledge.getProviders().stream()
var knownItems = getProviders().stream()
.flatMap(provider -> provider.get().getKnowledge().stream())
.map(item -> AEItemKey.of(item.getItem()))
.collect(Collectors.toSet());
Expand All @@ -94,9 +113,26 @@ public List<IPatternDetails> getPatterns() {
return patterns;
}

public BigInteger getEmc() {
return getProviders().stream()
.map(provider -> provider.get().getEmc())
.reduce(BigInteger.ZERO, BigInteger::add);
}

public boolean knowsItem(AEItemKey item) {
return getProviders().stream().anyMatch(provider -> provider.get().hasKnowledge(item.toStack()));
}

public void syncEmc() {
if (server != null) {
knowledge.syncAllEmc(server);
providers.forEach((uuid, provider) -> {
var id = IPlayerRegistry.getMapping(server).getPlayerId(uuid);
var player = IPlayerRegistry.getConnected(server, id);

if (player != null) {
provider.get().syncEmc(player);
}
});
}
}
}

0 comments on commit 87d3623

Please sign in to comment.