From cd33d4905072849f20071e762c3c400f8641ea26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=A1=E3=83=BC=E3=81=9A=28=EF=BD=A58=EF=BD=A5=29?= =?UTF-8?q?=E3=81=91=E3=83=BC=E3=81=8D?= <31585494+MineCake147E@users.noreply.github.com> Date: Tue, 2 Sep 2025 12:43:16 +0900 Subject: [PATCH] * Fixed bug that ME Bridge could void chemicals during exporting/importing. * ME Bridge now respects `toSlot` and `fromSlot` during exporting/importing chemical. --- .../appliedenergistics/MEChemicalHandler.java | 2 +- .../common/util/inventory/ChemicalUtil.java | 55 ++++++++++++++----- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/appliedenergistics/MEChemicalHandler.java b/src/main/java/de/srendi/advancedperipherals/common/addons/appliedenergistics/MEChemicalHandler.java index 83e0dcfdb..89280b950 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/appliedenergistics/MEChemicalHandler.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/appliedenergistics/MEChemicalHandler.java @@ -51,7 +51,7 @@ public ChemicalStack extractChemical(ChemicalFilter filter, long count, Action s ChemicalStack extracted = chemicalKey.getRight().getStack(); - long amountExtracted = storageMonitor.extract(chemicalKey.getRight(), filter.getCount(), simulate == Action.SIMULATE ? Actionable.SIMULATE : Actionable.MODULATE, actionSource); + long amountExtracted = storageMonitor.extract(chemicalKey.getRight(), count, simulate == Action.SIMULATE ? Actionable.SIMULATE : Actionable.MODULATE, actionSource); extracted.setAmount(amountExtracted); AdvancedPeripherals.debug("Extracted chemical: " + extracted + " from filter: " + filter); return extracted; diff --git a/src/main/java/de/srendi/advancedperipherals/common/util/inventory/ChemicalUtil.java b/src/main/java/de/srendi/advancedperipherals/common/util/inventory/ChemicalUtil.java index 1774b9755..64744ad19 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/util/inventory/ChemicalUtil.java +++ b/src/main/java/de/srendi/advancedperipherals/common/util/inventory/ChemicalUtil.java @@ -33,32 +33,57 @@ public static long moveChemical(IChemicalHandler inventoryFrom, IChemicalHandler // The logic changes with storage systems since these systems do not have slots if (inventoryFrom instanceof IStorageSystemChemicalHandler storageSystemHandler) { ChemicalStack extracted = storageSystemHandler.extractChemical(filter, amount, Action.SIMULATE); - ChemicalStack remaining = inventoryTo.insertChemical(extracted, Action.EXECUTE); - - transferableAmount += storageSystemHandler.extractChemical(filter, amount - remaining.getAmount(), Action.EXECUTE).getAmount(); - + amount = Math.min(amount, extracted.getAmount()); + if (amount > 0) { + ChemicalStack remaining; + int toSlot = filter.getToSlot(); + if (toSlot >= 0) { + remaining = inventoryTo.insertChemical(toSlot, extracted, Action.EXECUTE); + } else { + remaining = inventoryTo.insertChemical(extracted, Action.EXECUTE); + } + amount -= remaining.getAmount(); + } + if (amount > 0) { + transferableAmount += storageSystemHandler.extractChemical(filter, amount, Action.EXECUTE).getAmount(); + } return transferableAmount; } if (inventoryTo instanceof IStorageSystemChemicalHandler storageSystemHandler) { - if (filter.test(inventoryFrom.getChemicalInTank(0))) { - ChemicalStack toExtract = inventoryFrom.getChemicalInTank(0).copy(); - toExtract.setAmount(amount); - ChemicalStack extracted = inventoryFrom.extractChemical(toExtract, Action.SIMULATE); - if (extracted.isEmpty()) - return 0; - long remaining = storageSystemHandler.insertChemical(extracted, Action.EXECUTE).getAmount(); - - extracted.setAmount(extracted.getAmount() + remaining); - transferableAmount += inventoryFrom.extractChemical(extracted, Action.EXECUTE).getAmount(); + int fromSlot = filter.getFromSlot(); + if (fromSlot >= 0) { + transferableAmount = importChemical(inventoryFrom, filter, storageSystemHandler, amount, fromSlot); + } else { + int tanks = inventoryFrom.getChemicalTanks(); + for (int i = 0; amount > 0 && i < tanks; i++) { + long imported = importChemical(inventoryFrom, filter, storageSystemHandler, amount, i); + transferableAmount += imported; + amount -= imported; + } } - return transferableAmount; } return transferableAmount; } + private static long importChemical(IChemicalHandler inventoryFrom, ChemicalFilter filter, IStorageSystemChemicalHandler storageSystemHandler, long amount, int tank) { + long transferableAmount = 0; + ChemicalStack chemicalInTank = inventoryFrom.getChemicalInTank(tank); + if (filter.test(chemicalInTank)) { + ChemicalStack extracted = inventoryFrom.extractChemical(tank, amount, Action.SIMULATE); + if (!extracted.isEmpty()) { + long remaining = storageSystemHandler.insertChemical(extracted, Action.EXECUTE).getAmount(); + long extracting = extracted.getAmount() - remaining; + if (extracting > 0) { + transferableAmount += inventoryFrom.extractChemical(tank, extracting, Action.EXECUTE).getAmount(); + } + } + } + return transferableAmount; + } + public static IChemicalHandler extractHandler(@Nullable Object object, @Nullable Level level, @Nullable BlockPos pos, @Nullable Direction direction) { if (object instanceof IChemicalHandler itemHandler) return itemHandler;