openperipheral.api.adapter.method.Env Java Examples

The following examples show how to use openperipheral.api.adapter.method.Env. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AdapterNetwork.java    From OpenPeripheral-Integration with MIT License 6 votes vote down vote up
@ScriptCallable(description = "Get a list of the stored and craftable items in the network.", returnTypes = ReturnType.TABLE)
public List<?> getAvailableItems(IGridHost host,
		@Env(Constants.ARG_CONVERTER) IConverter converter,
		@Optionals @Arg(name = "details", description = "Format of stored items details (defalt: none)") ItemDetails format) {
	IStorageGrid storageGrid = getStorageGrid(host);
	final IItemList<IAEItemStack> storageList = storageGrid.getItemInventory().getStorageList();

	List<Object> result = Lists.newArrayList();
	for (IAEItemStack stack : storageList) {
		@SuppressWarnings("unchecked")
		Map<String, Object> map = (Map<String, Object>)converter.fromJava(stack);
		if (format != null && format != ItemDetails.NONE) {
			final ItemStack itemStack = stack.getItemStack();
			if (format == ItemDetails.PROXY) map.put("item", OpcAccess.itemStackMetaBuilder.createProxy(itemStack));
			else if (format == ItemDetails.ALL) map.put("item", itemStack);
		}
		result.add(map);
	}

	return result;
}
 
Example #2
Source File: AdapterInventory.java    From OpenPeripheral-Integration with MIT License 5 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.TABLE, description = "Get a table with all the items of the chest")
public Map<Index, Object> getAllStacks(IInventory target,
		@Env(Constants.ARG_ARCHITECTURE) IArchitecture access,
		@Optionals @Arg(name = "proxy", description = "If false, method will compute whole table, instead of returning proxy") Boolean proxy) {
	final IInventory inventory = InventoryUtils.getInventory(target);
	Map<Index, Object> result = Maps.newHashMap();

	for (int i = 0; i < inventory.getSizeInventory(); i++) {
		ItemStack stack = inventory.getStackInSlot(i);
		if (stack != null) result.put(access.createIndex(i), (proxy != Boolean.FALSE)? OpcAccess.itemStackMetaBuilder.createProxy(stack) : stack);
	}
	return result;
}
 
Example #3
Source File: TileEntitySelector.java    From OpenPeripheral-Addons with MIT License 5 votes vote down vote up
@ScriptCallable(description = "Set the items being displayed in all slots")
public void setSlots(
		@Env(Constants.ARG_ARCHITECTURE) IArchitecture access,
		@Arg(name = "items", description = "A table containing itemstacks") Map<Index, ItemStack> stacks) {
	for (int slot = 0; slot < 9; slot++) {
		final ItemStack value = stacks.get(access.wrapObject(slot));
		if (value != null) value.stackSize = 1;

		this.slots[slot].set(value);
	}

	sync();
}
 
Example #4
Source File: AdapterInterface.java    From OpenPeripheral-Integration with MIT License 5 votes vote down vote up
@ScriptCallable(description = "Requests the specified item to get crafted.")
public void requestCrafting(IActionHost host,
		@Env(Constants.ARG_ACCESS) IArchitectureAccess access,
		@Env(Constants.ARG_CONVERTER) IConverter converter,
		@Arg(name = "fingerprint", description = "Details of the item you want to craft. Can be found with .getStackInSlot on inventory and .getAvailableItems on AE network") ItemFingerprint needle,
		@Optionals @Arg(name = "qty", description = "The quantity of items you want to craft") Long quantity,
		@Arg(name = "cpu", description = "The name of the CPU you want to use") String wantedCpuName) {
	ICraftingGrid craftingGrid = getCraftingGrid(host);
	if (quantity == null) quantity = 1L;

	ICraftingCPU wantedCpu = findCpu(craftingGrid, wantedCpuName);

	IStorageGrid storageGrid = getStorageGrid(host);
	IMEMonitor<IAEItemStack> monitor = storageGrid.getItemInventory();

	IAEItemStack stack = findCraftableStack(storageGrid.getItemInventory().getStorageList(), needle);
	Preconditions.checkArgument(stack != null, "Can't find craftable item fingerprint %s", needle);

	final IAEItemStack toCraft = stack.copy();
	toCraft.setStackSize(quantity);

	// Create a new CraftingCallback. This callback is called when
	// the network finished calculating the required items. It can do two things for us:
	// a) It sends an event when items are missing to complete the request
	// b) Otherwise it starts the crafting job, which itself is a CraftingRequester OSsending more events to the computer.
	final CraftingCallback craftingCallback = new CraftingCallback(access, converter, craftingGrid, monitor, host, wantedCpu, toCraft);

	// We will need access to the worldObj of the ME Interface -> cast to TileEntity
	final TileEntity tileEntity = (TileEntity)host;

	// Tell the craftingGrid to begin calculating and to report everything to the CraftingCallback
	craftingGrid.beginCraftingJob(tileEntity.getWorldObj(), getGrid(host), new MachineSource(host), toCraft, craftingCallback);

}
 
