Jump to content

Instantiating separate event executor


Camaro

Recommended Posts

1 hour ago, camaro 09 said:

I'm creating a framework for one script with multiple tasks.

Is it feasible to instantiate a separate event executor to run events?

What kind of other tasks are you trying to run while running the main task?

 

You could multi-thread: More Info Here

Class

Spoiler

class HelperThread implements Runnable {
    private volatile boolean run = true;
    private volatile Script script;

    public HelperThread(Script script) {
        this.script = script;
    }

    @Override
    public void run() {
        while (run) {
            if (conditionalSleep(() -> script.myPlayer().isAnimating(), 10000, 500)) {
                if (conditionalSleep(() -> !script.myPlayer().isAnimating(), 10000, 500)) {
                    script.log("No Longer Animating");
                }
            }
        }
    }

    private boolean conditionalSleep(BooleanSupplier condition, int maxTime, int checkTime) {
        long startTime = System.currentTimeMillis();
        while (!condition.getAsBoolean() && (System.currentTimeMillis() - startTime) < maxTime) {
            try {
                Thread.sleep(checkTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return condition.getAsBoolean();
    }

    public void stop() {
        this.run = false;
    }
}

Usage

Spoiler

import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

@ScriptManifest(name = "@Test", author = "BravoTaco", version = 0, info = "", logo = "")
public class Main extends Script {

    private HelperThread helperThread;

    @Override
    public void onStart() throws InterruptedException {
        helperThread = new HelperThread(this);
        new Thread(helperThread).start();
    }

    @Override
    public int onLoop() throws InterruptedException {

        log("Main Thread Running!");

        return random(1300, 2300);

    }

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

 

Edited by BravoTaco
  • Like 1
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...