mirror of
https://github.com/2006-Scape/Parabot.git
synced 2026-07-03 00:37:55 +00:00
Proper RandomHandler implementation
This commit is contained in:
@@ -9,6 +9,7 @@ import org.parabot.core.ui.components.GamePanel;
|
||||
import org.parabot.environment.api.interfaces.Paintable;
|
||||
import org.parabot.environment.input.Keyboard;
|
||||
import org.parabot.environment.input.Mouse;
|
||||
import org.parabot.environment.randoms.RandomHandler;
|
||||
import org.parabot.environment.scripts.Script;
|
||||
import org.parabot.environment.servers.ServerProvider;
|
||||
|
||||
@@ -23,35 +24,35 @@ import java.util.TimerTask;
|
||||
* @author Everel
|
||||
*/
|
||||
public class Context {
|
||||
public static HashMap<ThreadGroup, Context> threadGroups = new HashMap<ThreadGroup, Context>();
|
||||
public static final HashMap<ThreadGroup, Context> threadGroups = new HashMap<ThreadGroup, Context>();
|
||||
private static ArrayList<Paintable> paintables = new ArrayList<Paintable>();
|
||||
private static int id = 1;
|
||||
|
||||
private ASMClassLoader classLoader = null;
|
||||
private ClassPath classPath = null;
|
||||
private ServerProvider serverProvider = null;
|
||||
private int tab = 0;
|
||||
private Applet gameApplet = null;
|
||||
private HookParser hookParser = null;
|
||||
private Script runningScript = null;
|
||||
|
||||
private Object clientInstance = null;
|
||||
|
||||
private static ArrayList<Paintable> paintables = new ArrayList<Paintable>();
|
||||
|
||||
private PaintDebugger paintDebugger = new PaintDebugger();
|
||||
|
||||
public boolean added = false;
|
||||
|
||||
private Mouse mouse = null;
|
||||
private Keyboard keyboard = null;
|
||||
public boolean added;
|
||||
private ASMClassLoader classLoader;
|
||||
private ClassPath classPath;
|
||||
private ServerProvider serverProvider;
|
||||
private int tab;
|
||||
private Applet gameApplet;
|
||||
private HookParser hookParser;
|
||||
private Script runningScript;
|
||||
private RandomHandler randomHandler;
|
||||
private Object clientInstance;
|
||||
private PaintDebugger paintDebugger;
|
||||
private Mouse mouse;
|
||||
private Keyboard keyboard;
|
||||
|
||||
public Context(final ServerProvider serverProvider) {
|
||||
threadGroups.put(Thread.currentThread().getThreadGroup(), this);
|
||||
tab = id;
|
||||
|
||||
this.serverProvider = serverProvider;
|
||||
id++;
|
||||
this.paintDebugger = new PaintDebugger();
|
||||
this.classPath = new ClassPath();
|
||||
classLoader = new ASMClassLoader(classPath);
|
||||
this.classLoader = new ASMClassLoader(classPath);
|
||||
this.randomHandler = new RandomHandler();
|
||||
this.tab = id;
|
||||
|
||||
id++;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -297,5 +298,13 @@ public class Context {
|
||||
public Script getRunningScript() {
|
||||
return this.runningScript;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the random handler
|
||||
* @return random handler
|
||||
*/
|
||||
public RandomHandler getRandomHandler() {
|
||||
return this.randomHandler;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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