Jump to content

A Beginners Guide to Writing OSBot Scripts (where to get started!) by Apaec


Apaec

Recommended Posts

5 minutes ago, Malcolm_OS said:

First, this should be included in your onLoop() not onStart().

Second. This is very basic here, This is not completed code, you should be doing a lot more checks for other things here. For example I amalgamated the two parts for you for a basic idea.

Use myPlayer() whenever you're checking if you're in an area.

Also when you're walking to an area I always like to use .getRandomPosition() that way you aren't always walking to the same spot every time.


    public int onLoop() throws InterruptedException {
        Area pen = new Area(3253, 3255, 3264, 3279);
        if (!pen.contains(myPlayer())) {
            getWalking().webWalk(pen.getRandomPosition());
        } else {
            if (getInventory().contains("Bones")) {
                getInventory().interact("Bury", "Bones");
            } else {
                GroundItem cowbones = getGroundItems().closest("Bones");
                if (cowbones != null) {
                    cowbones.interact("Pick up");
                }
            }
        }

        return random(600, 900);

    }

 

So if I explain what will happen above. First of all the script will check if you are in that area, if not, you will walk to that area, but if you are (else) then you will begin to check if your inventory has bones. If your inventory has bones it will bury the bones, if not (else) then the script will declare the closest bones on the ground and pick them up. 

Thanks for the help, why do you want to have the walking in onLoop? Is it just to make sure I stay in the cow pen? Also, what will happen if I just say to go to the pen like I did? Will it not go to a random position in the area I designated?

Link to comment
Share on other sites

1 hour ago, Malcolm_OS said:

I would do something along this too but at this point I'm trying to not overwhelm the guy ?

Ahh that's great, means I'm on the right path lol, I'm also very new to this stuff and usually the one asking all the questions! Won't be long before I pop up with a new one aswell ?

Link to comment
Share on other sites

Hello there, 

Sorry if this is asking for a bit much, but I've been stuck on this for a while. Will share my code now.

Spoiler

public class LobsterWebWalk extends Script {
	
	private enum karamFishing{
		smallNetFishing("Small fishing net", "Net"),
		baitFishing("Fishing rod", "Bait"),
		cageFishing("Lobster pot", "Cage");
		
		private final String tool;
		private final String action;
		
		karamFishing(String t, String a){
			tool = t;
			action = a;
		}
		public String getTool() {
			return tool;
		}
		public String getAction() {
			return action;
		}
		
	}

	@Override
	public void onStart() {
		log("Let's get glitched out!");
	}

	@Override
	public int onLoop() throws InterruptedException {
		
		return random(200, 300);
	}
	// ============================================= THE ISSUE IS THIS PART
	private void fishingType() {
		if(getSkills().getStatic(Skill.FISHING) >= 1 && getSkills().getStatic(Skill.FISHING) < 20) {
			tool = karamFishing.smallNetFishing; //What goes in here?!
		}
	}

	@Override
	public void onExit() {
		log("Houston, we have a problem...");
	}

	@Override
	public void onPaint(Graphics2D g) {

	}
}

 

 

I've looked into numerous tutorials about enumeration and the like, how to use them with println etc, but the implementation here seems a little different for my scenario. I managed to get my last issue sorted with the fish pickup, but would like to move onto more advanced stuff. Right now, I'm working on a method which will be called to determine the type of fishing the bot will undertake.

Basically, the question here is... How the hell do I call whatever it is I'm trying to achieve? Tried a couple ways, but with errors. Do I have to create additional variables? Or is it just a quick fix? Trying to make a more dynamic script which looks into the player's levels. 

Many thanks!

EDIT: Or if there is a tutorial in Java which covers this, just pointing me there is more than enough! Same with if whether or not what I'm trying to do has a term. Typing "Implementing enums" into Google isn't giving the most desirable results in this case...

Edited by Glaciation96
Link to comment
Share on other sites

2 hours ago, Malcolm_OS said:

@Glaciation96 I absolutely hate using Enum's

I avoid it at all costs. They are just annoying to me. 

But, I think your issue is just this, I didn't test this but I believe this should be declared.


karamFishing tool = karamFishing.smallNetFishing; 

 

It worked! No errors, thank you. Knowing what to declare and how to call out more complicated coding is a damn pain :/... Guess the next time I run into an issue like this, I gotta try calling out the parent code aswell. Whatever the proper term for that is...

Link to comment
Share on other sites

