Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

Question Help with an inactivity timer

Joined
Dec 20, 2015
Messages
6
Can someone help me make an in-activity timer, the idea being: the script stops if you do not gain xp in a desired skill within a specific time.

-I do not know javascript/java
-I have a feeling you would need to use a timer, but as you can see below I have no idea how to use timers
-I dont know where i would place this bit of code.

here is what i have so far:

int cExp = 0;

if(Skill.WOODCUTTING.getExperience() > cExp) { cExp = Skill.WOODCUTTING.getExperience(); } else {
Timer idle = new Timer(5000);
idle.start();
int x = 1;
if(idle.getElapsedTime() > x) {
System.out.println("Idle for " +x + "; Stopping");
stop();}
}

Just a simple bit of code with a lot of potential, especially within darkscape.

Thanks!
 
12 year old normie
Joined
Jan 8, 2015
Messages
2,768
JavaScript:
startExp = Skill.WOODCUTTING.getExperience();
Execution.delay(60000); //1 minute/60000 milliseconds
currentExp = Skill.WOODCUTTING.getExperience();
if (startExp == currentExp)  {
    this.stop();
}
 
EDIT: you probably want to have a timer instead of a delay, since it delays the entire bot.
 
JavaScript:
Timer timer = new Timer(50000);
startExp = Skill.WOODCUTTING.getExperience();
timer.start();
if (timer.getRemainingTime() == 0) {
    currentExp = Skill.WOODCUTTING.getExperience();
    if (startExp == currentExp) {
        this.stop();
        }
}
 
Last edited:
Joined
Dec 10, 2014
Messages
3,332
JavaScript:
startExp = Skill.WOODCUTTING.getExperience();
Execution.delay(60000); //1 minute/60000 milliseconds
currentExp = Skill.WOODCUTTING.getExperience();
if (startExp == currentExp)  {
    this.stop();
}
 
EDIT: you probably want to have a timer instead of a delay, since it delays the entire bot.
 
JavaScript:
Timer timer = new Timer(50000);
startExp = Skill.WOODCUTTING.getExperience();
timer.start();
if (timer.getRemainingTime() == 0) {
    currentExp = Skill.WOODCUTTING.getExperience();
    if (startExp == currentExp) {
        this.stop();
        }
}
Even then, the remaining time will always be > 0 because you're creating and starting the timer just before you check it xD

Code:
public class ExampleBot extends LoopingScript {
    private Timer inactivityTimer = new Timer(60000);
    private int lastExp;
    private Skill skillToCheck = Skill.WOODCUTTING;

    public void onStart(String... args) {
        inactivityTimer.start();
        lastExp = skillToCheck.getExperience();
    }

    public void onLoop() {
        if (lastExp == skillToCheck.getExperience() && !inactivityTimer.isRunning()) { //No exp gained since last timer reset
            stop();
            return;
        } else if (lastExp < skillToCheck.getExperience()) {
            lastExp = skillToCheck.getExperience();
            inactivityTimer.reset();
        }
        //Your code :D
    }
}

Or if you're using a TaskScript, you could create a Task to handle it.
Code:
public class InactivityStopper extends Task {
    private Timer inactivityTimer = new Timer(60000);
    private int lastExp;
    private Skill skillToCheck = Skill.WOODCUTTING;

    public InactivityStopper() {
        inactivityTimer.start();
        lastExp = skillToCheck.getExperience();
    }

    @Override
    public boolean validate() {
        if (lastExp == skillToCheck.getExperience() && !inactivityTimer.isRunning()) {
            return true;
        } else if (lastExp < skillToCheck.getExperience()) {
            lastExp = skillToCheck.getExperience();
            inactivityTimer.reset();
        }
        return false;
    }

    @Override
    public void execute() {
        AbstractScript bot = Environment.getScript();
        if (bot != null)
            bot.stop();
    }
}
Written off of the top of my head, without an IDE so there may be some mistakes xD

You could track multiple Skills by using a Skill array or Collection instead of a single Skill, and setting lastExp to the sum of all of their exp.
 
12 year old normie
Joined
Jan 8, 2015
Messages
2,768
Even then, the remaining time will always be > 0 because you're creating and starting the timer just before you check it xD

Code:
public class ExampleBot extends LoopingScript {
    private Timer inactivityTimer = new Timer(60000);
    private int lastExp;
    private Skill skillToCheck = Skill.WOODCUTTING;

    public void onStart(String... args) {
        inactivityTimer.start();
        lastExp = skillToCheck.getExperience();
    }

    public void onLoop() {
        if (lastExp == skillToCheck.getExperience() && !inactivityTimer.isRunning()) { //No exp gained since last timer reset
            stop();
            return;
        } else if (lastExp < skillToCheck.getExperience()) {
            lastExp = skillToCheck.getExperience();
            inactivityTimer.reset();
        }
        //Your code :D
    }
}

Or if you're using a TaskScript, you could create a Task to handle it.
Code:
public class InactivityStopper extends Task {
    private Timer inactivityTimer = new Timer(60000);
    private int lastExp;
    private Skill skillToCheck = Skill.WOODCUTTING;

    public InactivityStopper() {
        inactivityTimer.start();
        lastExp = skillToCheck.getExperience();
    }

    @Override
    public boolean validate() {
        if (lastExp == skillToCheck.getExperience() && !inactivityTimer.isRunning()) {
            return true;
        } else if (lastExp < skillToCheck.getExperience()) {
            lastExp = skillToCheck.getExperience();
            inactivityTimer.reset();
        }
        return false;
    }

    @Override
    public void execute() {
        AbstractScript bot = Environment.getScript();
        if (bot != null)
            bot.stop();
    }
}
Written off of the top of my head, without an IDE so there may be some mistakes xD

You could track multiple Skills by using a Skill array or Collection instead of a single Skill, and setting lastExp to the sum of all of their exp.
You are going to have to explain me why my code wouldn't work. On slack or here I don't mind. :p
 
Joined
Dec 10, 2014
Messages
3,332
You are going to have to explain me why my code wouldn't work. On slack or here I don't mind. :p
Copy/pasted it wouldn't work, because you're creating the timer, setting the cExp, starting the timer, and then checking the exp in the same code block/scope.
Your timer start is followed straight away by your timer check, and for 50 seconds to pass between those 2 lines would take some pretty rare conditions :p
 
12 year old normie
Joined
Jan 8, 2015
Messages
2,768
JavaScript:
timer.start();
if (timer.getRemainingTime() == 0) {
   //do stuff
}

According to my thought-train it starts the counter (from 60000 milliseconds), counts down to zero, if the remaining time on the timer reaches 0, execute if statement.

I must be misunderstanding timers if it wouldn't work.
 
Joined
Dec 10, 2014
Messages
3,332
JavaScript:
timer.start();
if (timer.getRemainingTime() == 0) {
   //do stuff
}

According to my thought-train it starts the counter (from 60000 milliseconds), counts down to zero, if the remaining time on the timer reaches 0, execute if statement.

I must be misunderstanding timers if it wouldn't work.
Timers don't block execution of the thread. They either run concurrently or calculate the time remaining when the methods are called, as far as I can tell from the decompiled code. timer.start(); sets the "start time", as does reset(),
That code is pretty much the same as
Code:
long startTime = System.currentTimeMillis();
long timerLength = 60000;
if (System.currentTimeMillis() - startTime > timerLength) {
    //do stuff
}
 
Last edited:
Top