Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Gem Trader


    Pugaa99

    Recommended Posts

    import org.dreambot.api.methods.Calculations;
    import org.dreambot.api.methods.map.Tile;
    import org.dreambot.api.script.AbstractScript;
    import org.dreambot.api.script.Category;
    import org.dreambot.api.script.ScriptManifest;
    import org.dreambot.api.utilities.Timer;
    import org.dreambot.api.wrappers.interactive.NPC;
    
    import java.awt.*;
    
    @ScriptManifest(name = "Al-Kharid Gem Trader", author = "Pugaa", description = "Buys gems in Al-Kharid", version = 1, category = Category.MONEYMAKING)
    public class gemTrader extends AbstractScript {
    	
    	Tile gemStand = new Tile(3288,3210);
    	Tile bankArea = new Tile(3270,3166);
    
    	private Timer t = new Timer();
    
    	@Override
    	public void onStart() {
    		log("Thank you for running my script!");
    	}
    
    	@Override
    	public int onLoop() {
    			if(!getInventory().isFull()){
    				log("buying");
    				NPC gemTrader = getNpcs().closest(npc -> npc != null && npc.getName().equals("Gem trader"));
    				if(gemTrader.interact("Trade")){
    					if(getShop().count("Uncut sapphire") >= 1){
    						getShop().purchaseOne("Uncut sapphire");
    					}
    				if(getInventory().isFull()){
    						log("Walking");
    						getWalking().walk(bankArea.getRandomizedTile());
    				}
    			}
    			log("nothing");
    			return 600;
    		}
    
    		return Calculations.random(300, 600);
    	}
    
    	@Override
    	public void onExit() {
    
    	}
    
    	public void onPaint(Graphics2D g) {
    		g.setColor(Color.WHITE);
    		g.setFont(new Font("Arial", 1, 11));
    		g.drawString("Time Running: " + t.formatTime(), 25, 50);
    	}
    }
    

    I am making an Al-Kharid gem buyer, banker and hopper. Am I going about it the right way? I'm doing this mainly to learn. I can't get my player to move to the bank tile that I set. Any improvement suggestions?

    Link to comment
    Share on other sites

    First of all you can use the lovely API Dreambot devs have specially made: Banklocations.

     

    You don't have to set a tile, the API will handle your walking.

     

    First suggestion

    For "Tile bankArea = new Tile(3270,3166);" you could use something static, like BankLocation.

    Tile bankArea = BankLocation.AL_KHARID.getCenter();
    

    If you don't want the center tile, but a random tile in the bank area, use this:

    Be aware that if you put this outside of your loop, the random tile will be generated ONCE on startup and will always be the same untill you re-run the script. So place it inside your loop if you want it to be real random (see step 5)

    Tile bankArea = BankLocation.AL_KHARID.getArea(4).getRandomTile();
    // the "4" is the radius of the Area, based on the center tile of the bank
    

    Second suggestion

    If you make an object of the Timer class in your onStart(), the time will actually start running when the script starts. For example: if i set it as a global variable, the Timer will start upon loading the script. If i take 10 minutes to fill out the GUI for the script, the painted time in your onPaint() will "start" at 10 minutes. Nothing big really, but i like to make most of my objects at onStart() to prevent them from being made multiple times.

     

    Third suggestion

    @Override
    public int onLoop() {
       if(!getInventory().isFull()){
    

    Put your first line like this and let it jump in like that (feather i believe it's called) for the next lines, that'll give you some overview of the script. Oh it might also be the forums just messing with the feathering, my bad.

     

    Fourth suggestion

    Does the shop always have one sapphire in stock? If so:

    if(getShop().count("Uncut sapphire") = 1){ //notice i replaced ">=" with just "="
       getShop().purchaseOne("Uncut sapphire");
    

    If the shop has or can have more than one, lets say 7, you're better of making it more human-like rather than choosing "buy one" option 7 times:

    if(getShop().count("Uncut sapphire") >= 1){
       getShop().purchaseTen("Uncut sapphire");
    

    If the shop has an "buy-X" option, then you could check how many sapphires are in stock at that time, or just let it loop "buy ten".

     

    Fifth suggestion

    Make it more flawless by double checking stuff (E.G. if the store is open before you want to buy). Using nested statements is useful in this situation ("else" keys).

    I found out that you were checking if the inventory is full INSIDE of your if(!getInventory().isFull()){ body. This will fail for sure, becuase if the inventory is full, it skips the whole body. Also the log("nothing") and "return 600;" aren't useful. To explain it visually, take a look at the code below:

     

     

     

    if(!getInventory().isFull()){ //IF THIS RETURNS FALSE (SO THE INVENTORY IS FULL)
       if(getShop().isOpen()){
          log("buying");
          if(getShop().count("Uncut sapphire") >= 1){
             getShop().purchaseTen("Uncut sapphire");
          }
    } else {
       NPC gemTrader = getNpcs().closest(npc -> npc != null && npc.getName().equals("Gem trader"));
       if(gemTrader.interact("Trade")){
          sleepUntil(getShop().isOpen(), 6000); //pretty sure this isn't the correct code, writing this without an IDE lol.
          }
       }
    //THEN ALL THE STRIKED CODE WILL BE SKIPPED.
    } else { //THE ELSE KEY THEN TELLS THE SCRIPT TO CONTINUE HERE, READ IT AS: ELSE IF INVENTORY IS FULL, THEN DO THIS..
       if(getInventory().isFull()){ //don't need to check for this, because this code will execute if !getInventory().isFull() returns false (inventory then is full)
          log("Walking");
          Tile bankArea = BankLocation.AL_KHARID.getArea(4).getRandomTile();
          getWalking().walk(bankArea); //could add more checks like if player distance > 5 to tile and stuff, this is just a basic walking method.
       }
    }

     

     

    Look at my adjusted code below. It should be more flawless:

    if(!getInventory().isFull()){
    	if(getShop().isOpen()){
    	log("buying");
    		if(getShop().count("Uncut sapphire") >= 1){
    			getShop().purchaseTen("Uncut sapphire");
    		}
    	} else {
    		NPC gemTrader = getNpcs().closest(npc -> npc != null && npc.getName().equals("Gem trader"));
    		if(gemTrader.interact("Trade")){
    			sleepUntil(getShop().isOpen(), 6000); //pretty sure this isn't the correct code, writing this without an IDE lol.
    		}
    	}
    } else {
    	if(getInventory().isFull()){ //don't need to check for this, because this code will execute if !getInventory().isFull() returns false (inventory then is full)
    		log("Walking");
    		Tile bankArea = BankLocation.AL_KHARID.getArea(4).getRandomTile();
    			getWalking().walk(bankArea); //could add more checks like if player distance > 5 to tile and stuff, this is just a basic walking method.
    		}
    	}
    

    Overall your code is not too bad dude! I just made little changes. Great job on that!

    Link to comment
    Share on other sites

    Cardozz gave some helpful suggestions, but it isn't working because you have a check for the inventory being full within a check for the inventory not being full. It will never reach it with the inventory full.'

     

    edit: rip didnt read Cardozz's full post

    Link to comment
    Share on other sites

    Cardozz gave some helpful suggestions, but it isn't working because you have a check for the inventory being full within a check for the inventory not being full. It will never reach it with the inventory full.

    Thank you! I also explained that in my fifth suggestion :)

    Link to comment
    Share on other sites

    Thank you! I also explained that in my fifth suggestion :)

     

     

    First of all you can use the lovely API Dreambot devs have specially made: Banklocations.

     

    You don't have to set a tile, the API will handle your walking.

     

    First suggestion

    For "Tile bankArea = new Tile(3270,3166);" you could use something static, like BankLocation.

    Tile bankArea = BankLocation.AL_KHARID.getCenter();
    

    If you don't want the center tile, but a random tile in the bank area, use this:

    Be aware that if you put this outside of your loop, the random tile will be generated ONCE on startup and will always be the same untill you re-run the script. So place it inside your loop if you want it to be real random (see step 5)

    Tile bankArea = BankLocation.AL_KHARID.getArea(4).getRandomTile();
    // the "4" is the radius of the Area, based on the center tile of the bank
    

    Second suggestion

    If you make an object of the Timer class in your onStart(), the time will actually start running when the script starts. For example: if i set it as a global variable, the Timer will start upon loading the script. If i take 10 minutes to fill out the GUI for the script, the painted time in your onPaint() will "start" at 10 minutes. Nothing big really, but i like to make most of my objects at onStart() to prevent them from being made multiple times.

     

    Third suggestion

    @Override
    public int onLoop() {
       if(!getInventory().isFull()){
    

    Put your first line like this and let it jump in like that (feather i believe it's called) for the next lines, that'll give you some overview of the script. Oh it might also be the forums just messing with the feathering, my bad.

     

    Fourth suggestion

    Does the shop always have one sapphire in stock? If so:

    if(getShop().count("Uncut sapphire") = 1){ //notice i replaced ">=" with just "="
       getShop().purchaseOne("Uncut sapphire");
    

    If the shop has or can have more than one, lets say 7, you're better of making it more human-like rather than choosing "buy one" option 7 times:

    if(getShop().count("Uncut sapphire") >= 1){
       getShop().purchaseTen("Uncut sapphire");
    

    If the shop has an "buy-X" option, then you could check how many sapphires are in stock at that time, or just let it loop "buy ten".

     

    Fifth suggestion

    Make it more flawless by double checking stuff (E.G. if the store is open before you want to buy). Using nested statements is useful in this situation ("else" keys).

    I found out that you were checking if the inventory is full INSIDE of your if(!getInventory().isFull()){ body. This will fail for sure, becuase if the inventory is full, it skips the whole body. Also the log("nothing") and "return 600;" aren't useful. To explain it visually, take a look at the code below:

     

     

     

    if(!getInventory().isFull()){ //IF THIS RETURNS FALSE (SO THE INVENTORY IS FULL)

       if(getShop().isOpen()){

          log("buying");

          if(getShop().count("Uncut sapphire") >= 1){

             getShop().purchaseTen("Uncut sapphire");

          }

    } else {

       NPC gemTrader = getNpcs().closest(npc -> npc != null && npc.getName().equals("Gem trader"));

       if(gemTrader.interact("Trade")){

          sleepUntil(getShop().isOpen(), 6000); //pretty sure this isn't the correct code, writing this without an IDE lol.

          }

       } //THEN ALL THE STRIKED CODE WILL BE SKIPPED.

    } else { //THE ELSE KEY THEN TELLS THE SCRIPT TO CONTINUE HERE, READ IT AS: ELSE IF INVENTORY IS FULL, THEN DO THIS..

       if(getInventory().isFull()){ //don't need to check for this, because this code will execute if !getInventory().isFull() returns false (inventory then is full)

          log("Walking");

          Tile bankArea = BankLocation.AL_KHARID.getArea(4).getRandomTile();

          getWalking().walk(bankArea); //could add more checks like if player distance > 5 to tile and stuff, this is just a basic walking method.

       }

    }

     

     

    Look at my adjusted code below. It should be more flawless:

    if(!getInventory().isFull()){
    	if(getShop().isOpen()){
    	log("buying");
    		if(getShop().count("Uncut sapphire") >= 1){
    			getShop().purchaseTen("Uncut sapphire");
    		}
    	} else {
    		NPC gemTrader = getNpcs().closest(npc -> npc != null && npc.getName().equals("Gem trader"));
    		if(gemTrader.interact("Trade")){
    			sleepUntil(getShop().isOpen(), 6000); //pretty sure this isn't the correct code, writing this without an IDE lol.
    		}
    	}
    } else {
    	if(getInventory().isFull()){ //don't need to check for this, because this code will execute if !getInventory().isFull() returns false (inventory then is full)
    		log("Walking");
    		Tile bankArea = BankLocation.AL_KHARID.getArea(4).getRandomTile();
    			getWalking().walk(bankArea); //could add more checks like if player distance > 5 to tile and stuff, this is just a basic walking method.
    		}
    	}
    

    Overall your code is not too bad dude! I just made little changes. Great job on that!

    Thank you all so much! Hopefully some day I can make some good scripts like you!

    Link to comment
    Share on other sites

    Thank you all so much! Hopefully some day I can make some good scripts like you!

    Remember that i also started at your level, even below that. Just keep practicing and playing around with code!

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.