Jump to content

eat anything edible in inventory


fre024

Recommended Posts

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 by fre024
Link to comment
Share on other sites

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 by Pseudo
  • Like 2
Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

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