Skip to content

Commit

Permalink
Misc changes (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramidzkh authored Nov 3, 2023
1 parent b45b5d4 commit a432405
Show file tree
Hide file tree
Showing 17 changed files with 168 additions and 238 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import net.minecraft.world.item.ItemStack;

import me.ramidzkh.mekae2.MekCapabilities;
import me.ramidzkh.mekae2.util.ChemicalBridge;
import mekanism.api.Action;
import mekanism.api.chemical.gas.GasStack;
import mekanism.api.chemical.infuse.InfusionStack;
Expand Down Expand Up @@ -83,7 +82,7 @@ public ItemStack findCarriedContext(Player player, AbstractContainerMenu menu) {

@Override
public long extract(ItemStack context, MekanismKey what, long amount, Actionable mode) {
var stack = ChemicalBridge.withAmount(what.getStack(), amount);
var stack = what.withAmount(amount);
var action = Action.fromFluidAction(mode.getFluidAction());

if (stack instanceof GasStack gas) {
Expand All @@ -105,7 +104,7 @@ public long extract(ItemStack context, MekanismKey what, long amount, Actionable

@Override
public long insert(ItemStack context, MekanismKey what, long amount, Actionable mode) {
var stack = ChemicalBridge.withAmount(what.getStack(), amount);
var stack = what.withAmount(amount);
var action = Action.fromFluidAction(mode.getFluidAction());

if (stack instanceof GasStack gas) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.jetbrains.annotations.Nullable;

import me.ramidzkh.mekae2.util.ChemicalBridge;
import mekanism.api.chemical.ChemicalStack;
import mezz.jei.api.ingredients.IIngredientType;

Expand All @@ -22,7 +21,7 @@ public IIngredientType<S> getIngredientType() {
@Override
public S getIngredientFromStack(GenericStack stack) {
if (stack.what() instanceof MekanismKey key) {
return ChemicalBridge.withAmount((S) key.getStack(), Math.max(1, stack.amount()));
return (S) key.withAmount(Math.max(1, stack.amount()));
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public boolean isValid(int tank, S stack) {

@Override
public S insertChemical(int tank, S stack, Action action) {
var total = 0;
var total = 0L;

var outputTunnels = part.getOutputs().size();
var amount = stack.getAmount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public int getTanks() {
@Override
public S getChemicalInTank(int tank) {
if (inv.getKey(tank) instanceof MekanismKey what && what.getForm() == form) {
return (S) ChemicalBridge.withAmount(what.getStack(), inv.getAmount(tank));
return (S) what.withAmount(inv.getAmount(tank));
}

return getEmptyStack();
Expand Down Expand Up @@ -90,7 +90,7 @@ public S extractChemical(int tank, long amount, Action action) {
var extracted = inv.extract(tank, what, amount, Actionable.of(action.toFluidAction()));

if (extracted > 0) {
return (S) ChemicalBridge.withAmount(what.getStack(), extracted);
return (S) what.withAmount(extracted);
}

return getEmptyStack();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/me/ramidzkh/mekae2/ae2/MekanismKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public ChemicalStack<?> getStack() {
return stack;
}

public ChemicalStack<?> withAmount(long amount) {
return ChemicalBridge.withAmount(stack, amount);
}

public byte getForm() {
if (stack instanceof GasStack) {
return GAS;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package me.ramidzkh.mekae2.ae2.stack;

import org.jetbrains.annotations.Nullable;

import net.minecraft.network.chat.Component;

import me.ramidzkh.mekae2.AMText;
import me.ramidzkh.mekae2.ae2.MekanismKey;
import mekanism.api.Action;
import mekanism.api.chemical.IChemicalHandler;

import appeng.api.config.Actionable;
import appeng.api.networking.security.IActionSource;
import appeng.api.stacks.AEKey;
import appeng.api.stacks.KeyCounter;
import appeng.api.storage.MEStorage;
import appeng.core.localization.GuiText;

public record ChemicalHandlerFacade(@Nullable IChemicalHandler[] handlers, boolean extractableOnly,
Runnable changeListener) implements MEStorage {

@Override
public long insert(AEKey what, long amount, Actionable mode, IActionSource source) {
if (!(what instanceof MekanismKey key)) {
return 0;
}

var handler = handlers[key.getForm()];

if (handler == null) {
return 0;
}

var inserted = amount - handler.insertChemical(key.withAmount(amount),
Action.fromFluidAction(mode.getFluidAction())).getAmount();

if (inserted > 0 && mode == Actionable.MODULATE) {
if (this.changeListener != null) {
this.changeListener.run();
}
}

return inserted;
}

@Override
public long extract(AEKey what, long amount, Actionable mode, IActionSource source) {
if (!(what instanceof MekanismKey key)) {
return 0;
}

var handler = handlers[key.getForm()];

if (handler == null) {
return 0;
}

var extracted = handler.extractChemical(key.withAmount(amount),
Action.fromFluidAction(mode.getFluidAction())).getAmount();

if (extracted > 0 && mode == Actionable.MODULATE) {
if (this.changeListener != null) {
this.changeListener.run();
}
}

return extracted;
}

@Override
public Component getDescription() {
return GuiText.ExternalStorage.text(AMText.CHEMICAL);
}

@Override
public void getAvailableStacks(KeyCounter out) {
for (var handler : handlers) {
if (handler == null) {
continue;
}

for (var i = 0; i < handler.getTanks(); i++) {
// Skip resources that cannot be extracted if that filter was enabled
var stack = handler.getChemicalInTank(i);
var key = MekanismKey.of(stack);

if (key == null) {
continue;
}

if (extractableOnly && handler.extractChemical(stack, Action.SIMULATE).isEmpty()) {
continue;
}

out.add(key, stack.getAmount());
}
}
}
}

This file was deleted.

114 changes: 0 additions & 114 deletions src/main/java/me/ramidzkh/mekae2/ae2/stack/HandlerStrategy.java

This file was deleted.

Loading

0 comments on commit a432405

Please sign in to comment.