So I just tried making a new script that walks to the barbarian village fishing spot, picks up the salmon that fishers drop, banks at edgeville, and walks back. It first checks if it has space in it's inventory. If it doesn't (meaning its full of salmon) then it will walk to barbarian village and pick up salmon. Once it's inv is full, it walks to the bank and deposits. It now has an empty inventory and is told to walk back to barb village. Hopefully that is the correct train-of-thought there. I have multiple questions now.

1: I'm pretty sure I didn't mess with the skeleton or anything but the script will not start

2: I'll often get red line errors under stuff from the api, but then I just have to tell it to import from the osbot.jar. Isn't that what the beginning few lines is supposed to do?

3: How does the script look? I'd obviously test it out myself but it won't start for some reason

Spoiler

package Main;

import java.awt.Graphics2D;

import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.model.GroundItem;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

@ScriptManifest(author = "Dark798", info = "My first script", name = "FishPickerUpper", version = 0, logo = "")
public class Main extends Script {

	@Override
	public void onStart() {
		log("Let's get started!");
	}

	@Override
	public int onLoop() throws InterruptedException {
		Area fishingspot = new Area(3108, 3432, 3109, 3433);
		Area bank = new Area(3092, 3489, 3094, 3498);
		
		if (!getInventory().isFull()) {
			if (!fishingspot.contains(myPlayer())) {
				getWalking().webWalk(fishingspot.getRandomPosition());}
					GroundItem salmon = getGroundItems().closest("Raw Salmon");
						if (salmon != null) {
							if (!myPlayer().isAnimating() && !myPlayer().isMoving()) {
								if (getMap().canReach(salmon)) {
									salmon.interact("Take");
									sleep(random(1000, 1200));
								}
							}
						}
					}
				else getWalking().webWalk(bank.getRandomPosition());
					
				if (bank.contains(myPlayer())) {
					   RS2Object bankBooth = objects.closest("Bank booth");
					    if (bankBooth != null) {
					        if (bankBooth.interact("Bank")) {
					            while (!bank.isOpen())
					                sleep(random(500, 1000));
					            bank.depositAll();
					        }
					    }
					}
				}
		return random(200, 300);
	}

	@Override
	public void onExit() {
		log("Thanks for running my Tea Thiever!");
	}

	@Override
	public void onPaint(Graphics2D g) {

	}

}

 

Thank you for all the help so far !!

Link to comment
Share on other sites

15 hours ago, Malcolm_OS said:

First of all, you don't need to declare the bank area. The API does that easily for us.

IMO I'd say its bad practice to have your Area in your onLoop() section. You should be putting those areas with all of your variables above that (where you would declare something like x=5;)

Also,

I changed this:


if (!fishingspot.contains(myPlayer())) {
				getWalking().webWalk(fishingspot.getRandomPosition());}
					GroundItem salmon = getGroundItems().closest("Raw Salmon");

to:


    if (!fishingspot.contains(myPlayer())) {
                getWalking().webWalk(fishingspot.getRandomPosition());
            } else {
                GroundItem salmon = getGroundItems().closest("Raw Salmon");

Basically this states that if you're not in the Area fishingspot then you will walk to that area and if you are (else) you will begin to declare salmon on the ground etc.

Also this, I lowkey wanted to cry when I saw it:


				if (bank.contains(myPlayer())) {
					   RS2Object bankBooth = objects.closest("Bank booth");
					    if (bankBooth != null) {
					        if (bankBooth.interact("Bank")) {
					            while (!bank.isOpen())
					                sleep(random(500, 1000));
					            bank.depositAll();
					        }
					    }
					}

I wouldn't use a while statement. Also, this just wouldn't work. I adjusted it here:


            if (!bank.contains(myPlayer())) {
                getWalking().webWalk(bank.getRandomPosition());
            } else {
                if (!getBank().isOpen()) {
                    getBank().open();
                    // conditional sleep satisfied by bank being open goes here
                } else {
                    getBank().depositAll();
                    // conditional sleep satisfied by inventory being empty goes here
                    getBank().close();
                    //conditional sleep satisfied by bank being closed
                }
            }
        }

 

