2019-01-22 00:44:01 +01:00
|
|
|
package world.bentobox.greenhouses.managers;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
2019-01-23 05:54:43 +01:00
|
|
|
import java.util.Optional;
|
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;
|
|
|
|
|
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());
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* @throws FileNotFoundException - no file found
|
2019-01-22 00:44:01 +01:00
|
|
|
*/
|
2019-01-26 17:38:13 +01:00
|
|
|
private void loadBiomeRecipes() throws FileNotFoundException, IOException, InvalidConfigurationException {
|
2019-01-22 00:44:01 +01:00
|
|
|
biomeRecipes.clear();
|
|
|
|
YamlConfiguration biomes = new YamlConfiguration();
|
|
|
|
File biomeFile = new File(addon.getDataFolder(), "biomes.yml");
|
|
|
|
if (!biomeFile.exists()) {
|
|
|
|
addon.logError("No biomes.yml file!");
|
|
|
|
addon.saveResource("biomes.yml", true);
|
|
|
|
}
|
|
|
|
biomes.load(biomeFile);
|
|
|
|
ConfigurationSection biomeSection = biomes.getConfigurationSection("biomes");
|
|
|
|
if (biomeSection == null) {
|
|
|
|
addon.logError("biomes.yml file is missing, empty or corrupted. Delete and reload plugin again!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop through all the entries
|
|
|
|
for (String type: biomeSection.getValues(false).keySet()) {
|
|
|
|
try {
|
|
|
|
ConfigurationSection biomeRecipe = biomeSection.getConfigurationSection(type);
|
2019-01-26 17:38:13 +01:00
|
|
|
Biome thisBiome;
|
2019-01-22 00:44:01 +01:00
|
|
|
if (biomeRecipe.contains("biome")) {
|
|
|
|
// Try and get the biome via the biome setting
|
|
|
|
thisBiome = Biome.valueOf(biomeRecipe.getString("biome").toUpperCase());
|
|
|
|
} else {
|
|
|
|
// Old style, where type was the biome name
|
|
|
|
thisBiome = Biome.valueOf(type);
|
|
|
|
}
|
2019-01-26 17:38:13 +01:00
|
|
|
int priority = biomeRecipe.getInt("priority", 0);
|
|
|
|
BiomeRecipe b = new BiomeRecipe(addon, thisBiome,priority);
|
|
|
|
// Set the name
|
|
|
|
b.setName(type);
|
|
|
|
// Set the permission
|
|
|
|
b.setPermission(biomeRecipe.getString("permission",""));
|
|
|
|
// Set the icon
|
|
|
|
b.setIcon(Material.valueOf(biomeRecipe.getString("icon", "SAPLING")));
|
|
|
|
b.setFriendlyName(ChatColor.translateAlternateColorCodes('&', biomeRecipe.getString("friendlyname", Util.prettifyText(type))));
|
|
|
|
// 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
|
|
|
|
b.setWatercoverage(biomeRecipe.getInt("watercoverage",-1));
|
|
|
|
b.setLavacoverage(biomeRecipe.getInt("lavacoverage",-1));
|
|
|
|
b.setIcecoverage(biomeRecipe.getInt("icecoverage",-1));
|
|
|
|
b.setMobLimit(biomeRecipe.getInt("moblimit", 9));
|
|
|
|
// Set the needed blocks
|
|
|
|
ConfigurationSection reqContents = biomeRecipe.getConfigurationSection("contents");
|
|
|
|
if (reqContents != null) {
|
|
|
|
for (String rq : reqContents.getKeys(false)) {
|
|
|
|
try {
|
|
|
|
b.addReqBlocks(Material.valueOf(rq.toUpperCase()), reqContents.getInt(rq));
|
|
|
|
} catch(Exception e) {
|
|
|
|
addon.logError("Could not parse required block " + rq);
|
2019-01-22 00:44:01 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-26 17:38:13 +01:00
|
|
|
}
|
|
|
|
// Load plants
|
|
|
|
// # Plant Material: Probability in %:Block Material on what they grow
|
|
|
|
ConfigurationSection temp = biomes.getConfigurationSection("biomes." + type + ".plants");
|
|
|
|
if (temp != null) {
|
|
|
|
HashMap<String,Object> plants = (HashMap<String,Object>)temp.getValues(false);
|
|
|
|
if (plants != null) {
|
|
|
|
for (String s: plants.keySet()) {
|
|
|
|
Material plantMaterial = Material.valueOf(s);
|
|
|
|
String[] split = ((String)plants.get(s)).split(":");
|
|
|
|
int plantProbability = Integer.valueOf(split[0]);
|
|
|
|
Material plantGrowOn = Material.valueOf(split[1]);
|
|
|
|
b.addPlants(plantMaterial, plantProbability, plantGrowOn);
|
2019-01-22 00:44:01 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-26 17:38:13 +01:00
|
|
|
}
|
|
|
|
// Load mobs!
|
|
|
|
// Mob EntityType: Probability:Spawn on Material
|
|
|
|
temp = biomes.getConfigurationSection("biomes." + type + ".mobs");
|
|
|
|
if (temp != null) {
|
|
|
|
HashMap<String,Object> mobs = (HashMap<String,Object>)temp.getValues(false);
|
|
|
|
if (mobs != null) {
|
|
|
|
for (String s: mobs.keySet()) {
|
|
|
|
try {
|
|
|
|
EntityType mobType = EntityType.valueOf(s.toUpperCase());
|
2019-01-22 00:44:01 +01:00
|
|
|
String[] split = ((String)mobs.get(s)).split(":");
|
|
|
|
int mobProbability = Integer.valueOf(split[0]);
|
|
|
|
Material mobSpawnOn = Material.valueOf(split[1]);
|
|
|
|
b.addMobs(mobType, mobProbability, mobSpawnOn);
|
2019-01-26 17:38:13 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
addon.logError("Could not parse " + s);
|
2019-01-22 00:44:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-26 17:38:13 +01:00
|
|
|
}
|
|
|
|
// Load block conversions
|
|
|
|
ConfigurationSection conversionSec = biomeSection.getConfigurationSection(type + ".conversions");
|
|
|
|
if (conversionSec != null) {
|
|
|
|
for (String oldMat : conversionSec.getKeys(false)) {
|
|
|
|
try {
|
|
|
|
Material oldMaterial = Material.valueOf(oldMat.toUpperCase());
|
2019-01-22 00:44:01 +01:00
|
|
|
String conversions = conversionSec.getString(oldMat);
|
|
|
|
if (!conversions.isEmpty()) {
|
|
|
|
String[] split = conversions.split(":");
|
|
|
|
int convChance = Integer.valueOf(split[0]);
|
|
|
|
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
|
|
|
} catch (Exception e) {
|
|
|
|
addon.logError("Could not parse " + oldMat);
|
2019-01-22 00:44:01 +01:00
|
|
|
}
|
|
|
|
|
2019-01-26 17:38:13 +01:00
|
|
|
}
|
2019-01-22 00:44:01 +01:00
|
|
|
}
|
2019-01-26 17:38:13 +01:00
|
|
|
// Add the recipe to the list
|
|
|
|
biomeRecipes.add(b);
|
2019-01-22 00:44:01 +01:00
|
|
|
} catch (Exception e) {
|
2019-01-26 17:38:13 +01:00
|
|
|
addon.logError("Problem loading biome recipe - skipping! " + e.getMessage());
|
|
|
|
StringBuilder validBiomes = new StringBuilder();
|
2019-01-22 00:44:01 +01:00
|
|
|
for (Biome biome : Biome.values()) {
|
2019-01-26 17:38:13 +01:00
|
|
|
validBiomes.append(" ").append(biome.name());
|
2019-01-22 00:44:01 +01:00
|
|
|
}
|
2019-01-26 17:38:13 +01:00
|
|
|
addon.logError("Valid biomes are " + validBiomes);
|
2019-01-22 00:44:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check maximum number
|
|
|
|
if (biomeRecipes.size() == MAXIMUM_INVENTORY_SIZE) {
|
2019-01-26 17:38:13 +01:00
|
|
|
addon.logWarning("Cannot load any more biome recipies - limit is " + MAXIMUM_INVENTORY_SIZE);
|
2019-01-22 00:44:01 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
addon.log("Loaded " + biomeRecipes.size() + " biome recipes.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the biomeRecipes
|
|
|
|
*/
|
|
|
|
public List<BiomeRecipe> getBiomeRecipes() {
|
|
|
|
return biomeRecipes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|