mirror of
https://github.com/2006-Scape/Parabot.git
synced 2026-07-03 16:49:10 +00:00
[!] [FEATURE] Added new Random system, to have different type of Randoms
This is a breaking change for old randoms Related to issue #144
This commit is contained in:
@@ -6,6 +6,7 @@ import org.parabot.core.ui.BotUI;
|
||||
import org.parabot.core.ui.Logger;
|
||||
import org.parabot.environment.api.utils.PBPreferences;
|
||||
import org.parabot.environment.api.utils.Time;
|
||||
import org.parabot.environment.randoms.RandomType;
|
||||
import org.parabot.environment.scripts.framework.*;
|
||||
import org.parabot.environment.scripts.framework.Frameworks;
|
||||
import org.parabot.environment.scripts.randoms.Random;
|
||||
@@ -13,170 +14,173 @@ import org.parabot.environment.scripts.randoms.Random;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
*
|
||||
* Script template, scripts are 'add-ons' which executes various tasks in-game
|
||||
*
|
||||
* @author Everel
|
||||
*
|
||||
*/
|
||||
public class Script implements Runnable {
|
||||
public static final int TYPE_STRATEGY = 0;
|
||||
public static final int TYPE_LOOP = 1;
|
||||
public static final int TYPE_OTHER = 2;
|
||||
public static final int TYPE_STRATEGY = 0;
|
||||
public static final int TYPE_LOOP = 1;
|
||||
public static final int TYPE_OTHER = 2;
|
||||
|
||||
public static final int STATE_RUNNING = 0;
|
||||
public static final int STATE_PAUSE = 1;
|
||||
public static final int STATE_STOPPED = 2;
|
||||
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 PBPreferences preferences;
|
||||
private AbstractFramework frameWork;
|
||||
private int state;
|
||||
private int frameWorkType;
|
||||
private int scriptID;
|
||||
private Collection<Strategy> strategies;
|
||||
private PBPreferences preferences;
|
||||
private AbstractFramework frameWork;
|
||||
private int state;
|
||||
private int frameWorkType;
|
||||
private int scriptID;
|
||||
|
||||
public boolean onExecute() {
|
||||
return true;
|
||||
}
|
||||
public boolean onExecute() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void onFinish() {
|
||||
public void onFinish() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public final void provide(final Collection<Strategy> strategies) {
|
||||
this.strategies = strategies;
|
||||
}
|
||||
public final void provide(final Collection<Strategy> strategies) {
|
||||
this.strategies = strategies;
|
||||
}
|
||||
|
||||
public final int getFrameWorkType() {
|
||||
return frameWorkType;
|
||||
}
|
||||
public final int getFrameWorkType() {
|
||||
return frameWorkType;
|
||||
}
|
||||
|
||||
public final void setFrameWork(int frameWorkType) {
|
||||
if(frameWorkType < 0 || frameWorkType > 2) {
|
||||
throw new RuntimeException("Invalid framework type");
|
||||
}
|
||||
this.frameWorkType = frameWorkType;
|
||||
}
|
||||
public final void setFrameWork(int frameWorkType) {
|
||||
if (frameWorkType < 0 || frameWorkType > 2) {
|
||||
throw new RuntimeException("Invalid framework type");
|
||||
}
|
||||
this.frameWorkType = frameWorkType;
|
||||
}
|
||||
|
||||
public final void setAbstractFrameWork(AbstractFramework f) {
|
||||
this.frameWork = f;
|
||||
}
|
||||
public final void setAbstractFrameWork(AbstractFramework f) {
|
||||
this.frameWork = f;
|
||||
}
|
||||
|
||||
public final void addRandom(Random random) {
|
||||
Context.getInstance().getRandomHandler().addRandom(random);
|
||||
}
|
||||
public final void addRandom(Random random) {
|
||||
Context.getInstance().getRandomHandler().addRandom(random);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void run() {
|
||||
Context context = Context.getInstance();
|
||||
@Override
|
||||
public final void run() {
|
||||
Context context = Context.getInstance();
|
||||
|
||||
Core.verbose("Initializing script...");
|
||||
context.getServerProvider().initScript(this);
|
||||
Core.verbose("Done.");
|
||||
Core.verbose("Initializing script...");
|
||||
context.getServerProvider().initScript(this);
|
||||
Core.verbose("Done.");
|
||||
|
||||
if(!onExecute()) {
|
||||
Core.verbose("Script#onExecute returned false, unloading and stopping script...");
|
||||
context.getServerProvider().unloadScript(this);
|
||||
this.state = STATE_STOPPED;
|
||||
Core.verbose("Done.");
|
||||
return;
|
||||
}
|
||||
if (!onExecute()) {
|
||||
Core.verbose("Script#onExecute returned false, unloading and stopping script...");
|
||||
context.getServerProvider().unloadScript(this);
|
||||
this.state = STATE_STOPPED;
|
||||
Core.verbose("Done.");
|
||||
return;
|
||||
}
|
||||
|
||||
Core.verbose("Detecting script framework...");
|
||||
context.setRunningScript(this);
|
||||
BotUI.getInstance().toggleRun();
|
||||
if(this instanceof LoopTask) {
|
||||
Core.verbose("Script framework detected: LoopTask");
|
||||
frameWorkType = TYPE_LOOP;
|
||||
frameWork = Frameworks.getLooper((LoopTask) this);
|
||||
} else if(strategies != null && !strategies.isEmpty()) {
|
||||
Core.verbose("Script framework detected: Strategies");
|
||||
frameWorkType = TYPE_STRATEGY;
|
||||
frameWork = Frameworks.getStrategyWorker(strategies);
|
||||
} else {
|
||||
Core.verbose("Unknown script framework: Other");
|
||||
frameWorkType = TYPE_OTHER;
|
||||
}
|
||||
Core.verbose("Running script...");
|
||||
Logger.addMessage("Script started.", true);
|
||||
try {
|
||||
while(this.state != STATE_STOPPED) {
|
||||
if(context.getRandomHandler().checkAndRun()) {
|
||||
continue;
|
||||
}
|
||||
context.getRandomHandler().checkAndRun(RandomType.ON_SCRIPT_START);
|
||||
|
||||
if(this.state == STATE_PAUSE) {
|
||||
sleep(500);
|
||||
continue;
|
||||
}
|
||||
if(!frameWork.execute()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
Core.verbose("Script stopped/finished, unloading and stopping...");
|
||||
onFinish();
|
||||
Logger.addMessage("Script stopped.", false);
|
||||
context.getServerProvider().unloadScript(this);
|
||||
this.state = STATE_STOPPED;
|
||||
context.setRunningScript(null);
|
||||
Core.verbose("Detecting script framework...");
|
||||
context.setRunningScript(this);
|
||||
BotUI.getInstance().toggleRun();
|
||||
if (this instanceof LoopTask) {
|
||||
Core.verbose("Script framework detected: LoopTask");
|
||||
frameWorkType = TYPE_LOOP;
|
||||
frameWork = Frameworks.getLooper((LoopTask) this);
|
||||
} else if (strategies != null && !strategies.isEmpty()) {
|
||||
Core.verbose("Script framework detected: Strategies");
|
||||
frameWorkType = TYPE_STRATEGY;
|
||||
frameWork = Frameworks.getStrategyWorker(strategies);
|
||||
} else {
|
||||
Core.verbose("Unknown script framework: Other");
|
||||
frameWorkType = TYPE_OTHER;
|
||||
}
|
||||
Core.verbose("Running script...");
|
||||
Logger.addMessage("Script started.", true);
|
||||
try {
|
||||
while (this.state != STATE_STOPPED) {
|
||||
if (context.getRandomHandler().checkAndRun(RandomType.SCRIPT)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Core.verbose("Resetting key bindings...");
|
||||
Context.getInstance().getPbKeyListener().resetBindings();
|
||||
if (this.state == STATE_PAUSE) {
|
||||
sleep(500);
|
||||
continue;
|
||||
}
|
||||
if (!frameWork.execute()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
Core.verbose("Script stopped/finished, unloading and stopping...");
|
||||
onFinish();
|
||||
|
||||
BotUI.getInstance().toggleRun();
|
||||
Core.verbose("Done.");
|
||||
}
|
||||
context.getRandomHandler().checkAndRun(RandomType.ON_SCRIPT_FINISH);
|
||||
|
||||
/**
|
||||
* Sleeps until the SleepCondition is valid.
|
||||
*
|
||||
* <B>DEPRECATED!</b> use {@link Time#sleep(SleepCondition, int)}
|
||||
*
|
||||
* @param conn
|
||||
* the condition.
|
||||
* @param timeout
|
||||
* the time in miliseconds before it stops sleeping.
|
||||
* @return whether it ran successfully without timing out.
|
||||
*/
|
||||
@Deprecated
|
||||
public final boolean sleep(SleepCondition conn, int timeout) {
|
||||
return Time.sleep(conn, timeout);
|
||||
}
|
||||
Logger.addMessage("Script stopped.", false);
|
||||
context.getServerProvider().unloadScript(this);
|
||||
this.state = STATE_STOPPED;
|
||||
context.setRunningScript(null);
|
||||
|
||||
/**
|
||||
* Sets the script's state
|
||||
* @param state
|
||||
*/
|
||||
public final void setState(final int state) {
|
||||
if(state < 0 || state > 2) {
|
||||
throw new IllegalArgumentException("Illegal state");
|
||||
}
|
||||
this.state = state;
|
||||
}
|
||||
Core.verbose("Resetting key bindings...");
|
||||
Context.getInstance().getPbKeyListener().resetBindings();
|
||||
|
||||
/**
|
||||
* Sleeps for an amount of milliseconds
|
||||
* @param ms
|
||||
*/
|
||||
public final void sleep(int ms) {
|
||||
Time.sleep(ms);
|
||||
}
|
||||
BotUI.getInstance().toggleRun();
|
||||
Core.verbose("Done.");
|
||||
}
|
||||
|
||||
public int getState() {
|
||||
return state;
|
||||
}
|
||||
/**
|
||||
* Sleeps until the SleepCondition is valid.
|
||||
* <p>
|
||||
* <B>DEPRECATED!</b> use {@link Time#sleep(SleepCondition, int)}
|
||||
*
|
||||
* @param conn the condition.
|
||||
* @param timeout the time in miliseconds before it stops sleeping.
|
||||
* @return whether it ran successfully without timing out.
|
||||
*/
|
||||
@Deprecated
|
||||
public final boolean sleep(SleepCondition conn, int timeout) {
|
||||
return Time.sleep(conn, timeout);
|
||||
}
|
||||
|
||||
public PBPreferences getPreferences(){
|
||||
if (this.preferences == null){
|
||||
this.preferences = new PBPreferences(scriptID);
|
||||
}
|
||||
return this.preferences;
|
||||
}
|
||||
/**
|
||||
* Sets the script's state
|
||||
*
|
||||
* @param state
|
||||
*/
|
||||
public final void setState(final int state) {
|
||||
if (state < 0 || state > 2) {
|
||||
throw new IllegalArgumentException("Illegal state");
|
||||
}
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public void setScriptID(int scriptID){
|
||||
this.scriptID = scriptID;
|
||||
}
|
||||
/**
|
||||
* Sleeps for an amount of milliseconds
|
||||
*
|
||||
* @param ms
|
||||
*/
|
||||
public final void sleep(int ms) {
|
||||
Time.sleep(ms);
|
||||
}
|
||||
|
||||
public int getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public PBPreferences getPreferences() {
|
||||
if (this.preferences == null) {
|
||||
this.preferences = new PBPreferences(scriptID);
|
||||
}
|
||||
return this.preferences;
|
||||
}
|
||||
|
||||
public void setScriptID(int scriptID) {
|
||||
this.scriptID = scriptID;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user