GitXplorerGitXplorer
e

item-inventory

public
3 stars
0 forks
0 issues

Commits

List of commits on branch master.
Unverified
51d442d5f95f4b595b7da2ed589747906149c9e3

Fixed README dependencies

eemilyploszaj committed 5 years ago
Unverified
f35301713676914d005403f2a0bbd01737806e67

Added shulker box support

eemilyploszaj committed 5 years ago
Unverified
a1a4bde6f5e3b1e5fdf8110d7b9e8489962768d9

Added wrapper for BlockItems

eemilyploszaj committed 5 years ago
Unverified
4567b8abd463a3d0eb148d5dba5a42d8338cd0fa

README changes

eemilyploszaj committed 5 years ago
Unverified
a453cb9963ea98325f984ca127f602d694109a97

Initial commit

eemilyploszaj committed 5 years ago

README

The README file for this repository.

Item Inventory

An API for mod developers who want to read the inventories stored inside items and developers who want to provide a way for their items with inventories to be read.

Developers

To add Item Inventory to your project you need to add jitpack to your repositories in your build.gradle

repositories {
	maven {
		url = "https://jitpack.io"
	}
}

And then to add Item Inventory you add it as a dependency in your build.gradle

dependencies {
	modImplementation "com.github.emilyploszaj:item-inventory:v1.1.0"
	include "com.github.emilyploszaj:item-inventory:v1.1.0"
}

Implementing an ItemInventory

Here's an example of an inventory item that implements ItemInventory.

public class MyBackpack extends Item implements ItemInventory {

	//Normal item code

	@Override
	public int getInvSize(ItemStack invItem) {
		//Return the amount of slots your inventory has
	}

	@Override
	public ItemStack getStack(ItemStack invItem, int index) {
		//Deserialize your inventory and return an ItemStack
		//For instance, read the nbt storing the items and return the relevant one
	}

	@Override
	public void setStack(ItemStack invItem, int index, ItemStack stack) {
		//Check to make sure the provided variables are valid
		if (canInsert(invItem, index, stack)) {
			//Serialize the change and store it back in your format
		}
	}

	@Override
	public default boolean canInsert(ItemStack invItem, int index, ItemStack stack) {
		//Only allow unstackable items into the inventory
		return canTake(invItem, index) && stack.getItem().getMaxCount() == 1;
	}
}

Using the API

Here's an example of a recursive function that returns how many items are stored in the provided list of items, including those inside of those items' inventories (excluding empty slots) and the inventories of the items inside items with inventories etc.

public int getAmountOfItems(List<ItemStack> items) {
	int amount = 0;
	for (int i = 0; i < items.size(); i++) {
		ItemStack stack = items.get(i);

		if (!stack.isEmpty()) {
			amount++;

			if (stack.getItem() instanceof ItemInventory) {
				List<ItemStack> innerStacks = new ArrayList<ItemStack>();
				ItemInventory inv = (ItemInventory) stack.getItem();

				for (int j = 0; j < inv.getInvSize; j++) {
					innerStacks.add(inv.getStack(stack, j));
				}
				amount += getAmountOfItems(innerStacks);
			}
		}
	}
	return amount;
}