Tempest Posted March 25, 2018 Share Posted March 25, 2018 Hi everyone, I am trying to create a function that determine the pickaxe the player is able to wield. void determinePickaxe() { String pickAxe = null; int miningLvl = getSkills().getDynamic(Skill.MINING); int attackLvl = getSkills().getDynamic(Skill.ATTACK); String[] pickAxes = { "Iron pickaxe", "Mithril pickaxe", "Adamant pickaxe", "Rune pickaxe" }; log("Player's Mining Level: " + miningLvl); log("Player's Attack Level: " + attackLvl); if(getInventory().contains(pickAxes)) { log("Determining Which Pickaxe To Use.."); for (int i = 0; i < pickAxes.length; i++) { log("Pickaxes Avaialble: " + pickAxes[i]); } if (getSkills().getDynamic(Skill.MINING) < 6) { getInventory().interact("Wield", pickAxes[0]); log("Using: " + pickAxes[0]); pickAxe = pickAxes[0]; if (getSkills().getDynamic(Skill.MINING) < 21) { log("HERE"); getInventory().interact("Wield", pickAxes[1]); log("Using: " + pickAxes[1]); pickAxe = pickAxes[1]; if (miningLvl < 31) { inventory.interact("Wield", pickAxes[2]); log("Using: " + pickAxes[2]); if (miningLvl < 41) { inventory.interact("Wield", pickAxes[3]); log("Using: " + pickAxes[3]); } } } } } } I later the function into public void onStart() throws InterruptedException { //Run Once Here, Variables Etc. determinePickaxe(); } It seems to be able to detect the inventory having the pickaxes but does not wield the pickaxes.. Anyone know why? Sorry if I did something stupid. Quote Link to comment Share on other sites More sharing options...
LoudPacks Posted March 25, 2018 Share Posted March 25, 2018 (edited) public String getBestAxe() { String name = ""; int lvl = getSkills().getStatic(Skill.WOODCUTTING); if (lvl >= 41) { //you could also add inventory checks here so it only returns the best axe in inventory name = "Rune axe"; } else if (lvl >= 31) { name = "Adamant axe"; } else if (lvl >= 21) { name = "Mithril axe"; } else { name = "Bronze axe"; } return name; } public int loop() { String axe = getBestAxe(); if (getInventory().contains(axe) && !getEquipment().contains(axe)) { if (getInventory.interaction("Equip", axe)) { new ConditionalSleep(1500, 2000) { @Override public boolean condition() { return getEquipment().contains(axe); } }.sleep(); } } return 250; } I wrote this in the editor so format is shit. You can edit it a big for pickaxes and throw in an attack level check. Edit: @Apaec way is better but I didn't think you'd want to mess with enums or streams. Edited March 25, 2018 by LoudPacks 1 Quote Link to comment Share on other sites More sharing options...
Apaec Posted March 25, 2018 Share Posted March 25, 2018 (edited) It's getting a little bit spaghetti, might be worth re-thinking how you approach this problem! I would suggest creating a pickaxe enum, for example: public enum Pickaxe { BRONZE_PICKAXE(1), IRON_PICKAXE(1), /* ... */ DRAGON_PICKAXE(61); private final int levelReq; Pickaxe(int levelReq) { this.levelReq = levelReq; } public int getRequiredLevel() { return levelReq; } public boolean hasRequiredLevel(MethodProvider mp) { return mp.getSkills().getStatic(Skill.MINING) >= levelReq; } public boolean equip(MethodProvider mp) { /* ... */ } @Override public String toString() { return super./* ... */; } } Then you can use this to determine the highest tier pickaxe to use, i.e Arrays.asList(Pickaxe.values()).stream().filter(pick -> pick.hasRequiredLevel(mp)). /* ... Reduction to highest required level ... */ I've left gaps here and there for you to implement so hopefully you learn something new ! Also, hopefully there are no errors, but I wrote the code in the reply box so if there are, sorry about that, just let me know! Edit: Or, alternatively, you could just as easily use a for loop with some simple logic (i.e "for (Pickaxe axe : Pickaxe.values()) { ... }" ) to find the best pickaxe. I just like streams cause you can make it a tidy one-liner Good luck! -Apa Edited March 25, 2018 by Apaec 3 Quote Link to comment Share on other sites More sharing options...
andrewboss Posted March 25, 2018 Share Posted March 25, 2018 (edited) Why do you just use if statement without else if? This might be the problem... Not sure though. Edited March 25, 2018 by andrewboss 1 Quote Link to comment Share on other sites More sharing options...
liverare Posted March 25, 2018 Share Posted March 25, 2018 (edited) There's no point looping through an array if you still intent to reference elements from the array manually. Now that you're requiring to know more than just the pickaxe name, you need to look into different data structures that'll help you achieve what you're looking for. @Apaec solution is a good starting point. However, his enumerator is a little overly complicated for someone of your learning level (especially the Lambda stuff), so I've simplified it a bit: /* * Pickaxe.java */ public enum Pickaxe { BRONZE("Bronze pickaxe", 1, 1), IRON("Iron pickaxe", 1, 1), DRAGON("Dragon pickaxe", 61, 60); private final String name; private final int miningLevel; private final int attackLevel; private Pickaxe(String name, int miningLevel, int attackLevel) { this.name = name; this.miningLevel = miningLevel; this.attackLevel = attackLevel; } @Override public boolean toString() { return name; } public int getMiningLevel() { return miningLevel; } public int getAttackLevel() { return attackLevel; } } Now you can iterate through this enumerator using its values method: for (Pickaxe pickaxe : Pickaxe.values()) { String pickaxeName = pickaxe.toString(); int pickaxeMiningLevel = pickaxe.getMiningLevel(); int pickaxeAttackLevel = pickaxe.getAttackLevel(); /* TODO - checks n stuff */ } What you'll find is that you can treat the enumerators similarly to how you were treating the array. Except now, you have more than just the names to work with. See if you can replace your array of pickaxe names with an enumerator. Edited March 25, 2018 by liverare 2 Quote Link to comment Share on other sites More sharing options...
Tempest Posted March 25, 2018 Author Share Posted March 25, 2018 Thanks a lot for all your feedback & help, I will be looking into it now! 2 Quote Link to comment Share on other sites More sharing options...