Greenhouses/src/main/java/world/bentobox/greenhouses/managers/RecipeManager.java

258 lines
11 KiB
Java
Raw Normal View History

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;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
2019-11-01 05:36:05 +01:00
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
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;
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());
}
}
/**
* 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
2021-09-18 19:33:48 +02:00
assert biomeSection != null;
2019-01-22 00:44:01 +01:00
for (String type: biomeSection.getValues(false).keySet()) {
2019-11-01 23:45:40 +01:00
processEntries(type, biomeSection);
// 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) {
try {
2019-11-01 23:49:21 +01:00
ConfigurationSection biomeRecipeConfig = biomeSection.getConfigurationSection(biomeType);
2021-09-18 19:33:48 +02:00
assert biomeRecipeConfig != null;
Biome thisBiome = loadBiome(biomeType, biomeRecipeConfig);
if (thisBiome == null) return;
2019-11-01 23:49:21 +01:00
int priority = biomeRecipeConfig.getInt("priority", 0);
// Create the biome recipe
2019-11-01 23:49:21 +01:00
BiomeRecipe biomeRecipe = getBiomeRecipe(biomeRecipeConfig, biomeType, thisBiome, priority);
// Set the needed blocks
2019-11-01 23:49:21 +01:00
ConfigurationSection reqContents = biomeRecipeConfig.getConfigurationSection("contents");
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:45:40 +01:00
// Load plants
2019-11-01 23:49:21 +01:00
loadPlants(biomeRecipeConfig, biomeRecipe);
// Load mobs!
2019-11-01 23:49:21 +01:00
loadMobs(biomeRecipeConfig, biomeRecipe);
// Load block conversions
2019-11-01 23:49:21 +01:00
loadBlockConversions(biomeRecipeConfig, biomeRecipe);
// Add the recipe to the list
2019-11-01 23:49:21 +01:00
biomeRecipes.add(biomeRecipe);
} 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);
}
}
private Biome loadBiome(String biomeType, ConfigurationSection biomeRecipeConfig) {
if (!biomeRecipeConfig.contains("biome")) {
addon.logError("No biome defined in the biome reciepe " + biomeType + ". Skipping...");
return null;
}
2021-09-18 19:33:48 +02:00
String name = Objects.requireNonNull(biomeRecipeConfig.getString("biome")).toUpperCase(Locale.ENGLISH);
if (Enums.getIfPresent(Biome.class, name).isPresent()) {
return Biome.valueOf(name);
}
// 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));
}
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) {
BiomeRecipe b = new BiomeRecipe(addon, thisBiome, priority);
// Set the name
2019-11-01 23:49:21 +01:00
b.setName(biomeType);
if (addon.getSettings().isStartupLog()) addon.log("Adding biome recipe for " + biomeType);
// Set the permission
2019-11-01 23:49:21 +01:00
b.setPermission(biomeRecipeConfig.getString("permission",""));
// 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))));
// 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));
return b;
}
2019-11-01 23:49:21 +01:00
private void loadPlants(ConfigurationSection biomeRecipeConfig, BiomeRecipe b) {
ConfigurationSection temp = biomeRecipeConfig.getConfigurationSection("plants");
// # 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(":");
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-01-22 00:44:01 +01:00
}
2019-11-01 23:49:21 +01:00
private void loadBlockConversions(ConfigurationSection biomeRecipeConfig, BiomeRecipe b) {
ConfigurationSection conversionSec = biomeRecipeConfig.getConfigurationSection("conversions");
if (conversionSec != null) {
for (String oldMat : conversionSec.getKeys(false)) {
try {
Material oldMaterial = Material.valueOf(oldMat.toUpperCase(Locale.ENGLISH));
String conversions = conversionSec.getString(oldMat);
2021-09-18 19:33:48 +02:00
if (!Objects.requireNonNull(conversions).isEmpty()) {
String[] split = conversions.split(":");
double convChance = Double.parseDouble(split[0]);
Material newMaterial = Material.valueOf(split[1]);
Material localMaterial = null;
if(split.length > 2) {
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
}
}
}
// 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());
double convChance = Double.parseDouble(split[1]);
Material newMaterial = Material.valueOf(split[2]);
Material localMaterial = null;
if(split.length > 3) {
localMaterial = Material.valueOf(split[3]);
}
b.addConvBlocks(oldMaterial, newMaterial, convChance, localMaterial);
} catch (Exception e) {
addon.logError("Could not parse " + oldMat);
}
}
}
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");
// Mob EntityType: Probability:Spawn on Material
if (temp != null) {
2021-08-01 08:10:07 +02:00
temp.getValues(false).entrySet().forEach(s -> parseMob(s,b));
2019-01-22 00:44:01 +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 {
EntityType mobType = EntityType.valueOf(s.getKey().toUpperCase(Locale.ENGLISH));
2019-11-01 05:36:05 +01:00
String[] split = ((String)s.getValue()).split(":");
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 {
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;
}
}