Jump to content

Returning an array of Inventory Items?


Recommended Posts

Posted

Hello, once again I come with a question but this time regarding the Inventory.

 

I was surfing the API when I wanted to find out if it was possible to return the id of an item in a specific slot OR return the entire array of items held in ones inventory.

 

Thanks for any help, I have found the Item[] getItems() and then getSlot(). If somone could help clarify the use for these and how one could use them for an array that would be great.

 

Thanks again OSBot community.

Posted

Hello, once again I come with a question but this time regarding the Inventory.

 

I was surfing the API when I wanted to find out if it was possible to return the id of an item in a specific slot OR return the entire array of items held in ones inventory.

 

Thanks for any help, I have found the Item[] getItems() and then getSlot(). If somone could help clarify the use for these and how one could use them for an array that would be great.

 

Thanks again OSBot community.

getItems(); returns an array of type Item.

 

what do you plan on doing?

 

 

Precise

Posted

getItems(); returns an array of type Item.

 

what do you plan on doing?

 

 

Precise

 

I basically plan on grabbing all the ids from the inventory and loading it into an array in which if the person needs to stock up with that exact load out they started with, it will withdraw that same load out by going through the array.

 

The getItems() is an Items array, how would I turn that into an int.

Posted

I basically plan on grabbing all the ids from the inventory and loading it into an array in which if the person needs to stock up with that exact load out they started with, it will withdraw that same load out by going through the array.

 

The getItems() is an Items array, how would I turn that into an int.

 

may i ask, do you know the basics of java?

anyways:

int[] itemIds = new int[getInventory().getItems().length-1];

for (int i = 0; i < itemIds.length; i++) 
    itemIds[i] = getInventory().getItems()[i].getId();
  • Like 1
Posted

may i ask, do you know the basics of java?

 

Yes, I've been coding in Java for around 5 years. Started on old clients but they had methods that allowed you to select the slot and return that id which would be easier for creating the array of items.

 

the getItems() like I said are held by the Items[] array which are held by inventory.

 

Like I was my question is, the Items array. If I did         Item[] temp = inventory.getItems();

then returned the size of temp and put it into a loop, would it be spitting out integers or more?

 

may i ask, do you know the basics of java?

anyways:

int[] itemIds = new int[getInventory().getItems().length-1];

for (int i = 0; i < itemIds.length; i++) 
    itemIds[i] = getInventory().getItems()[i].getId();

 

Also thanks for that, still reviewing this api. A bit more lengthy then some I have seen but it's a lot more accurate.

  • Like 1
Posted

So confusing on what you want..so I'll do my best.

 

Usage for grabbing the players inventory as an Item array:

inventory.getItems()

 

Since you want to store their initial setup, I'd do something like:

initial = inventory.getItems()
upon startup

 

To grab the amount of an item object (returned as an integer), simply do:

item.getAmount()
 

 

Put it all together, you should end up with something like this:

for (int i = 0; i < inventory.getItems().length; i++) {
Item current = inventory.getItems()[i]; //the current item in the respective slot
Item in = initial[i];  //the initial item in the respective slot
//probably want to skip iteration if the item is null but idk
if (in.getAmount() != current.getAmount()) { //if the initial amount is different to the current amount...
bank.withdraw(current.getId(), in.getAmount() - current.getAmount()); //withdraw the necessary amount
}
}

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...