Java源码示例:li.cil.oc.api.machine.Arguments

示例1
@Callback(doc = "function(face, direction):boolean -- Sets the motor's face and direction. Returns true if it succeeded, false if it didn't.")
public Object[] set(Context context, Arguments args) {

    if (args.count() < 2)
        throw new RuntimeException("At least 2 arguments are required (face, direction)");

    ForgeDirection face = toFD(args.checkAny(0));
    ForgeDirection direction = toFD(args.checkAny(1));

    if (face == null || direction == null)
        throw new RuntimeException("Invalid directions!");
    if (face == direction || face == direction.getOpposite())
        throw new RuntimeException("Motors cannot push or pull blocks!");

    // te.setFace(face, true);
    // te.setDirection(direction, true);

    return new Object[] { true };
}
 
示例2
@Override
@Nullable
@Optional.Method(modid = "opencomputers")
public Object[] invoke(@Nullable String methodName, Context context, @Nullable Arguments args) throws Exception {
    // Step 0. Fetch the respective method.
    final IAgriPeripheralMethod method = AgriApi.getPeripheralMethodRegistry().get(methodName).orElse(null);

    // Step 1. Check method actually exists.
    if (method == null) {
        return null;
    }

    // Step 2. Convert arguments to object array.
    final Object[] argObjects = (args == null) ? null : args.toArray();

    // Step 3. Attempt to evaluate the method.
    try {
        return method.call(this.getWorld(), this.getPos(), this.getJournal(), argObjects);
    } catch (IAgriPeripheralMethod.InvocationException e) {
        throw new Exception(e.getDescription());
    }
}
 
示例3
protected Object[] executeSignallingTask(ITaskSink taskSink, Object target, IMethodExecutor executor, final String signal, final Context context, Arguments arguments) {
	final Object[] args = arguments.toArray();
	final IMethodCall preparedCall = prepareCall(target, executor, context);
	final int callbackId = SignallingGlobals.instance.nextCallbackId();

	taskSink.accept(new Runnable() {
		@Override
		public void run() {
			if (context.isRunning() || context.isPaused()) {
				Object[] result = callForSignal(args, preparedCall, callbackId);
				context.signal(signal, result);
			}
		}
	});

	return new Object[] { callbackId };
}
 
示例4
private static void testMethod(Object target, Object wrapper, Class<?> generatedClass, String name, IMethodExecutor executor, IMethodCall call, ArgVerifier verifier) throws Exception {
	Method m = getMethod(generatedClass, name.substring(0, 1));

	Callback callback = m.getAnnotation(Callback.class);
	Assert.assertNotNull(callback);

	Assert.assertEquals(executor.isAsynchronous(), callback.direct());

	// that's what I get for not injecting ...
	Assert.assertEquals("function()", callback.doc());

	Arguments args = mock(Arguments.class);
	final Object[] argArray = new Object[] { 1, 2, 3 };
	when(args.toArray()).thenReturn(argArray);
	Context context = mock(Context.class);

	m.invoke(wrapper, context, args);

	verify(executor).startCall(target);

	verify(args).toArray();
	verifier.verifyCall(call, context);
	verify(call).call(argArray);
}
 
示例5
@Callback
@Optional.Method(modid = "opencomputers")
public Object[] setProgram(Context context, Arguments args) {

    boolean success = false;
    String message = "There was an error setting the program";

    try {

        int max = FabricatorRecipes.getRecipes().size() - 1;

        Object[] arguments = args.toArray();

        if (arguments == null) {
            message = "Invalid argument. Must be integer between 0 and " + max;
            return new Object[] { success, message };
        }

        Double thing = (Double) arguments[0];

        int program = thing.intValue();

        if (program < 0 || program > max) {
            message = "Invalid argument. Must be integer between 0 and " + max;
        } else {
            this.setProgram(program);
            success = true;
            message = "Fabricator program set to " + program;
        }

    } catch (Exception ex) {
        message = ex.toString();
    }

    return new Object[] { success, message };
}
 
