Jump to content
Nezz

Methods to use potions

Recommended Posts

I wrote three methods to help use potions more dynamically.

 

First is getPotName, which given something like "Super strength" or "Strength potion" will return the full name (includes the dosage on the end, essentially)

Second is getPotBonus, which given something like "Super strength" or "Strength potion" will return the bonus you get from drinking the potion - based on  your current stats. (Could easily add prayer or other skills to this, even make it dynamic with a Skill argument)

	public String getPotName(String pot_type){
		Item temp = client.getInventory().getItemForNameThatContains(pot_type);
		if(temp != null){
			return temp.getName();
		}
		else{
			return null;
		}
	}
	public int getPotBonus(String pot_type){
		int str = client.getSkills().getLevel(Skill.STRENGTH);
		int att = client.getSkills().getLevel(Skill.ATTACK);
		int rang = client.getSkills().getLevel(Skill.RANGED);
		if(pot_type == "Super strength")
			return (5+(int)(str * .15));
		else if(pot_type == "Strength potion")
			return (int)(str * .12);
		else if(pot_type == "Super attack")
			return (5 + (int)(att*.15));
		else if(pot_type == "Attack potion")
			return ((int)(att*.12));
		else if(pot_type == "Ranging potion")
			return ((int)(rang*.12));
		else
			return 0;
	}

Finally we have usePotion, which again, given something like "Super strength" or "Strength potion" will use the above two methods to determine *if* you need to use the potion, and if you do, it will.

Potion use is based on 2/3 the amount of stat bonus you get from the potion.

This last method can probably be refined a lot, but it worked for what I needed it to so this is as far as it got.

	public boolean usePotion(String potion_type) throws InterruptedException{
		String potion_name = getPotName(potion_type);
		if(potion_name != null){
			Item pot = client.getInventory().getItemForName(potion_name);
			int pot_bonus = getPotBonus(potion_type);
			if(pot != null){
				//get current skill level
				if(potion_name.contains("ength")){
					int skill_level = client.getSkills().getLevel(Skill.STRENGTH);
					int curr_skill_level = client.getSkills().getCurrentLevel(Skill.STRENGTH);
					int pot_at = (int)(pot_bonus*2/3);
					//once bonus str is at 2/3
					if((curr_skill_level - skill_level) <= pot_at){
						log("Using Strength Potion!");
						client.getInventory().interactWithName(potion_name, "Drink");
						return true;
					}
				}
				else if(potion_name.contains("ttack")){
					int skill_level = client.getSkills().getLevel(Skill.ATTACK);
					int curr_skill_level = client.getSkills().getCurrentLevel(Skill.ATTACK);
					int pot_at = (int)(pot_bonus*2/3);
					//once bonus str is at 2/3
					if((curr_skill_level - skill_level) <= pot_at){
						log("Using Attack Potion!");
						client.getInventory().interactWithName(potion_name, "Drink");
						return true;
					}
				}
				else if(potion_name.contains("ang")){
					int skill_level = client.getSkills().getLevel(Skill.RANGED);
					int curr_skill_level = client.getSkills().getCurrentLevel(Skill.RANGED);
					int pot_at = (int)(pot_bonus*2/3);
					//once bonus str is at 2/3
					if((curr_skill_level - skill_level) <= pot_at){
						log("Using Ranging Potion!");
						client.getInventory().interactWithName(potion_name, "Drink");
						return true;
					}
				}
				else{
					log("Unknown potion type!");
				}
			}
			else{
				log("Couldn't find " + potion_type + " in your inventory!");
				
			}
		}
		return false;
	}
Link to comment
Share on other sites

