mirror of
https://github.com/2006-Scape/Parabot.git
synced 2026-07-07 08:39:12 +00:00
Proper RandomHandler implementation
This commit is contained in:
@@ -1,49 +0,0 @@
|
||||
package org.parabot.environment.randoms;
|
||||
|
||||
import org.parabot.environment.scripts.Script;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* User: Jeroen
|
||||
* Date: 18/02/14
|
||||
* Time: 19:37
|
||||
*/
|
||||
public class Handler {
|
||||
|
||||
|
||||
public static class RandomChecker {
|
||||
private ArrayList<Random> randoms;
|
||||
|
||||
public void addRandom(Random random) {
|
||||
randoms.add(random);
|
||||
}
|
||||
|
||||
public void checkAndRun() {
|
||||
Script s = new Script();
|
||||
for(Random r : randoms) {
|
||||
if(r.shouldRun()) {
|
||||
s.setState(1);
|
||||
r.run();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface Random {
|
||||
public boolean shouldRun();
|
||||
public void run();
|
||||
}
|
||||
|
||||
public abstract class RandomScript {
|
||||
//private RandomChecker randomChecker = ServerProvider.getRandomChecker();
|
||||
|
||||
public void scriptLoop() {
|
||||
while(true) {
|
||||
// randomChecker.checkAndRun();
|
||||
// framework.loop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package org.parabot.environment.randoms;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Everel
|
||||
*
|
||||
*/
|
||||
public class RandomHandler {
|
||||
private ArrayList<Random> randoms;
|
||||
|
||||
public RandomHandler() {
|
||||
randoms = new ArrayList<Random>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a random to the random list
|
||||
* @param random
|
||||
*/
|
||||
public void addRandom(Random random) {
|
||||
randoms.add(random);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if random occures and runs it
|
||||
* @return returns <b>true</b> if a random has been executed, otherwise <b>false</b>
|
||||
*/
|
||||
public boolean checkAndRun() {
|
||||
for(Random r : randoms) {
|
||||
if(r.activate()) {
|
||||
r.execute();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -20,19 +20,19 @@ import java.util.Collection;
|
||||
*
|
||||
*/
|
||||
public class Script implements Runnable {
|
||||
private Collection<Strategy> strategies = null;
|
||||
private int frameWorkType = 0;
|
||||
private AbstractFramework frameWork = null;
|
||||
|
||||
public static final int TYPE_STRATEGY = 0;
|
||||
public static final int TYPE_LOOP = 1;
|
||||
public static final int TYPE_OTHER = 2;
|
||||
|
||||
private int state = 0;
|
||||
public static final int STATE_RUNNING = 0;
|
||||
public static final int STATE_PAUSE = 1;
|
||||
public static final int STATE_STOPPED = 2;
|
||||
|
||||
private Collection<Strategy> strategies;
|
||||
private AbstractFramework frameWork;
|
||||
private int state;
|
||||
private int frameWorkType;
|
||||
|
||||
public boolean onExecute() {
|
||||
return true;
|
||||
}
|
||||
@@ -62,8 +62,10 @@ public class Script implements Runnable {
|
||||
|
||||
@Override
|
||||
public final void run() {
|
||||
Context context = Context.resolve();
|
||||
|
||||
Core.verbose("Initializing script...");
|
||||
Context.resolve().getServerProvider().initScript(this);
|
||||
context.getServerProvider().initScript(this);
|
||||
Core.verbose("Done.");
|
||||
|
||||
if(!onExecute()) {
|
||||
@@ -75,7 +77,7 @@ public class Script implements Runnable {
|
||||
}
|
||||
|
||||
Core.verbose("Detecting script framework...");
|
||||
Context.resolve().setRunningScript(this);
|
||||
context.setRunningScript(this);
|
||||
BotToolbar.getInstance().toggleRun();
|
||||
if(this instanceof LoopTask) {
|
||||
Core.verbose("Script framework detected: LoopTask");
|
||||
@@ -93,6 +95,10 @@ public class Script implements Runnable {
|
||||
LogArea.log("Script started.");
|
||||
try {
|
||||
while(this.state != STATE_STOPPED) {
|
||||
if(context.getRandomHandler().checkAndRun()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(this.state == STATE_PAUSE) {
|
||||
sleep(500);
|
||||
continue;
|
||||
|
||||
Reference in New Issue
Block a user