Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework on AE/RS Bridge #600

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public final MethodResult listCraftableItems() {
return notConnected();

List<Object> items = new ArrayList<>();
RefinedStorage.getCraftableItems(getNetwork()).forEach(item -> items.add(RefinedStorage.getObjectFromStack(item.copy(), getNetwork())));
RefinedStorage.getCraftableItems(getNetwork()).forEach(item -> items.add(RefinedStorage.getObjectFromStack(item)));
return MethodResult.of(items);
}

Expand All @@ -103,7 +103,7 @@ public final MethodResult listCraftableFluids() {
return notConnected();

List<Object> fluids = new ArrayList<>();
RefinedStorage.getCraftableFluids(getNetwork()).forEach(fluid -> fluids.add(RefinedStorage.getObjectFromFluid(fluid, getNetwork())));
RefinedStorage.getCraftableFluids(getNetwork()).forEach(fluid -> fluids.add(RefinedStorage.getObjectFromFluid(fluid)));
return MethodResult.of(fluids);
}

Expand Down Expand Up @@ -248,19 +248,24 @@ public final MethodResult getAvgPowerInjection() {
@Override
@LuaFunction(mainThread = true)
public final MethodResult getFilteredPatterns(IArguments arguments) throws LuaException {
if (!isAvailable())
if (!isAvailable()) {
return notConnected();
}

Pair<ItemFilter, String> filter = ItemFilter.parse(arguments.getTable(0));
if (filter.rightPresent())
if (filter.rightPresent()) {
return MethodResult.of(false, filter.getRight());
}

ItemFilter parsedFilter = filter.getLeft();
if (parsedFilter.isEmpty())
if (parsedFilter.isEmpty()) {
return MethodResult.of(false, "EMPTY_FILTER");
}

ItemStack patternItem = RefinedStorage.findStackFromFilter(getNetwork(), getNetwork().getCraftingManager(), parsedFilter);

ItemStack patternItem = RefinedStorage.findPatternFromFilter(getNetwork().getCraftingManager(), parsedFilter);
if (patternItem == null || patternItem == ItemStack.EMPTY) {
return MethodResult.of(null, "NOT_CRAFTABLE");
}
return MethodResult.of(RefinedStorage.getObjectFromPattern(getNetwork().getCraftingManager().getPattern(patternItem), getNetwork()));
}

Expand All @@ -272,57 +277,66 @@ public final MethodResult getPatterns() {

protected MethodResult exportToChest(@NotNull IArguments arguments, @Nullable IItemHandler targetInventory) throws LuaException {
RsItemHandler itemHandler = new RsItemHandler(getNetwork());
if (targetInventory == null)
if (targetInventory == null) {
return MethodResult.of(0, "INVALID_TARGET");
}

Pair<ItemFilter, String> filter = ItemFilter.parse(arguments.getTable(0));
if (filter.rightPresent())
if (filter.rightPresent()) {
return MethodResult.of(0, filter.getRight());
}

return MethodResult.of(InventoryUtil.moveItem(itemHandler, targetInventory, filter.getLeft()), null);
}

protected MethodResult importToSystem(@NotNull IArguments arguments, @Nullable IItemHandler targetInventory) throws LuaException {
RsItemHandler itemHandler = new RsItemHandler(getNetwork());
if (targetInventory == null)
if (targetInventory == null) {
return MethodResult.of(0, "INVALID_TARGET");
}

Pair<ItemFilter, String> filter = ItemFilter.parse(arguments.getTable(0));
if (filter.rightPresent())
if (filter.rightPresent()) {
return MethodResult.of(0, filter.getRight());
}

return MethodResult.of(InventoryUtil.moveItem(targetInventory, itemHandler, filter.getLeft()), null);
}

protected MethodResult exportToTank(@NotNull IArguments arguments, @Nullable IFluidHandler targetInventory) throws LuaException {
RsFluidHandler itemHandler = new RsFluidHandler(getNetwork());
if (targetInventory == null)
if (targetInventory == null) {
return MethodResult.of(0, "INVALID_TARGET");
}

Pair<FluidFilter, String> filter = FluidFilter.parse(arguments.getTable(0));
if (filter.rightPresent())
if (filter.rightPresent()) {
return MethodResult.of(0, filter.getRight());
}

return MethodResult.of(InventoryUtil.moveFluid(itemHandler, targetInventory, filter.getLeft()), null);
}

protected MethodResult importToSystem(@NotNull IArguments arguments, @Nullable IFluidHandler targetInventory) throws LuaException {
RsFluidHandler itemHandler = new RsFluidHandler(getNetwork());
if (targetInventory == null)
if (targetInventory == null) {
return MethodResult.of(0, "INVALID_TARGET");
}

Pair<FluidFilter, String> filter = FluidFilter.parse(arguments.getTable(0));
if (filter.rightPresent())
if (filter.rightPresent()) {
return MethodResult.of(0, filter.getRight());
}

return MethodResult.of(InventoryUtil.moveFluid(targetInventory, itemHandler, filter.getLeft()), null);
}

@Override
@LuaFunction(mainThread = true)
public final MethodResult exportItem(IComputerAccess computer, IArguments arguments) throws LuaException {
if (!isAvailable())
if (!isAvailable()) {
return notConnected();
}

String side = arguments.getString(1);
IItemHandler inventory;
Expand All @@ -339,8 +353,9 @@ public final MethodResult exportItem(IComputerAccess computer, IArguments argume
@Override
@LuaFunction(mainThread = true)
public final MethodResult importItem(IComputerAccess computer, IArguments arguments) throws LuaException {
if (!isAvailable())
if (!isAvailable()) {
return notConnected();
}

String side = arguments.getString(1);
IItemHandler inventory;
Expand All @@ -357,8 +372,9 @@ public final MethodResult importItem(IComputerAccess computer, IArguments argume
@Override
@LuaFunction(mainThread = true)
public final MethodResult exportFluid(IComputerAccess computer, IArguments arguments) throws LuaException {
if (!isAvailable())
if (!isAvailable()) {
return notConnected();
}

String side = arguments.getString(1);
IFluidHandler fluidHandler;
Expand Down Expand Up @@ -390,23 +406,31 @@ public final MethodResult importFluid(IComputerAccess computer, IArguments argum
fluidHandler = FluidUtil.getHandlerFromName(computer, arguments.getString(1));
}

if (fluidHandler == null)
if (fluidHandler == null) {
return MethodResult.of(0, "The target tank does not exist. Make sure the bridge is exposed in the computer network. Reach out to our discord or our documentation for help.");
}

return importToSystem(arguments, fluidHandler);
}

@Override
@LuaFunction(mainThread = true)
public final MethodResult getItem(IArguments arguments) throws LuaException {
if (!isAvailable())
if (!isAvailable()) {
return notConnected();
}

Pair<ItemFilter, String> filter = ItemFilter.parse(arguments.getTable(0));
if (filter.rightPresent())
if (filter.rightPresent()) {
return MethodResult.of(null, filter.getRight());

return MethodResult.of(RefinedStorage.getObjectFromStack(RefinedStorage.findStackFromFilter(getNetwork(), getNetwork().getCraftingManager(), filter.getLeft()), getNetwork()));
}
ItemStack stored = RefinedStorage.findStackFromFilter(getNetwork(), filter.getLeft());
ItemStack craftable = RefinedStorage.findPatternFromFilter(getNetwork().getCraftingManager(), filter.getLeft());
Object res = RefinedStorage.getObjectFromStack(stored, craftable);
if (res == null) {
return MethodResult.of(null, "ITEM_NOT_FOUND");
}
return MethodResult.of(res);
}

@Override
Expand All @@ -418,22 +442,26 @@ public final MethodResult getFluid(IArguments arguments) throws LuaException {
@Override
@LuaFunction(mainThread = true)
public final MethodResult craftItem(IComputerAccess computerAccess, IArguments arguments) throws LuaException {
if (!isAvailable())
if (!isAvailable()) {
return notConnected();
}


Pair<ItemFilter, String> filter = ItemFilter.parse(arguments.getTable(0));
if (filter.rightPresent())
if (filter.rightPresent()) {
return MethodResult.of(null, filter.getRight());
}

ItemStack stack = RefinedStorage.findStackFromFilter(getNetwork(), getNetwork().getCraftingManager(), filter.getLeft());
if (stack == null)
ItemStack stack = RefinedStorage.findPatternFromFilter(getNetwork().getCraftingManager(), filter.getLeft());
if (stack == null || stack == ItemStack.EMPTY) {
return MethodResult.of(null, "NOT_CRAFTABLE");
}

ICalculationResult result = getNetwork().getCraftingManager().create(stack, filter.getLeft().getCount());
CalculationResultType type = result.getType();
if (type == CalculationResultType.OK)
if (type == CalculationResultType.OK) {
getNetwork().getCraftingManager().start(result.getTask());
}
AdvancedPeripherals.debug("Crafting Result of '" + ItemUtil.getRegistryKey(stack).toString() + "':" + type);
return MethodResult.of(type == CalculationResultType.OK);
}
Expand All @@ -448,14 +476,16 @@ public final MethodResult craftFluid(IComputerAccess computerAccess, IArguments
if (filter.rightPresent())
return MethodResult.of(null, filter.getRight());

FluidStack stack = RefinedStorage.findFluidFromFilter(getNetwork(), getNetwork().getCraftingManager(), filter.getLeft());
if (stack == null)
FluidStack stack = RefinedStorage.findFluidPatternFromFilter(getNetwork().getCraftingManager(), filter.getLeft());
if (stack == null || stack == FluidStack.EMPTY) {
return MethodResult.of(null, "NOT_CRAFTABLE");
}

ICalculationResult result = getNetwork().getCraftingManager().create(stack, filter.getLeft().getCount());
CalculationResultType type = result.getType();
if (type == CalculationResultType.OK)
if (type == CalculationResultType.OK) {
getNetwork().getCraftingManager().start(result.getTask());
}
AdvancedPeripherals.debug("Crafting Result of '" + FluidUtil.getRegistryKey(stack).toString() + "':" + type);
return MethodResult.of(type == CalculationResultType.OK);
}
Expand All @@ -470,52 +500,91 @@ public MethodResult cancelCraftingTasks(IArguments arguments) throws LuaExceptio
return null;
}

@Override
@LuaFunction(mainThread = true)
public final MethodResult isItemCraftable(IArguments arguments) throws LuaException {
if (!isAvailable()) {
return notConnected();
}

Pair<ItemFilter, String> filter = ItemFilter.parse(arguments.getTable(0));
if (filter.rightPresent()) {
return MethodResult.of(false, filter.getRight());
}

ItemFilter parsedFilter = filter.getLeft();
if (parsedFilter.isEmpty()) {
return MethodResult.of(false, "EMPTY_FILTER");
}
return MethodResult.of(RefinedStorage.isItemCraftable(getNetwork(), parsedFilter.toItemStack()));
}

@Override
@LuaFunction(mainThread = true)
public final MethodResult isItemCrafting(IArguments arguments) throws LuaException {
if (!isAvailable())
if (!isAvailable()) {
return notConnected();
}
Pair<ItemFilter, String> filter = ItemFilter.parse(arguments.getTable(0));
if (filter.rightPresent())
if (filter.rightPresent()) {
return MethodResult.of(null, filter.getRight());
}

ItemStack stack = RefinedStorage.findStackFromFilter(getNetwork(), getNetwork().getCraftingManager(), filter.getLeft());
if (stack == null)
ItemStack stack = RefinedStorage.findPatternFromFilter(getNetwork().getCraftingManager(), filter.getLeft());
if (stack == null || stack == ItemStack.EMPTY) {
return MethodResult.of(null, "NOT_CRAFTABLE");
}

for (ICraftingTask task : getNetwork().getCraftingManager().getTasks()) {
ItemStack taskStack = task.getRequested().getItem();
if (taskStack != null && taskStack.sameItem(stack))
if (taskStack != null && taskStack.sameItem(stack)) {
return MethodResult.of(true);
}
}
return MethodResult.of(false);
}

@Override
@LuaFunction(mainThread = true)
public final MethodResult isFluidCraftable(IArguments arguments) throws LuaException {
return null;
}
if (!isAvailable()) {
return notConnected();
}

@Override
@LuaFunction(mainThread = true)
public final MethodResult isFluidCrafting(IArguments arguments) throws LuaException {
return null;
Pair<FluidFilter, String> filter = FluidFilter.parse(arguments.getTable(0));
if (filter.rightPresent()) {
return MethodResult.of(false, filter.getRight());
}

FluidFilter parsedFilter = filter.getLeft();
if (parsedFilter.isEmpty()) {
return MethodResult.of(false, "EMPTY_FILTER");
}
return MethodResult.of(RefinedStorage.isFluidCraftable(getNetwork(), parsedFilter.toFluidStack()));
}

@Override
@LuaFunction(mainThread = true)
public final MethodResult isItemCraftable(IArguments arguments) throws LuaException {
if (!isAvailable())
public final MethodResult isFluidCrafting(IArguments arguments) throws LuaException {
if (!isAvailable()) {
return notConnected();
}
Pair<FluidFilter, String> filter = FluidFilter.parse(arguments.getTable(0));
if (filter.rightPresent()) {
return MethodResult.of(null, filter.getRight());
}

Pair<ItemFilter, String> filter = ItemFilter.parse(arguments.getTable(0));
if (filter.rightPresent())
return MethodResult.of(false, filter.getRight());
FluidStack stack = RefinedStorage.findFluidPatternFromFilter(getNetwork().getCraftingManager(), filter.getLeft());
if (stack == null || stack == FluidStack.EMPTY) {
return MethodResult.of(null, "NOT_CRAFTABLE");
}

ItemFilter parsedFilter = filter.getLeft();
if (parsedFilter.isEmpty())
return MethodResult.of(false, "EMPTY_FILTER");
return MethodResult.of(RefinedStorage.isItemCraftable(getNetwork(), parsedFilter.toItemStack()));
for (ICraftingTask task : getNetwork().getCraftingManager().getTasks()) {
FluidStack taskStack = task.getRequested().getFluid();
if (taskStack != null && taskStack.isFluidEqual(stack)) {
return MethodResult.of(true);
}
}
return MethodResult.of(false);
}
}
Loading
Loading