Skip to content

Commit

Permalink
Simplify chemical import and export strategies (#32)
Browse files Browse the repository at this point in the history
* Simplify chemical import and export strategies
* Update actions
* Ensure other chemical types attempt extraction
  • Loading branch information
ramidzkh committed Apr 9, 2023
1 parent 6cfc4bb commit 3a627d2
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 70 deletions.
11 changes: 6 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Validate Gradle wrapper
uses: gradle/wrapper-validation-action@v1
- name: Set up JDK 17
uses: actions/setup-java@v1
uses: actions/setup-java@v3
with:
distribution: temurin
java-version: 17
- name: Use Gradle cache for faster builds
uses: actions/cache@v1
uses: actions/cache@v3
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
Expand All @@ -28,7 +29,7 @@ jobs:
rm -f ~/.gradle/caches/modules-2/modules-2.lock
rm -f ~/.gradle/caches/modules-2/gc.properties
- name: Setup Gradle Wrapper Cache
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: ~/.gradle/wrapper
key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('**/gradle/wrapper/gradle-wrapper.properties') }}
Expand All @@ -37,7 +38,7 @@ jobs:
- name: Build
run: ./gradlew build --max-workers 1
- name: Upload artifacts
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: appmek
path: build/libs/
5 changes: 3 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ jobs:
runs-on: ubuntu-20.04
steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Validate Gradle wrapper
uses: gradle/wrapper-validation-action@v1
- name: Set up JDK 17
uses: actions/setup-java@v1
uses: actions/setup-java@v3
with:
distribution: temurin
java-version: 17
- name: Generate data
run: ./gradlew downloadAssets runData --max-workers 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import me.ramidzkh.mekae2.MekCapabilities;
import me.ramidzkh.mekae2.ae2.MekanismKey;
import me.ramidzkh.mekae2.util.ChemicalBridge;
import mekanism.api.Action;
import mekanism.api.chemical.IChemicalHandler;

import appeng.api.behaviors.StackExportStrategy;
Expand Down Expand Up @@ -43,48 +45,44 @@ public long transfer(StackTransferContext context, AEKey what, long amount) {
return 0;
}