In this spoiler is the full code that I rearranged for you. This is for learning purposes only, I wouldn't suggest to copypasta this. Look at it, try and understand it and then re-write it yourself would be my suggestion.

  Hide contents


    final Area fishingspot = new Area(3108, 3432, 3109, 3433);
    final Area bank = new Area(3092, 3489, 3094, 3498); // you can use the Banks API to make this easier, you dont need to find the area

    public int onLoop() throws InterruptedException {

        if (!getInventory().isFull()) {
            if (!fishingspot.contains(myPlayer())) {
                getWalking().webWalk(fishingspot.getRandomPosition());
            } else {
                GroundItem salmon = getGroundItems().closest("Raw Salmon");
                if (salmon != null) {
                    if (!myPlayer().isAnimating() && !myPlayer().isMoving()) {
                        if (getMap().canReach(salmon)) {
                            salmon.interact("Take");
                            sleep(random(1000, 1200)); // should be a conditional sleep
                        }
                    }
                }
            }
        } else {
            if (!bank.contains(myPlayer())) {
                getWalking().webWalk(bank.getRandomPosition());
            } else {
                if (!getBank().isOpen()) {
                    getBank().open();
                    // conditional sleep satisfied by bank being open goes here
                } else {
                    getBank().depositAll();
                    // conditional sleep satisfied by inventory being empty goes here
                    getBank().close();
                    //conditional sleep satisfied by bank being closed goes here
                }
            }
        }

        return random(200, 300);
    }

 

Now if I go to explain it for you again here. First of all we check if your inventory is full, if its not full, we check if we are in the Area fishingspot. if we are not, we walk to that spot, if we are (1st else) we then declare the salmon on the ground, make sure we aren't moving/animating and pick them up. 

Once the inventory is full (2nd else) we check if we are in the bank. If we aren't in the bank we then walk to the bank, if we are in the bank (3rd else) we then check if the bank is open. If the bank isn't open, we open it, if the bank is open (4th else), we deposit everything and then close the bank.

 

 

1: Is it bad if I use the Area bank? I've used plenty of scripts that struggle with finding a bank, so I figured I'd manually add it to solve that problem.

2: Should I put all variables that don't depend on the player in onStart then?

3: I copied the bank section from another guide ? I can see your version is probably better

4: Any idea why it wouldn't run? I didn't mess with the skeleton so I'm guessing it was a client problem or something

 

Thanks for the help, it's definitely coming along with your help =D

 

 

Link to comment
Share on other sites

Hello fellas. The scrub is back. With a question as always. 

I'm trying to use the above example as a basis for my second script. He uses various techniques that are a step beyond what I've been using, so it's a good direction for me to further my understanding. However, there is something that I've been struggling to wrap my head around. It is this condition which he uses multiple times during his onLoop. 

if(currentFishType == FishingType.LEVEL) {
//code here...
}

Those are variables that he had taken from his enum, to store his actions and tools, depending on the player's levels. I've copied it below. 

private FishingType currentFishingType = FishingType.SMALLNET;

private enum FishingType{
        SMALLNET("Small fishing net", "Net", new int[]{303}),
        LURE("Fly fishing rod", "Lure", new int[]{309, 314}),
        BAIT("Fishing rod", "Bait", new int[]{307, 313}),
        CAGE("Lobster pot", "Cage", new int[]{301}),
        HARPOON("Harpoon", "Harpoon", new int[]{311}),
        BIGNET("Big fishing net", "Net", new int[]{305}),
        LEVEL("?", "?", new int[]{});
 
        private final String tool;
        private final String action;
        private final int[] reqItems;
        
        FishingType(String tool, String action, int[] reqItems) {
            this.tool = tool;
            this.action = action;
            this.reqItems = reqItems;
        }
 
        public String getTool() {
            return tool;
        }
 
        public String getAction() {
            return action;
        }
        
        public int[] getRequiredItems(){
            return reqItems;
        }
    }

From that information, isn't what he's doing no different than this?

if(FishingType.SMALLNET == FishingType.LEVEL) {
//code here
}

He's comparing two different variables or constants in his enum! Won't that just return false every time? What's the point in this? Been looking through this for a couple days now lol. Thought I'd finally ask for some assistance in understanding an example that was written by someone far better than me. 

Thanks. If this is confusing then I've missed out some necessary code from his script. Please check the link if that's the case please. Shouldn't take you pros too long to skim through his code right? ?

Link to comment
Share on other sites

16 minutes ago, Glaciation96 said:

Hello fellas. The scrub is back. With a question as always. 

I'm trying to use the above example as a basis for my second script. He uses various techniques that are a step beyond what I've been using, so it's a good direction for me to further my understanding. However, there is something that I've been struggling to wrap my head around. It is this condition which he uses multiple times during his onLoop. 


