openperipheral.api.adapter.method.Optionals Java Examples

The following examples show how to use openperipheral.api.adapter.method.Optionals. 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: AdapterWritingDesk.java    From OpenPeripheral-Integration with MIT License 6 votes vote down vote up
@ScriptCallable(
		returnTypes = ReturnType.NUMBER,
		description = "Push a page from the notebook into a specific slot in external inventory. Returns the amount of items moved")
public int pushNotebookPage(
		TileEntity desk,
		@Arg(name = "deskSlot") Index deskSlot,
		@Arg(name = "direction", description = "The direction of the other inventory. (north, south, east, west, up or down)") ForgeDirection direction,
		@Arg(name = "fromSlot", description = "The page slot in inventory that you're pushing from") Index fromSlot,
		@Optionals @Arg(name = "intoSlot", description = "The slot in the other inventory that you want to push into") Index intoSlot) {
	IInventory source = createInventoryWrapper(desk, deskSlot);
	IInventory target = getTargetTile(desk, direction);

	if (intoSlot == null) intoSlot = Index.fromJava(-1, 0);

	final int amount = ItemDistribution.moveItemInto(source, fromSlot.value, target, intoSlot.value, 64, direction.getOpposite(), true);
	if (amount > 0) target.markDirty();
	return amount;
}
 
Example #2
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 #3
Source File: AdapterInventory.java    From OpenPeripheral-Integration with MIT License 6 votes vote down vote up
@Asynchronous(false)
@ScriptCallable(description = "Swap two slots in the inventory")
public void swapStacks(IInventory target,
		@Arg(name = "from", description = "The first slot") Index fromSlot,
		@Arg(name = "to", description = "The other slot") Index intoSlot,
		@Optionals @Arg(name = "fromDirection") ForgeDirection fromDirection,
		@Arg(name = "fromDirection") ForgeDirection toDirection) {
	IInventory inventory = InventoryUtils.getInventory(target);
	Preconditions.checkNotNull(inventory, "Invalid target!");
	final int size = inventory.getSizeInventory();
	fromSlot.checkElementIndex("first slot id", size);
	intoSlot.checkElementIndex("second slot id", size);
	if (inventory instanceof ISidedInventory) {
		InventoryUtils.swapStacks((ISidedInventory)inventory,
				fromSlot.value, Objects.firstNonNull(fromDirection, ForgeDirection.UNKNOWN),
				intoSlot.value, Objects.firstNonNull(toDirection, ForgeDirection.UNKNOWN));
	} else InventoryUtils.swapStacks(inventory, fromSlot.value, intoSlot.value);

	inventory.markDirty();
}
 
Example #4
Source File: AdapterWritingDesk.java    From OpenPeripheral-Integration with MIT License 5 votes vote down vote up
@ScriptCallable(
		returnTypes = ReturnType.NUMBER,
		description = "Push a page from the notebook into a specific slot in external inventory. Returns the amount of items moved")
public int pushNotebookPage(
		TileEntity desk,
		@Arg(name = "deskSlot") Index deskSlot,
		@Arg(name = "direction", description = "The direction of the other inventory. (north, south, east, west, up or down)") ForgeDirection direction,
		@Arg(name = "fromSlot", description = "The page slot in inventory that you're pushing from") Index fromSlot,
		@Optionals @Arg(name = "intoSlot", description = "The slot in the other inventory that you want to push into") Index intoSlot) {
	final NotebookWrapper wrapper = createInventoryWrapper(desk, deskSlot);

	ItemStack page = wrapper.getPageFromSlot(fromSlot);

	ItemStack removedPage = wrapper.removePage(page);
	Preconditions.checkNotNull(removedPage, "No page in notebook");

	GenericInventory tmp = new GenericInventory("tmp", false, 1);
	tmp.setInventorySlotContents(0, removedPage.copy());

	final IInventory target = getTargetTile(desk, direction);

	if (intoSlot == null) intoSlot = Index.fromJava(-1, 0);

	int result = ItemDistribution.moveItemInto(tmp, 0, target, intoSlot.value, removedPage.stackSize, direction, true);

	int remains = removedPage.stackSize - result;
	if (remains >= 0) {
		ItemStack returns = removedPage.copy();
		returns.stackSize = remains;
		wrapper.addPage(returns);
	}

	return result;
}
 
