Improved the random handler

This commit is contained in:
JKetelaar
2014-09-10 18:26:18 +02:00
parent 5a2abac789
commit f83a7947bc
@@ -1,9 +1,9 @@
package org.parabot.environment.scripts.randoms;
import java.util.ArrayList;
import org.parabot.core.Core;
import java.util.ArrayList;
/**
*
* @author Everel
@@ -11,14 +11,20 @@ import org.parabot.core.Core;
*/
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<>();
}
/**
* Adds a random to the random list
* @param random
* @param random The random that will be added to the arraylist
*/
public void addRandom(Random random) {
if(random == null) {
@@ -32,12 +38,39 @@ public class RandomHandler {
}
randoms.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() {
randoms.clear();
this.randoms.clear();
}
/**
* Clears all active randoms
*/
public void clearActiveRandoms(){
this.activeRandoms.clear();
}
/**
@@ -45,12 +78,21 @@ public class RandomHandler {
* @return returns <b>true</b> if a random has been executed, otherwise <b>false</b>
*/
public boolean checkAndRun() {
for(Random r : randoms) {
for(Random r : this.activeRandoms) {
if(r.activate()) {
Core.verbose("Running random '" + r.getName() + "'.");
r.execute();
return true;
}
}
return false;
}
public ArrayList<Random> getRandoms(){
return this.randoms;
}
public ArrayList<Random> getActiveRandoms(){
return this.activeRandoms;
}
}