Skip to content

Commit 68e9d07

Browse files
committed
[#555] Implement storage information functions and some crafting functions for the rs bridge
1 parent c9c37ae commit 68e9d07

File tree

2 files changed

+190
-21
lines changed

2 files changed

+190
-21
lines changed

src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/RsBridgePeripheral.java

Lines changed: 127 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package de.srendi.advancedperipherals.common.addons.computercraft.peripheral;
22

3+
import com.refinedmods.refinedstorage.api.autocrafting.ICraftingManager;
34
import com.refinedmods.refinedstorage.api.autocrafting.ICraftingPattern;
45
import com.refinedmods.refinedstorage.api.autocrafting.task.CalculationResultType;
56
import com.refinedmods.refinedstorage.api.autocrafting.task.ICalculationResult;
@@ -148,76 +149,120 @@ public final MethodResult getTotalFluidStorage() {
148149
}
149150

150151
@Override
152+
@LuaFunction(mainThread = true)
151153
public MethodResult getTotalChemicalStorage() {
152-
return null;
154+
if (!isAvailable())
155+
return notConnected();
156+
157+
return MethodResult.of(-1);
153158
}
154159

155160
@Override
156161
@LuaFunction(mainThread = true)
157162
public final MethodResult getUsedExternItemStorage() {
158-
return null;
163+
if (!isAvailable())
164+
return notConnected();
165+
166+
return MethodResult.of(RefinedStorage.getUsedItemExternalStorage(getNetwork()));
159167
}
160168

161169
@Override
162170
@LuaFunction(mainThread = true)
163171
public final MethodResult getUsedExternFluidStorage() {
164-
return null;
172+
if (!isAvailable())
173+
return notConnected();
174+
175+
return MethodResult.of(RefinedStorage.getUsedFluidExternalStorage(getNetwork()));
165176
}
166177

167178
@Override
179+
@LuaFunction(mainThread = true)
168180
public MethodResult getUsedExternChemicalStorage() {
169-
return null;
181+
if (!isAvailable())
182+
return notConnected();
183+
184+
return MethodResult.of(-1);
170185
}
171186

172187
@Override
173188
@LuaFunction(mainThread = true)
174189
public final MethodResult getUsedItemStorage() {
175-
return null;
190+
if (!isAvailable())
191+
return notConnected();
192+
193+
return MethodResult.of(RefinedStorage.getUsedItemDiskStorage(getNetwork()));
176194
}
177195

178196
@Override
179197
@LuaFunction(mainThread = true)
180198
public final MethodResult getUsedFluidStorage() {
181-
return null;
199+
if (!isAvailable())
200+
return notConnected();
201+
202+
return MethodResult.of(RefinedStorage.getUsedFluidDiskStorage(getNetwork()));
182203
}
183204

184205
@Override
206+
@LuaFunction(mainThread = true)
185207
public MethodResult getUsedChemicalStorage() {
186-
return null;
208+
if (!isAvailable())
209+
return notConnected();
210+
211+
return MethodResult.of(-1);
187212
}
188213

189214
@Override
190215
@LuaFunction(mainThread = true)
191216
public final MethodResult getAvailableExternItemStorage() {
192-
return null;
217+
if (!isAvailable())
218+
return notConnected();
219+
220+
return MethodResult.of(RefinedStorage.getMaxItemExternalStorage(getNetwork()) - RefinedStorage.getUsedItemExternalStorage(getNetwork()));
193221
}
194222

195223
@Override
196224
@LuaFunction(mainThread = true)
197225
public final MethodResult getAvailableExternFluidStorage() {
198-
return null;
226+
if (!isAvailable())
227+
return notConnected();
228+
229+
return MethodResult.of(RefinedStorage.getMaxFluidExternalStorage(getNetwork()) - RefinedStorage.getUsedFluidExternalStorage(getNetwork()));
199230
}
200231

201232
@Override
233+
@LuaFunction(mainThread = true)
202234
public MethodResult getAvailableExternChemicalStorage() {
203-
return null;
235+
if (!isAvailable())
236+
return notConnected();
237+
238+
return MethodResult.of(-1);
204239
}
205240

206241
@Override
207242
@LuaFunction(mainThread = true)
208243
public final MethodResult getAvailableItemStorage() {
209-
return null;
244+
if (!isAvailable())
245+
return notConnected();
246+
247+
return MethodResult.of(RefinedStorage.getMaxItemDiskStorage(getNetwork()) - RefinedStorage.getUsedItemDiskStorage(getNetwork()));
210248
}
211249

212250
@Override
213251
@LuaFunction(mainThread = true)
214252
public final MethodResult getAvailableFluidStorage() {
215-
return null;
253+
if (!isAvailable())
254+
return notConnected();
255+
256+
return MethodResult.of(RefinedStorage.getMaxFluidDiskStorage(getNetwork()) - RefinedStorage.getUsedFluidDiskStorage(getNetwork()));
216257
}
217258

218259
@Override
260+
@LuaFunction(mainThread = true)
219261
public MethodResult getAvailableChemicalStorage() {
220-
return null;
262+
if (!isAvailable())
263+
return notConnected();
264+
265+
return MethodResult.of(-1);
221266
}
222267

223268
@Override
@@ -239,8 +284,12 @@ public final MethodResult getTotalExternFluidStorage() {
239284
}
240285

241286
@Override
287+
@LuaFunction(mainThread = true)
242288
public MethodResult getTotalExternChemicalStorage() {
243-
return null;
289+
if (!isAvailable())
290+
return notConnected();
291+
292+
return MethodResult.of(-1);
244293
}
245294

246295
@Override
@@ -539,20 +588,52 @@ public final MethodResult craftFluid(IComputerAccess computerAccess, IArguments
539588
}
540589

541590
@Override
591+
@LuaFunction(mainThread = true)
542592
public MethodResult getCraftingTasks() {
543-
return null;
593+
if (!isAvailable())
594+
return notConnected();
595+
596+
return MethodResult.of(RefinedStorage.getCraftingTasks(getNetwork()));
544597
}
545598

546599
@Override
600+
@LuaFunction(mainThread = true)
547601
public MethodResult cancelCraftingTasks(IArguments arguments) throws LuaException {
548-
return null;
602+
if (!isAvailable())
603+
return notConnected();
604+
605+
Pair<? extends GenericFilter<?>, String> filter = GenericFilter.parseGeneric(arguments.getTable(0));
606+
if (filter.getRight() != null)
607+
return MethodResult.of(null, filter.getRight());
608+
609+
ICraftingManager craftingManager = getNetwork().getCraftingManager();
610+
int canceled = 0;
611+
612+
for (ICraftingTask task : craftingManager.getTasks()) {
613+
if (filter.getLeft() instanceof ItemFilter itemFilter) {
614+
if (itemFilter.test(task.getRequested().getItem())) {
615+
craftingManager.cancel(task.getId());
616+
canceled++;
617+
}
618+
}
619+
620+
if (filter.getLeft() instanceof FluidFilter fluidFilter) {
621+
if (fluidFilter.test(task.getRequested().getFluid())) {
622+
craftingManager.cancel(task.getId());
623+
canceled++;
624+
}
625+
}
626+
}
627+
628+
return MethodResult.of(canceled);
549629
}
550630

551631
@Override
552632
@LuaFunction(mainThread = true)
553633
public final MethodResult isItemCrafting(IArguments arguments) throws LuaException {
554634
if (!isAvailable())
555635
return notConnected();
636+
556637
Pair<ItemFilter, String> filter = ItemFilter.parse(arguments.getTable(0));
557638
if (filter.rightPresent())
558639
return MethodResult.of(null, filter.getRight());
@@ -572,13 +653,40 @@ public final MethodResult isItemCrafting(IArguments arguments) throws LuaExcepti
572653
@Override
573654
@LuaFunction(mainThread = true)
574655
public final MethodResult isFluidCraftable(IArguments arguments) throws LuaException {
575-
return null;
656+
if (!isAvailable())
657+
return notConnected();
658+
659+
Pair<FluidFilter, String> filter = FluidFilter.parse(arguments.getTable(0));
660+
if (filter.rightPresent())
661+
return MethodResult.of(false, filter.getRight());
662+
663+
FluidFilter parsedFilter = filter.getLeft();
664+
if (parsedFilter.isEmpty())
665+
return MethodResult.of(false, "EMPTY_FILTER");
666+
667+
return MethodResult.of(RefinedStorage.isFluidCraftable(getNetwork(), parsedFilter.toFluidStack()));
576668
}
577669

578670
@Override
579671
@LuaFunction(mainThread = true)
580672
public final MethodResult isFluidCrafting(IArguments arguments) throws LuaException {
581-
return null;
673+
if (!isAvailable())
674+
return notConnected();
675+
676+
Pair<FluidFilter, String> filter = FluidFilter.parse(arguments.getTable(0));
677+
if (filter.rightPresent())
678+
return MethodResult.of(null, filter.getRight());
679+
680+
FluidStack stack = RefinedStorage.findFluidFromFilter(getNetwork(), getNetwork().getCraftingManager(), filter.getLeft());
681+
if (stack.isEmpty())
682+
return MethodResult.of(null, "NOT_CRAFTABLE");
683+
684+
for (ICraftingTask task : getNetwork().getCraftingManager().getTasks()) {
685+
FluidStack taskStack = task.getRequested().getFluid();
686+
if (taskStack != null && taskStack.isFluidEqual(stack))
687+
return MethodResult.of(true);
688+
}
689+
return MethodResult.of(false);
582690
}
583691

584692
@Override
@@ -594,6 +702,7 @@ public final MethodResult isItemCraftable(IArguments arguments) throws LuaExcept
594702
ItemFilter parsedFilter = filter.getLeft();
595703
if (parsedFilter.isEmpty())
596704
return MethodResult.of(false, "EMPTY_FILTER");
705+
597706
return MethodResult.of(RefinedStorage.isItemCraftable(getNetwork(), parsedFilter.toItemStack()));
598707
}
599708
}

