fre024 Posted June 3, 2014 Posted June 3, 2014 (edited) Why is this not working, it only eats the item in the first inventory slot. public boolean eat() { if (!foodInInv) { Mylog("No food in inventory."); return false; } Item[] invItems = getInventory().getItems(); if (invItems != null) { for (int i = 0; i < 28; i++) { Mylog(Integer.toString(i)); if (invItems[i].getName() != null) { Mylog("test"); String[] itemActions = invItems[i].getDefinition() .getActions(); Mylog("test 1"); for (int ii = 0; ii < itemActions.length; ii++) { Mylog("test 2"); if (itemActions[ii].equalsIgnoreCase("Eat")) { Mylog("test 3"); if (invItems[i].interact("Eat")){ return true; } Mylog("test 4"); } Mylog("test 5"); } } Mylog("No item at this inv slot: " + i); } } foodInInv = false; Mylog("No food in inventory."); return false; } this is the log i get: [iNFO][bot #1][06/03 01:54:29 PM]: 0 [iNFO][bot #1][06/03 01:54:29 PM]: test [iNFO][bot #1][06/03 01:54:29 PM]: test 1 [iNFO][bot #1][06/03 01:54:29 PM]: test 2 Edited June 3, 2014 by fre024
Ziy Posted June 3, 2014 Posted June 3, 2014 It's because you return after eating an item, if you want to return true if at least one item was eaten then hold it in a boolean variable until the loop has finished then return the result
Pseudo Posted June 3, 2014 Posted June 3, 2014 (edited) There's no need to complicate it as much as you have either. Something like: private boolean eat() { for (Item i : getInventory().getItems()) { if (i != null && Arrays.asList(i.getDefinition().getActions()).contains("Eat")) return getInventory().interact(i.getId(), "Eat"); } return false; } Should work. Edited June 3, 2014 by Pseudo 2
Apaec Posted June 3, 2014 Posted June 3, 2014 There's no need to complicate it as much as you have either. Something like: private boolean eat() { for (Item i : getInventory().getItems()) { if (i != null && Arrays.asList(i.getDefinition().getActions()).contains("Eat")) return getInventory().interact(i.getId(), "Eat"); } return false; } Should work. This is all you need ^ Also, press ctrl+shift+f