Example #5
Source File: LuaReflectionHelper.java    From OpenPeripheral with MIT License 5 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.TABLE)
public List<String> getInterfaces(@Env("target") Object owner) {
	List<String> results = Lists.newArrayList();
	for (Class<?> cls : owner.getClass().getInterfaces())
		results.add(cls.toString());
	return results;
}
 
Example #6
Source File: MethodDescriptionTest.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.BOOLEAN)
public boolean test(@Env("target") B target, @Env("target") B target2) {
	return true;
}
 
Example #7
Source File: MethodDescriptionTest.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.BOOLEAN)
public boolean test(@Optionals @Env("target") B target) {
	return true;
}
 
Example #8
Source File: MethodDescriptionTest.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.BOOLEAN)
public boolean test(@Env("env1") D e1, B target) {
	return true;
}
 
Example #9
Source File: MethodDescriptionTest.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.BOOLEAN)
public boolean test(@Arg(name = "foo") String arg, @Env("env1") D target) {
	return true;
}
 
Example #10
Source File: MethodDescriptionTest.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.STRING)
public String test(T target, @Env("env1") E access, @Arg(name = "a") P a) {
	return String.valueOf(a);
}
 
Example #11
Source File: MethodDescriptionTest.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.STRING)
public String test(B target, @Env("env1") D access, @Arg(name = "a") int a, @Optionals @Arg(name = "b") String b, @Arg(name = "var") Integer... v) {
	return access.test();
}
 
Example #12
Source File: MethodDescriptionTest.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.STRING)
public String test(B target, @Env("env1") D access, @Arg(name = "a") Integer a) {
	return access.toString();
}
 
Example #13
Source File: MethodDescriptionTest.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.STRING)
public String test(@Env("env1") D access) {
	return access.toString();
}
 
Example #14
Source File: MethodDescriptionTest.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.STRING)
public String test(@Env("env1") D access, @Env("target") Object target) {
	return access.toString();
}
 
Example #15
Source File: MethodDescriptionTest.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.STRING)
public String test(B target, @Env("env1") D access) {
	return access.test();
}
 
Example #16
Source File: MethodDescriptionTest.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.BOOLEAN)
public boolean test(A target, @Env("env1") A e) {
	return true;
}
 
