Jump to content

Task based scripting


RXScripts

Recommended Posts

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.

Link to comment
Share on other sites

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 by Eagle Scripts
  • Like 1
Link to comment
Share on other sites

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 by Imthabawse
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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 😎.

 

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...