diff --git a/src/main/java/gripe/_90/appliede/iface/EMCInterfaceLogic.java b/src/main/java/gripe/_90/appliede/iface/EMCInterfaceLogic.java index c9fc813..ee25b36 100644 --- a/src/main/java/gripe/_90/appliede/iface/EMCInterfaceLogic.java +++ b/src/main/java/gripe/_90/appliede/iface/EMCInterfaceLogic.java @@ -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; } diff --git a/src/main/java/gripe/_90/appliede/service/CompositeKnowledgeProvider.java b/src/main/java/gripe/_90/appliede/service/CompositeKnowledgeProvider.java deleted file mode 100644 index 431e57f..0000000 --- a/src/main/java/gripe/_90/appliede/service/CompositeKnowledgeProvider.java +++ /dev/null @@ -1,64 +0,0 @@ -package gripe._90.appliede.service; - -import java.math.BigInteger; -import java.util.Map; -import java.util.Set; -import java.util.UUID; -import java.util.function.Supplier; -import java.util.stream.Collectors; - -import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap; - -import net.minecraft.server.MinecraftServer; - -import appeng.api.features.IPlayerRegistry; -import appeng.api.networking.IGridNode; -import appeng.api.stacks.AEItemKey; - -import moze_intel.projecte.api.capabilities.IKnowledgeProvider; -import moze_intel.projecte.api.proxy.ITransmutationProxy; - -public class CompositeKnowledgeProvider { - private final Map> providers = new Object2ObjectLinkedOpenHashMap<>(); - - Set> getProviders() { - return providers.values().stream().collect(Collectors.toUnmodifiableSet()); - } - - void addNode(IGridNode node) { - var uuid = node.getOwningPlayerProfileId(); - - if (uuid != null) { - providers.put(uuid, () -> ITransmutationProxy.INSTANCE.getKnowledgeProviderFor(uuid)); - } - } - - void removeNode(IGridNode node) { - var uuid = node.getOwningPlayerProfileId(); - - if (uuid != null) { - providers.remove(uuid); - } - } - - void syncAllEmc(MinecraftServer 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); - } - }); - } - - public boolean knowsItem(AEItemKey item) { - return getProviders().stream().anyMatch(provider -> provider.get().hasKnowledge(item.toStack())); - } - - public BigInteger getEmc() { - return getProviders().stream() - .map(provider -> provider.get().getEmc()) - .reduce(BigInteger.ZERO, BigInteger::add); - } -} diff --git a/src/main/java/gripe/_90/appliede/service/EMCStorage.java b/src/main/java/gripe/_90/appliede/service/EMCStorage.java index dd7c628..1dbd2c7 100644 --- a/src/main/java/gripe/_90/appliede/service/EMCStorage.java +++ b/src/main/java/gripe/_90/appliede/service/EMCStorage.java @@ -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) { @@ -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(); @@ -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(); diff --git a/src/main/java/gripe/_90/appliede/service/KnowledgeService.java b/src/main/java/gripe/_90/appliede/service/KnowledgeService.java index 7469515..9acb33f 100644 --- a/src/main/java/gripe/_90/appliede/service/KnowledgeService.java +++ b/src/main/java/gripe/_90/appliede/service/KnowledgeService.java @@ -1,7 +1,13 @@ package gripe._90.appliede.service; +import java.math.BigInteger; import java.util.ArrayList; +import java.util.HashMap; 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; @@ -11,6 +17,7 @@ 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; @@ -23,12 +30,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 modules = new ArrayList<>(); + private final Map> providers = new HashMap<>(); + private final MEStorage storage = new EMCStorage(this); + private MinecraftServer server; public KnowledgeService() { @@ -44,7 +54,11 @@ 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)); + } } } @@ -52,12 +66,16 @@ public void addNode(IGridNode gridNode, @Nullable CompoundTag savedData) { 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> getProviders() { + return providers.values().stream().collect(Collectors.toUnmodifiableSet()); } public MEStorage getStorage() { @@ -70,7 +88,7 @@ public MEStorage getStorage(EMCModulePart module) { public List getPatterns() { var patterns = new ArrayList(); - var emc = knowledge.getEmc(); + var emc = getEmc(); var highestTier = 1; while (emc.divide(AppliedE.TIER_LIMIT).signum() == 1) { @@ -82,7 +100,7 @@ public List 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()); @@ -94,9 +112,26 @@ public List 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); + } + }); } } }