|
1 | 1 | package de.srendi.advancedperipherals.common.addons.appliedenergistics; |
2 | 2 |
|
| 3 | +import appeng.api.networking.crafting.ICraftingService; |
| 4 | +import appeng.api.stacks.AEKey; |
| 5 | +import appeng.api.stacks.KeyCounter; |
| 6 | +import appeng.api.storage.AEKeyFilter; |
3 | 7 | import appeng.api.storage.MEStorage; |
4 | | -import dan200.computercraft.api.lua.IArguments; |
5 | | -import dan200.computercraft.api.lua.LuaException; |
6 | | -import dan200.computercraft.api.lua.MethodResult; |
7 | | -import dan200.computercraft.api.lua.ObjectLuaTable; |
8 | | -import dan200.computercraft.api.peripheral.IComputerAccess; |
9 | | -import de.srendi.advancedperipherals.common.addons.computercraft.peripheral.MEBridgePeripheral; |
10 | | -import de.srendi.advancedperipherals.common.blocks.blockentities.MEBridgeEntity; |
| 8 | +import de.srendi.advancedperipherals.common.addons.APAddon; |
| 9 | +import de.srendi.advancedperipherals.common.util.LuaConverter; |
11 | 10 | import de.srendi.advancedperipherals.common.util.Pair; |
12 | | -import de.srendi.advancedperipherals.common.util.StatusConstants; |
13 | 11 | import de.srendi.advancedperipherals.common.util.inventory.ChemicalFilter; |
14 | | -import de.srendi.advancedperipherals.common.util.inventory.ChemicalUtil; |
15 | | -import mekanism.api.chemical.IChemicalHandler; |
| 12 | +import it.unimi.dsi.fastutil.objects.Object2LongMap; |
| 13 | +import me.ramidzkh.mekae2.ae2.MekanismKey; |
16 | 14 | import org.jetbrains.annotations.NotNull; |
17 | | - |
18 | | -/** |
19 | | - * Offloading mekanism related ME Bridge functions to prevent class loading errors at runtime |
20 | | - */ |
21 | | -public class AEMekanismApi { |
22 | | - |
23 | | - /** |
24 | | - * imports a fluid to the system from a valid tank |
25 | | - * |
26 | | - * @param arguments the arguments given by the computer |
27 | | - * @param computer the computer connected to the peripheral - used for peripheral attached inventories |
28 | | - * @param peripheral the ME Bridge peripheral |
29 | | - * @return the imported amount or null with a string if something went wrong |
30 | | - */ |
31 | | - public static MethodResult importToME(@NotNull IArguments arguments, IComputerAccess computer, MEBridgePeripheral peripheral) throws LuaException { |
32 | | - MEBridgeEntity bridge = peripheral.getBridge(); |
33 | | - MEStorage monitor = AEApi.getMonitor(bridge.getActionableNode()); |
34 | | - Pair<ChemicalFilter, String> filter = ChemicalFilter.parse(new ObjectLuaTable(arguments.getTable(0))); |
35 | | - |
36 | | - if (filter.rightPresent()) |
37 | | - return MethodResult.of(0, filter.right()); |
38 | | - |
39 | | - String side = arguments.getString(1); |
40 | | - IChemicalHandler targetTank = ChemicalUtil.getHandlerFromDirection(side, peripheral.getPeripheralOwner()); |
41 | | - |
42 | | - if (targetTank == null) { |
43 | | - targetTank = ChemicalUtil.getHandlerFromName(computer, side); |
| 15 | +import org.jetbrains.annotations.Nullable; |
| 16 | + |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.Set; |
| 21 | + |
| 22 | +public final class AEMekanismApi { |
| 23 | + @NotNull |
| 24 | + public static Pair<Long, MekanismKey> findAEChemicalFromFilter(MEStorage monitor, @Nullable ICraftingService crafting, ChemicalFilter filter) { |
| 25 | + for (Object2LongMap.Entry<AEKey> temp : monitor.getAvailableStacks()) { |
| 26 | + if (temp.getKey() instanceof MekanismKey key && filter.test(key.getStack())) |
| 27 | + return Pair.of(temp.getLongValue(), key); |
44 | 28 | } |
45 | 29 |
|
46 | | - if (targetTank == null) |
47 | | - return MethodResult.of(0, StatusConstants.INVENTORY_NOT_FOUND.name()); |
| 30 | + if (crafting == null) |
| 31 | + return Pair.of(0L, null); |
48 | 32 |
|
49 | | - MEChemicalHandler chemicalHandler = new MEChemicalHandler(monitor, bridge); |
| 33 | + for (var temp : crafting.getCraftables(param -> true)) { |
| 34 | + if (temp instanceof MekanismKey key && filter.test(key.getStack())) |
| 35 | + return Pair.of(0L, key); |
| 36 | + } |
50 | 37 |
|
51 | | - return MethodResult.of(ChemicalUtil.moveChemical(targetTank, chemicalHandler, filter.left())); |
| 38 | + return Pair.of(0L, null); |
52 | 39 | } |
53 | 40 |
|
54 | | - /** |
55 | | - * imports a fluid to the system from a valid tank |
56 | | - * |
57 | | - * @param arguments the arguments given by the computer |
58 | | - * @param computer the computer connected to the peripheral - used for peripheral attached inventories |
59 | | - * @param peripheral the ME Bridge peripheral |
60 | | - * @return the exportable amount or null with a string if something went wrong |
61 | | - */ |
62 | | - public static MethodResult exportToTank(@NotNull IArguments arguments, IComputerAccess computer, MEBridgePeripheral peripheral) throws LuaException { |
63 | | - MEBridgeEntity bridge = peripheral.getBridge(); |
64 | | - MEStorage monitor = AEApi.getMonitor(bridge.getActionableNode()); |
65 | | - Pair<ChemicalFilter, String> filter = ChemicalFilter.parse(new ObjectLuaTable(arguments.getTable(0))); |
66 | | - |
67 | | - if (filter.rightPresent()) |
68 | | - return MethodResult.of(0, filter.right()); |
69 | | - |
70 | | - String side = arguments.getString(1); |
71 | | - IChemicalHandler targetTank = ChemicalUtil.getHandlerFromDirection(side, peripheral.getPeripheralOwner()); |
72 | | - |
73 | | - if (targetTank == null) { |
74 | | - targetTank = ChemicalUtil.getHandlerFromName(computer, side); |
| 41 | + @NotNull |
| 42 | + public static List<Pair<Long, MekanismKey>> findAEChemicalsFromFilter(MEStorage monitor, ChemicalFilter filter) { |
| 43 | + List<Pair<Long, MekanismKey>> chemicals = new ArrayList<>(); |
| 44 | + for (Object2LongMap.Entry<AEKey> temp : monitor.getAvailableStacks()) { |
| 45 | + if (temp.getKey() instanceof MekanismKey key && filter.test(key.getStack())) { |
| 46 | + chemicals.add(Pair.of(temp.getLongValue(), key)); |
| 47 | + } |
75 | 48 | } |
| 49 | + return chemicals; |
| 50 | + } |
76 | 51 |
|
77 | | - if (targetTank == null) |
78 | | - return MethodResult.of(0, StatusConstants.INVENTORY_NOT_FOUND.name()); |
79 | | - |
80 | | - MEChemicalHandler chemicalHandler = new MEChemicalHandler(monitor, bridge); |
| 52 | + public static List<Object> listChemicals(MEStorage monitor, ICraftingService service, ChemicalFilter filter) { |
| 53 | + List<Object> items = new ArrayList<>(); |
| 54 | + for (Object2LongMap.Entry<AEKey> aeKey : monitor.getAvailableStacks()) { |
| 55 | + if (APAddon.APP_MEKANISTICS.isLoaded() && aeKey.getKey() instanceof MekanismKey mekanismKey && filter.test(mekanismKey.getStack())) { |
| 56 | + items.add(AEApi.parseAeStack(Pair.of(aeKey.getLongValue(), mekanismKey), service)); |
| 57 | + } |
| 58 | + } |
| 59 | + return items; |
| 60 | + } |
81 | 61 |
|
82 | | - return MethodResult.of(ChemicalUtil.moveChemical(chemicalHandler, targetTank, filter.left())); |
| 62 | + public static List<Object> listCraftableChemicals(MEStorage monitor, ICraftingService service, ChemicalFilter filter) { |
| 63 | + List<Object> items = new ArrayList<>(); |
| 64 | + KeyCounter keyCounter = monitor.getAvailableStacks(); |
| 65 | + Set<AEKey> craftables = service.getCraftables(AEKeyFilter.none()); |
| 66 | + for (AEKey aeKey : craftables) { |
| 67 | + if (aeKey instanceof MekanismKey mekanismKey && filter.test(mekanismKey.getStack())) { |
| 68 | + items.add(AEApi.parseAeStack(Pair.of(keyCounter.get(aeKey), aeKey), service)); |
| 69 | + } |
| 70 | + } |
| 71 | + return items; |
83 | 72 | } |
84 | 73 |
|
| 74 | + public static Map<String, Object> parseChemStack(Pair<Long, MekanismKey> stack, @Nullable ICraftingService craftingService) { |
| 75 | + Map<String, Object> properties = LuaConverter.chemicalStackToLua(stack.right().withAmount(stack.left())); |
| 76 | + properties.put("isCraftable", craftingService != null && craftingService.isCraftable(stack.right())); |
| 77 | + return properties; |
| 78 | + } |
85 | 79 | } |
0 commit comments