Epos OSBot Posted June 6, 2020 Share Posted June 6, 2020 public class Main extends Script { private ArrayList<Task> tasks = new ArrayList<Task>(); public static MethodProvider MP = new MethodProvider(); @Override public void onStart() throws InterruptedException { Collections.addAll(tasks, new Bank(), new Teleport(), new BuyBoots()); } @Override public int onLoop() throws InterruptedException { for (Task task : tasks) { if (task != null && task.validate()) { task.execute(); } } return random(250, 750); } } public class BuyBoots implements Task { @Override public boolean validate() { return !Main.MP.getInventory().contains("Coins"); } @Override public void execute() { Main.MP.log("Executing BuyBoots task."); } } This part is giving me an NPE: return Main.MP.getInventory().contains("Coins"); I tried defining a MethodProvider instance in the BuyBoots class, tried using a getter method in the Main class instead of a static variable etc. but when I start the script this line gives me an NPE and the script stops responding. Can anyone help? Quote Link to comment Share on other sites More sharing options...
Avrae Posted June 6, 2020 Share Posted June 6, 2020 (edited) Try a private method provider type in your buyboots class, then in the onstart of main call the constructor for buyboots class, passing in (this) to assign to the private method provider type in the buyboots class main class- onstart { buyboots = new buyboots(this) } // boots class- private methodprovider methods public void buyboots (methodprovider mp) { methods = mp } then u can do return methods.getInventory().contains("Coins"); Edited June 6, 2020 by Avrae Quote Link to comment Share on other sites More sharing options...
Nbacon Posted June 6, 2020 Share Posted June 6, 2020 Avrae works but this will fix your problem without change any of your old code. public static MethodProvider MP = this;//script extends the MethodProvider 1 Quote Link to comment Share on other sites More sharing options...
Epos OSBot Posted June 6, 2020 Author Share Posted June 6, 2020 Fixed by using a constructor in the BuyBoots class. Quote Link to comment Share on other sites More sharing options...
Camaro Posted June 6, 2020 Share Posted June 6, 2020 You could also fix that by calling MP.exchangeContext(getBot()) in onStart() Quote Link to comment Share on other sites More sharing options...
Avrae Posted June 6, 2020 Share Posted June 6, 2020 39 minutes ago, Malcolm said: Unless you are calling a method from your main class I would just extend MethodProvider in your other classes and exchange context, therefore inheriting all of the methods from that class, public class BuyBoots extends MethodProvider implements Task { @Override public boolean validate() { return !getInventory().contains("Coins"); } @Override public void execute() { log("Executing BuyBoots task."); } } public class Main extends Script { private ArrayList<Task> tasks = new ArrayList<Task>(); private final BuyBoots boots = new BuyBoots(); public void onStart() { boots.exchangeContext(getBot()); Collections.addAll(tasks, new Bank(), new Teleport(), boots); //would use the same logic with the other classes as well. } That's cool I've just been passing the methodprovider like i said above, could you explain the collections.addAll part for me? Is this giving the boots class access to the bank and teleport classes and methods without the need for a methodprovider? so you can just do bank.xyz in the boots class instead of mp.bank.xyz? Quote Link to comment Share on other sites More sharing options...
Avrae Posted June 6, 2020 Share Posted June 6, 2020 34 minutes ago, Malcolm said: @Avrae Collections.addAll(tasks, new Bank(), new Teleport(), boots); //would use the same logic with the other classes as well. All this does is add the tasks to the ArrayList here: private ArrayList<Task> tasks = new ArrayList<Task>(); You would not be able to access the methods of the other classes (Bank/Teleport/Any other tasks you have) but at the same time if a script has this kind of structure you should never have to. All that gets done is the loop goes through all of the tasks and if the validate method returns true it will execute that task. (the execute method in that specific task class) @Override public int onLoop() throws InterruptedException { for (Task task : tasks) { if (task != null && task.validate()) { task.execute(); } } return random(250, 750); } Ah! That makes more sense, thanks for the explanation gonna try this soon Quote Link to comment Share on other sites More sharing options...