[!] [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
@@ -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;
}
}