sudoinit6 Posted May 1, 2020 Share Posted May 1, 2020 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"? Quote Link to comment Share on other sites More sharing options...
Gunman Posted May 1, 2020 Share Posted May 1, 2020 Inventory slots start at 0 not 1. Quote Link to comment Share on other sites More sharing options...
sudoinit6 Posted May 1, 2020 Author Share Posted May 1, 2020 Just now, Gunman said: Inventory slots start at 0 not 1. Well, that would explain why it is skipping coins, thanks a lot for that! Now about breaking the loop when the item is null? Quote Link to comment Share on other sites More sharing options...
Gunman Posted May 1, 2020 Share Posted May 1, 2020 10 minutes ago, sudoinit6 said: Well, that would explain why it is skipping coins, thanks a lot for that! Now about breaking the loop when the item is null? break; Quote Link to comment Share on other sites More sharing options...
sudoinit6 Posted May 1, 2020 Author Share Posted May 1, 2020 Just now, Gunman said: break; I want it to NOT break, it is breaking and starting over when the item is null. Quote Link to comment Share on other sites More sharing options...
Gunman Posted May 1, 2020 Share Posted May 1, 2020 (edited) 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 May 1, 2020 by Gunman Quote Link to comment Share on other sites More sharing options...
sudoinit6 Posted May 1, 2020 Author Share Posted May 1, 2020 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? Quote Link to comment Share on other sites More sharing options...
Gunman Posted May 1, 2020 Share Posted May 1, 2020 4 minutes ago, sudoinit6 said: 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? https://pastebin.com/5MCdcRbv 1 Quote Link to comment Share on other sites More sharing options...
sudoinit6 Posted May 1, 2020 Author Share Posted May 1, 2020 4 minutes ago, Gunman said: https://pastebin.com/5MCdcRbv Yeah, that looks smarter then my idea. Thanks very much. 1 Quote Link to comment Share on other sites More sharing options...
Rick Posted May 1, 2020 Share Posted May 1, 2020 28 minutes ago, Gunman said: https://pastebin.com/5MCdcRbv stop spoiling him dad Quote Link to comment Share on other sites More sharing options...
Khaleesi Posted May 1, 2020 Share Posted May 1, 2020 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? Quote Link to comment Share on other sites More sharing options...
sudoinit6 Posted May 2, 2020 Author Share Posted May 2, 2020 (edited) 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 May 2, 2020 by sudoinit6 Quote Link to comment Share on other sites More sharing options...
Khaleesi Posted May 2, 2020 Share Posted May 2, 2020 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 Quote Link to comment Share on other sites More sharing options...