Example #5
Source File: MethodsListerHelper.java    From OpenPeripheral with MIT License 5 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.TABLE, description = "Get a complete table of information about all available methods")
public Map<?, ?> getAdvancedMethodsData(@Optionals @Arg(name = "method") String methodName) {
	if (methodName != null) {
		IMethodExecutor method = methods.get(methodName);
		Preconditions.checkArgument(method != null, "Method not found");
		return DocUtils.describe(method.description());
	} else {
		Map<String, Object> info = Maps.newHashMap();
		for (Map.Entry<String, IMethodExecutor> e : methods.entrySet()) {
			final IMethodDescription desc = e.getValue().description();
			info.put(e.getKey(), DocUtils.describe(desc));
		}
		return info;
	}
}
 
Example #6
Source File: TileEntityTicketMachine.java    From OpenPeripheral-Addons with MIT License 5 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.BOOLEAN, description = "Create a new ticket to the specified destination")
public boolean createTicket(@Arg(name = "destination", description = "The destination for the ticket") String destination,
		@Arg(name = "amount") @Optionals Integer amount) {
	if (amount == null) amount = 1;
	else Preconditions.checkArgument(amount > 0 && amount <= 64, "Amount must be between 1 and 64");

	ItemStack paperStack = inventory.getStackInSlot(SLOT_PAPER);
	Preconditions.checkArgument(inventory.isItemValidForSlot(SLOT_PAPER, paperStack) && paperStack.stackSize >= amount, "Not enough paper");

	ItemStack inkStack = inventory.getStackInSlot(SLOT_INK);
	Preconditions.checkArgument(inventory.isItemValidForSlot(SLOT_INK, inkStack) && inkStack.stackSize >= amount, "Not enough ink");

	ItemStack output = inventory.getStackInSlot(SLOT_OUTPUT);

	ItemStack newTicket = new ItemStack(ticketItem);
	NBTTagCompound tag = ItemUtils.getItemTag(newTicket);
	tag.setString("owner", owner.getValue());
	tag.setString("dest", destination);

	if (output == null) {
		inventory.setInventorySlotContents(SLOT_OUTPUT, newTicket);
	} else if (ItemStack.areItemStackTagsEqual(output, newTicket) && output.isItemEqual(newTicket) && output.stackSize + amount <= output.getMaxStackSize()) {
		output.stackSize += amount;
	} else throw new IllegalArgumentException("No place in output slot");

	inventory.decrStackSize(SLOT_PAPER, amount);
	inventory.decrStackSize(SLOT_INK, amount);

	worldObj.playSoundEffect(xCoord + 0.5D, yCoord + 0.5D, zCoord + 0.5D, "openperipheraladdons:ticketmachine", 0.3F, 0.6F);
	sync();

	return true;
}
 
Example #7
Source File: GuiCaptureControl.java    From OpenPeripheral-Addons with MIT License 5 votes vote down vote up
@ScriptCallable(description = "Set background on capture mode screen")
public void setBackground(@Arg(name = "background") int background,
		@Optionals @Arg(name = "alpha") Integer alpha) {
	EntityPlayer player = getPlayer();
	final int a = alpha != null? (alpha << 24) : 0x2A000000;
	new GlassesChangeBackgroundEvent(guid, background & 0x00FFFFFF | a).sendToPlayer(player);
}
 