示例6
@Optional.Method(modid = "OpenComputers")
@Callback
public Object[] listData(Context context, Arguments args) {
    Set<String> ret = new HashSet<>();
    for (Map.Entry<Long,GT_NBT_DataBase> entry : OrbDataBase.entrySet()){
        ret.add((entry.getValue().getId()+Long.MAX_VALUE)+". "+entry.getValue().getmDataTitle());
    }
    return ret.toArray(new String[0]);
}
 
示例7
@Callback
@Optional.Method(modid = "opencomputers")
public Object[] getPosition(Context context, Arguments args) {
    java.util.Optional<PhysicsObject> physicsObjectOptional = ValkyrienUtils
        .getPhysicsObject(getWorld(), getPos());
    if (physicsObjectOptional.isPresent()) {
        BlockPos pos = physicsObjectOptional.get()
            .getWrapperEntity()
            .getPosition();
        return new Object[]{pos.getX(), pos.getY(), pos.getZ()};
    }
    return null;
}
 
示例8
@Callback
@Optional.Method(modid = "opencomputers")
public Object[] getRotation(Context context, Arguments args) {
    java.util.Optional<PhysicsObject> physicsObjectOptional = ValkyrienUtils
        .getPhysicsObject(getWorld(), getPos());
    if (physicsObjectOptional.isPresent()) {
        PhysicsWrapperEntity ship = physicsObjectOptional.get()
            .getWrapperEntity();
        return new Object[]{ship.getYaw(), ship.getPitch(), ship.getRoll()};
    }
    return null;
}
 
示例9
@Callback(doc = "function(face):boolean -- Sets the motor's face. Returns true if it succeeded, false if it didn't.")
public Object[] setFace(Context context, Arguments args) {

    if (args.count() == 0)
        throw new RuntimeException("At least 1 argument is required (direction)");
    return new Object[] { te.setFace(toFD(args.checkAny(0))) };
}
 
示例10
@Callback(doc = "function(direction):boolean -- Sets the motor's direction. Returns true if it succeeded, false if it didn't.")
public Object[] setDirection(Context context, Arguments args) {

    if (args.count() == 0)
        throw new RuntimeException("At least 1 argument is required (direction)");
    return new Object[] { /* te.setDirection(toFD(args.checkAny(0))) */};
}
 
示例11
@Override
@Optional.Method(modid = ModIds.OPEN_COMPUTERS)
public Object[] invoke(String method, Context context, Arguments args) throws Exception{
    if("greet".equals(method)) return new Object[]{String.format("Hello, %s!", args.checkString(0))};
    for(ILuaMethod m : luaMethods) {
        if(m.getMethodName().equals(method)) {
            return m.call(args.toArray());
        }
    }
    throw new IllegalArgumentException("Can't invoke method with name \"" + method + "\". not registered");
}
 
示例12
@Override
public Object[] invoke(String method, Context context, Arguments args) throws Exception{
    if("greet".equals(method)) {
        return new Object[]{String.format("Hello, %s!", args.checkString(0))};
    }
    List<ILuaMethod> luaMethods = tile.getLuaMethods();
    for(ILuaMethod m : luaMethods) {
        if(m.getMethodName().equals(method)) {
            return m.call(args.toArray());
        }
    }
    throw new IllegalArgumentException("Can't invoke method with name \"" + method + "\". not registered");
}
 
示例13
@Override
@Optional.Method(modid = "OpenComputers")
public Object[] invoke(final String method, final Context context,
					   final Arguments args) throws Exception {
	final Object[] arguments = new Object[args.count()];
	for (int i = 0; i < args.count(); ++i) {
		arguments[i] = args.checkAny(i);
	}
	final Integer methodId = methodIds.get(method);
	if (methodId == null) {
		throw new NoSuchMethodError();
	}
	return callMethod(methodId, arguments);
}
 
示例14
@Override
@Optional.Method(modid = "OpenComputers")
public Object[] invoke(final String method, final Context context,
					   final Arguments args) throws Exception {
	final Object[] arguments = new Object[args.count()];
	for (int i = 0; i < args.count(); ++i) {
		arguments[i] = args.checkAny(i);
	}
	final Integer methodId = methodIds.get(method);
	if (methodId == null) {
		throw new NoSuchMethodError();
	}
	return callMethod(methodId, arguments);
}
 
