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
  • Parent Script / Running a secondary script


    DesolationB

    Recommended Posts

    Fairly new to Java and still figuring out the basics of Dreambot API, but how would I go about making a script that I run upon client start to 'monitor' my status and react accordingly. Basically it just needs to run a few conditions, if the script I run after the 'monitor script' stops, run a third script, once the third script stops, run the second 'main' script again. I've tried a few methods without any luck as the Dreambot client doesn't seem to support dual scripts / parent scripts or I lack the knowledge to do so, any tips would be appreciated.

     

    Below is an example of some code I've found around the forums, tinkered with it in different formats as well.  I'm not entirely sure if the 'ScriptManager' is like a hidden class we can't access from a script or something as it says it isn't visible, not to familiar with the hierarchy stuff for Java, do I have to import it/make it accessible some how in Eclipse? 

    	public void onStart() {
    		//Start Restocker
    		log("Welcome to JHH Handling\n_________________________________________________\nTEST");
    		blast = false;
    		
    		for (java.lang.Class<? extends AbstractScript> scriptz : LocalLoader.getScripts()) {
    		  ScriptManager m = new ScriptManager(); //THIS LINE GIVES AN ERROR FOR SCRIPTMANAGER NOT BEING VISIBLE
    		  if (scriptz.getName().equalsIgnoreCase("main")) {
    			  Client.getInstance().getScriptManager().start(scriptz);
    		  }
    		}
    	}

     

    I have also tried something like this but it just seems to return the class name, I can't seem to run the onStart() function via the scriptz output, tried it with getAllScripts() and it returned the ScriptManifest I believe rather then the 'scriptz' object itself so I cant call the native 'OnStart' function to that 'scriptz' object. Think I need to call the onStart function for my other script via my monitor script, e.g: scriptz.onStart(); and then I assume I can find a way to check if that scripts running and toggle back and forth between the two scripts I need my monitor script to run. 

    		for (java.lang.Class<? extends AbstractScript> scriptz : LocalLoader.getScripts()) {
    		  scriptz.getClass();
    		  if (scriptz.getName().equalsIgnoreCase("G.E Restocker")) {
    			  log(scriptz.getSuperclass());
    			  log("Starting JHH - Handle Script\n Lets start the restocker.\n");
    		  }
    		}

     

    From looking at console logs of another script that does this it seems to 'stop' the script somehow without actually stopping, and this allows the client to run the secondary(or primary) script on the client?

    Apologies if I'm all over the place, come from other coding languages and Java is proving quite difficult for me to just pick up.

     

     

    Tldr; Making a parent script that will toggle between two other scripts, run one, if one stops, run two, if two stops, run one, repeat till stopped.

     

     

     

     

     

    Trying this new method for the time being.... it's definitely not optimized but it seems to do the job, if anyone has a better method please share / give any tips. Going to be running it tonight to see what happens, I have to start the script and click reload scripts, but once done it starts my first script, finishes, moves to the second script, once it stops, it should start the first script again. Will see in my tests...

    import java.util.Timer;
    import java.util.TimerTask;
    
    import org.dreambot.api.Client;
    import org.dreambot.api.methods.Calculations;
    import org.dreambot.api.script.AbstractScript;
    import org.dreambot.api.script.ScriptManifest;
    import org.dreambot.api.script.loader.LocalLoader;
    import org.dreambot.api.script.Category;
    import org.dreambot.api.script.ScriptManager;
    
    
    @ScriptManifest(author = "Kunit", category = Category.MISC, name = "JHH", version = 1.0)
    public class jhh extends AbstractScript {
    	
    	boolean started;
    	boolean ranx;
    	boolean shutdown;
    	boolean blast;
    	boolean stock;
    	boolean startscript;
    	public ScriptManager mg;
    	private Thread t1;
    	
    	public void onStart() {
    		//Start Restocker
    		log("Welcome to JHH Handling\n_________________________________________________\nTEST");
    		blast = false;	
    		
    	}
    	
    	@Override
    	public int onLoop() {
    		if (shutdown) {
    			log("SHUTTING DOWN - 3..2..1..");
    			stop();
    		} else {
    		ranx = true;
    		log("PRE CALL");
    		for (java.lang.Class<? extends AbstractScript> scriptz : LocalLoader.getScripts()) {
    			  //final ScriptManager mg = Instance.getInstance().getScriptManager();
    
    
    			  if (scriptz.getName().equalsIgnoreCase("main")) {
    				  if(t1 == null)
    				  {
    			          t1 = new Thread (this, "TestThread");
    			          t1.start();
    			          sleep(3000);
    					  log("Running our script.\n\n");
    					  stop();
    					  sleep(3000);
    				  }
    				  else
    				  {
    					  log("Thread already exists.\n\n\n\n");
    					  stop();
    				  }
    			  }
    			  stop();
    			}
    		}
    		return Calculations.random(250, 400);
    	}
    	
    	boolean ni = false;
    	boolean startedb = false;
    	
    	public void run() {
    		log("TESTINZZZ");
    
    		if(ScriptManager.getScriptManager().isRunning())
    		{
    			try {
    				Thread.sleep(1000);
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    
    			Timer rotationTimer = new Timer(true);
    			TimerTask task = new TimerTask() {
    			@Override
    				public void run() {
    					if(Client.getInstance() == null || Client.getInstance().getScriptManager() == null || Client.getInstance().getScriptManager().getCurrentScript() == null) {
    						log("Client or instance was null.\n_____________________________________________________________\n\n\n");
    						ni = true;
    						Client.getInstance().getScriptManager().start("# Blast Furnace", "adamant", "no", "yes", "no");
    						Client.getInstance().getScriptManager().reloadLastScript();
    					}
    					else
    					{
    						String SN = Client.getInstance().getScriptManager().getCurrentScript().toString();
    				    	int intIndex = SN.indexOf("main");
    				    	log(SN);
    						if(ScriptManager.getScriptManager().isRunning() && started && intIndex == - 1)
    						{
    							//Were good
    							int intIndexb = SN.indexOf("jhh");
    							if(intIndexb == - 1)
    							{
    								int intIndexd = SN.indexOf("y");
    								if(intIndexd == - 1 || startedb == false)
    								{
    									Client.getInstance().getScriptManager().start("# Blast Furnace", "adamant", "no", "yes", "no");
    									Client.getInstance().getScriptManager().reloadLastScript();
    									startedb = true;
    								}
    							}
    							else
    							{
    								log("Dont need to run new script.\n");
    							}
    						}
    						else
    						{
    							//Start
    							if(stock && intIndex == - 1)
    							{
    								log("Starting blast furnace.\n____________________");
    								blast = true;
    								stock = false;
    								Client.getInstance().getScriptManager().start("# Blast Furnace", "adamant", "no", "yes");
    							}
    							else
    							{
    								AbstractScript SNN = Client.getInstance().getScriptManager().getCurrentScript();
    								if(SNN != null)
    								{
    									String TStr = SNN.toString();
    									int intIndexc = TStr.indexOf("main");
    									if(intIndexc == - 1)
    									{
    										log("Starting stocker.\n____________________");
    										blast = false;
    										stock = true;
    										Client.getInstance().getScriptManager().start("G.E Restocker");
    									}
    								}
    							}
    							started = true;
    						}
    					}
    				}
    			};
    			if(!ni)
    			{
    				rotationTimer.scheduleAtFixedRate(task, 5000, 7000);
    			}
    		}
    		
    	 }
    	
    	public class TestThread {
    		   public void main(String args[]) {
    			   log("WE ARE RUNNING A THREAD");
    		   }   
    		}
    }
    

     

    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.