src/main/java/de/srendi/advancedperipherals/common/addons/refinedstorage/RefinedStorage.java

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.refinedmods.refinedstorage.api.IRSAPI;
44
import com.refinedmods.refinedstorage.api.autocrafting.ICraftingManager;
55
import com.refinedmods.refinedstorage.api.autocrafting.ICraftingPattern;
6+
import com.refinedmods.refinedstorage.api.autocrafting.task.ICraftingTask;
67
import com.refinedmods.refinedstorage.api.network.INetwork;
78
import com.refinedmods.refinedstorage.api.network.INetworkNodeGraphEntry;
89
import com.refinedmods.refinedstorage.api.network.node.INetworkNode;
@@ -211,6 +212,26 @@ public static long getMaxFluidDiskStorage(INetwork network) {
211212
return creative ? -1 : total;
212213
}
213214

215+
public static long getUsedItemDiskStorage(INetwork network) {
216+
long used = 0;
217+
for (IStorage<ItemStack> store : network.getItemStorageCache().getStorages()) {
218+
if (store instanceof IStorageDisk<ItemStack> storageDisk) {
219+
used += storageDisk.getStored();
220+
}
221+
}
222+
return used;
223+
}
224+
225+
public static long getUsedFluidDiskStorage(INetwork network) {
226+
long used = 0;
227+
for (IStorage<FluidStack> store : network.getFluidStorageCache().getStorages()) {
228+
if (store instanceof IStorageDisk<FluidStack> storageDisk) {
229+
used += storageDisk.getStored();
230+
}
231+
}
232+
return used;
233+
}
234+
214235
public static long getMaxItemExternalStorage(INetwork network) {
215236
long total = 0;
216237
for (IStorage<ItemStack> store : network.getItemStorageCache().getStorages()) {
@@ -231,6 +252,26 @@ public static long getMaxFluidExternalStorage(INetwork network) {
231252
return total;
232253
}
233254

255+
public static long getUsedItemExternalStorage(INetwork network) {
256+
long used = 0;
257+
for (IStorage<ItemStack> store : network.getItemStorageCache().getStorages()) {
258+
if (store instanceof IExternalStorage<ItemStack> externalStorage) {
259+
used += externalStorage.getStored();
260+
}
261+
}
262+
return used;
263+
}
264+
265+
public static long getUsedFluidExternalStorage(INetwork network) {
266+
long used = 0;
267+
for (IStorage<FluidStack> store : network.getFluidStorageCache().getStorages()) {
268+
if (store instanceof IExternalStorage<FluidStack> externalStorage) {
269+
used += externalStorage.getStored();
270+
}
271+
}
272+
return used;
273+
}
274+
234275
public static Object getItem(INetwork network, ItemStack item) {
235276
for (ItemStack itemStack : getItems(network)) {
236277
if (itemStack.sameItem(item) && Objects.equals(itemStack.getTag(), item.getTag()))
@@ -314,9 +355,7 @@ public static List<Object> getStorageDisks(INetwork network) {
314355
public static List<Object> getDiskDrives(INetwork network) {
315356
List<Object> diskDrives = new ArrayList<>();
316357

317-
Collection<INetworkNodeGraphEntry> collection = network.getNodeGraph().all();
318-
319-
for (INetworkNodeGraphEntry graphEntry : collection) {
358+
for (INetworkNodeGraphEntry graphEntry : network.getNodeGraph().all()) {
320359
INetworkNode node = graphEntry.getNode();
321360
if (node instanceof DiskDriveNetworkNode diskDrive) {
322361
diskDrives.add(parseDiskDrive(diskDrive));
@@ -326,6 +365,27 @@ public static List<Object> getDiskDrives(INetwork network) {
326365
return diskDrives;
327366
}
328367

368+
public static List<Object> getCraftingTasks(INetwork network) {
369+
List<Object> tasks = new ArrayList<>();
370+
371+
for (ICraftingTask task : network.getCraftingManager().getTasks()) {
372+
tasks.add(parseCraftingTask(task, network));
373+
}
374+
375+
return tasks;
376+
}
377+
378+
public static Object parseCraftingTask(ICraftingTask task, INetwork network) {
379+
Map<String, Object> properties = new HashMap<>();
380+
381+
properties.put("id", task.getId());
382+
properties.put("pattern", parsePattern(task.getPattern(), network));
383+
properties.put("quantity", task.getQuantity());
384+
properties.put("completion", task.getCompletionPercentage());
385+
386+
return properties;
387+
}
388+
329389
public static Object parseStorageDisk(IStorageDisk<?> disk) {
330390
Map<String, Object> properties = new HashMap<>();
331391

0 commit comments

Comments
 (0)