Example #17
Source File: MethodDeclaration.java    From OpenPeripheral with MIT License 4 votes vote down vote up
public MethodDeclaration(Class<?> rootClass, Method method, ScriptCallable meta, String source) {
	this.method = method;
	this.source = source;

	this.names = getNames(method, meta);

	this.description = meta.description();
	this.returnTypes = ImmutableList.copyOf(meta.returnTypes());
	this.validateReturn = meta.validateReturn();

	this.multipleReturn = method.isAnnotationPresent(MultipleReturn.class);

	this.wrappedReturn = TypeHelper.createFromReturn(returnTypes);

	if (validateReturn) validateResultCount();

	final Type methodArgs[] = method.getGenericParameterTypes();
	final boolean isVarArg = method.isVarArgs();

	ArgParseState state = ArgParseState.ENV_UNNAMED;

	TypeToken<?> scopeType = TypeToken.of(rootClass);

	final Annotation[][] argsAnnotations = method.getParameterAnnotations();
	for (int argIndex = 0; argIndex < methodArgs.length; argIndex++) {
		try {
			final TypeToken<?> argType = scopeType.resolveType(methodArgs[argIndex]);

			AnnotationMap argAnnotations = new AnnotationMap(argsAnnotations[argIndex]);

			boolean optionalStart = argAnnotations.get(Optionals.class) != null;

			Env envArg = argAnnotations.get(Env.class);
			Arg luaArg = argAnnotations.get(Arg.class);

			Preconditions.checkState(envArg == null || luaArg == null, "@Arg and @Env are mutually exclusive");
			if (luaArg != null) {

				if (state != ArgParseState.ARG_OPTIONAL) state = ArgParseState.ARG_REQUIRED;

				if (optionalStart) {
					Preconditions.checkState(state != ArgParseState.ENV_NAMED, "@Optional used more than once");
					state = ArgParseState.ARG_OPTIONAL;
				}

				boolean isLastArg = argIndex == (methodArgs.length - 1);

				ArgumentBuilder builder = new ArgumentBuilder();
				builder.setVararg(isLastArg && isVarArg);
				builder.setOptional(state == ArgParseState.ARG_OPTIONAL);
				builder.setNullable(luaArg.isNullable());

				final Argument arg = builder.build(luaArg.name(), luaArg.description(), luaArg.type(), argType, argIndex);
				callArgs.add(arg);
			} else {
				Preconditions.checkState(state == ArgParseState.ENV_NAMED || state == ArgParseState.ENV_UNNAMED, "Unannotated arg in script part (perhaps missing @Arg annotation?)");
				Preconditions.checkState(!optionalStart, "@Optionals does not work for env arguments");

				Class<?> rawArgType = argType.getRawType();
				if (envArg != null) {
					Preconditions.checkState(state == ArgParseState.ENV_NAMED || state == ArgParseState.ENV_UNNAMED, "@Env annotation used in script part of arguments");
					final String envName = envArg.value();
					EnvArg prev = envArgs.put(envName, new EnvArg(rawArgType, argIndex));
					if (prev != null) { throw new IllegalStateException(String.format("Conflict on name %s, args: %s, %s", envArg, prev.index, argIndex)); }
					state = ArgParseState.ENV_NAMED;
				} else {
					Preconditions.checkState(state == ArgParseState.ENV_UNNAMED, "Unnamed env cannot occur after named");
					unnamedEnvArg.put(argIndex, rawArgType);
				}
			}
		} catch (Throwable t) {
			throw new ArgumentDefinitionException(argIndex, t);
		}
	}

	this.argCount = unnamedEnvArg.size() + envArgs.size() + callArgs.size();
	Preconditions.checkState(this.argCount == methodArgs.length, "Internal error for method %s", method);
}
 
Example #18
Source File: LuaReflectionHelper.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.TABLE)
public Map<String, Map<String, Object>> getDeclaredMethods(@Env("target") Object owner) {
	return describe(owner.getClass().getDeclaredMethods());
}
 
Example #19
Source File: LuaReflectionHelper.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.TABLE)
public Map<String, Map<String, Object>> getMethods(@Env("target") Object owner) {
	return describe(owner.getClass().getMethods());
}
 
Example #20
Source File: LuaReflectionHelper.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.STRING)
public String getSuperclass(@Env("target") Object owner) {
	return owner.getClass().getSuperclass().toString();
}
 
Example #21
Source File: LuaReflectionHelper.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.STRING)
public String getClass(@Env("target") Object owner) {
	return owner.getClass().toString();
}
 
Example #22
Source File: StructuredObjectBase.java    From OpenPeripheral-Addons with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.NUMBER, name = "getId")
public Index getId(@Env(Constants.ARG_ARCHITECTURE) IArchitecture access) {
	checkState();
	return access.createIndex(id);
}
 
Example #23
Source File: IContainer.java    From OpenPeripheral-Addons with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.TABLE, description = "Get all objects")
public Map<Index, E> getAllObjects(@Env(Constants.ARG_ARCHITECTURE) IArchitecture access);
 
Example #24
Source File: IContainer.java    From OpenPeripheral-Addons with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.TABLE, description = "Get the ids of all the objects")
public Set<Index> getAllIds(@Env(Constants.ARG_ARCHITECTURE) IArchitecture access);