From d5296bdc8c983aef259d419c322dd76fed52176e Mon Sep 17 00:00:00 2001 From: Sn0wStorm Date: Wed, 25 May 2016 20:01:28 +0200 Subject: [PATCH] Check loading Recipes better for validity --- src/com/dre/brewery/BRecipe.java | 52 +++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/src/com/dre/brewery/BRecipe.java b/src/com/dre/brewery/BRecipe.java index ec2fc5d..14a9db7 100644 --- a/src/com/dre/brewery/BRecipe.java +++ b/src/com/dre/brewery/BRecipe.java @@ -104,6 +104,53 @@ public class BRecipe { } } + // check every part of the recipe for validity + public boolean isValid() { + if (name == null || name.length < 1) { + P.p.errorLog("Recipe Name missing or invalid!"); + return false; + } + if (getName(5) == null || getName(5).length() < 1) { + P.p.errorLog("Recipe Name invalid"); + return false; + } + if (ingredients == null || ingredients.isEmpty()) { + P.p.errorLog("No ingredients could be loaded for Recipe: " + getName(5)); + return false; + } + if (cookingTime < 1) { + P.p.errorLog("Invalid cooking time '" + cookingTime + "' in Recipe: " + getName(5)); + return false; + } + if (distillruns < 0) { + P.p.errorLog("Invalid distillruns '" + distillruns + "' in Recipe: " + getName(5)); + return false; + } + if (wood < 0 || wood > 6) { + P.p.errorLog("Invalid wood type '" + wood + "' in Recipe: " + getName(5)); + return false; + } + if (age < 0) { + P.p.errorLog("Invalid age time '" + age + "' in Recipe: " + getName(5)); + return false; + } + try { + Brew.PotionColor.valueOf(getColor()); + } catch (IllegalArgumentException e) { + P.p.errorLog("Invalid Color '" + color + "' in Recipe: " + getName(5)); + return false; + } + if (difficulty < 0 || difficulty > 10) { + P.p.errorLog("Invalid difficulty '" + difficulty + "' in Recipe: " + getName(5)); + return false; + } + if (alcohol < 0) { + P.p.errorLog("Invalid alcohol '" + alcohol + "' in Recipe: " + getName(5)); + return false; + } + return true; + } + // allowed deviation to the recipes count of ingredients at the given difficulty public int allowedCountDiff(int count) { if (count < 8) { @@ -218,11 +265,6 @@ public class BRecipe { return potion; } - // true if name and ingredients are correct - public boolean isValid() { - return (name != null && ingredients != null && !ingredients.isEmpty()); - } - // Getter