I just use this for my potion handler, it's really simple, but it works fantastically! Though a suggestion for yours would to use a Switch instead of if/else, because the only time it's efficient to use multiple if/else over a switch is when your working with less than 3, if more than that its best to use a switch statement.

    private void potUp(Script s) throws InterruptedException{
        for(Item i : s.client.getInventory().getItems()){
            for(PotionData p : PotionData.values()){
                if(i != null && p.contains(i.getId())){
                    switch (p) {
                        case ATTACK:
                            if(s.client.getSkills().getCurrentLevel(Skill.ATTACK) <= s.client.getSkills().getLevel(Skill.ATTACK)){
                                if(s.client.getInventory().interactWithId(i.getId(), "Drink")){
                                    break;
                                }
                            }
                            break;
                        case STRENGTH:
                            if(s.client.getSkills().getCurrentLevel(Skill.STRENGTH) <= s.client.getSkills().getLevel(Skill.STRENGTH)){
                                if(s.client.getInventory().interactWithId(i.getId(), "Drink")){
                                    break;
                                }
                            }
                            break;
                        case DEFENCE:
                            if(s.client.getSkills().getCurrentLevel(Skill.DEFENCE) <= s.client.getSkills().getLevel(Skill.DEFENCE)){
                                if(s.client.getInventory().interactWithId(i.getId(), "Drink")){
                                    break;
                                }
                            }
                            break;
                        case SUPER_ATTACK:
                            if(s.client.getSkills().getCurrentLevel(Skill.ATTACK) <= s.client.getSkills().getLevel(Skill.ATTACK)){
                                if(s.client.getInventory().interactWithId(i.getId(), "Drink")){
                                    break;
                                }
                            }
                            break;
                        case SUPER_STRENGTH:
                            if(s.client.getSkills().getCurrentLevel(Skill.STRENGTH) <= s.client.getSkills().getLevel(Skill.STRENGTH)){
                                if(s.client.getInventory().interactWithId(i.getId(), "Drink")){
                                    break;
                                }
                            }
                            break;
                        case SUPER_DEFENCE:
                            if(s.client.getSkills().getCurrentLevel(Skill.DEFENCE) <= s.client.getSkills().getLevel(Skill.DEFENCE)){
                                if(s.client.getInventory().interactWithId(i.getId(), "Drink")){
                                    break;
                                }
                            }
                            break;
                    }
                }
            }
        }
    }
Link to comment
Share on other sites

 

I just use this for my potion handler, it's really simple, but it works fantastically! Though a suggestion for yours would to use a Switch instead of if/else, because the only time it's efficient to use multiple if/else over a switch is when your working with less than 3, if more than that its best to use a switch statement.

    private void potUp(Script s) throws InterruptedException{
        for(Item i : s.client.getInventory().getItems()){
            for(PotionData p : PotionData.values()){
                if(i != null && p.contains(i.getId())){
                    switch (p) {
                        case ATTACK:
                            if(s.client.getSkills().getCurrentLevel(Skill.ATTACK) <= s.client.getSkills().getLevel(Skill.ATTACK)){
                                if(s.client.getInventory().interactWithId(i.getId(), "Drink")){
                                    break;
                                }
                            }
                            break;
                        case STRENGTH:
                            if(s.client.getSkills().getCurrentLevel(Skill.STRENGTH) <= s.client.getSkills().getLevel(Skill.STRENGTH)){
                                if(s.client.getInventory().interactWithId(i.getId(), "Drink")){
                                    break;
                                }
                            }
                            break;
                        case DEFENCE:
                            if(s.client.getSkills().getCurrentLevel(Skill.DEFENCE) <= s.client.getSkills().getLevel(Skill.DEFENCE)){
                                if(s.client.getInventory().interactWithId(i.getId(), "Drink")){
                                    break;
                                }
                            }
                            break;
                        case SUPER_ATTACK:
                            if(s.client.getSkills().getCurrentLevel(Skill.ATTACK) <= s.client.getSkills().getLevel(Skill.ATTACK)){
                                if(s.client.getInventory().interactWithId(i.getId(), "Drink")){
                                    break;
                                }
                            }
                            break;
                        case SUPER_STRENGTH:
                            if(s.client.getSkills().getCurrentLevel(Skill.STRENGTH) <= s.client.getSkills().getLevel(Skill.STRENGTH)){
                                if(s.client.getInventory().interactWithId(i.getId(), "Drink")){
                                    break;
                                }
                            }
                            break;
                        case SUPER_DEFENCE:
                            if(s.client.getSkills().getCurrentLevel(Skill.DEFENCE) <= s.client.getSkills().getLevel(Skill.DEFENCE)){
                                if(s.client.getInventory().interactWithId(i.getId(), "Drink")){
                                    break;
                                }
                            }
                            break;
                    }
                }
            }
        }
    }

 

why dont you supply us with that PotionData enum?

 
Link to comment
Share on other sites

 

