From 697feef3bb89966e526d8d937e5d4308ef978d17 Mon Sep 17 00:00:00 2001 From: tastybento Date: Fri, 1 Nov 2019 15:09:28 -0700 Subject: [PATCH] Refactored recipe loading to be easier to understand. --- .../greenhouses/managers/RecipeManager.java | 208 ++++++++++-------- 1 file changed, 118 insertions(+), 90 deletions(-) diff --git a/src/main/java/world/bentobox/greenhouses/managers/RecipeManager.java b/src/main/java/world/bentobox/greenhouses/managers/RecipeManager.java index f16edd5..a370c3a 100644 --- a/src/main/java/world/bentobox/greenhouses/managers/RecipeManager.java +++ b/src/main/java/world/bentobox/greenhouses/managers/RecipeManager.java @@ -65,105 +65,133 @@ public class RecipeManager { // Loop through all the entries for (String type: biomeSection.getValues(false).keySet()) { - try { - ConfigurationSection biomeRecipe = biomeSection.getConfigurationSection(type); - Biome thisBiome; - 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); - } - int priority = biomeRecipe.getInt("priority", 0); - BiomeRecipe b = new BiomeRecipe(addon, thisBiome,priority); - // Set the name - b.setName(type); - addon.log("Adding biome recipe for " + 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)) { - parseReqBlock(b, rq, reqContents); - } - } - // Load plants - // # Plant Material: Probability in %:Block Material on what they grow - ConfigurationSection temp = biomes.getConfigurationSection("biomes." + type + ".plants"); - if (temp != null) { - HashMap plants = (HashMap)temp.getValues(false); - if (plants != null) { - for (Entry s: plants.entrySet()) { - Material plantMaterial = Material.valueOf(s.getKey()); - String[] split = ((String)s.getValue()).split(":"); - int plantProbability = Integer.parseInt(split[0]); - Material plantGrowOn = Material.valueOf(split[1]); - b.addPlants(plantMaterial, plantProbability, plantGrowOn); - } - } - } - // Load mobs! - // Mob EntityType: Probability:Spawn on Material - temp = biomes.getConfigurationSection("biomes." + type + ".mobs"); - if (temp != null) { - HashMap mobs = (HashMap)temp.getValues(false); - if (mobs != null) { - for (Entry s: mobs.entrySet()) { - parseMob(s,b); - } - } - } - // 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()); - String conversions = conversionSec.getString(oldMat); - if (!conversions.isEmpty()) { - String[] split = conversions.split(":"); - int convChance = Integer.parseInt(split[0]); - Material newMaterial = Material.valueOf(split[1]); - Material localMaterial = Material.valueOf(split[2]); - b.addConvBlocks(oldMaterial, newMaterial, convChance, localMaterial); - } - } catch (Exception e) { - addon.logError("Could not parse " + oldMat); - } - - } - } - // Add the recipe to the list - biomeRecipes.add(b); - } 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); - } - + processEntries(type, biomeSection, biomes); // 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."); } + private void processEntries(String type, ConfigurationSection biomeSection, YamlConfiguration biomes) { + try { + ConfigurationSection biomeRecipe = biomeSection.getConfigurationSection(type); + Biome thisBiome; + 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); + } + int priority = biomeRecipe.getInt("priority", 0); + + // Create the biome recipe + BiomeRecipe b = getBiomeRecipe(biomeRecipe, type, thisBiome, priority); + + // Set the needed blocks + ConfigurationSection reqContents = biomeRecipe.getConfigurationSection("contents"); + if (reqContents != null) { + for (String rq : reqContents.getKeys(false)) { + parseReqBlock(b, rq, reqContents); + } + } + ConfigurationSection temp = biomes.getConfigurationSection("biomes." + type + ".plants"); + // Load plants + loadPlants(type, temp, biomes, b); + + // Load mobs! + loadMobs(type, temp, biomes, b); + + // Load block conversions + loadBlockConversions(type, biomeSection, b); + + // Add the recipe to the list + biomeRecipes.add(b); + } 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 BiomeRecipe getBiomeRecipe(ConfigurationSection biomeRecipe, String type, Biome thisBiome, int priority) { + BiomeRecipe b = new BiomeRecipe(addon, thisBiome, priority); + // Set the name + b.setName(type); + addon.log("Adding biome recipe for " + 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)); + return b; + } + + private void loadPlants(String type, ConfigurationSection temp, YamlConfiguration biomes, BiomeRecipe b) { + // # Plant Material: Probability in %:Block Material on what they grow + if (temp != null) { + HashMap plants = (HashMap)temp.getValues(false); + if (plants != null) { + for (Entry s: plants.entrySet()) { + Material plantMaterial = Material.valueOf(s.getKey()); + String[] split = ((String)s.getValue()).split(":"); + int plantProbability = Integer.parseInt(split[0]); + Material plantGrowOn = Material.valueOf(split[1]); + b.addPlants(plantMaterial, plantProbability, plantGrowOn); + } + } + } + + } + + private void loadBlockConversions(String type, ConfigurationSection biomeSection, BiomeRecipe b) { + ConfigurationSection conversionSec = biomeSection.getConfigurationSection(type + ".conversions"); + if (conversionSec != null) { + for (String oldMat : conversionSec.getKeys(false)) { + try { + Material oldMaterial = Material.valueOf(oldMat.toUpperCase()); + String conversions = conversionSec.getString(oldMat); + if (!conversions.isEmpty()) { + String[] split = conversions.split(":"); + int convChance = Integer.parseInt(split[0]); + Material newMaterial = Material.valueOf(split[1]); + Material localMaterial = Material.valueOf(split[2]); + b.addConvBlocks(oldMaterial, newMaterial, convChance, localMaterial); + } + } catch (Exception e) { + addon.logError("Could not parse " + oldMat); + } + + } + } + } + + private void loadMobs(String type, ConfigurationSection temp, YamlConfiguration biomes, BiomeRecipe b) { + // Mob EntityType: Probability:Spawn on Material + temp = biomes.getConfigurationSection("biomes." + type + ".mobs"); + if (temp != null) { + HashMap mobs = (HashMap)temp.getValues(false); + if (mobs != null) { + for (Entry s: mobs.entrySet()) { + parseMob(s,b); + } + } + } + + } + private void parseMob(Entry s, BiomeRecipe b) { try { EntityType mobType = EntityType.valueOf(s.getKey().toUpperCase());