Example #8
Source File: IDrawableFactory.java    From OpenPeripheral-Addons with MIT License 5 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.OBJECT, description = "Add a quad")
public Drawable addQuad(
		@Arg(name = "p1", description = "Coordinates of first point") Point2d p1,
		@Arg(name = "p2", description = "Coordinates of second point") Point2d p2,
		@Arg(name = "p3", description = "Coordinates of third point") Point2d p3,
		@Arg(name = "p4", description = "Coordinates of fourth point") Point2d p4,
		@Optionals @Arg(name = "color", description = "The color of the line") Integer color,
		@Arg(name = "opacity", description = "The opacity of the line (from 0 to 1)") Float opacity);
 
Example #9
Source File: IDrawableFactory.java    From OpenPeripheral-Addons with MIT License 5 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.OBJECT, description = "Add a triangle")
public Drawable addTriangle(
		@Arg(name = "p1", description = "Coordinates of first point") Point2d p1,
		@Arg(name = "p2", description = "Coordinates of second point") Point2d p2,
		@Arg(name = "p3", description = "Coordinates of third point") Point2d p3,
		@Optionals @Arg(name = "color", description = "The color of the line") Integer color,
		@Arg(name = "opacity", description = "The opacity of the line (from 0 to 1)") Float opacity);
 
Example #10
Source File: IDrawableFactory.java    From OpenPeripheral-Addons with MIT License 5 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.OBJECT, description = "Add a new box to the screen")
public Drawable addBox(
		@Arg(name = "x", description = "The x position from the top left") float x,
		@Arg(name = "y", description = "The y position from the top left") float y,
		@Arg(name = "width", description = "The width of the box") float width,
		@Arg(name = "height", description = "The height of the box") float height,
		@Optionals @Arg(name = "color", description = "The color of the box") Integer color,
		@Arg(name = "opacity", description = "The opacity of the box (from 0 to 1)") Float opacity);
 
Example #11
Source File: TileEntitySelector.java    From OpenPeripheral-Addons with MIT License 5 votes vote down vote up
@ScriptCallable(description = "Set the item being displayed on a specific slot")
public void setSlot(
		@Arg(name = "slot", description = "The slot you want to modify") Index slot,
		@Optionals @Arg(name = "item", description = "The item you want to display. nil to set empty") ItemStack stack) {

	slot.checkElementIndex("slot id", 10);

	if (stack != null) stack.stackSize = 1;

	this.slots[slot.value].set(stack);
	sync();
}
 
Example #12
Source File: AdapterNoteBlock.java    From OpenPeripheral-Integration with MIT License 5 votes vote down vote up
@Asynchronous
@ScriptCallable(description = "Plays a minecraft sound")
public void playSound(TileEntityNote noteblock,
		@Arg(name = "sound", description = "The identifier for the sound") String name,
		@Arg(name = "pitch", description = "The pitch from 0 to 1") float pitch,
		@Arg(name = "volume", description = "The volume from 0 to 1") float volume,
		@Optionals @Arg(name = "x", description = "X coordinate od sound (relative to block)") Double dx,
		@Arg(name = "y", description = "Y coordinate of sound (relative to block)") Double dy,
		@Arg(name = "z", description = "Z coordinate of sound (relative to block)") Double dz) {
	noteblock.getWorldObj().playSoundEffect(
			noteblock.xCoord + 0.5 + Objects.firstNonNull(dx, 0.0),
			noteblock.yCoord + 0.5 + Objects.firstNonNull(dy, 0.0),
			noteblock.zCoord + 0.5 + Objects.firstNonNull(dz, 0.0),
			name, volume, pitch);
}
 
Example #13
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 #14
Source File: AdapterInventory.java    From OpenPeripheral-Integration with MIT License 5 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.OBJECT, description = "Get details of an item in a particular slot")
public Object getStackInSlot(IInventory target,
		@Arg(name = "slotNumber", description = "Slot number") Index slot,
		@Optionals @Arg(name = "proxy", description = "If true, method will return proxy instead of computing whole table") Boolean proxy) {
	IInventory inventory = InventoryUtils.getInventory(target);
	slot.checkElementIndex("slot id", inventory.getSizeInventory());
	ItemStack stack = inventory.getStackInSlot(slot.value);
	return proxy == Boolean.TRUE? OpcAccess.itemStackMetaBuilder.createProxy(stack) : stack;
}
 