I just use this for my potion handler, it's really simple, but it works fantastically! Though a suggestion for yours would to use a Switch instead of if/else, because the only time it's efficient to use multiple if/else over a switch is when your working with less than 3, if more than that its best to use a switch statement.

    private void potUp(Script s) throws InterruptedException{
        for(Item i : s.client.getInventory().getItems()){
            for(PotionData p : PotionData.values()){
                if(i != null && p.contains(i.getId())){
                    switch (p) {
                        case ATTACK:
                            if(s.client.getSkills().getCurrentLevel(Skill.ATTACK) <= s.client.getSkills().getLevel(Skill.ATTACK)){
                                if(s.client.getInventory().interactWithId(i.getId(), "Drink")){
                                    break;
                                }
                            }
                            break;
                        case STRENGTH:
                            if(s.client.getSkills().getCurrentLevel(Skill.STRENGTH) <= s.client.getSkills().getLevel(Skill.STRENGTH)){
                                if(s.client.getInventory().interactWithId(i.getId(), "Drink")){
                                    break;
                                }
                            }
                            break;
                        case DEFENCE:
                            if(s.client.getSkills().getCurrentLevel(Skill.DEFENCE) <= s.client.getSkills().getLevel(Skill.DEFENCE)){
                                if(s.client.getInventory().interactWithId(i.getId(), "Drink")){
                                    break;
                                }
                            }
                            break;
                        case SUPER_ATTACK:
                            if(s.client.getSkills().getCurrentLevel(Skill.ATTACK) <= s.client.getSkills().getLevel(Skill.ATTACK)){
                                if(s.client.getInventory().interactWithId(i.getId(), "Drink")){
                                    break;
                                }
                            }
                            break;
                        case SUPER_STRENGTH:
                            if(s.client.getSkills().getCurrentLevel(Skill.STRENGTH) <= s.client.getSkills().getLevel(Skill.STRENGTH)){
                                if(s.client.getInventory().interactWithId(i.getId(), "Drink")){
                                    break;
                                }
                            }
                            break;
                        case SUPER_DEFENCE:
                            if(s.client.getSkills().getCurrentLevel(Skill.DEFENCE) <= s.client.getSkills().getLevel(Skill.DEFENCE)){
                                if(s.client.getInventory().interactWithId(i.getId(), "Drink")){
                                    break;
                                }
                            }
                            break;
                    }
                }
            }
        }
    }

I never really knew the functionality difference between if else and switch cases were :x

I think more of the focus in my snippet should be on the getPotName and getPotBonus, as that's what gave me the most trouble.

with getPotName, you don't need to update ID's ;) and some people say if you wait until your pot runs all the way out, you lose out on xp, so I created the getPotBonus to figure out how much it gives you, and at what point to repot.

 

I like your code though, so much neater than mine. <3

Link to comment
Share on other sites

I never really knew the functionality difference between if else and switch cases were :x

I think more of the focus in my snippet should be on the getPotName and getPotBonus, as that's what gave me the most trouble.

with getPotName, you don't need to update ID's wink.png and some people say if you wait until your pot runs all the way out, you lose out on xp, so I created the getPotBonus to figure out how much it gives you, and at what point to repot.

 

I like your code though, so much neater than mine. QwPha8E.png

 

Item ID's don't ever change, so you don't have to worry about them changing on you!!! smile.png

 

But I understand your idea about getPotBonus, which a really cool actually!

 

But thank you!

Edited by NotoriousPP
Link to comment
Share on other sites

Item ID's don't ever change, so you don't have to worry about them changing on you!!! smile.png

 

But I understand your idea about getPotBonus, which a really cool actually!

 

But thank you!

:x they don't?

Every time someone tells me an ID doesn't change, I use it and find out that it changes. lol

Though I have been using ID for noted pure ess on my rcer.

Link to comment
Share on other sites

:x they don't?

Every time someone tells me an ID doesn't change, I use it and find out that it changes. lol

Though I have been using ID for noted pure ess on my rcer.

 

Yea Item IDs always seem to stay constant, I've been using the same Enum for Food since I started scripting, the ID's have always stayed the same. So you should be safe using IDs for Items, just that's about it really.

  • Like 1
Link to comment
Share on other sites

sorry to say this but if you are using this method you simply don't understand anything about strings..

I realize == isn't as accurate as .compareTo or .equals.

but it's similar to the idea of "is" and "=="

they both mean the same thing, just one works better than the other in different situations.

 

Like I said,  I use .compareTo in pretty much all the rest of my code. Moreso since the first time == failed on me.

It's been 2 years since I've used Java, man. Give me a break.

 

But again.

If all you're going to do is say "why are you doing it like this, it's so stupid.", don't comment. Constructive criticism is always welcome. If you disagree with how I'm doing it, please explain why and give an example of how you might do it. Otherwise all that's going to happen is you'll look like a cock, and my code will stay the same.

 

Thanks. :)

Link to comment
Share on other sites

I realize == isn't as accurate as .compareTo or .equals.

but it's similar to the idea of "is" and "=="

they both mean the same thing, just one works better than the other in different situations.

Like I said, I use .compareTo in pretty much all the rest of my code. Moreso since the first time == failed on me.

It's been 2 years since I've used Java, man. Give me a break.

But again.

If all you're going to do is say "why are you doing it like this, it's so stupid.", don't comment. Constructive criticism is always welcome. If you disagree with how I'm doing it, please explain why and give an example of how you might do it. Otherwise all that's going to happen is you'll look like a cock, and my code will stay the same.

Thanks. :)

you should be using .equals to check for equality, compareTo should be used to check lexical ordering. Also, equals is faster
  • Like 1
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...