Jump to content

Tasks Timer overriding another classes timer


Tylerart

Recommended Posts

Hey guys im new here so bare with me. I currently have my script set up in tasks which rechecks every 7 secconds

 

public void onStart(){
    tasks.add(new BasicTask(this));
    tasks.add(new LetsAfk(this));
    tasks.add(new HandleBank(this));
    tasks.add(new HandleLobby(this));
}


@Override
public int onLoop() throws InterruptedException {
    tasks.forEach(Task::run);
    return 7000;
}

 

From what i understand the way tasks work is it checks for a task every 7 seconds and once it finds a one that is valid it will run that until its not. One of my tasks works that if its in a certain position to start a afk timer and then move the mouse in a random interval from 1 second to like 4 mins and then repeat. My issue is tho even though the task is being activated and running the correct code for that task it is still getting refreshed every 7 seconds completely overriding the longer afk timer.

 

    @Override
    public boolean canProcess() {
        return api.myPosition().equals(AFK_POS_Z);

    }


    public int waitTime(int min, int max) {
        return (int)(Math.random() * (max - min)) + min;
    }


    @Override
    public int process() {
        api.log("in AFK AREA");
        String zamMessage = api.getWidgets().get(59, 17).getMessage().substring(10);
        api.log(zamMessage);
         //int zamMessageInt = Integer.parseInt(zamMessage);
        //api.log(zamMessageInt);
        String saraMessage = api.getWidgets().get(59, 18).getMessage().substring(0,1);
        api.log(saraMessage);
        int saraMessageInt = Integer.parseInt(zamMessage);
        api.log(saraMessageInt);

        if (api.mouse.isOnScreen()) {
            api.mouse.moveOutsideScreen();
        } else {
            api.mouse.move((int) (100 + (Math.random() * 660)), (int) (100 + (Math.random() * 400)));
        }
        return waitTime(1000, 260000);//(int) (Math.random() * 1000));
    }
}
Link to comment
Share on other sites

I believe what is happening is the Task is an Async operation causing the task to run, but not freezing the rest of the code execution.

I created a CustomEvent class that can be set as an Async operation or kept as a Sync one.

Quote

package data;

import java.util.Random;

public abstract class CustomEvent {

    private int minWaitTime, maxWaitTime;
    private boolean isAsync = false;

    // Construct with the isAsync parameter, allowing you to set it as Async or not.
    public CustomEvent(boolean isAsync) {
        this.isAsync = isAsync;
    }

    // Construct without the isAsync parameter, defaulting the operation as a Sync one.
    public CustomEvent(int minWaitTime, int maxWaitTime) {
        this.minWaitTime = minWaitTime;
        this.maxWaitTime = maxWaitTime;
    }

    // The check that can be used before executing the task to see if you should or not.
    public abstract boolean canExecute();

    // Everything the task will perform will come from here.
    public abstract void onExecute();

