[!] [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:
JKetelaar
2016-11-01 23:09:25 +01:00
parent 6b04523865
commit bc69dd7e0a
10 changed files with 369 additions and 248 deletions
+1 -1
View File
@@ -14,8 +14,8 @@ import org.parabot.core.ui.listeners.PBKeyListener;
import org.parabot.environment.api.interfaces.Paintable; import org.parabot.environment.api.interfaces.Paintable;
import org.parabot.environment.input.Keyboard; import org.parabot.environment.input.Keyboard;
import org.parabot.environment.input.Mouse; import org.parabot.environment.input.Mouse;
import org.parabot.environment.randoms.RandomHandler;
import org.parabot.environment.scripts.Script; import org.parabot.environment.scripts.Script;
import org.parabot.environment.scripts.randoms.RandomHandler;
import org.parabot.environment.servers.ServerProvider; import org.parabot.environment.servers.ServerProvider;
import java.applet.Applet; import java.applet.Applet;
+1 -2
View File
@@ -6,12 +6,11 @@ import org.parabot.core.Directories;
import org.parabot.core.ui.components.GamePanel; import org.parabot.core.ui.components.GamePanel;
import org.parabot.core.ui.components.VerboseLoader; import org.parabot.core.ui.components.VerboseLoader;
import org.parabot.core.ui.images.Images; import org.parabot.core.ui.images.Images;
import org.parabot.core.ui.listeners.PBKeyListener;
import org.parabot.core.ui.utils.SwingUtil; import org.parabot.core.ui.utils.SwingUtil;
import org.parabot.environment.OperatingSystem; import org.parabot.environment.OperatingSystem;
import org.parabot.environment.api.utils.StringUtils; import org.parabot.environment.api.utils.StringUtils;
import org.parabot.environment.randoms.Random;
import org.parabot.environment.scripts.Script; import org.parabot.environment.scripts.Script;
import org.parabot.environment.scripts.randoms.Random;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.swing.*; import javax.swing.*;
@@ -2,7 +2,7 @@ package org.parabot.core.ui;
import org.parabot.core.Context; import org.parabot.core.Context;
import org.parabot.core.Core; import org.parabot.core.Core;
import org.parabot.environment.scripts.randoms.Random; import org.parabot.environment.randoms.Random;
import javax.swing.*; import javax.swing.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
@@ -0,0 +1,41 @@
package org.parabot.environment.randoms;
/**
* @author JKetelaar
*/
public interface Random {
/**
* Determines whether this random should activate
*
* @return <b>true</b> if this random should be activate
*/
public boolean activate();
/**
* Executes this random
*/
public void execute();
/**
* Returns the name of the random
*
* @return Name of the random
*/
public String getName();
/**
* Returns the name of the server which the random is made for
*
* @return Name of the server
*/
public String getServer();
/**
* Returns the RandomType of the random
*
* @return The RandomType of the random
*/
public RandomType getRandomType();
}
@@ -0,0 +1,140 @@
package org.parabot.environment.randoms;
import com.sun.javaws.exceptions.InvalidArgumentException;
import org.parabot.core.Core;
import org.parabot.core.parsers.randoms.RandomParser;
import org.parabot.core.ui.Logger;
import java.util.ArrayList;
/**
* @author JKetelaar
*/
public class RandomHandler {
private ArrayList<Random> randoms;
/**
* The randoms that will actually run
*/
private ArrayList<Random> activeRandoms;
public RandomHandler() {
this.randoms = new ArrayList<>();
this.activeRandoms = new ArrayList<>();
}
public void init() {
RandomParser.enable();
checkAndRun(RandomType.ON_SERVER_START);
}
/**
* Adds a random to the random list
*
* @param random The random that will be added to the arraylist
*/
public void addRandom(Random random) {
if (random == null) {
throw new NullPointerException("Null random");
}
for (Random r : randoms) {
if (r.getClass() == random.getClass()) {
Core.verbose("Ignored added random, duplicate.");
return;
}
}
randoms.add(random);
setActive(random);
}
/**
* @param random
* @deprecated
*/
@Deprecated
public void addRandom(org.parabot.environment.scripts.randoms.Random random) {
new InvalidArgumentException(new String[]{"This type of random is deprecated"}).printStackTrace();
}
/**
* Adds a random to the active randoms
*
* @param random
*/
public void setActive(Random random) {
this.activeRandoms.add(random);
}
/**
* Adds a random to the active randoms
*
* @param random
*/
public void setActive(String random) {
for (Random r : this.randoms) {
if (r.getName().equalsIgnoreCase(random.toLowerCase())) {
this.activeRandoms.add(r);
}
}
}
/**
* Sets the whole random arraylist to the arraylist given as argument
*
* @param randoms The new random arraylist
*/
public void setRandoms(ArrayList<Random> randoms) {
this.randoms = randoms;
}
/**
* Clears all added randoms
*/
public void clearRandoms() {
this.randoms.clear();
}
/**
* Clears all active randoms
*/
public void clearActiveRandoms() {
this.activeRandoms.clear();
}
/**
* Checks if random occurs and runs it
*
* @return returns <b>true</b> if a random has been executed, otherwise <b>false</b>
*/
public boolean checkAndRun(RandomType type) {
for (Random r : this.activeRandoms) {
if (r.getRandomType().getId() == type.getId() && r.activate()) {
Logger.addMessage("Running random '" + r.getName() + "'", true);
r.execute();
return true;
}
}
return false;
}
/**
* Checks if random occurs and runs it
*
* @return returns <b>true</b> if a random has been executed, otherwise <b>false</b>
* @see RandomHandler#checkAndRun(RandomType)
* @deprecated
*/
@Deprecated
public boolean checkAndRun() {
new InvalidArgumentException(new String[]{"This method is deprecated"}).printStackTrace();
return false;
}
public ArrayList<Random> getRandoms() {
return this.randoms;
}
public ArrayList<Random> getActiveRandoms() {
return this.activeRandoms;
}
}
@@ -0,0 +1,32 @@
package org.parabot.environment.randoms;
/**
* @author JKetelaar
*/
public enum RandomType {
SCRIPT(0, "Script"),
ON_SCRIPT_START(0, "On script start"),
ON_SERVER_START(0, "On server start"),
ON_SCRIPT_FINISH(0, "On script finish");
private int id;
private String name;
RandomType(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public static RandomType getDefault() {
return SCRIPT;
}
}
@@ -6,6 +6,7 @@ import org.parabot.core.ui.BotUI;
import org.parabot.core.ui.Logger; import org.parabot.core.ui.Logger;
import org.parabot.environment.api.utils.PBPreferences; import org.parabot.environment.api.utils.PBPreferences;
import org.parabot.environment.api.utils.Time; 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.*;
import org.parabot.environment.scripts.framework.Frameworks; import org.parabot.environment.scripts.framework.Frameworks;
import org.parabot.environment.scripts.randoms.Random; import org.parabot.environment.scripts.randoms.Random;
@@ -13,11 +14,9 @@ import org.parabot.environment.scripts.randoms.Random;
import java.util.Collection; import java.util.Collection;
/** /**
*
* Script template, scripts are 'add-ons' which executes various tasks in-game * Script template, scripts are 'add-ons' which executes various tasks in-game
* *
* @author Everel * @author Everel
*
*/ */
public class Script implements Runnable { public class Script implements Runnable {
public static final int TYPE_STRATEGY = 0; public static final int TYPE_STRATEGY = 0;
@@ -82,6 +81,8 @@ public class Script implements Runnable {
return; return;
} }
context.getRandomHandler().checkAndRun(RandomType.ON_SCRIPT_START);
Core.verbose("Detecting script framework..."); Core.verbose("Detecting script framework...");
context.setRunningScript(this); context.setRunningScript(this);
BotUI.getInstance().toggleRun(); BotUI.getInstance().toggleRun();
@@ -101,7 +102,7 @@ public class Script implements Runnable {
Logger.addMessage("Script started.", true); Logger.addMessage("Script started.", true);
try { try {
while (this.state != STATE_STOPPED) { while (this.state != STATE_STOPPED) {
if(context.getRandomHandler().checkAndRun()) { if (context.getRandomHandler().checkAndRun(RandomType.SCRIPT)) {
continue; continue;
} }
@@ -118,6 +119,9 @@ public class Script implements Runnable {
} }
Core.verbose("Script stopped/finished, unloading and stopping..."); Core.verbose("Script stopped/finished, unloading and stopping...");
onFinish(); onFinish();
context.getRandomHandler().checkAndRun(RandomType.ON_SCRIPT_FINISH);
Logger.addMessage("Script stopped.", false); Logger.addMessage("Script stopped.", false);
context.getServerProvider().unloadScript(this); context.getServerProvider().unloadScript(this);
this.state = STATE_STOPPED; this.state = STATE_STOPPED;
@@ -132,13 +136,11 @@ public class Script implements Runnable {
/** /**
* Sleeps until the SleepCondition is valid. * Sleeps until the SleepCondition is valid.
* * <p>
* <B>DEPRECATED!</b> use {@link Time#sleep(SleepCondition, int)} * <B>DEPRECATED!</b> use {@link Time#sleep(SleepCondition, int)}
* *
* @param conn * @param conn the condition.
* the condition. * @param timeout the time in miliseconds before it stops sleeping.
* @param timeout
* the time in miliseconds before it stops sleeping.
* @return whether it ran successfully without timing out. * @return whether it ran successfully without timing out.
*/ */
@Deprecated @Deprecated
@@ -148,6 +150,7 @@ public class Script implements Runnable {
/** /**
* Sets the script's state * Sets the script's state
*
* @param state * @param state
*/ */
public final void setState(final int state) { public final void setState(final int state) {
@@ -159,6 +162,7 @@ public class Script implements Runnable {
/** /**
* Sleeps for an amount of milliseconds * Sleeps for an amount of milliseconds
*
* @param ms * @param ms
*/ */
public final void sleep(int ms) { public final void sleep(int ms) {
@@ -1,10 +1,11 @@
package org.parabot.environment.scripts.randoms; package org.parabot.environment.scripts.randoms;
/** /**
*
* @author Everel * @author Everel
* * @deprecated
* @see org.parabot.environment.randoms.Random
*/ */
@Deprecated
public interface Random { public interface Random {
/** /**
@@ -1,108 +1,11 @@
package org.parabot.environment.scripts.randoms; package org.parabot.environment.scripts.randoms;
import org.parabot.core.Core;
import org.parabot.core.ui.Logger;
import java.util.ArrayList;
/** /**
*
* @author Everel * @author Everel
* * @deprecated
* @see org.parabot.environment.randoms.RandomHandler
*/ */
public class RandomHandler { @Deprecated
private ArrayList<Random> randoms; public class RandomHandler extends org.parabot.environment.randoms.RandomHandler {
/**
* The randoms that will actually run
*/
private ArrayList<Random> activeRandoms;
public RandomHandler() {
this.randoms = new ArrayList<>();
this.activeRandoms = new ArrayList<>();
}
/**
* Adds a random to the random list
* @param random The random that will be added to the arraylist
*/
public void addRandom(Random random) {
if(random == null) {
throw new NullPointerException("Null random");
}
for(Random r : randoms) {
if(r.getClass() == random.getClass()) {
Core.verbose("Ignored added random, duplicate.");
return;
}
}
randoms.add(random);
setActive(random);
}
/**
* Adds a random to the active randoms
* @param random
*/
public void setActive(Random random){
this.activeRandoms.add(random);
}
/**
* Adds a random to the active randoms
* @param random
*/
public void setActive(String random){
for (Random r : this.randoms){
if (r.getName().equalsIgnoreCase(random.toLowerCase())){
this.activeRandoms.add(r);
}
}
}
/**
* Sets the whole random arraylist to the arraylist given as argument
* @param randoms The new random arraylist
*/
public void setRandoms(ArrayList<Random> randoms){
this.randoms = randoms;
}
/**
* Clears all added randoms
*/
public void clearRandoms() {
this.randoms.clear();
}
/**
* Clears all active randoms
*/
public void clearActiveRandoms(){
this.activeRandoms.clear();
}
/**
* Checks if random occurs and runs it
* @return returns <b>true</b> if a random has been executed, otherwise <b>false</b>
*/
public boolean checkAndRun() {
for(Random r : this.activeRandoms) {
if(r.activate()) {
Logger.addMessage("Running random '" + r.getName() + "'", true);
r.execute();
return true;
}
}
return false;
}
public ArrayList<Random> getRandoms(){
return this.randoms;
}
public ArrayList<Random> getActiveRandoms(){
return this.activeRandoms;
}
} }
@@ -27,7 +27,8 @@ public abstract class ServerExecuter {
Context context = Context.getInstance(provider); Context context = Context.getInstance(provider);
context.load(); context.load();
PaintComponent.getInstance().startPainting(context); PaintComponent.getInstance().startPainting(context);
RandomParser.enable();
Context.getInstance().getRandomHandler().init();
} catch (Throwable t) { } catch (Throwable t) {
t.printStackTrace(); t.printStackTrace();
} }