RXScripts Posted April 24, 2019 Share Posted April 24, 2019 I am getting into scripting using tasks and I have a question about inheritance and how to go about doing this. I have my main class: public class Test extends Script { ArrayList<Task> tasks = new ArrayList<Task>(); @Override public void onStart() throws InterruptedException { // Add tasks tasks.add(new DropTask(this)); tasks.add(new CutTask(this)); } @Override public final int onLoop() throws InterruptedException { tasks.forEach(tasks -> tasks.run()); return random(100,300); } } And I have my task class: public abstract class Task extends Test { protected MethodProvider api; public Task(MethodProvider api) { this.api = api; } public abstract boolean canProcess(); public abstract void process(); public void run() { if (canProcess()) process(); } } And I have my cutting task: public class CutTask extends Task { public CutTask(MethodProvider api) { super(api); } @Override public boolean canProcess() { return !api.getInventory().isFull(); } @Override public void process() { api.log("Chop tree task initiated!"); Entity tree = api.getObjects().closest(1276,1278); if (tree != null && tree.interact("Chop down")) { // Just testing } } } Now the above code is super basic while I get my feet wet again in scripting (I haven't scripted in over 6 years so I'm trying to jump start my memory). Basically what I want to do is set a variable, most likely should be in the main class (Test), for the tree Entity. And I want to be able to access and edit this Entity variable from within the tasks themselves. My reasoning for this is that I want to be able to check in the task canProcess() that a tree has been selected, and if it has, that it still exists. This will allow my script to more quickly respond to when a tree is cut down, rather than relying on the cutting animation which often lags behind the actual tree being cut down. Quote Link to comment Share on other sites More sharing options...
Eagle Scripts Posted April 24, 2019 Share Posted April 24, 2019 (edited) You can do this by making an instance of a variable/class in your main class and passing it to your tasks. If you change anything on the passed object in a task, its values will also 'sync' in the main as the object has the same pointer, you're referring to the same object. That being said, look into OOP principles as this is basic stuff. Edited April 24, 2019 by Eagle Scripts 1 Quote Link to comment Share on other sites More sharing options...
Imthabawse Posted April 24, 2019 Share Posted April 24, 2019 (edited) Why not just make a drop method and cut method like so Private void drop () { If (getInventory.isFull) { GetInventory.dropAllExcept("whateveraxe"); } } Private void cut () { Entity tree = getObjects.closest ("Tree"); If (!getInventory.isFull && !myPlayer.isAnimating && tree != null) { Tree.interact ("Chop"); } } ! Means NOT so !getInventory.isFull means inventory is NOT full Then put these methods in your onLoop This is how I build my scripts, what works for me may not work for you. Hope this helps. Edited April 24, 2019 by Imthabawse Quote Link to comment Share on other sites More sharing options...
Night Posted April 24, 2019 Share Posted April 24, 2019 43 minutes ago, Imthabawse said: Why not just make a drop method and cut method like so Private void drop () { If (getInventory.isFull) { GetInventory.dropAllExcept("whateveraxe"); } } Private void cut () { Entity tree = getObjects.closest ("Tree"); If (!getInventory.isFull && !myPlayer.isAnimating && tree != null) { Tree.interact ("Chop"); } } ! Means NOT so !getInventory.isFull means inventory is NOT full Then put these methods in your onLoop This is how I build my scripts, what works for me may not work for you. Hope this helps. This is bad coding design and will only lead to problems. Some form of framework is necessary to keep code clean, organized, and efficient. Quote Link to comment Share on other sites More sharing options...
Imthabawse Posted April 24, 2019 Share Posted April 24, 2019 @Night understandable hence "What works for me may not work for you" in my case I've created numerous scripts that have gotten me many levels n no bans yet (knock on wood) with this form of coding. With that said I know this probably is not the best way to code but it's simple and easy for me to understand and maybe others as well. Quote Link to comment Share on other sites More sharing options...
Night Posted April 25, 2019 Share Posted April 25, 2019 47 minutes ago, Imthabawse said: @Night understandable hence "What works for me may not work for you" in my case I've created numerous scripts that have gotten me many levels n no bans yet (knock on wood) with this form of coding. With that said I know this probably is not the best way to code but it's simple and easy for me to understand and maybe others as well. Just because its simple and easy to understand now does not mean you should encourage poor practices on others. Part of programming is growing and learning, staying stuck in your ways because it seems easy is a poor attitude to have for anything. 1 Quote Link to comment Share on other sites More sharing options...
Imthabawse Posted April 25, 2019 Share Posted April 25, 2019 38 minutes ago, Night said: Just because its simple and easy to understand now does not mean you should encourage poor practices on others. Part of programming is growing and learning, staying stuck in your ways because it seems easy is a poor attitude to have for anything. Agree'd sir not saying stick with this form of coding but it's a start. I'm fairly new to scripting myself so just throwing my (very small) knowledge of code out there to (maybe) help someone out. onLoop gang . Quote Link to comment Share on other sites More sharing options...
RXScripts Posted April 25, 2019 Author Share Posted April 25, 2019 8 hours ago, Eagle Scripts said: You can do this by making an instance of a variable/class in your main class and passing it to your tasks. If you change anything on the passed object in a task, its values will also 'sync' in the main as the object has the same pointer, you're referring to the same object. That being said, look into OOP principles as this is basic stuff. Perfect thank you, I do remember OOP to a certain extent, its just that I need a refresher and the best way I learn is through trial and error like this. Quote Link to comment Share on other sites More sharing options...