示例15
@Test(expected = IllegalStateException.class)
public void testPeripheralNullTarget() throws Throwable {
	setupEnvMocks();
	setupOpenComputersApiMock();

	Map<String, Pair<IMethodExecutor, IMethodCall>> mocks = Maps.newHashMap();
	addMethod(mocks, "a1", true, "desc1");

	Map<String, IMethodExecutor> methods = extractExecutors(mocks);

	ICodeGenerator generator = new PeripheralCodeGenerator();

	Class<?> cls = generateClass("TestClass\u2655", TargetClass.class, ImmutableSet.<Class<?>> of(), methods, generator);

	Object o = cls.getConstructor(TargetClass.class).newInstance(new Object[] { null });

	Assert.assertTrue(o instanceof ManagedEnvironment);

	Method m = getMethod(cls, "a1");

	Arguments args = mock(Arguments.class);
	Context context = mock(Context.class);

	try {
		m.invoke(o, context, args);
	} catch (InvocationTargetException e) {
		throw e.getTargetException();
	}
}
 
示例16
@Callback
@Optional.Method(modid = "opencomputers")
public Object[] getEnergyLevel(Context context, Arguments args) {
    int level = getEnergy();
    return new Object[] { level };
}
 
示例17
@Callback
@Optional.Method(modid = "opencomputers")
public Object[] getWater(Context context, Arguments args) {
    int value = getWater();
    return new Object[] { value };
}
 
示例18
@Callback
@Optional.Method(modid = "opencomputers")
public Object[] getCoresDistribution(Context context, Arguments args) {
    Object value = this.getPacket();
    return new Object[] { value };
}
 
示例19
@Callback
@Optional.Method(modid = "opencomputers")
public Object[] getWaterLevel(Context context, Arguments args) {
    int level = getWater();
    return new Object[] { level };
}
 
示例20
@Callback
@Optional.Method(modid = "opencomputers")
public Object[] getEnergyLevel(Context context, Arguments args) {
    int level = getEnergy();
    return new Object[] { level };
}
 
示例21
@Callback
@Optional.Method(modid = "opencomputers")
public Object[] getProgress(Context context, Arguments args) {
    int value = getProgress();
    return new Object[] { value };
}
 
示例22
@Callback
@Optional.Method(modid = "opencomputers")
public Object[] getGas(Context context, Arguments args) {
    int value = getGas();
    return new Object[] { value };
}
 
示例23
@Callback
@Optional.Method(modid = "opencomputers")
public Object[] getEnergyLevel(Context context, Arguments args) {
    int level = getEnergy();
    return new Object[] { level };
}
 
示例24
@Callback
@Optional.Method(modid = "opencomputers")
public Object[] getProgress(Context context, Arguments args) {
    int value = getProgress();
    return new Object[] { value };
}
 
示例25
@Callback
@Optional.Method(modid = "opencomputers")
public Object[] getEnergyLevel(Context context, Arguments args) {
    int level = getEnergy();
    return new Object[] { level };
}
 
示例26
@Callback
@Optional.Method(modid = "opencomputers")
public Object[] getProgress(Context context, Arguments args) {
    int value = getProgress();
    return new Object[] { value };
}
 
示例27
@Callback
@Optional.Method(modid = "opencomputers")
public Object[] getProgram(Context context, Arguments args) {
    int value = getSelection();
    return new Object[] { value };
}
 
示例28
@Callback
@Optional.Method(modid = "opencomputers")
public Object[] getIsPrinting(Context context, Arguments args) {
    boolean value = getIsPrinting() > 0;
    return new Object[] { value };
}
 
示例29
@Callback
@Optional.Method(modid = "opencomputers")
public Object[] startPrint(Context context, Arguments args) {
    this.startPrinting();
    return new Object[] { "Printing started." };
}
 
示例30
@Callback
@Optional.Method(modid = "opencomputers")
public Object[] stopPrint(Context context, Arguments args) {
    this.stopPrinting();
    return new Object[] { "Printing stopped." };
}