Jump to content

Items and Inventory.


sudoinit6

Recommended Posts

Scenario: Our backpack contains coins in slot 1, Diamond in slot 2, Gold necklace in slot 3, empty other than that.

 

I am trying to loop through the backpack slots and do something if the slot contains coins, something else if the slot contains something else and just increment the counter if the item is null.

 

Here is my code:

 

int x = 1;
		totalVal = 0;
		while (x < 29) {
			Item curItem = api.getInventory().getItemInSlot(x);
			api.log("Current item is " + curItem);
			String valueItemName = curItem.getName();
			api.log("valueItemName is " + valueItemName);
			if (valueItemName == "Coins") {
				api.log("Coins are a match");
				x = x + 1;

				MethodProvider.sleep(rnd3);
			}else if (curItem != null && valueItemName != "Coins") {
				buyItemId = 0;
				for(Map.Entry entry: stuff.entrySet()){
		            if(valueItemName.equals(entry.getValue())){
		                buyItemId = (int) entry.getKey();
		                break; //breaking because its one to one map
		            }
		        }
				api.log("item is " + valueItemName);
				api.log("item id is " + buyItemId);
				
				x = x + 1;
				api.log("Item is not null, adding one to x. x now equals " + x);
				MethodProvider.sleep(rnd3);
			} else {
				x = x + 1;
				api.log("Item is null, adding one to x. x now equals " + x);
				MethodProvider.sleep(rnd3);
			}
	
			
			
		}

The only one that seems to be working is the second check, I am getting nothing on "Coins" and nothing on null. Here is the log:

 

[INFO][Bot #1][04/30 10:49:29 PM]: Current item is Item id: 1602, Name: Diamond, Amount: 50
[INFO][Bot #1][04/30 10:49:29 PM]: valueItemName is Diamond
[INFO][Bot #1][04/30 10:49:29 PM]: item is Diamond
[INFO][Bot #1][04/30 10:49:29 PM]: item id is 1601
[INFO][Bot #1][04/30 10:49:29 PM]: Item is not null, adding one to x. x now equals 2
[INFO][Bot #1][04/30 10:49:37 PM]: Current item is Item id: 1655, Name: Gold necklace, Amount: 1080
[INFO][Bot #1][04/30 10:49:37 PM]: valueItemName is Gold necklace
[INFO][Bot #1][04/30 10:49:37 PM]: item is Gold necklace
[INFO][Bot #1][04/30 10:49:37 PM]: item id is 1654
[INFO][Bot #1][04/30 10:49:37 PM]: Item is not null, adding one to x. x now equals 3
[INFO][Bot #1][04/30 10:49:45 PM]: Current item is null

Then the loop breaks and starts over. Are "Coins" somehow a different item? Why wont this fall through to the third "if"?

 

Link to comment
Share on other sites

5 minutes ago, sudoinit6 said:

I want it to NOT break, it is breaking and starting over when the item is null.

String valueItemName = curItem.getName();

Stuff like that will cause an NPE and break it since curItem is null. Null check bro

EDIT: What you should do is null check curItem after declaring it so you won't have to null check it again.

Edited by Gunman
Link to comment
Share on other sites

3 minutes ago, Gunman said:

String valueItemName = curItem.getName();

Stuff like that will cause an NPE and break it since curItem is null. Null check bro

EDIT: What you should do is null check curItem after declaring it so you won't have to null check it again.

OK, that makes sense. I am not a very experienced coder. So how do I make it do something if it IS null? Oh, I just had a thought. Check the slot for empty, if not empty do stuff, if it is empty then increment the counter. Does that make sense?

 

Link to comment
Share on other sites

3 hours ago, Khaleesi said:

I'm not sure what you are trying to do here, but there are probably better ways of doing this.
What's the initial idea behind this?

Oh I am SURE there are better ways to do it, but here is what I am trying to do:

Get the value of each item in inventory add the number of coins and print out the total value.

I got it working with the above help, (had to tweak the pastebin a little) but you guys know way more than I do so I am open to suggestions to improve.

 

 

Edited by sudoinit6
Link to comment
Share on other sites

4 hours ago, sudoinit6 said:

Oh I am SURE there are better ways to do it, but here is what I am trying to do:

Get the value of each item in inventory add the number of coins and print out the total value.

I got it working with the above help, (had to tweak the pastebin a little) but you guys know way more than I do so I am open to suggestions to improve.

 

 

Oh I see :)
That's good, if you need anything else you can always ask me

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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