Jump to content

Vilius

Scripter II
  • Posts

    1495
  • Joined

  • Last visited

  • Days Won

    1
  • Feedback

    100%

Everything posted by Vilius

  1. People are intimidated by Task/Node based scripts because they think its difficult to write and understand. Well you shouldn't be scared, because I'm going to cover everything you need to know. Before going on to the tutorial, I'm expecting from you, To have at least basic knowledge of java To have at least basic knowledge of the OSbots API So what are Task/Node based scripts? Well its simply states but in OOP design. Alright, so we are going to make an abstract class for a Task. We will use this code which I will explain in a bit. import org.osbot.rs07.script.MethodProvider; public abstract class Task { protected MethodProvider api; public Task(MethodProvider api) { this.api = api; } public abstract boolean canProcess(); public abstract void process(); public void run() { if (canProcess()) process(); } } Breaking down this code just comes simply to a few things. A constructor which accepts the MethodProvider class, but why shouldn't it accept the Script class? Well because we only want the task to know about the OSbot api, not the whole methods that the Script class can have, like onLoop(), onStart(), etc. protected MethodProvider api; public Task(MethodProvider api) { this.api = api; } An abstract boolean public abstract boolean canProcess(); An abstract method public abstract void process(); And a run method public void run() { if (canProcess()) process(); } When a class will inherit the methods from the Task class, that class will have a constructor, an abstract method and boolean. The boolean canProcess() will be the condition on which the process() method will execute code. So basically we do the checks and processing with the run() method, we would just check if the condition is true and let the process() method execute code accordingly. Now, we got that abstract class Task, we are going to make it do work for us. We are going to make a drop task, the class will extend the Task class and it will inherit the abstract methods which the Task class has. The drop task will have a condition, the condition will be api.getInventory().isFull(), the condition will return true after the inventory is full and let the process() method execute script accordingly, the code will be api.getInventory().dropAll(); Our class will look something like this import org.osbot.rs07.script.MethodProvider; public class DropTask extends Task { public DropTask(MethodProvider api) { super(api); } @Override public boolean canProcess() { return api.getInventory().isFull(); } @Override public void process() { api.getInventory().dropAll(); } } As you can see we declare a constructor public DropTask(MethodProvider api) { super(api); } Now you might ask why the constructor looks a bit differently from the abstract Task class and what super(api) means? the super() keyword invokes the parents class constructor AKA the Task's class constructor. Now going further we see @Override public boolean canProcess() { return api.getInventory().isFull(); } So that is how our condition is handled, we just return the booleans variable based on what the getInventory().isFull() returns. In term, if the inventory is full the condition will be true, if the inventory is empty the condition will be false. Further on we see @Override public void process() { api.getInventory().dropAll(); } This is basically what our custom task will do after the canProcess() is true, aka when the condition is true. If inventory is full -> we will drop the items. ' After we got our custom task done, we need to let our script execute the tasks, now how we will do it? Simply we will open our main class which extends Script, meaning that the main class is a script. We declare a new ArrayList at the top of our class, to which we will add tasks to. Our main class should look like this import java.util.ArrayList; import org.osbot.rs07.script.Script; public class Main extends Script{ //this is our array list which will contain our tasks ArrayList<Task> tasks = new ArrayList<Task>(); @Override public int onLoop() throws InterruptedException { return 700; } } Now we have our ArrayList which we have a non primitive data type in it (<Task>), in term think of it like the ArrayList is inheriting the methods from Task class. All though its a different topic called https://en.wikipedia.org/wiki/Generics_in_Java you can look into that if you want. Alright, so we have our ArrayList, time to add our tasks to it. In our onStart() method, which only executes once when the script starts we simply add tasks.add(new DropTask(this)); So basically we are adding a new task to the ArrayList, by doing that, to the .add() we add a new object of our DropTask by doing .add(new DropTask(this)); our DropTask has a constructor for MethodProvider which we simply pass by doing DropTask(this) the keyword this references this class that contains the code. Now why does it work by referencing this class, its because the Main class extends Script, the Script class extends MethodProvider as its stated in the OSBots API docs. So we added our tasks to the ArrayList, now we need to check the conditions state and execute the code accordingly. We simply add this to our onLoop() method. tasks.forEach(tasks -> tasks.run()); Which will iterate trough all the tasks in the ArrayList and execute the run() method, as we remember the run() method in our abstract Task script simply checks the condition and executes code if the condition is true. After all this our main class should look like import java.util.ArrayList; import org.osbot.rs07.script.Script; public class Main extends Script{ ArrayList<Task> tasks = new ArrayList<Task>(); @Override public void onStart(){ tasks.add(new DropTask(this)); } @Override public int onLoop() throws InterruptedException { tasks.forEach(tasks -> tasks.run()); return 700; } } Now after all this you can start making your own tasks and adding them to the list. Alright for those who want quick pastes you can find them here in this spoiler: So no need to be scared, you just really need to try and do it, hope this tutorial helped.
  2. Vilius

    Varies ~Antiban~

    Holy mother of spaghetti code
  3. Its monday here, valentines day ended, fuck off
  4. Hmm, Im alone, one parent is dead.
  5. http://osbot.org/forum/topic/92518-help-scripts-not-showing-psa/
  6. The developers are making updates to the SDN, scripts should show up when its updates. Please don't come to the chatbox and ask why there is no scripts. -Love, Vilius and the chatbox people
  7. Yes, 07. Let us know in chatbox or on skype if you want to join :P
  8. Anyone interested in pking tommorow? 80+ cmb pls Who will pk? Me, Zappa, Keven(maybe) need a few more people
  9. Make crepes, put slice of banana in it, cook it in a pan till its crispy brown Get some grape jelly put on top, whipped cream/sour cream And some apple juice with those crepes. Luv it
  10. Oh, well. I guess the true love of my life is a computer. Which would mean that I'm going to seducevely touch its keys while I write code.
  11. You could try this: startButton.addActionListener(e -> { started = true; monsterSelect = selectYourMonster.getText().toString(); gui.setVisible(false); }); And if you are doing this to stop your script: public void onStart(){ gui.setVisible(true); while(started != true){ Thread.sleep(); } } Don't do it, do it like I show in this tutorial: http://osbot.org/forum/topic/91963-using-synchronized-to-your-advantage/
×
×
  • Create New...