diff --git a/src/main/java/me/ramidzkh/mekae2/ae2/stack/ChemicalHandlerFacade.java b/src/main/java/me/ramidzkh/mekae2/ae2/stack/ChemicalHandlerFacade.java new file mode 100644 index 0000000..7b81b68 --- /dev/null +++ b/src/main/java/me/ramidzkh/mekae2/ae2/stack/ChemicalHandlerFacade.java @@ -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()); + } + } + } +} diff --git a/src/main/java/me/ramidzkh/mekae2/ae2/stack/CompositeFormStorage.java b/src/main/java/me/ramidzkh/mekae2/ae2/stack/CompositeFormStorage.java deleted file mode 100644 index e97598e..0000000 --- a/src/main/java/me/ramidzkh/mekae2/ae2/stack/CompositeFormStorage.java +++ /dev/null @@ -1,59 +0,0 @@ -package me.ramidzkh.mekae2.ae2.stack; - -import java.util.Map; -import java.util.Objects; - -import net.minecraft.network.chat.Component; - -import me.ramidzkh.mekae2.ae2.MekanismKey; - -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; -import appeng.me.storage.NullInventory; - -record CompositeFormStorage(Map storages) implements MEStorage { - - @Override - public boolean isPreferredStorageFor(AEKey what, IActionSource source) { - return getStorage(what).isPreferredStorageFor(what, source); - } - - @Override - public long insert(AEKey what, long amount, Actionable mode, IActionSource source) { - return getStorage(what).insert(what, amount, mode, source); - } - - @Override - public long extract(AEKey what, long amount, Actionable mode, IActionSource source) { - return getStorage(what).extract(what, amount, mode, source); - } - - /** - * Describes the types of storage represented by this object. - */ - @Override - public Component getDescription() { - return GuiText.ExternalStorage.text(Component.literal("Chemicals")); - } - - @Override - public void getAvailableStacks(KeyCounter out) { - for (var storage : storages.values()) { - storage.getAvailableStacks(out); - } - } - - private MEStorage getStorage(AEKey key) { - MEStorage storage = null; - - if (key instanceof MekanismKey mekanismKey) { - storage = storages.get(mekanismKey.getForm()); - } - - return Objects.requireNonNullElse(storage, NullInventory.of()); - } -} diff --git a/src/main/java/me/ramidzkh/mekae2/ae2/stack/HandlerStrategy.java b/src/main/java/me/ramidzkh/mekae2/ae2/stack/HandlerStrategy.java deleted file mode 100644 index 7e3cfdb..0000000 --- a/src/main/java/me/ramidzkh/mekae2/ae2/stack/HandlerStrategy.java +++ /dev/null @@ -1,113 +0,0 @@ -package me.ramidzkh.mekae2.ae2.stack; - -import java.util.Set; - -import org.jetbrains.annotations.Nullable; - -import me.ramidzkh.mekae2.ae2.MekanismKey; -import me.ramidzkh.mekae2.ae2.MekanismKeyType; -import mekanism.api.Action; -import mekanism.api.chemical.IChemicalHandler; - -import appeng.api.config.Actionable; -import appeng.api.stacks.AEKey; -import appeng.api.stacks.AEKeyType; -import appeng.api.stacks.GenericStack; -import appeng.api.stacks.KeyCounter; -import appeng.me.storage.ExternalStorageFacade; - -class HandlerStrategy { - - public static ExternalStorageFacade getFacade(IChemicalHandler handler) { - return new ExternalStorageFacade() { - @Override - public int getSlots() { - return handler.getTanks(); - } - - @Nullable - @Override - public GenericStack getStackInSlot(int slot) { - var stack = handler.getChemicalInTank(slot); - var key = MekanismKey.of(stack); - - if (key == null) { - return null; - } - - return new GenericStack(key, stack.getAmount()); - } - - @Override - public AEKeyType getKeyType() { - return MekanismKeyType.TYPE; - } - - @Override - public void getAvailableStacks(KeyCounter out) { - 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()); - } - } - - @Override - protected int insertExternal(AEKey what, int amount, Actionable mode) { - return (int) HandlerStrategy.insert(handler, what, amount, mode); - } - - @Override - public boolean containsAnyFuzzy(Set keys) { - for (var i = 0; i < handler.getTanks(); i++) { - var what = MekanismKey.of(handler.getChemicalInTank(i)); - - if (what != null && keys.contains(what.dropSecondary())) { - return true; - } - } - - return false; - } - - @Override - protected int extractExternal(AEKey what, int amount, Actionable mode) { - return (int) HandlerStrategy.extract(handler, what, amount, mode); - } - }; - } - - public static long extract(IChemicalHandler handler, AEKey what, long amount, Actionable mode) { - if (!(what instanceof MekanismKey key)) { - return 0; - } - - var stack = key.withAmount(amount); - return handler.extractChemical(stack, Action.fromFluidAction(mode.getFluidAction())).getAmount(); - } - - @Nullable - public static AEKey getStackInTank(int slot, IChemicalHandler handler) { - var stack = handler.getChemicalInTank(slot); - return MekanismKey.of(stack); - } - - public static long insert(IChemicalHandler handler, AEKey what, long amount, Actionable mode) { - if (what instanceof MekanismKey key) { - return amount - handler.insertChemical(key.withAmount(amount), - Action.fromFluidAction(mode.getFluidAction())).getAmount(); - } - - return 0; - } -} diff --git a/src/main/java/me/ramidzkh/mekae2/ae2/stack/MekanismExternalStorageStrategy.java b/src/main/java/me/ramidzkh/mekae2/ae2/stack/MekanismExternalStorageStrategy.java index 561043d..0202e2c 100644 --- a/src/main/java/me/ramidzkh/mekae2/ae2/stack/MekanismExternalStorageStrategy.java +++ b/src/main/java/me/ramidzkh/mekae2/ae2/stack/MekanismExternalStorageStrategy.java @@ -1,6 +1,5 @@ package me.ramidzkh.mekae2.ae2.stack; -import java.util.HashMap; import java.util.Map; import org.jetbrains.annotations.Nullable; @@ -36,7 +35,8 @@ public MekanismExternalStorageStrategy(ServerLevel level, @Nullable @Override public MEStorage createWrapper(boolean extractableOnly, Runnable injectOrExtractCallback) { - var storages = new HashMap(); + var handlers = new IChemicalHandler[4]; + var empty = true; for (var entry : lookups.entrySet()) { var storage = entry.getValue().find(fromSide); @@ -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); } }