    // You can add the canExecute() check in here if you do not want to check for it before running the task.
    public void execute() {

        if (!isAsync) {
            try {
                int randomSleepTime = random(minWaitTime, maxWaitTime);
                onExecute();
                Thread.sleep(randomSleepTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } else {
            new Thread(this::onExecute).start();
        }
    }

    private int random(int min, int max) {
        Random random = new Random();
        return random.nextInt(max - min + 1) + min;
    }
}

Example:

Quote

package core;

import data.CustomEvent;
import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

import java.util.HashSet;

@ScriptManifest(info = "", version = 1.0, logo = "", author = "BravoTaco", name = "Custom Events")
public class Main extends Script {

    // The HashSet that will hold all the tasks.
    private HashSet<CustomEvent> customEvents = new HashSet<>();

    // Temp var to be used within the example.
    private Position afkPosZ;

    // Creating of the task. The two integer parameters are the min and max sleep time to be chosen between.
    private CustomEvent moveMouseRandomly = new CustomEvent(1000, 260000) {
        @Override
        public boolean canExecute() {
            return myPosition().equals(afkPosZ);
        }

        @Override
        public void onExecute() {
            getMouse().move(random(100, 500), random(100, 500));
        }
    };

    // Creating an Async event.
    private CustomEvent asyncOperation = new CustomEvent(true) {
        @Override
        public boolean canExecute() {
            return true;
        }

        @Override
        public void onExecute() {
            int amountOfExecutions = 0;

            while (amountOfExecutions < 5) {
                log("Executing Async");
                getMouse().move(random(100, 500), random(100, 500));
                try {
                    Thread.sleep(random(1000, 5000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                amountOfExecutions++;
            }
        }
    };

    // Set the temp position var to be used equal to my players position.
    // I than add the tasks to the HashSet
    @Override
    public void onStart() throws InterruptedException {
        super.onStart();
        afkPosZ = myPosition();
        customEvents.add(moveMouseRandomly);
        customEvents.add(asyncOperation);
    }

    // Every time this loops it will check the tasks and than execute them accordingly.
    // You wont need the check for the canExecute() if you have that inside of the execute()
    @Override
    public int onLoop() throws InterruptedException {
        customEvents.forEach((e) -> {
            if (e.canExecute())
                e.execute();
        });
        return 7000;
    }

    @Override
    public void onExit() throws InterruptedException {
        super.onExit();
    }
}

 

Edited by BravoTaco
  • Like 1
Link to comment
Share on other sites

7 hours ago, BravoTaco said:

I believe what is happening is the Task is an Async operation causing the task to run, but not freezing the rest of the code execution.

I created a CustomEvent class that can be set as an Async operation or kept as a Sync one.

This is no different.

If you run a non-sync event followed by an async event, the async event is blocked from the former event running and will only start once the first event finishes (the non-sync event).

Link to comment
Share on other sites

51 minutes ago, dreameo said:

This is no different.

If you run a non-sync event followed by an async event, the async event is blocked from the former event running and will only start once the first event finishes (the non-sync event).

This is true but if you run the async event first it can execute while the sync one is executing. Just depends on the order of which you are calling your tasks.

Link to comment
Share on other sites

1 hour ago, BravoTaco said:

This is true but if you run the async event first it can execute while the sync one is executing. Just depends on the order of which you are calling your tasks.

Consider this:

A = blocking for 5 seconds

B = non-blocking

 

AB -> we don't reach B until 5 seconds pass

BA-> B executes concurrently with A. On the next Iteration however (BABA), the second B must wait until the first A finishes.

Doesn't matter how it gets ordered, A is a blocking call that affect all events.

 

few options:

- A synchronous event is long lived (has it's own loop and gets called once)

- Non-sync threads are responsible for creating async events when needed

 

Really depends on the situation/context.

Link to comment
Share on other sites

1 hour ago, dreameo said:

Consider this:

A = blocking for 5 seconds

B = non-blocking

 

AB -> we don't reach B until 5 seconds pass

BA-> B executes concurrently with A. On the next Iteration however (BABA), the second B must wait until the first A finishes.

Doesn't matter how it gets ordered, A is a blocking call that affect all events.

 

few options:

- A synchronous event is long lived (has it's own loop and gets called once)

- Non-sync threads are responsible for creating async events when needed

 

Really depends on the situation/context.

Oooh you are talking about it not getting called within the main thread until the pause is over. This is just a logic thing though? The sync call is made to block the current thread it is running on. In knowing that you would just have to structure your code for it.

Link to comment
Share on other sites

2 hours ago, BravoTaco said:

Oooh you are talking about it not getting called within the main thread until the pause is over. This is just a logic thing though? The sync call is made to block the current thread it is running on. In knowing that you would just have to structure your code for it.

Not really sure what you're saying.

 

Just pretend OP uses your stuff. He will have some sync and async events. The async events will be blocked by the sync events and his problem will not be solved.

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