Skip to content

Commit

Permalink
Simplify MekanismExternalStorageStrategy
Browse files Browse the repository at this point in the history
  • Loading branch information
ramidzkh committed Oct 7, 2023
1 parent ec77149 commit cf584a5
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 180 deletions.
101 changes: 101 additions & 0 deletions src/main/java/me/ramidzkh/mekae2/ae2/stack/ChemicalHandlerFacade.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package me.ramidzkh.mekae2.ae2.stack;

import com.google.common.primitives.Ints;

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(Ints.saturatedCast(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.

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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package me.ramidzkh.mekae2.ae2.stack;

import java.util.HashMap;
import java.util.Map;

import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -36,7 +35,8 @@ public MekanismExternalStorageStrategy(ServerLevel level,
@Nullable
@Override
public MEStorage createWrapper(boolean extractableOnly, Runnable injectOrExtractCallback) {
var storages = new HashMap<Byte, MEStorage>();
var handlers = new IChemicalHandler[4];
var empty = true;

for (var entry : lookups.entrySet()) {
var storage = entry.getValue().find(fromSide);
Expand All @@ -45,16 +45,14 @@ public MEStorage createWrapper(boolean extractableOnly, Runnable injectOrExtract
continue;
}

var result = HandlerStrategy.getFacade(storage);
result.setChangeListener(injectOrExtractCallback);
result.setExtractableOnly(extractableOnly);
storages.put(entry.getKey(), result);
handlers[entry.getKey()] = storage;
empty = false;
}

if (storages.isEmpty()) {
if (empty) {
return null;
}

return new CompositeFormStorage(storages);
return new ChemicalHandlerFacade(handlers, extractableOnly, injectOrExtractCallback);
}
}

0 comments on commit cf584a5

Please sign in to comment.