Example #15
Source File: AdapterWorldInventory.java    From OpenPeripheral-Integration with MIT License 5 votes vote down vote up
@Alias("pushItemIntoSlot")
@ScriptCallable(returnTypes = ReturnType.NUMBER, description = "Push an item from the current inventory into pipe or slot on the other inventory. Returns the amount of items moved")
public int pushItem(IInventory target,
		@Arg(name = "direction", description = "The direction of the other inventory") ForgeDirection direction,
		@Arg(name = "slot", description = "The slot in the current inventory that you're pushing from") Index fromSlot,
		@Optionals @Arg(name = "maxAmount", description = "The maximum amount of items you want to push") Integer maxAmount,
		@Arg(name = "intoSlot", description = "The slot in the other inventory that you want to push into (ignored when target is pipe") Index intoSlot) {

	final TileEntity otherTarget = getNeighborTarget(target, direction);
	Preconditions.checkNotNull(otherTarget, "Other target not found");
	final IInventory thisInventory = InventoryUtils.getInventory(target);

	if (maxAmount == null) maxAmount = 64;
	if (intoSlot == null) intoSlot = ANY_SLOT_INDEX;

	checkSlotId(thisInventory, fromSlot, "input");

	final int amount;
	if (otherTarget instanceof IInventory) {
		final IInventory otherInventory = InventoryUtils.getInventory((IInventory)otherTarget);
		checkSlotId(otherInventory, intoSlot, "output");
		amount = ItemDistribution.moveItemInto(thisInventory, fromSlot.value, otherInventory, intoSlot.value, maxAmount, direction, true);
		if (amount > 0) otherInventory.markDirty();
	} else {
		final CustomSinks.ICustomSink adapter = CustomSinks.createSink(otherTarget);
		if (adapter == null) throw new IllegalArgumentException("Invalid target");
		amount = ItemDistribution.moveItemInto(thisInventory, fromSlot.value, adapter, maxAmount, direction, true);
	}

	if (amount > 0) thisInventory.markDirty();
	return amount;
}
 
Example #16
Source File: AdapterWorldInventory.java    From OpenPeripheral-Integration with MIT License 5 votes vote down vote up
@Alias("pullItemIntoSlot")
@ScriptCallable(returnTypes = ReturnType.NUMBER, description = "Pull an item from a slot in another inventory into a slot in this one. Returns the amount of items moved")
public int pullItem(IInventory target,
		@Arg(name = "direction", description = "The direction of the other inventory") ForgeDirection direction,
		@Arg(name = "slot", description = "The slot in the OTHER inventory that you're pulling from") Index fromSlot,
		@Optionals @Arg(name = "maxAmount", description = "The maximum amount of items you want to pull") Integer maxAmount,
		@Arg(name = "intoSlot", description = "The slot in the current inventory that you want to pull into") Index intoSlot) {

	final TileEntity otherTarget = getNeighborTarget(target, direction);
	if (otherTarget == null) throw new IllegalArgumentException("Other block not found");
	if (!(otherTarget instanceof IInventory)) throw new IllegalArgumentException("Other block is not inventory");

	final IInventory otherInventory = InventoryUtils.getInventory((IInventory)otherTarget);
	final IInventory thisInventory = InventoryUtils.getInventory(target);

	if (otherTarget == target) return 0;
	if (maxAmount == null) maxAmount = 64;
	if (intoSlot == null) intoSlot = ANY_SLOT_INDEX;

	checkSlotId(otherInventory, fromSlot, "input");
	checkSlotId(thisInventory, intoSlot, "output");

	final int amount = ItemDistribution.moveItemInto(otherInventory, fromSlot.value, thisInventory, intoSlot.value, maxAmount, direction.getOpposite(), true);
	if (amount > 0) {
		thisInventory.markDirty();
		otherInventory.markDirty();
	}
	return amount;

}
 
