Java Code Examples for openperipheral.api.helpers.Index#checkElementIndex()

The following examples show how to use openperipheral.api.helpers.Index#checkElementIndex() . 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.BOOLEAN, description = "Pull an item from the target inventory into any slot in the current one. Returns true if item was consumed")
public boolean pullNotebookPage(TileEntity desk,
		@Arg(name = "deskSlot") Index deskSlot,
		@Arg(name = "direction", description = "The direction of the other inventory)") ForgeDirection direction,
		@Arg(name = "fromSlot", description = "The slot in the other inventory that you're pulling from") Index fromSlot) {

	IInventory inv = getTargetTile(desk, direction);

	fromSlot.checkElementIndex("input slot", inv.getSizeInventory());

	ItemStack stack = inv.getStackInSlot(fromSlot.value);
	Preconditions.checkNotNull(stack, "Other inventory empty");

	final NotebookWrapper wrapper = createInventoryWrapper(desk, deskSlot);

	ItemStack added = wrapper.addPage(stack);
	inv.setInventorySlotContents(fromSlot.value, added);
	inv.markDirty();
	return added == null;
}
 
Example 2
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 3
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 4
Source File: AdapterInventory.java    From OpenPeripheral-Integration with MIT License 5 votes vote down vote up
@Asynchronous(false)
@ScriptCallable(description = "Destroy a stack")
public void destroyStack(IInventory target,
		@Arg(name = "slotNumber", description = "The slot number") Index slot) {
	IInventory inventory = InventoryUtils.getInventory(target);
	slot.checkElementIndex("slot id", inventory.getSizeInventory());
	inventory.setInventorySlotContents(slot.value, null);
	inventory.markDirty();
}
 
Example 5
Source File: AdapterSign.java    From OpenPeripheral-Integration with MIT License 5 votes vote down vote up
@ScriptCallable(description = "Sets the text on the sign")
public void setLine(TileEntitySign sign,
		@Arg(name = "line", description = "The line number to set the text on the sign") Index line,
		@Arg(name = "text", description = "The text to display on the sign") String text) {
	line.checkElementIndex("line", sign.signText.length);
	setLine(sign, line.value, text);
	updateSign(sign);
}
 
Example 6
Source File: AdapterSign.java    From OpenPeripheral-Integration with MIT License 5 votes vote down vote up
@Asynchronous
@ScriptCallable(returnTypes = ReturnType.STRING, description = "Gets the text from the supplied line of the sign")
public String getLine(TileEntitySign sign,
		@Arg(name = "line", description = "The line number to get from the sign") Index line) {
	line.checkElementIndex("line", sign.signText.length);
	return sign.signText[line.value];
}
 
Example 7
Source File: TileEntitySelector.java    From OpenPeripheral-Addons with MIT License 5 votes vote down vote up
@Asynchronous
@ScriptCallable(description = "Get the item currently being displayed in a specific slot", returnTypes = ReturnType.TABLE, name = "getSlot")
public ItemStack getSlotOneBased(@Arg(name = "slot", description = "The slot you want to get details about") Index slot) {

	slot.checkElementIndex("slot id", 10);
	return slots[slot.value].get();
}
 
Example 8
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 9
Source File: NotebookWrapper.java    From OpenPeripheral-Integration with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.TABLE)
public ItemStack getPageFromSlot(@Arg(name = "slot") Index slot) {
	List<ItemStack> pages = getPages();
	slot.checkElementIndex("slot", pages.size());
	return pages.get(slot.value);
}
 
Example 10
Source File: AdapterWorldInventory.java    From OpenPeripheral-Integration with MIT License 4 votes vote down vote up
private static void checkSlotId(IInventory inventory, Index slot, String name) {
	Preconditions.checkNotNull(inventory, "Invalid inventory");
	if (slot.value != ANY_SLOT) slot.checkElementIndex(name + " slot id", inventory.getSizeInventory());
}