for (var entry : lookups.entrySet()) {
if (entry.getKey() != mekanismKey.getForm()) {
continue;
}
var storage = lookups.get(mekanismKey.getForm()).find(fromSide);

var storage = entry.getValue().find(fromSide);
if (storage == null) {
return 0;
}

if (storage == null) {
continue;
}
var inv = context.getInternalStorage();

var inv = context.getInternalStorage();
var extracted = StorageHelper.poweredExtraction(
context.getEnergySource(),
inv.getInventory(),
what,
amount,
context.getActionSource(),
Actionable.SIMULATE);

var extracted = StorageHelper.poweredExtraction(
var wasInserted = extracted
- storage.insertChemical(ChemicalBridge.withAmount(mekanismKey.getStack(), extracted),
Action.SIMULATE).getAmount();

if (wasInserted > 0) {
extracted = StorageHelper.poweredExtraction(
context.getEnergySource(),
inv.getInventory(),
what,
amount,
wasInserted,
context.getActionSource(),
Actionable.SIMULATE);

var wasInserted = HandlerStrategy.insert(storage, what, extracted, Actionable.SIMULATE);

if (wasInserted > 0) {
extracted = StorageHelper.poweredExtraction(
context.getEnergySource(),
inv.getInventory(),
what,
wasInserted,
context.getActionSource(),
Actionable.MODULATE);

wasInserted = HandlerStrategy.insert(storage, what, extracted, Actionable.MODULATE);
Actionable.MODULATE);

if (wasInserted < extracted) {
LOGGER.error("Storage export issue, voided {}x{}", extracted - wasInserted, what);
}
wasInserted = extracted
- storage.insertChemical(ChemicalBridge.withAmount(mekanismKey.getStack(), extracted),
Action.EXECUTE).getAmount();

return wasInserted;
if (wasInserted < extracted) {
LOGGER.error("Storage export issue, voided {}x{}", extracted - wasInserted, what);
}

return 0;
return wasInserted;
}

return 0;
Expand All @@ -96,20 +94,14 @@ public long push(AEKey what, long amount, Actionable mode) {
return 0;
}

for (var entry : lookups.entrySet()) {
if (entry.getKey() != mekanismKey.getForm()) {
continue;
}

var storage = entry.getValue().find(fromSide);

if (storage == null) {
continue;
}
var storage = lookups.get(mekanismKey.getForm()).find(fromSide);

return HandlerStrategy.insert(storage, what, amount, mode);
if (storage == null) {
return 0;
}

return 0;
return amount - storage.insertChemical(ChemicalBridge.withAmount(mekanismKey.getStack(), amount),
Action.fromFluidAction(mode.getFluidAction())).getAmount();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import net.minecraft.server.level.ServerLevel;

import me.ramidzkh.mekae2.MekCapabilities;
import me.ramidzkh.mekae2.ae2.MekanismKey;
import me.ramidzkh.mekae2.ae2.MekanismKeyType;
import me.ramidzkh.mekae2.util.ChemicalBridge;
import mekanism.api.Action;
import mekanism.api.chemical.IChemicalHandler;

import appeng.api.behaviors.StackImportStrategy;
Expand All @@ -16,6 +19,7 @@
import appeng.core.AELog;
import appeng.util.BlockApiCache;

@SuppressWarnings("UnstableApiUsage")
public class MekanismStackImportStrategy implements StackImportStrategy {

private final List<BlockApiCache<? extends IChemicalHandler>> lookups;
Expand Down Expand Up @@ -51,7 +55,8 @@ public boolean transfer(StackTransferContext context) {

// Try to find an extractable resource that fits our filter
for (var i = 0; i < adjacentHandler.getTanks() && remainingTransferAmount > 0; i++) {
var resource = HandlerStrategy.getStackInTank(i, adjacentHandler);
var stack = adjacentHandler.getChemicalInTank(i);
var resource = MekanismKey.of(stack);

if (resource == null
// Regard a filter that is set on the bus
Expand All @@ -66,8 +71,9 @@ public boolean transfer(StackTransferContext context) {
context.getActionSource());

// Try to simulate-extract it
var amount = HandlerStrategy.extract(adjacentHandler, resource, amountForThisResource,
Actionable.MODULATE);
var amount = adjacentHandler
.extractChemical(ChemicalBridge.withAmount(stack, amountForThisResource), Action.EXECUTE)
.getAmount();

if (amount > 0) {
var inserted = inv.getInventory().insert(resource, amount, Actionable.MODULATE,
Expand All @@ -76,7 +82,9 @@ public boolean transfer(StackTransferContext context) {
if (inserted < amount) {
// Be nice and try to give the overflow back
var leftover = amount - inserted;
leftover -= HandlerStrategy.insert(adjacentHandler, resource, leftover, Actionable.MODULATE);
leftover = adjacentHandler
.insertChemical(ChemicalBridge.withAmount(stack, leftover), Action.EXECUTE).getAmount();

if (leftover > 0) {
AELog.warn(
"Extracted %dx%s from adjacent storage and voided it because network refused insert",
Expand All @@ -89,8 +97,6 @@ public boolean transfer(StackTransferContext context) {
remainingTransferAmount -= inserted;
}
}

return false;
}

return false;
Expand Down
18 changes: 3 additions & 15 deletions src/main/java/me/ramidzkh/mekae2/util/ChemicalBridge.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
package me.ramidzkh.mekae2.util;

import mekanism.api.chemical.ChemicalStack;
import mekanism.api.chemical.gas.GasStack;
import mekanism.api.chemical.infuse.InfusionStack;
import mekanism.api.chemical.pigment.PigmentStack;
import mekanism.api.chemical.slurry.SlurryStack;

public interface ChemicalBridge {

static <S extends ChemicalStack<?>> S withAmount(S stack, long amount) {
if (stack instanceof GasStack gas) {
return (S) new GasStack(gas, amount);
} else if (stack instanceof InfusionStack infusion) {
return (S) new InfusionStack(infusion, amount);
} else if (stack instanceof PigmentStack pigment) {
return (S) new PigmentStack(pigment, amount);
} else if (stack instanceof SlurryStack slurry) {
return (S) new SlurryStack(slurry, amount);
} else {
throw new UnsupportedOperationException();
}
var copy = stack.copy();
copy.setAmount(amount);
return (S) copy;
}
}

0 comments on commit 3a627d2

Please sign in to comment.