openmods.utils.InventoryUtils Java Examples

The following examples show how to use openmods.utils.InventoryUtils. 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: AdapterInventory.java    From OpenPeripheral-Integration with MIT License 6 votes vote down vote up
@Asynchronous(false)
@ScriptCallable(description = "Condense and tidy the stacks in an inventory")
public void condenseItems(IInventory target) {
	IInventory inventory = InventoryUtils.getInventory(target);
	List<ItemStack> stacks = Lists.newArrayList();
	for (int i = 0; i < inventory.getSizeInventory(); i++) {
		ItemStack sta = inventory.getStackInSlot(i);
		if (sta != null) stacks.add(sta.copy());
		inventory.setInventorySlotContents(i, null);
	}

	for (ItemStack stack : stacks)
		ItemDistribution.insertItemIntoInventory(inventory, stack, ForgeDirection.UNKNOWN, ANY_SLOT);

	target.markDirty();
}
 
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: TerminalAddonRecipe.java    From OpenPeripheral-Addons with MIT License 6 votes vote down vote up
@Override
public boolean matches(InventoryCrafting inv, World world) {
	boolean glassesFound = false;
	boolean targetFound = false;

	for (ItemStack itemStack : InventoryUtils.asIterable(inv)) {
		if (itemStack != null) {
			if (itemStack.getItem() instanceof ItemGlasses) {
				if (glassesFound) return false;
				glassesFound = true;
			} else if (isSuitableItem(itemStack)) {
				if (targetFound) return false;
				targetFound = true;
			} else return false;
		}
	}

	return glassesFound && targetFound;
}
 
Example #4
Source File: UseItemAction.java    From OpenModsLib with MIT License 6 votes vote down vote up
@Override
@Nonnull
public ItemStack usePlayer(OpenModsFakePlayer player) {
	player.inventory.currentItem = 0;
	player.inventory.setInventorySlotContents(0, stack);

	player.setPositionAndRotation(playerPos.x, playerPos.y, playerPos.z, yaw, pitch);

	player.rightClick(
			stack,
			new BlockPos(clickPos.x, clickPos.y, clickPos.z),
			hand,
			side,
			(float)hitPos.x, (float)hitPos.y, (float)hitPos.z);

	return InventoryUtils.returnItem(player.inventory.getCurrentItem());
}
 
Example #5
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 #6
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 #7
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 #8
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 #9
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 #10
Source File: TerminalAddonRecipe.java    From OpenPeripheral-Addons with MIT License 5 votes vote down vote up
@Override
public ItemStack getCraftingResult(InventoryCrafting inv) {
	ItemStack targetStack = null;
	ItemStack glassesStack = null;

	for (ItemStack itemStack : InventoryUtils.asIterable(inv)) {
		if (itemStack != null) {
			if (itemStack.getItem() instanceof ItemGlasses) {
				if (glassesStack != null) return null;
				glassesStack = itemStack;
			} else if (isSuitableItem(itemStack)) {
				if (targetStack != null) return null;
				targetStack = itemStack;
			} else return null;
		}
	}

	if (glassesStack == null || targetStack == null) return null;

	final ItemGlasses glassesItem = (ItemGlasses)glassesStack.getItem();
	Optional<Long> guid = Optional.fromNullable(glassesItem.getTerminalGuid(glassesStack));

	final ItemStack result = targetStack.copy();
	NbtGuidProviders.setTerminalGuid(result, guid);

	return result;
}
 
Example #11
Source File: AdapterWritingDesk.java    From OpenPeripheral-Integration with MIT License 4 votes vote down vote up
@ScriptCallable(description = "Swap notebook slots")
public void swapNotebookPages(Object desk, @Arg(name = "deskSlot") Index deskSlot, @Arg(name = "from") Index from, @Arg(name = "to") Index to) {
	InventoryUtils.swapStacks(createInventoryWrapper(desk, deskSlot), from.value, to.value);
}
 
Example #12
Source File: AdapterInterface.java    From OpenPeripheral-Integration with MIT License 4 votes vote down vote up
private static IInventory getNeighborInventory(Object te, ForgeDirection dir) {
	TileEntity tileEntity = (TileEntity)te;
	return InventoryUtils.getInventory(tileEntity.getWorldObj(), tileEntity.xCoord, tileEntity.yCoord, tileEntity.zCoord, dir);
}
 
Example #13
Source File: InventoryMetaProvider.java    From OpenPeripheral-Integration with MIT License 4 votes vote down vote up
public static Map<Integer, IMetaProviderProxy> wrapToProxyTable(IInventory target) {
	Map<Integer, IMetaProviderProxy> result = Maps.newHashMap();
	for (Map.Entry<Integer, ItemStack> e : InventoryUtils.getAllItems(target).entrySet())
		result.put(e.getKey() + 1, OpcAccess.itemStackMetaBuilder.createProxy(e.getValue()));
	return result;
}
 
Example #14
Source File: AdapterInventory.java    From OpenPeripheral-Integration with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.STRING, description = "Get the name of this inventory")
public String getInventoryName(IInventory target) {
	IInventory inventory = InventoryUtils.getInventory(target);
	return inventory != null? inventory.getInventoryName() : null;
}
 
Example #15
Source File: AdapterInventory.java    From OpenPeripheral-Integration with MIT License 4 votes vote down vote up
@ScriptCallable(returnTypes = ReturnType.NUMBER, description = "Get the size of this inventory")
public int getInventorySize(IInventory target) {
	IInventory inventory = InventoryUtils.getInventory(target);
	return inventory != null? inventory.getSizeInventory() : 0;
}