diff --git a/src/main/java/gripe/_90/appliede/AppliedE.java b/src/main/java/gripe/_90/appliede/AppliedE.java
index 738929c..fbc9784 100644
--- a/src/main/java/gripe/_90/appliede/AppliedE.java
+++ b/src/main/java/gripe/_90/appliede/AppliedE.java
@@ -106,6 +106,10 @@ public static ResourceLocation id(String path) {
return new ResourceLocation(MODID, path);
}
+ public static long clampedLong(BigInteger toClamp) {
+ return toClamp.min(BigInteger.valueOf(Long.MAX_VALUE)).longValue();
+ }
+
private static
Item part(Class
partClass, Function, P> factory) {
PartModels.registerModels(PartModelsHelper.createModels(partClass));
return new PartItem<>(new Item.Properties(), partClass, factory);
diff --git a/src/main/java/gripe/_90/appliede/iface/EMCInterfaceLogic.java b/src/main/java/gripe/_90/appliede/iface/EMCInterfaceLogic.java
index e4d584c..0b84d6c 100644
--- a/src/main/java/gripe/_90/appliede/iface/EMCInterfaceLogic.java
+++ b/src/main/java/gripe/_90/appliede/iface/EMCInterfaceLogic.java
@@ -265,39 +265,13 @@ private boolean tryUsePlan(int slot, AEKey what, int amount) {
}
private boolean acquireFromNetwork(IGrid grid, int slot, AEKey what, long amount) {
- if (!(what instanceof AEItemKey itemKey)) {
+ if (!(what instanceof AEItemKey item)) {
return false;
}
- var emcStorage = grid.getService(KnowledgeService.class).getStorage();
- var energy = grid.getEnergyService();
-
- var itemEmc = BigInteger.valueOf(IEMCProxy.INSTANCE.getValue(itemKey.getItem()));
- var totalEmc = itemEmc.multiply(BigInteger.valueOf(amount));
- var acquiredItems = 0L;
-
- while (totalEmc.compareTo(BigInteger.ZERO) > 0) {
- var toWithdraw = clampedLong(totalEmc);
- var canWithdraw = emcStorage.extract(EMCKey.base(), toWithdraw, Actionable.SIMULATE, requestSource);
-
- if (canWithdraw < toWithdraw) {
- break;
- }
-
- var energyToExpend = PowerMultiplier.CONFIG.multiply(toWithdraw);
- var availablePower = energy.extractAEPower(energyToExpend, Actionable.SIMULATE, PowerMultiplier.CONFIG);
-
- if (availablePower < energyToExpend) {
- break;
- }
-
- energy.extractAEPower(energyToExpend, Actionable.MODULATE, PowerMultiplier.CONFIG);
- emcStorage.extract(EMCKey.base(), toWithdraw, Actionable.MODULATE, requestSource);
-
- var withdrawn = BigInteger.valueOf(toWithdraw);
- acquiredItems += withdrawn.divide(itemEmc).longValue();
- totalEmc = totalEmc.subtract(withdrawn).add(withdrawn.remainder(itemEmc));
- }
+ var acquiredItems = grid.getService(KnowledgeService.class)
+ .getStorage()
+ .extractItem(item, amount, Actionable.MODULATE, requestSource);
if (acquiredItems > 0) {
var inserted = storage.insert(slot, what, acquiredItems, Actionable.MODULATE);
diff --git a/src/main/java/gripe/_90/appliede/service/EMCStorage.java b/src/main/java/gripe/_90/appliede/service/EMCStorage.java
index a98bef3..7a590ac 100644
--- a/src/main/java/gripe/_90/appliede/service/EMCStorage.java
+++ b/src/main/java/gripe/_90/appliede/service/EMCStorage.java
@@ -7,7 +7,9 @@
import net.minecraft.network.chat.Component;
import appeng.api.config.Actionable;
+import appeng.api.config.PowerMultiplier;
import appeng.api.networking.security.IActionSource;
+import appeng.api.stacks.AEItemKey;
import appeng.api.stacks.AEKey;
import appeng.api.stacks.KeyCounter;
import appeng.api.storage.MEStorage;
@@ -15,6 +17,8 @@
import gripe._90.appliede.AppliedE;
import gripe._90.appliede.key.EMCKey;
+import moze_intel.projecte.api.proxy.IEMCProxy;
+
public record EMCStorage(KnowledgeService service) implements MEStorage {
@Override
public void getAvailableStacks(KeyCounter out) {
@@ -58,7 +62,15 @@ public long insert(AEKey what, long amount, Actionable mode, IActionSource sourc
@Override
public long extract(AEKey what, long amount, Actionable mode, IActionSource source) {
- if (amount == 0 || !(what instanceof EMCKey emc)) {
+ if (amount == 0) {
+ return 0;
+ }
+
+ if (what instanceof AEItemKey item && source.player().isPresent()) {
+ return extractItem(item, amount, mode, source);
+ }
+
+ if (!(what instanceof EMCKey emc)) {
return 0;
}
@@ -106,6 +118,50 @@ public long extract(AEKey what, long amount, Actionable mode, IActionSource sour
return extracted;
}
+ public long extractItem(AEItemKey what, long amount, Actionable mode, IActionSource source) {
+ var grid = service.getGrid();
+
+ if (grid == null) {
+ return 0;
+ }
+
+ var energy = grid.getEnergyService();
+ var itemEmc = BigInteger.valueOf(IEMCProxy.INSTANCE.getValue(what.getItem()));
+ var totalEmc = itemEmc.multiply(BigInteger.valueOf(amount));
+ var acquiredItems = 0L;
+
+ while (totalEmc.compareTo(BigInteger.ZERO) > 0) {
+ var toWithdraw = AppliedE.clampedLong(totalEmc);
+ var canWithdraw = extract(EMCKey.base(), toWithdraw, Actionable.SIMULATE, source);
+
+ if (canWithdraw < toWithdraw) {
+ break;
+ }
+
+ var energyToExpend = PowerMultiplier.CONFIG.multiply(toWithdraw);
+ var availablePower = energy.extractAEPower(energyToExpend, Actionable.SIMULATE, PowerMultiplier.CONFIG);
+
+ if (availablePower < energyToExpend) {
+ break;
+ }
+
+ BigInteger withdrawn;
+
+ if (mode == Actionable.MODULATE) {
+ energy.extractAEPower(energyToExpend, Actionable.MODULATE, PowerMultiplier.CONFIG);
+ extract(EMCKey.base(), toWithdraw, Actionable.MODULATE, source);
+ withdrawn = BigInteger.valueOf(toWithdraw);
+ } else {
+ withdrawn = BigInteger.valueOf(canWithdraw);
+ }
+
+ acquiredItems += withdrawn.divide(itemEmc).longValue();
+ totalEmc = totalEmc.subtract(withdrawn).add(withdrawn.remainder(itemEmc));
+ }
+
+ return acquiredItems;
+ }
+
@Override
public Component getDescription() {
return AppliedE.EMC_MODULE.get().getDescription();
diff --git a/src/main/java/gripe/_90/appliede/service/KnowledgeService.java b/src/main/java/gripe/_90/appliede/service/KnowledgeService.java
index 00d8839..16a026b 100644
--- a/src/main/java/gripe/_90/appliede/service/KnowledgeService.java
+++ b/src/main/java/gripe/_90/appliede/service/KnowledgeService.java
@@ -18,6 +18,7 @@
import appeng.api.crafting.IPatternDetails;
import appeng.api.features.IPlayerRegistry;
+import appeng.api.networking.IGrid;
import appeng.api.networking.IGridNode;
import appeng.api.networking.IGridService;
import appeng.api.networking.IGridServiceProvider;
@@ -37,9 +38,10 @@
public class KnowledgeService implements IGridService, IGridServiceProvider {
private final List modules = new ArrayList<>();
private final Map> providers = new HashMap<>();
- private final MEStorage storage = new EMCStorage(this);
+ private final EMCStorage storage = new EMCStorage(this);
private MinecraftServer server;
+ private IGrid grid;
public KnowledgeService() {
MinecraftForge.EVENT_BUS.addListener((PlayerKnowledgeChangeEvent event) -> {
@@ -55,6 +57,10 @@ public void addNode(IGridNode gridNode, @Nullable CompoundTag savedData) {
server = gridNode.getLevel().getServer();
}
+ if (grid == null) {
+ grid = gridNode.getGrid();
+ }
+
if (gridNode.getOwner() instanceof EMCModulePart module) {
modules.add(module);
var uuid = gridNode.getOwningPlayerProfileId();
@@ -81,7 +87,7 @@ Set> getProviders() {
return providers.values().stream().collect(Collectors.toUnmodifiableSet());
}
- public MEStorage getStorage() {
+ public EMCStorage getStorage() {
return storage;
}
@@ -115,6 +121,11 @@ public List getPatterns() {
return patterns;
}
+ @Nullable
+ public IGrid getGrid() {
+ return grid;
+ }
+
public BigInteger getEmc() {
return getProviders().stream()
.map(provider -> provider.get().getEmc())