Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Creating a Function

Featured Replies

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.

 

	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 by LoudPacks

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 by Apaec

Why do you just use if statement without else if? This might be the problem... Not sure though.

Edited by andrewboss

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 by liverare

  • Author

Thanks a lot for all your feedback & help, I will be looking into it now! 

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.