Example #17
Source File: AdapterNetwork.java    From OpenPeripheral-Integration with MIT License 5 votes vote down vote up
@ScriptCallable(description = "Retrieves details about the specified item from the ME Network.", returnTypes = ReturnType.OBJECT)
public Object getItemDetail(IGridHost host,
		@Arg(name = "item", description = "Details of the item you are looking for") ItemFingerprint needle,
		@Optionals @Arg(name = "proxy", description = "If false, method will compute whole table, instead of returning proxy") Boolean proxy) {
	final IItemList<IAEItemStack> items = getStorageGrid(host).getItemInventory().getStorageList();
	final IAEItemStack stack = findStack(items, needle);
	if (stack == null) return null;
	ItemStack vanillaStack = stack.getItemStack();
	return proxy != Boolean.FALSE? OpcAccess.itemStackMetaBuilder.createProxy(vanillaStack) : vanillaStack;
}
 
Example #18
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 #19
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 #20
Source File: AdapterFluidHandler.java    From OpenPeripheral-Integration with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.TABLE, description = "A table of tanks will be returned, each with a table of information")
public FluidTankInfo[] getTankInfo(IFluidHandler fluidHandler,
		@Optionals @Arg(name = "direction", description = "The internal direction of the tank. If you're not sure, use 'unknown' (north, south, east, west, up, down or unknown)") ForgeDirection direction) {
	return fluidHandler.getTankInfo(direction != null? direction : ForgeDirection.UNKNOWN);
}
 
Example #21
Source File: MethodDescriptionTest.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.BOOLEAN)
public boolean test(@Optionals B target) {
	return true;
}
 
Example #22
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 #23
Source File: MethodDescriptionTest.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.STRING)
public String test(B target, @Optionals @Arg(name = "a") String b) {
	return "A";
}
 
Example #24
Source File: MethodDescriptionTest.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.STRING)
public String test(B target, @Arg(name = "a") int a, @Optionals @Arg(name = "b") String b) {
	return "A";
}
 
Example #25
Source File: MethodDescriptionTest.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.STRING)
public String test(@Optionals @Arg(name = "a") Integer... a) {
	return Arrays.toString(a);
}
 
Example #26
Source File: MethodDescriptionTest.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.STRING)
public String test(@Optionals @Arg(name = "a") Integer a) {
	return "" + a;
}
 
Example #27
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 #28
Source File: AdapterEnergyProvider.java    From OpenPeripheral-Integration with MIT License 4 votes vote down vote up
@ScriptCallable(description = "Get the energy stored in the machine.", returnTypes = ReturnType.NUMBER)
public int getEnergyStored(IEnergyProvider tileEntity,
		@Optionals @Arg(name = "side", description = "The direction you are interested in. (north, south, east, west, up or down)") ForgeDirection side) {
	return tileEntity.getEnergyStored(side != null? side : ForgeDirection.UNKNOWN);
}
 
Example #29
Source File: AdapterEnergyProvider.java    From OpenPeripheral-Integration with MIT License 4 votes vote down vote up
@ScriptCallable(description = "Get the max energy stored in the machine.", returnTypes = ReturnType.NUMBER)
public int getMaxEnergyStored(IEnergyProvider tileEntity,
		@Optionals @Arg(name = "side", description = "The direction you are interested in. (north, south, east, west, up or down)") ForgeDirection side) {
	return tileEntity.getMaxEnergyStored(side != null? side : ForgeDirection.UNKNOWN);
}
 
Example #30
Source File: AdapterEnergyReceiver.java    From OpenPeripheral-Integration with MIT License 4 votes vote down vote up
@ScriptCallable(description = "Get the energy stored in the machine.", returnTypes = ReturnType.NUMBER)
public int getEnergyStored(IEnergyReceiver tileEntity,
		@Optionals @Arg(name = "side", description = "The direction you are interested in. (north, south, east, west, up or down)") ForgeDirection side) {
	return tileEntity.getEnergyStored(side != null? side : ForgeDirection.UNKNOWN);
}