Refactored recipe loading to be easier to understand.

This commit is contained in:
tastybento 2019-11-01 15:09:28 -07:00
parent 470124c389
commit 697feef3bb
1 changed files with 118 additions and 90 deletions

View File

@ -65,6 +65,17 @@ public class RecipeManager {
// Loop through all the entries // Loop through all the entries
for (String type: biomeSection.getValues(false).keySet()) { for (String type: biomeSection.getValues(false).keySet()) {
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 { try {
ConfigurationSection biomeRecipe = biomeSection.getConfigurationSection(type); ConfigurationSection biomeRecipe = biomeSection.getConfigurationSection(type);
Biome thisBiome; Biome thisBiome;
@ -76,7 +87,42 @@ public class RecipeManager {
thisBiome = Biome.valueOf(type); thisBiome = Biome.valueOf(type);
} }
int priority = biomeRecipe.getInt("priority", 0); int priority = biomeRecipe.getInt("priority", 0);
BiomeRecipe b = new BiomeRecipe(addon, thisBiome,priority);
// 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 // Set the name
b.setName(type); b.setName(type);
addon.log("Adding biome recipe for " + type); addon.log("Adding biome recipe for " + type);
@ -90,16 +136,11 @@ public class RecipeManager {
b.setLavacoverage(biomeRecipe.getInt("lavacoverage",-1)); b.setLavacoverage(biomeRecipe.getInt("lavacoverage",-1));
b.setIcecoverage(biomeRecipe.getInt("icecoverage",-1)); b.setIcecoverage(biomeRecipe.getInt("icecoverage",-1));
b.setMobLimit(biomeRecipe.getInt("moblimit", 9)); b.setMobLimit(biomeRecipe.getInt("moblimit", 9));
// Set the needed blocks return b;
ConfigurationSection reqContents = biomeRecipe.getConfigurationSection("contents");
if (reqContents != null) {
for (String rq : reqContents.getKeys(false)) {
parseReqBlock(b, rq, reqContents);
} }
}
// Load plants private void loadPlants(String type, ConfigurationSection temp, YamlConfiguration biomes, BiomeRecipe b) {
// # Plant Material: Probability in %:Block Material on what they grow // # Plant Material: Probability in %:Block Material on what they grow
ConfigurationSection temp = biomes.getConfigurationSection("biomes." + type + ".plants");
if (temp != null) { if (temp != null) {
HashMap<String,Object> plants = (HashMap<String,Object>)temp.getValues(false); HashMap<String,Object> plants = (HashMap<String,Object>)temp.getValues(false);
if (plants != null) { if (plants != null) {
@ -112,18 +153,10 @@ public class RecipeManager {
} }
} }
} }
// 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 (Entry<String, Object> s: mobs.entrySet()) {
parseMob(s,b);
} }
}
} private void loadBlockConversions(String type, ConfigurationSection biomeSection, BiomeRecipe b) {
// Load block conversions
ConfigurationSection conversionSec = biomeSection.getConfigurationSection(type + ".conversions"); ConfigurationSection conversionSec = biomeSection.getConfigurationSection(type + ".conversions");
if (conversionSec != null) { if (conversionSec != null) {
for (String oldMat : conversionSec.getKeys(false)) { for (String oldMat : conversionSec.getKeys(false)) {
@ -143,26 +176,21 @@ public class RecipeManager {
} }
} }
// 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);
} }
// Check maximum number private void loadMobs(String type, ConfigurationSection temp, YamlConfiguration biomes, BiomeRecipe b) {
if (biomeRecipes.size() == MAXIMUM_INVENTORY_SIZE) { // Mob EntityType: Probability:Spawn on Material
addon.logWarning("Cannot load any more biome recipies - limit is " + MAXIMUM_INVENTORY_SIZE); temp = biomes.getConfigurationSection("biomes." + type + ".mobs");
break; if (temp != null) {
HashMap<String,Object> mobs = (HashMap<String,Object>)temp.getValues(false);
if (mobs != null) {
for (Entry<String, Object> s: mobs.entrySet()) {
parseMob(s,b);
}
}
} }
} }
addon.log("Loaded " + biomeRecipes.size() + " biome recipes.");
}
private void parseMob(Entry<String, Object> s, BiomeRecipe b) { private void parseMob(Entry<String, Object> s, BiomeRecipe b) {
try { try {