Skip to content

Commit

Permalink
Remove all EMC from storage cells if they're non-empty
Browse files Browse the repository at this point in the history
  • Loading branch information
62832 committed May 17, 2024
1 parent e8f198c commit 408c97b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
4 changes: 0 additions & 4 deletions src/main/java/gripe/_90/appliede/AppliedE.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,6 @@ 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 void registerEMC(ItemLike item, int emc) {
APICustomEMCMapper.INSTANCE.registerCustomEMC(MODID, new CustomEMCRegistration(NSSItem.createItem(item), emc));
}
Expand Down
47 changes: 22 additions & 25 deletions src/main/java/gripe/_90/appliede/emc/InventoryItemProcessor.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package gripe._90.appliede.emc;

import java.math.BigInteger;

import org.jetbrains.annotations.NotNull;

import appeng.api.stacks.AEItemKey;
import net.minecraft.world.item.ItemStack;

import appeng.api.storage.StorageCells;
import appeng.api.upgrades.IUpgradeableItem;
import appeng.items.contents.NetworkToolMenuHost;
import appeng.items.tools.NetworkToolItem;

import gripe._90.appliede.AppliedE;

import moze_intel.projecte.api.ItemInfo;
import moze_intel.projecte.api.nbt.INBTProcessor;
import moze_intel.projecte.api.nbt.NBTProcessor;
Expand All @@ -32,43 +29,43 @@ public String getDescription() {

@Override
public long recalculateEMC(@NotNull ItemInfo itemInfo, long currentEmc) throws ArithmeticException {
if (itemInfo.getItem() instanceof NetworkToolItem) {
var stack = itemInfo.createStack();
var bigEmc = BigInteger.valueOf(currentEmc);
var stack = itemInfo.createStack();

if (stack.getItem() instanceof NetworkToolItem) {
var inventory = new NetworkToolMenuHost(null, -1, stack, null).getInventory();

for (var item : inventory) {
var itemEmc = IEMCProxy.INSTANCE.getValue(item);
bigEmc = bigEmc.add(BigInteger.valueOf(itemEmc).multiply(BigInteger.valueOf(item.getCount())));
currentEmc = addEmc(currentEmc, item);
}

return AppliedE.clampedLong(bigEmc);
return currentEmc;
}

if (!(itemInfo.getItem() instanceof IUpgradeableItem upgradeable)) {
if (!(stack.getItem() instanceof IUpgradeableItem upgradeable)) {
return currentEmc;
}

var stack = itemInfo.createStack();
var bigEmc = BigInteger.valueOf(currentEmc);

for (var upgrade : upgradeable.getUpgrades(stack)) {
bigEmc = bigEmc.add(BigInteger.valueOf(IEMCProxy.INSTANCE.getValue(upgrade)));
currentEmc = addEmc(currentEmc, upgrade);
}

var cell = StorageCells.getCellInventory(itemInfo.createStack(), null);
var cell = StorageCells.getCellInventory(stack, null);

if (cell == null) {
return AppliedE.clampedLong(bigEmc);
if (cell != null && !cell.getAvailableStacks().isEmpty()) {
return 0;
}

for (var key : cell.getAvailableStacks()) {
if (key.getKey() instanceof AEItemKey item) {
var keyEmc = IEMCProxy.INSTANCE.getValue(item.toStack());
bigEmc = bigEmc.add(BigInteger.valueOf(keyEmc).multiply(BigInteger.valueOf(key.getLongValue())));
}
return currentEmc;
}

private long addEmc(long currentEmc, ItemStack stack) throws ArithmeticException {
var itemEmc = IEMCProxy.INSTANCE.getValue(stack);

if (itemEmc > 0) {
var stackEmc = Math.multiplyExact(itemEmc, stack.getCount());
currentEmc = Math.addExact(currentEmc, stackEmc);
}

return AppliedE.clampedLong(bigEmc);
return currentEmc;
}
}

0 comments on commit 408c97b

Please sign in to comment.