if(currentFishType == FishingType.LEVEL) {
//code here...
}

Those are variables that he had taken from his enum, to store his actions and tools, depending on the player's levels. I've copied it below. 


private FishingType currentFishingType = FishingType.SMALLNET;

private enum FishingType{
        SMALLNET("Small fishing net", "Net", new int[]{303}),
        LURE("Fly fishing rod", "Lure", new int[]{309, 314}),
        BAIT("Fishing rod", "Bait", new int[]{307, 313}),
        CAGE("Lobster pot", "Cage", new int[]{301}),
        HARPOON("Harpoon", "Harpoon", new int[]{311}),
        BIGNET("Big fishing net", "Net", new int[]{305}),
        LEVEL("?", "?", new int[]{});
 
        private final String tool;
        private final String action;
        private final int[] reqItems;
        
        FishingType(String tool, String action, int[] reqItems) {
            this.tool = tool;
            this.action = action;
            this.reqItems = reqItems;
        }
 
        public String getTool() {
            return tool;
        }
 
        public String getAction() {
            return action;
        }
        
        public int[] getRequiredItems(){
            return reqItems;
        }
    }

From that information, isn't what he's doing no different than this?


if(FishingType.SMALLNET == FishingType.LEVEL) {
//code here
}

He's comparing two different variables or constants in his enum! Won't that just return false every time? What's the point in this? Been looking through this for a couple days now lol. Thought I'd finally ask for some assistance in understanding an example that was written by someone far better than me. 

Thanks. If this is confusing then I've missed out some necessary code from his script. Please check the link if that's the case please. Shouldn't take you pros too long to skim through his code right? ?

You're correct, he's comparing two Enum values. It will return false for every value of the variable 'currentFishType' apart from when currentFishType has the value FishingType.LEVEL

It's a weird way of laying out code -- it is not very good practice to have an enum value which holds falsified/placeholder data, but hopefully you understand why the comparison works now :)

-Apa

  • Like 1
Link to comment
Share on other sites

1 hour ago, Apaec said:

You're correct, he's comparing two Enum values. It will return false for every value of the variable 'currentFishType' apart from when currentFishType has the value FishingType.LEVEL

It's a weird way of laying out code -- it is not very good practice to have an enum value which holds falsified/placeholder data, but hopefully you understand why the comparison works now :)

-Apa

Thanks for the response! But unfortunately, I'm still not quite there :( 

When you said it will return false for every value of the variable 'currentFishType', I'm guessing, in that case, it'll be SMALLNET (as the script first starts anyway). Therefore those values being...

"Small fishing net", "Net", new int[]{303}

Right?

But the confusion is that, when does currentFishType actually have the values of FishingType.LEVEL? I ctrl+f searched for " = FishingType.LEVEL", and he never once sets anything to be FishingType.LEVEL. The only time I see it being used is when he doubles up the =, so he only ever compares a value to FishingType.LEVEL. Sorry my understanding of Java is at the bottom of the barrel basic lol, so if he used another method to do this then there's the culprit for my confusion. Could it be possible that he's calling this comparison for the purpose of it returning false every time? Surely not, as that would achieve nothing... 

Thanks again!

 

Edited by Glaciation96
Link to comment
Share on other sites

Trying to make a new script but it won't start at all, I copied the skeleton from my first script that would actually run but still doesn't start up

package Main;

import java.awt.Graphics2D;

import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.api.model.GroundItem;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

@ScriptManifest(author = "Dark798", info = "Progressive", name = "798Chopper", version = 0, logo = "")
public class Chopper extends Script {

	@Override
	public void onStart() {
		log("Let's get started!");
	}

	@Override
	public int onLoop() throws InterruptedException {
		if (getInventory()contains(int 1351, 1349, 1353, 1361, 1355, 1357, 1359, 6739))||(getEquipment()isWearingItem(EquipmentSlot weapon, int 1351, 1349, 1353, 1361, 1355, 1357, 1359, 6739));
			//is my level 1-14
				
			if (!getInventory()isFull()) {
				RS2Object tree = getObjects().closest("Tree");
					if (tree != null) {
						if (!myPlayer().isAnimating()) {
							tree.interact("Chop");
							sleep(random(300,500));
					}
				}
			}
			else getInventory()dropAllExcept(int 1351, 1349, 1353, 1361, 1355, 1357, 1359, 6739);{
		}
		logOut();
		
		
		return random(200, 300);}
	}

	private RS2Object getClosest() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onExit() {
		log("Thanks for running my Woodcutter!");
	}

	@Override
	public void onPaint(Graphics2D g) {

	}

}

 

