From f83a7947bc1ea8c4c39ec14d84affba6a4be0141 Mon Sep 17 00:00:00 2001 From: JKetelaar Date: Wed, 10 Sep 2014 18:26:18 +0200 Subject: [PATCH] Improved the random handler --- .../scripts/randoms/RandomHandler.java | 52 +++++++++++++++++-- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/parabotv2/src/org/parabot/environment/scripts/randoms/RandomHandler.java b/parabotv2/src/org/parabot/environment/scripts/randoms/RandomHandler.java index 4656d96..e880d3f 100644 --- a/parabotv2/src/org/parabot/environment/scripts/randoms/RandomHandler.java +++ b/parabotv2/src/org/parabot/environment/scripts/randoms/RandomHandler.java @@ -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 randoms; + + /** + * The randoms that will actually run + */ + private ArrayList 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 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 true if a random has been executed, otherwise false */ 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 getRandoms(){ + return this.randoms; + } + + public ArrayList getActiveRandoms(){ + return this.activeRandoms; + } } \ No newline at end of file