Kalispel Posted December 21, 2015 Posted December 21, 2015 (edited) Hi. I'm new to scripting and coding in general and had a question about how to use items on each other. I can't get the script to use one item on another (let alone select the knife in the first place). This is the code i have for making longbows in my fletching script: case FLETCH: if(myPlayer().isAnimating()) { antiBan(); } else if(!getWidgets().get(305, 4).isVisible()) { inventory.getItem("Knife").interact("Use"); inventory.getItem("Logs").interact("Use"); } else { getWidgets().get(305,4).interact("Make 10"); } Edited December 21, 2015 by Dora
Token Posted December 21, 2015 Posted December 21, 2015 You can simply use inventory.getItem("Knife").interact(); inventory.getItem("Logs").interact(); But the way you wrote it may also work. Don't forget to delay the 2nd interaction until the first one is completed otherwise your script will mess up. Also it's not on the topic but just so you know, myPlayer().isAnimating() is not what you want to use in any of your scripts. Fletching animation takes 3 or 4 ticks then 1 tick THE PLAYER WILL NOT BE ANIMATING and then start again, so your code will have bugs there. Also null checking the widget before checking its visibility might help.
Kalispel Posted December 21, 2015 Author Posted December 21, 2015 You can simply use inventory.getItem("Knife").interact(); inventory.getItem("Logs").interact(); But the way you wrote it may also work. Don't forget to delay the 2nd interaction until the first one is completed otherwise your script will mess up. Also it's not on the topic but just so you know, myPlayer().isAnimating() is not what you want to use in any of your scripts. Fletching animation takes 3 or 4 ticks then 1 tick THE PLAYER WILL NOT BE ANIMATING and then start again, so your code will have bugs there. Also null checking the widget before checking its visibility might help. Thanks for the tips!
Kalispel Posted December 21, 2015 Author Posted December 21, 2015 You can simply use inventory.getItem("Knife").interact(); inventory.getItem("Logs").interact(); But the way you wrote it may also work. Don't forget to delay the 2nd interaction until the first one is completed otherwise your script will mess up. Also it's not on the topic but just so you know, myPlayer().isAnimating() is not what you want to use in any of your scripts. Fletching animation takes 3 or 4 ticks then 1 tick THE PLAYER WILL NOT BE ANIMATING and then start again, so your code will have bugs there. Also null checking the widget before checking its visibility might help. I tried it again but it didn't work. I redid the fletching part of my method to be a bit more logical but there was still no fix. (I didn't implement your other suggestions yet but don't think that is the problem. Here is my code now: if(getWidgets().get(305, 4).isVisible()) { getWidgets().get(305,4).interact("Make 10"); } else if(!inventory.isItemSelected()){ inventory.getItem("Knife").interact("Use"); } else { inventory.getItem("Log").interact(); }
FrostBug Posted December 22, 2015 Posted December 22, 2015 I tried it again but it didn't work. I redid the fletching part of my method to be a bit more logical but there was still no fix. (I didn't implement your other suggestions yet but don't think that is the problem. Here is my code now: if(getWidgets().get(305, 4).isVisible()) { getWidgets().get(305,4).interact("Make 10"); } else if(!inventory.isItemSelected()){ inventory.getItem("Knife").interact("Use"); } else { inventory.getItem("Log").interact(); } try changing getWidgets().get(305, 4).isVisible() to getWidgets().isVisible(305, 4). The latter method has a built in null check 1
Kalispel Posted December 22, 2015 Author Posted December 22, 2015 try changing getWidgets().get(305, 4).isVisible() to getWidgets().isVisible(305, 4). The latter method has a built in null check Thanks, I will put that in. But do you have any solution to the problem i'm having with interacting with items?
Joseph Posted December 22, 2015 Posted December 22, 2015 You can simply use inventory.getItem("Knife").interact(); inventory.getItem("Logs").interact(); But the way you wrote it may also work. Don't forget to delay the 2nd interaction until the first one is completed otherwise your script will mess up. Also it's not on the topic but just so you know, myPlayer().isAnimating() is not what you want to use in any of your scripts. Fletching animation takes 3 or 4 ticks then 1 tick THE PLAYER WILL NOT BE ANIMATING and then start again, so your code will have bugs there. Also null checking the widget before checking its visibility might help. Thanks, I will put that in. But do you have any solution to the problem i'm having with interacting with items?,Grab both items, null check both items. Then you can interact with the items. Helper: getInventory()isItemSelected 1
FrostBug Posted December 22, 2015 Posted December 22, 2015 Thanks, I will put that in. But do you have any solution to the problem i'm having with interacting with items? The item interaction likely failed because of the missing null check with the widget. An NPE would have been thrown before reaching the code for item interaction.
Kalispel Posted December 22, 2015 Author Posted December 22, 2015 The item interaction likely failed because of the missing null check with the widget. An NPE would have been thrown before reaching the code for item interaction. Yeah. Thanks for the help! I'm on my way to making my first script.
liverare Posted December 23, 2015 Posted December 23, 2015 You should be fine-coding your bot so that it doesn't do any unecessary actions, for example: public boolean tryUseItemOnItem(Item toSelect, Item toUseOn) { /* * Store information near to the top */ boolean successful = false; String curSelectedItemName = null; boolean curSelectingItem = false; Item badItem = null; // This may come in handy later /* * Some basic error checking */ if (toSelect == null || toUseOn == null) { throw new IllegalArgumentException("Items must be valid"); } else if (!getTabs().open(Tab.INVENTORY)) { throw new RuntimeException("Failed to open inventory tab"); } /* * Main meat of the code */ curSelectedItemName = getInventory().getSelectedItemName(); curSelectingItem = getInventory().isItemSelected(); if (curSelectingItem) { // We're currently selecting an item assert (curSelectedItemName != null); // *optional* Simple truth: item selected = assert we have selected item's name if (curSelectedItemName.equals(toSelect.getName())) { // We're selecting item we want successful = toUseOn.interact(); // #interact() without arguments should be a simple 'click' } else if (curSelectedItemName.equals(toUseOn.getName())) { // We've selected the wrong item (toUseOn), but nonetheless, it'll work successful = toSelect.interact(); // #interact() without arguments should be a simple 'click' } else { // We're selecting a wrong item entirely -- deselect it! badItem = getInventory().getItem(curSelectedItemName); // Get selected item in inventory (it will exist) assert (badItem != null); // *optional* Simple truth: item selected, but not the one we want, so the item must exist if we've got it selected if (badItem.interact()) { // Try to simply click the selected item to nullify the selection successful = tryUseItemOnItem(toSelect, toUseOn); // Re-run the entire method again (recursion) } else { throw new RuntimeException("Failed to deselect item"); // Error deselecting bad item } } } else { // We're not currently selecting an item if (toSelect.interact("Use")) { // Be precise when selecting the option since some left-click options are actions successful = tryUseItemOnItem(toSelect, toUseOn); // Re-run the entire method again (recursion) } else { throw new RuntimeException("Failed to select item"); // Problem selecting the item } } return successful; } Example of usage: @Override public int onLoop() throws InterruptedException { Item knife = getInventory().getItem("Knife"); Item oakLogs = getInventory().getItem("Oak logs"); // If fletching interface is not open... // else... if (knife != null && oakLogs != null) { if (tryUseItemOnItem(knife, oakLogs)) { // sleep until fletching interface is open (conditional) } } return 1500; } This is untested code, but I think the logic is pretty air-right. The code can be extended upon to include sleeps, custom exceptions, etc. But that may be a bit overkill (for now).