Link to comment
Share on other sites

23 minutes ago, Malcolm_OS said:

First of all. Whenever you want to check for items. I would suggest always using the item name. Jagex sometimes changes item ID's and it will break your script if they do that. I didn't look up what these items are but I assume they are axes. So instead of using the int ID (which you don't need to declare int in the statement anyways) I would just do this:


(getInventory().contains("Bronze axe", "Iron axe", "next axe etc")

The same would apply for your drop statement


getInventory().dropAllExcept("Bronze axe", "Iron axe", "next axe etc");

Regarding your else statement to drop the items (I assume to be logs) you again want to make sure you're using your braces correctly, I will give you an example


if (condition) {
    //do bot stuff
} else {
    //do other bot stuff
}

Also when dealing with your equipment slots you need to tell the script which slot it is properly. I noticed that you aren't using periods a lot of the spots you are supposed to be. Also if you use this instead it should work for every axe 


if (getInventory().contains("Bronze axe", "Iron axe") || getEquipment().isWearingItemThatContains(EquipmentSlot.WEAPON, "axe")) {

I also gave you a hand figuring out how to check your woodcutting level.


if (getSkills().getStatic(Skill.WOODCUTTING) < 15) {

So I put this together for you. I didn't test it but I would assume this would work


    @Override
    public int onLoop() throws InterruptedException {
        if (getInventory().contains("Bronze axe", "Iron axe") || getEquipment().isWearingItemThatContains(EquipmentSlot.WEAPON, "axe")) {
            if (getSkills().getStatic(Skill.WOODCUTTING) < 15) {
                if (!getInventory().isFull()) {
                    RS2Object tree = getObjects().closest("Tree");
                    if (tree != null) {
                        if (!myPlayer().isAnimating() && !myPlayer().isMoving()) {
                            tree.interact("Chop");
                            new ConditionalSleep(5000) {
                                @Override
                                public boolean condition() throws InterruptedException {
                                    return (!myPlayer().isAnimating() || tree == null);
                                }
                            }.sleep();
                        }
                    }
                } else {
                    getInventory().dropAllExcept("Bronze axe", "Iron axe");
                }
            }
        }

        return random(200, 300);
    }

I am also curious, what IDE are you using?

Note: you'd probably want to add on to what I have given you because as it stands, if your inventory or equipment doesn't have an axe it will stand around and do nothing. I suggest adding on an else statement to go to the bank and withdraw an axe from the bank and if your bank doesn't have one to stop(); the script

I'm using eclipse. I know I need to change the script a lot, I just put it together really quickly, but I still don't know why it won't even start.

Link to comment
Share on other sites

14 hours ago, Dark798 said:

Trying to make a new script but it won't start at all, I copied the skeleton from my first script that would actually run but still doesn't start up


package Main;

import java.awt.Graphics2D;

import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.api.model.GroundItem;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

@ScriptManifest(author = "Dark798", info = "Progressive", name = "798Chopper", version = 0, logo = "")
public class Chopper extends Script {

	@Override
	public void onStart() {
		log("Let's get started!");
	}

	@Override
	public int onLoop() throws InterruptedException {
		if (getInventory()contains(int 1351, 1349, 1353, 1361, 1355, 1357, 1359, 6739))||(getEquipment()isWearingItem(EquipmentSlot weapon, int 1351, 1349, 1353, 1361, 1355, 1357, 1359, 6739));
			//is my level 1-14
				
			if (!getInventory()isFull()) {
				RS2Object tree = getObjects().closest("Tree");
					if (tree != null) {
						if (!myPlayer().isAnimating()) {
							tree.interact("Chop");
							sleep(random(300,500));
					}
				}
			}
			else getInventory()dropAllExcept(int 1351, 1349, 1353, 1361, 1355, 1357, 1359, 6739);{
		}
		logOut();
		
		
		return random(200, 300);}
	}

	private RS2Object getClosest() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onExit() {
		log("Thanks for running my Woodcutter!");
	}

	@Override
	public void onPaint(Graphics2D g) {

	}

}

 

Not sure if you've implemented the stuff that malcolm has suggested, but that script you first shared should have lit Eclipse up with red lines. Unless your Eclipse isn't showing you any red lines despite the errors in code, it should have been obvious as to why it wouldn't start if it's riddled with red? Also, even if you don't have any red, it STILL may not start. I had that happen to me once, but Apaec was able to fix it for me. It's because I called something which I shouldn't have. Maybe you've done the same?

Also, my last question was left unanswered:( I'm guessing it's because the guy who wrote that example probably went off on a weird, messy one. Making his code an effort to read. That's my bad. Not to worry as what he's doing apparently isn't good practice, so who needs to learn from that anyway right?! Lol.

EDIT: If you go back a couple pages, you can see him fix this issue for me. It might leave a hint as to why your script won't start. Assuming that you've gotten rid of the red lines first of course. 

Edited by Glaciation96
Link to comment
Share on other sites

Hey there, got another problem. Been quite a while! Slowly getting there...

It's about enums again, and passing parameters. As a bottom feeder, I'm not too sure how to implement certain techniques properly, in this case, varargs?

Below is a small snippet, hopefully it has everything you need to see what my issue is

Spoiler

private karamFishing levelFishType;

public final String[] Essentials = { levelFishType.getTool()[0], "Coins" };  //This is the problem. 

private enum karamFishing {
		smallNetFishing(new String[] { "Small fishing net" }, "Net"),
		baitFishing(new String[] { "Fishing rod", "Fishing bait" }, "Bait"),
		cageFishing(new String[] { "Lobster pot" }, "Cage");

		private final String[] tool;
		private final String action;

		karamFishing(String[] t, String a) {
			tool = t;
			action = a;
		}

		public String[] getTool() {
			return tool;
		}

		public String getAction() {
			return action;
		}

	}

public void bankstuff {
	bank.depositAllExcept(Essentials); //This is the dream - doesn't work how I want it to though, of course.
}

 

 

The mission here is to bank everything except the items that the bot will be using, plus coins. The above code works, but only if levelFishType isn't baitFishing. It won't crash the script, but instead the bot will bank everything except from the fishing rod(because of the [0] index), but bank the fishing bait which is not what I want. I've heard of varargs, and have tried multiple syntax combinations to try getting it to work. Could I get some guidance on this?

For example, I've tried this, but obviously, it doesn't work.

public final String[] Essentials = { levelFishType.getTool(String[]...varName), "Coins" };

Thanks!!

Link to comment
Share on other sites

54 minutes ago, Glaciation96 said:

Hey there, got another problem. Been quite a while! Slowly getting there...

It's about enums again, and passing parameters. As a bottom feeder, I'm not too sure how to implement certain techniques properly, in this case, varargs?

Below is a small snippet, hopefully it has everything you need to see what my issue is

  Hide contents


private karamFishing levelFishType;

public final String[] Essentials = { levelFishType.getTool()[0], "Coins" };  //This is the problem. 

private enum karamFishing {
		smallNetFishing(new String[] { "Small fishing net" }, "Net"),
		baitFishing(new String[] { "Fishing rod", "Fishing bait" }, "Bait"),
		cageFishing(new String[] { "Lobster pot" }, "Cage");

		private final String[] tool;
		private final String action;

		karamFishing(String[] t, String a) {
			tool = t;
			action = a;
		}

		public String[] getTool() {
			return tool;
		}

		public String getAction() {
			return action;
		}

	}

public void bankstuff {
	bank.depositAllExcept(Essentials); //This is the dream - doesn't work how I want it to though, of course.
}

 

 

The mission here is to bank everything except the items that the bot will be using, plus coins. The above code works, but only if levelFishType isn't baitFishing. It won't crash the script, but instead the bot will bank everything except from the fishing rod(because of the [0] index), but bank the fishing bait which is not what I want. I've heard of varargs, and have tried multiple syntax combinations to try getting it to work. Could I get some guidance on this?

For example, I've tried this, but obviously, it doesn't work.


public final String[] Essentials = { levelFishType.getTool(String[]...varName), "Coins" };

Thanks!!

Well, the reason that you're having problems is not because of syntax, it's because you're trying to put a String array inside a String array. Naturally, this isn't going to work since typing in arrays must be consistent and String[] is not the same as String.

If you've got dynamic sizing, it is much cleaner and easier to work with lists. You might want to look into those instead :)

Lists can easily be converted to and from arrays, and provide a versatile means to dynamically add and remove elements.

Good luck!

Apa

 

  • 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...