Skip to content

Commit

Permalink
Bring back previous large insertion logic, but limit to 64 iterations…
Browse files Browse the repository at this point in the history
… (max item stack size)
  • Loading branch information
62832 committed May 12, 2024
1 parent 15619c0 commit bdd2be0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
21 changes: 9 additions & 12 deletions src/main/java/gripe/_90/appliede/me/misc/EMCInterfaceLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,7 @@ public void readFromNBT(CompoundTag tag) {
config.readFromChildTag(tag, "config");
storage.readFromChildTag(tag, "storage");
upgrades.readFromNBT(tag, "upgrades");

hasConfig = !config.isEmpty();
updatePlan();
notifyNeighbours();
readConfig();
}

public void writeToNBT(CompoundTag tag) {
Expand Down Expand Up @@ -298,13 +295,17 @@ private boolean acquireFromNetwork(IGrid grid, int slot, AEKey what, long amount
}
}

private void onConfigRowChanged() {
private void readConfig() {
hasConfig = !config.isEmpty();
host.saveChanges();
updatePlan();
notifyNeighbours();
}

private void onConfigRowChanged() {
readConfig();
host.saveChanges();
}

private void onStorageChanged() {
host.saveChanges();
updatePlan();
Expand Down Expand Up @@ -344,12 +345,8 @@ public void addDrops(List<ItemStack> drops) {
var stack = storage.getStack(i);

if (stack != null) {
stack.what()
.addDrops(
stack.amount(),
drops,
host.getBlockEntity().getLevel(),
host.getBlockEntity().getBlockPos());
var be = host.getBlockEntity();
stack.what().addDrops(stack.amount(), drops, be.getLevel(), be.getBlockPos());
}
}

Expand Down
39 changes: 22 additions & 17 deletions src/main/java/gripe/_90/appliede/me/service/EMCStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Player;

import appeng.api.config.Actionable;
Expand Down Expand Up @@ -172,22 +173,28 @@ public long insertItem(
}
}

var itemEmc = IEMCProxy.INSTANCE.getSellValue(what.toStack());
var totalEmc = AppliedE.clampedLong(BigInteger.valueOf(itemEmc).multiply(BigInteger.valueOf(amount)));
var itemEmc = BigInteger.valueOf(IEMCProxy.INSTANCE.getSellValue(what.toStack()));
var totalEmc = itemEmc.multiply(BigInteger.valueOf(amount));
var totalInserted = 0L;

if (mode == Actionable.MODULATE) {
totalEmc = getAmountAfterPowerExpenditure(totalEmc) / itemEmc * itemEmc;
insert(EMCKey.BASE, totalEmc, Actionable.MODULATE, source);
}
for (var i = 0; i < Container.LARGE_MAX_STACK_SIZE && totalEmc.compareTo(BigInteger.ZERO) > 0; i++) {
var canDeposit = itemEmc.longValue();

var inserted = totalEmc / itemEmc;
totalInserted += inserted;
source.player().ifPresent(player -> {
if (mode == Actionable.MODULATE) {
AeStats.ItemsInserted.addToPlayer(player, Ints.saturatedCast(inserted));
canDeposit = getAmountAfterPowerExpenditure(canDeposit);
insert(EMCKey.BASE, canDeposit, Actionable.MODULATE, source);
}
});

var inserted = canDeposit / itemEmc.longValue();
totalInserted += inserted;
source.player().ifPresent(player -> {
if (mode == Actionable.MODULATE) {
AeStats.ItemsInserted.addToPlayer(player, Ints.saturatedCast(inserted));
}
});

totalEmc = totalEmc.subtract(itemEmc);
}

if (mode == Actionable.MODULATE && mayLearn && totalInserted > 0) {
source.player().ifPresent(player -> {
Expand Down Expand Up @@ -233,25 +240,23 @@ public long extractItem(AEItemKey what, long amount, Actionable mode, IActionSou
var totalEmc = itemEmc.multiply(BigInteger.valueOf(amount));
var totalExtracted = 0L;

while (totalEmc.compareTo(BigInteger.ZERO) > 0) {
var toWithdraw = AppliedE.clampedLong(totalEmc);
var canWithdraw = extract(EMCKey.BASE, toWithdraw, Actionable.SIMULATE, source);
for (var i = 0; i < Container.LARGE_MAX_STACK_SIZE && totalEmc.compareTo(BigInteger.ZERO) > 0; i++) {
var canWithdraw = extract(EMCKey.BASE, itemEmc.longValue(), Actionable.SIMULATE, source);

if (mode == Actionable.MODULATE) {
canWithdraw = getAmountAfterPowerExpenditure(canWithdraw);
extract(EMCKey.BASE, canWithdraw, Actionable.MODULATE, source);
}

var extracted = BigInteger.valueOf(canWithdraw).divide(itemEmc).longValue();
var extracted = canWithdraw / itemEmc.longValue();
totalExtracted += extracted;
source.player().ifPresent(player -> {
if (mode == Actionable.MODULATE) {
AeStats.ItemsExtracted.addToPlayer(player, Ints.saturatedCast(extracted));
}
});

var wouldHaveWithdrawn = BigInteger.valueOf(toWithdraw);
totalEmc = totalEmc.subtract(wouldHaveWithdrawn).add(wouldHaveWithdrawn.remainder(itemEmc));
totalEmc = totalEmc.subtract(itemEmc);
}

return totalExtracted;
Expand Down

0 comments on commit bdd2be0

Please sign in to comment.