Search in sources :

Example 1 with BoundComputerMethod

use of mekanism.common.integration.computer.BoundComputerMethod in project Mekanism by mekanism.

the class CCMethodCaller method callMethod.

// Note: This method intentionally matches the signature for IDynamicLuaObject, but this class doesn't implement it to make sure
// the peripheral doesn't have issues if something is doing an instance check. (There may not be any cases this is a problem)
@Nonnull
public MethodResult callMethod(@Nonnull ILuaContext context, int methodIndex, @Nonnull IArguments arguments) throws LuaException {
    if (methodIndex < 0 || methodIndex >= methods.length) {
        throw new LuaException(String.format(Locale.ROOT, "Method index '%d' is out of bounds. This %s only has '%d' methods.", methodIndex, getCallerType(), methods.length));
    }
    BoundComputerMethod method = methods[methodIndex];
    CCArgumentWrapper argumentWrapper = new CCArgumentWrapper(arguments);
    // Our argument type validator should be thread-safe, so can be run on the ComputerCraft Lua thread
    SelectedMethodInfo selectedImplementation = method.findMatchingImplementation(argumentWrapper);
    if (selectedImplementation.isThreadSafe()) {
        // If our selected implementation is thread-safe, run it directly
        return method.run(argumentWrapper, selectedImplementation);
    }
    // Otherwise, if it is not thread-safe (which will be the majority of our cases), queue it up to run on the game thread
    long task = context.issueMainThreadTask(() -> method.run(argumentWrapper, selectedImplementation).getResult());
    return new TaskCallback(task).pull;
}
Also used : BoundComputerMethod(mekanism.common.integration.computer.BoundComputerMethod) SelectedMethodInfo(mekanism.common.integration.computer.BoundComputerMethod.SelectedMethodInfo) LuaException(dan200.computercraft.api.lua.LuaException) Nonnull(javax.annotation.Nonnull)

Aggregations

LuaException (dan200.computercraft.api.lua.LuaException)1 Nonnull (javax.annotation.Nonnull)1 BoundComputerMethod (mekanism.common.integration.computer.BoundComputerMethod)1 SelectedMethodInfo (mekanism.common.integration.computer.BoundComputerMethod.SelectedMethodInfo)1