Jump to content

Creating a Function


Tempest

Recommended Posts

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.

 

Link to comment
Share on other sites

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

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

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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