2019-01-22 00:44:01 +01:00
|
|
|
package world.bentobox.greenhouses.managers;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
2020-07-07 01:26:08 +02:00
|
|
|
import java.util.Arrays;
|
2019-01-22 00:44:01 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
2020-11-16 00:35:59 +01:00
|
|
|
import java.util.Locale;
|
2019-11-01 05:36:05 +01:00
|
|
|
import java.util.Map.Entry;
|
2019-01-23 05:54:43 +01:00
|
|
|
import java.util.Optional;
|
2020-07-07 01:26:08 +02:00
|
|
|
import java.util.stream.Collectors;
|
2019-01-22 00:44:01 +01:00
|
|
|
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.block.Biome;
|
|
|
|
import org.bukkit.configuration.ConfigurationSection;
|
|
|
|
import org.bukkit.configuration.InvalidConfigurationException;
|
|
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
|
|
import org.bukkit.entity.EntityType;
|
|
|
|
|
2020-07-07 01:26:08 +02:00
|
|
|
import com.google.common.base.Enums;
|
|
|
|
|
2019-01-22 02:53:34 +01:00
|
|
|
import world.bentobox.bentobox.util.Util;
|
2019-01-22 00:44:01 +01:00
|
|
|
import world.bentobox.greenhouses.Greenhouses;
|
|
|
|
import world.bentobox.greenhouses.greenhouse.BiomeRecipe;
|
|
|
|
|
|
|
|
public class RecipeManager {
|
|
|
|
|
|
|
|
private static final int MAXIMUM_INVENTORY_SIZE = 49;
|
2019-01-26 17:38:13 +01:00
|
|
|
private final Greenhouses addon;
|
|
|
|
private static final List<BiomeRecipe> biomeRecipes = new ArrayList<>();
|
2019-01-22 00:44:01 +01:00
|
|
|
|
|
|
|
public RecipeManager(Greenhouses addon) {
|
|
|
|
this.addon = addon;
|
|
|
|
try {
|
|
|
|
loadBiomeRecipes();
|
|
|
|
} catch (Exception e) {
|
|
|
|
addon.logError(e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 05:54:43 +01:00
|
|
|
/**
|
|
|
|
* Get BiomeRecipe by name
|
|
|
|
* @param name - name
|
|
|
|
* @return Optional BiomeRecipe found
|
|
|
|
*/
|
|
|
|
public static Optional<BiomeRecipe> getBiomeRecipies(String name) {
|
|
|
|
return biomeRecipes.stream().filter(r -> r.getName().equals(name)).findFirst();
|
|
|
|
}
|
|
|
|
|
2019-01-22 00:44:01 +01:00
|
|
|
/**
|
|
|
|
* Loads all the biome recipes from the file biomes.yml.
|
2019-01-26 17:38:13 +01:00
|
|
|
* @throws InvalidConfigurationException - bad YAML
|
|
|
|
* @throws IOException - io exception
|
2019-01-22 00:44:01 +01:00
|
|
|
*/
|
2019-11-01 05:36:05 +01:00
|
|
|
private void loadBiomeRecipes() throws IOException, InvalidConfigurationException {
|
2019-01-22 00:44:01 +01:00
|
|
|
biomeRecipes.clear();
|
2019-11-01 23:45:40 +01:00
|
|
|
YamlConfiguration biomeConfig = new YamlConfiguration();
|
2019-01-22 00:44:01 +01:00
|
|
|
File biomeFile = new File(addon.getDataFolder(), "biomes.yml");
|
|
|
|
if (!biomeFile.exists()) {
|
|
|
|
addon.logError("No biomes.yml file!");
|
|
|
|
addon.saveResource("biomes.yml", true);
|
|
|
|
}
|
2019-11-01 23:45:40 +01:00
|
|
|
biomeConfig.load(biomeFile);
|
|
|
|
if (!biomeConfig.isConfigurationSection("biomes")) {
|
2019-01-22 00:44:01 +01:00
|
|
|
addon.logError("biomes.yml file is missing, empty or corrupted. Delete and reload plugin again!");
|
|
|
|
return;
|
|
|
|
}
|
2019-11-01 23:45:40 +01:00
|
|
|
ConfigurationSection biomeSection = biomeConfig.getConfigurationSection("biomes");
|
2019-01-22 00:44:01 +01:00
|
|
|
// Loop through all the entries
|
|
|
|
for (String type: biomeSection.getValues(false).keySet()) {
|
2019-11-01 23:45:40 +01:00
|
|
|
processEntries(type, biomeSection);
|
2019-11-01 23:09:28 +01:00
|
|
|
// Check maximum number
|
|
|
|
if (biomeRecipes.size() == MAXIMUM_INVENTORY_SIZE) {
|
|
|
|
addon.logWarning("Cannot load any more biome recipies - limit is " + MAXIMUM_INVENTORY_SIZE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addon.log("Loaded " + biomeRecipes.size() + " biome recipes.");
|
|
|
|
}
|
|
|
|
|
2019-11-01 23:49:21 +01:00
|
|
|
private void processEntries(String biomeType, ConfigurationSection biomeSection) {
|
2019-11-01 23:09:28 +01:00
|
|
|
try {
|
2019-11-01 23:49:21 +01:00
|
|
|
ConfigurationSection biomeRecipeConfig = biomeSection.getConfigurationSection(biomeType);
|
2020-07-07 01:26:08 +02:00
|
|
|
Biome thisBiome = loadBiome(biomeType, biomeRecipeConfig);
|
|
|
|
if (thisBiome == null) return;
|
2019-11-01 23:49:21 +01:00
|
|
|
int priority = biomeRecipeConfig.getInt("priority", 0);
|
2019-11-01 23:09:28 +01:00
|
|
|
|
|
|
|
// Create the biome recipe
|
2019-11-01 23:49:21 +01:00
|
|
|
BiomeRecipe biomeRecipe = getBiomeRecipe(biomeRecipeConfig, biomeType, thisBiome, priority);
|
2019-11-01 23:09:28 +01:00
|
|
|
|
|
|
|
// Set the needed blocks
|
2019-11-01 23:49:21 +01:00
|
|
|
ConfigurationSection reqContents = biomeRecipeConfig.getConfigurationSection("contents");
|
2019-11-01 23:09:28 +01:00
|
|
|
if (reqContents != null) {
|
|
|
|
for (String rq : reqContents.getKeys(false)) {
|
2019-11-01 23:49:21 +01:00
|
|
|
parseReqBlock(biomeRecipe, rq, reqContents);
|
2019-01-26 17:38:13 +01:00
|
|
|
}
|
2019-11-01 23:09:28 +01:00
|
|
|
}
|
2019-11-01 23:45:40 +01:00
|
|
|
|
2019-11-01 23:09:28 +01:00
|
|
|
// Load plants
|
2019-11-01 23:49:21 +01:00
|
|
|
loadPlants(biomeRecipeConfig, biomeRecipe);
|
2019-11-01 23:09:28 +01:00
|
|
|
|
|
|
|
// Load mobs!
|
2019-11-01 23:49:21 +01:00
|
|
|
loadMobs(biomeRecipeConfig, biomeRecipe);
|
2019-11-01 23:09:28 +01:00
|
|
|
|
|
|
|
// Load block conversions
|
2019-11-01 23:49:21 +01:00
|
|
|
loadBlockConversions(biomeRecipeConfig, biomeRecipe);
|
2019-11-01 23:09:28 +01:00
|
|
|
|
|
|
|
// Add the recipe to the list
|
2019-11-01 23:49:21 +01:00
|
|
|
biomeRecipes.add(biomeRecipe);
|
2019-11-01 23:09:28 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
addon.logError("Problem loading biome recipe - skipping! " + e.getMessage());
|
|
|
|
StringBuilder validBiomes = new StringBuilder();
|
|
|
|
for (Biome biome : Biome.values()) {
|
|
|
|
validBiomes.append(" ").append(biome.name());
|
|
|
|
}
|
|
|
|
addon.logError("Valid biomes are " + validBiomes);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-07 01:26:08 +02:00
|
|
|
private Biome loadBiome(String biomeType, ConfigurationSection biomeRecipeConfig) {
|
|
|
|
if (!biomeRecipeConfig.contains("biome")) {
|
|
|
|
addon.logError("No biome defined in the biome reciepe " + biomeType + ". Skipping...");
|
|
|
|
return null;
|
|
|
|
}
|
2020-11-16 00:35:59 +01:00
|
|
|
String name = biomeRecipeConfig.getString("biome").toUpperCase(Locale.ENGLISH);
|
2020-07-07 01:26:08 +02:00
|
|
|
if (Enums.getIfPresent(Biome.class, name).isPresent()) {
|
|
|
|
return Biome.valueOf(name);
|
2020-08-16 19:18:18 +02:00
|
|
|
}
|
2020-07-07 01:26:08 +02:00
|
|
|
// Special case for nether
|
|
|
|
if (name.equals("NETHER") || name.equals("NETHER_WASTES")) {
|
|
|
|
return Enums.getIfPresent(Biome.class, "NETHER").or(Enums.getIfPresent(Biome.class, "NETHER_WASTES").or(Biome.PLAINS));
|
2020-08-16 19:18:18 +02:00
|
|
|
}
|
2020-07-07 01:26:08 +02:00
|
|
|
addon.logError("Biome " + name + " is invalid! Use one of these...");
|
|
|
|
addon.logError(Arrays.stream(Biome.values()).map(Biome::name).collect(Collectors.joining(",")));
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-11-01 23:49:21 +01:00
|
|
|
private BiomeRecipe getBiomeRecipe(ConfigurationSection biomeRecipeConfig, String biomeType, Biome thisBiome, int priority) {
|
2019-11-01 23:09:28 +01:00
|
|
|
BiomeRecipe b = new BiomeRecipe(addon, thisBiome, priority);
|
|
|
|
// Set the name
|
2019-11-01 23:49:21 +01:00
|
|
|
b.setName(biomeType);
|
2020-01-30 18:52:53 +01:00
|
|
|
if (addon.getSettings().isStartupLog()) addon.log("Adding biome recipe for " + biomeType);
|
2019-11-01 23:09:28 +01:00
|
|
|
// Set the permission
|
2019-11-01 23:49:21 +01:00
|
|
|
b.setPermission(biomeRecipeConfig.getString("permission",""));
|
2019-11-01 23:09:28 +01:00
|
|
|
// Set the icon
|
2019-11-01 23:49:21 +01:00
|
|
|
b.setIcon(Material.valueOf(biomeRecipeConfig.getString("icon", "SAPLING")));
|
|
|
|
b.setFriendlyName(ChatColor.translateAlternateColorCodes('&', biomeRecipeConfig.getString("friendlyname", Util.prettifyText(biomeType))));
|
2019-11-01 23:09:28 +01:00
|
|
|
// A value of zero on these means that there must be NO coverage, e.g., desert. If the value is not present, then the default is -1
|
2019-11-01 23:49:21 +01:00
|
|
|
b.setWatercoverage(biomeRecipeConfig.getInt("watercoverage",-1));
|
|
|
|
b.setLavacoverage(biomeRecipeConfig.getInt("lavacoverage",-1));
|
|
|
|
b.setIcecoverage(biomeRecipeConfig.getInt("icecoverage",-1));
|
|
|
|
b.setMobLimit(biomeRecipeConfig.getInt("moblimit", 9));
|
2019-11-01 23:09:28 +01:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2019-11-01 23:49:21 +01:00
|
|
|
private void loadPlants(ConfigurationSection biomeRecipeConfig, BiomeRecipe b) {
|
|
|
|
ConfigurationSection temp = biomeRecipeConfig.getConfigurationSection("plants");
|
2019-11-01 23:09:28 +01:00
|
|
|
// # Plant Material: Probability in %:Block Material on what they grow
|
|
|
|
if (temp != null) {
|
|
|
|
HashMap<String,Object> plants = (HashMap<String,Object>)temp.getValues(false);
|
2019-11-01 23:22:30 +01:00
|
|
|
for (Entry<String, Object> s: plants.entrySet()) {
|
|
|
|
Material plantMaterial = Material.valueOf(s.getKey());
|
|
|
|
String[] split = ((String)s.getValue()).split(":");
|
2020-11-16 00:35:59 +01:00
|
|
|
double plantProbability = Double.parseDouble(split[0]);
|
2019-11-01 23:22:30 +01:00
|
|
|
Material plantGrowOn = Material.valueOf(split[1]);
|
|
|
|
b.addPlants(plantMaterial, plantProbability, plantGrowOn);
|
2019-11-01 23:09:28 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-22 00:44:01 +01:00
|
|
|
|
2019-11-01 23:09:28 +01:00
|
|
|
}
|
|
|
|
|
2019-11-01 23:49:21 +01:00
|
|
|
private void loadBlockConversions(ConfigurationSection biomeRecipeConfig, BiomeRecipe b) {
|
|
|
|
ConfigurationSection conversionSec = biomeRecipeConfig.getConfigurationSection("conversions");
|
2019-11-01 23:09:28 +01:00
|
|
|
if (conversionSec != null) {
|
|
|
|
for (String oldMat : conversionSec.getKeys(false)) {
|
|
|
|
try {
|
2020-11-16 00:35:59 +01:00
|
|
|
Material oldMaterial = Material.valueOf(oldMat.toUpperCase(Locale.ENGLISH));
|
2019-11-01 23:09:28 +01:00
|
|
|
String conversions = conversionSec.getString(oldMat);
|
|
|
|
if (!conversions.isEmpty()) {
|
|
|
|
String[] split = conversions.split(":");
|
2020-11-16 00:35:59 +01:00
|
|
|
double convChance = Double.parseDouble(split[0]);
|
2019-11-01 23:09:28 +01:00
|
|
|
Material newMaterial = Material.valueOf(split[1]);
|
|
|
|
Material localMaterial = Material.valueOf(split[2]);
|
|
|
|
b.addConvBlocks(oldMaterial, newMaterial, convChance, localMaterial);
|
2019-01-26 17:38:13 +01:00
|
|
|
}
|
2019-11-01 23:09:28 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
addon.logError("Could not parse " + oldMat);
|
2019-01-22 00:44:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-11-01 23:09:28 +01:00
|
|
|
}
|
2020-08-16 19:18:18 +02:00
|
|
|
// Get the list of conversions
|
|
|
|
for (String oldMat : biomeRecipeConfig.getStringList("conversion-list")) {
|
|
|
|
try {
|
|
|
|
// Split the string
|
|
|
|
String[] split = oldMat.split(":");
|
|
|
|
Material oldMaterial = Material.valueOf(split[0].toUpperCase());
|
2020-11-16 00:35:59 +01:00
|
|
|
double convChance = Double.parseDouble(split[1]);
|
2020-08-16 19:18:18 +02:00
|
|
|
Material newMaterial = Material.valueOf(split[2]);
|
|
|
|
Material localMaterial = Material.valueOf(split[3]);
|
|
|
|
b.addConvBlocks(oldMaterial, newMaterial, convChance, localMaterial);
|
|
|
|
} catch (Exception e) {
|
|
|
|
addon.logError("Could not parse " + oldMat);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-11-01 23:09:28 +01:00
|
|
|
}
|
2019-01-22 00:44:01 +01:00
|
|
|
|
2019-11-01 23:49:21 +01:00
|
|
|
private void loadMobs(ConfigurationSection biomeRecipeConfig, BiomeRecipe b) {
|
|
|
|
ConfigurationSection temp = biomeRecipeConfig.getConfigurationSection("mobs");
|
2019-11-01 23:09:28 +01:00
|
|
|
// Mob EntityType: Probability:Spawn on Material
|
|
|
|
if (temp != null) {
|
2019-11-01 23:22:30 +01:00
|
|
|
((HashMap<String,Object>)temp.getValues(false)).entrySet().forEach(s -> parseMob(s,b));
|
2019-01-22 00:44:01 +01:00
|
|
|
}
|
2019-11-01 23:09:28 +01:00
|
|
|
|
2019-01-22 00:44:01 +01:00
|
|
|
}
|
|
|
|
|
2019-11-01 05:36:05 +01:00
|
|
|
private void parseMob(Entry<String, Object> s, BiomeRecipe b) {
|
|
|
|
try {
|
2020-11-16 00:35:59 +01:00
|
|
|
EntityType mobType = EntityType.valueOf(s.getKey().toUpperCase(Locale.ENGLISH));
|
2019-11-01 05:36:05 +01:00
|
|
|
String[] split = ((String)s.getValue()).split(":");
|
2020-11-16 00:35:59 +01:00
|
|
|
double mobProbability = Double.parseDouble(split[0]);
|
2019-11-01 05:36:05 +01:00
|
|
|
Material mobSpawnOn = Material.valueOf(split[1]);
|
|
|
|
b.addMobs(mobType, mobProbability, mobSpawnOn);
|
|
|
|
} catch (Exception e) {
|
|
|
|
addon.logError("Could not parse " + s.getKey());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void parseReqBlock(BiomeRecipe b, String rq, ConfigurationSection reqContents) {
|
|
|
|
try {
|
2020-11-16 00:35:59 +01:00
|
|
|
b.addReqBlocks(Material.valueOf(rq.toUpperCase(Locale.ENGLISH)), reqContents.getInt(rq));
|
2019-11-01 05:36:05 +01:00
|
|
|
} catch(Exception e) {
|
|
|
|
addon.logError("Could not parse required block " + rq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-22 00:44:01 +01:00
|
|
|
/**
|
|
|
|
* @return the biomeRecipes
|
|
|
|
*/
|
|
|
|
public List<BiomeRecipe> getBiomeRecipes() {
|
|
|
|
return biomeRecipes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|