Support doubles for probabilities.

Fixes https://github.com/BentoBoxWorld/Greenhouses/issues/61
This commit is contained in:
tastybento 2020-11-15 15:35:59 -08:00
parent 79a203cbf5
commit 1a1c013626
3 changed files with 28 additions and 14 deletions

View File

@ -104,9 +104,9 @@ public class BiomeRecipe implements Comparable<BiomeRecipe> {
* @param mobSpawnOn - material to spawn on
* @return true if add is successful
*/
public boolean addMobs(EntityType mobType, int mobProbability, Material mobSpawnOn) {
public boolean addMobs(EntityType mobType, double mobProbability, Material mobSpawnOn) {
startupLog(" " + mobProbability + CHANCE_FOR + Util.prettifyText(mobType.toString()) + " to spawn on " + Util.prettifyText(mobSpawnOn.toString())+ ".");
double probability = ((double)mobProbability/100);
double probability = mobProbability/100;
double lastProb = mobTree.isEmpty() ? 0D : mobTree.lastKey();
// Add up all the probabilities in the list so far
if ((1D - lastProb) >= probability) {
@ -127,8 +127,8 @@ public class BiomeRecipe implements Comparable<BiomeRecipe> {
* @param plantGrowOn - material on which it must grow
* @return true if add is successful
*/
public boolean addPlants(Material plantMaterial, int plantProbability, Material plantGrowOn) {
double probability = ((double)plantProbability/100);
public boolean addPlants(Material plantMaterial, double plantProbability, Material plantGrowOn) {
double probability = plantProbability/100;
// Add up all the probabilities in the list so far
double lastProb = plantTree.isEmpty() ? 0D : plantTree.lastKey();
if ((1D - lastProb) >= probability) {

View File

@ -5,6 +5,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.stream.Collectors;
@ -123,7 +124,7 @@ public class RecipeManager {
addon.logError("No biome defined in the biome reciepe " + biomeType + ". Skipping...");
return null;
}
String name = biomeRecipeConfig.getString("biome").toUpperCase();
String name = biomeRecipeConfig.getString("biome").toUpperCase(Locale.ENGLISH);
if (Enums.getIfPresent(Biome.class, name).isPresent()) {
return Biome.valueOf(name);
}
@ -162,7 +163,7 @@ public class RecipeManager {
for (Entry<String, Object> s: plants.entrySet()) {
Material plantMaterial = Material.valueOf(s.getKey());
String[] split = ((String)s.getValue()).split(":");
int plantProbability = Integer.parseInt(split[0]);
double plantProbability = Double.parseDouble(split[0]);
Material plantGrowOn = Material.valueOf(split[1]);
b.addPlants(plantMaterial, plantProbability, plantGrowOn);
}
@ -175,11 +176,11 @@ public class RecipeManager {
if (conversionSec != null) {
for (String oldMat : conversionSec.getKeys(false)) {
try {
Material oldMaterial = Material.valueOf(oldMat.toUpperCase());
Material oldMaterial = Material.valueOf(oldMat.toUpperCase(Locale.ENGLISH));
String conversions = conversionSec.getString(oldMat);
if (!conversions.isEmpty()) {
String[] split = conversions.split(":");
int convChance = Integer.parseInt(split[0]);
double convChance = Double.parseDouble(split[0]);
Material newMaterial = Material.valueOf(split[1]);
Material localMaterial = Material.valueOf(split[2]);
b.addConvBlocks(oldMaterial, newMaterial, convChance, localMaterial);
@ -196,7 +197,7 @@ public class RecipeManager {
// Split the string
String[] split = oldMat.split(":");
Material oldMaterial = Material.valueOf(split[0].toUpperCase());
int convChance = Integer.parseInt(split[1]);
double convChance = Double.parseDouble(split[1]);
Material newMaterial = Material.valueOf(split[2]);
Material localMaterial = Material.valueOf(split[3]);
b.addConvBlocks(oldMaterial, newMaterial, convChance, localMaterial);
@ -218,9 +219,9 @@ public class RecipeManager {
private void parseMob(Entry<String, Object> s, BiomeRecipe b) {
try {
EntityType mobType = EntityType.valueOf(s.getKey().toUpperCase());
EntityType mobType = EntityType.valueOf(s.getKey().toUpperCase(Locale.ENGLISH));
String[] split = ((String)s.getValue()).split(":");
int mobProbability = Integer.parseInt(split[0]);
double mobProbability = Double.parseDouble(split[0]);
Material mobSpawnOn = Material.valueOf(split[1]);
b.addMobs(mobType, mobProbability, mobSpawnOn);
} catch (Exception e) {
@ -230,7 +231,7 @@ public class RecipeManager {
private void parseReqBlock(BiomeRecipe b, String rq, ConfigurationSection reqContents) {
try {
b.addReqBlocks(Material.valueOf(rq.toUpperCase()), reqContents.getInt(rq));
b.addReqBlocks(Material.valueOf(rq.toUpperCase(Locale.ENGLISH)), reqContents.getInt(rq));
} catch(Exception e) {
addon.logError("Could not parse required block " + rq);
}

View File

@ -161,7 +161,7 @@ public class BiomeRecipeTest {
int mobProbability = 50;
Material mobSpawnOn = Material.GRASS_PATH;
br.addMobs(mobType, mobProbability, mobSpawnOn);
verify(addon).log(eq(" 50% chance for Cat to spawn on Grass Path."));
verify(addon).log(eq(" 50.0% chance for Cat to spawn on Grass Path."));
}
/**
@ -178,6 +178,19 @@ public class BiomeRecipeTest {
verify(addon).logError(eq("Mob chances add up to > 100% in BADLANDS biome recipe! Skipping CAT"));
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#addMobs(org.bukkit.entity.EntityType, int, org.bukkit.Material)}.
*/
@Test
public void testAddMobsOver100PercentDouble() {
EntityType mobType = EntityType.CAT;
double mobProbability = 50.5;
Material mobSpawnOn = Material.GRASS_PATH;
br.addMobs(mobType, mobProbability, mobSpawnOn);
br.addMobs(mobType, mobProbability, mobSpawnOn);
verify(addon).logError(eq("Mob chances add up to > 100% in BADLANDS biome recipe! Skipping CAT"));
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#addPlants(org.bukkit.Material, int, org.bukkit.Material)}.
*/
@ -187,7 +200,7 @@ public class BiomeRecipeTest {
int plantProbability = 20;
Material plantGrowOn = Material.DIRT;
br.addPlants(plantMaterial, plantProbability, plantGrowOn);
verify(addon).log(eq(" 20% chance for Jungle Sapling to grow on Dirt"));
verify(addon).log(eq(" 20.0% chance for Jungle Sapling to grow on Dirt"));
}
/**