From 3db24ea4c0b71beed01f7f384df6182072672775 Mon Sep 17 00:00:00 2001 From: squeek Date: Fri, 19 Sep 2014 02:50:10 -0700 Subject: [PATCH] Fixed JSON-defined foods incorrectly using static variables * The variables being static would mean that a maximum of one food could ever be loaded --- .../iguanaman/hungeroverhaul/json/Food.java | 45 +++++++++++-------- .../hungeroverhaul/json/HOJsonData.java | 2 +- .../hungeroverhaul/json/JsonModule.java | 33 +++++++++----- 3 files changed, 49 insertions(+), 31 deletions(-) diff --git a/src/main/java/iguanaman/hungeroverhaul/json/Food.java b/src/main/java/iguanaman/hungeroverhaul/json/Food.java index e037f0e..6f5a2d1 100644 --- a/src/main/java/iguanaman/hungeroverhaul/json/Food.java +++ b/src/main/java/iguanaman/hungeroverhaul/json/Food.java @@ -11,50 +11,59 @@ * Class for serializing Foods to/from json based on format from Jaded's Blood mod by Minalien * https://github.com/Minalien/JadedsBlood */ -public class Food { +public class Food +{ //Registry Name - public static String name = ""; - public static String oreName = ""; - public static int meta = 0; - public static int count = 1; - public static float saturationModifier = 0.0F; - public static int hunger = 0; - public static boolean hasOredictEntry() { + public String name = ""; + public String oreName = ""; + public int meta = 0; + public int count = 1; + public float saturationModifier = 0.0F; + public int hunger = 0; + + public boolean hasOredictEntry() + { return oreName.isEmpty(); } + //TODO handle blocks as well!! - public static ItemStack toItemStack () { + public ItemStack toItemStack() + { Item i = GameData.getItemRegistry().getObject(name); - if (i == null) { + if (i == null) + { Block b = GameData.getBlockRegistry().getObject(name); - if(b != null) + if (b != null) i = Item.getItemFromBlock(b); } return i == null ? null : new ItemStack(GameData.getItemRegistry().getObject(name), count, meta); } - public static FoodValues toFoodValues() { + + public FoodValues toFoodValues() + { return new FoodValues(hunger, saturationModifier); } - public static Food fromItemStack (ItemStack is, FoodValues fv) { + public static Food fromItemStack(ItemStack is, FoodValues fv) + { return fromItemStack(is, fv.saturationModifier, fv.hunger); } - public static Food fromItemStack (ItemStack is, Float saturationModifier, int hunger) { + public static Food fromItemStack(ItemStack is, Float saturationModifier, int hunger) + { Food fd = new Food(); fd.name = GameData.getItemRegistry().getNameForObject(is.getItem()); - if(fd.name == null || fd.name.isEmpty()) + if (fd.name == null || fd.name.isEmpty()) fd.name = GameData.getBlockRegistry().getNameForObject(is.getItem()); - if(fd.name == null || fd.name.isEmpty()) + if (fd.name == null || fd.name.isEmpty()) return null; fd.meta = is.getItemDamage(); fd.count = is.stackSize; int[] oreIds = OreDictionary.getOreIDs(is); - if(oreIds.length > 0) + if (oreIds.length > 0) fd.oreName = OreDictionary.getOreName(oreIds[0]); fd.saturationModifier = saturationModifier; fd.hunger = hunger; return fd; } } - diff --git a/src/main/java/iguanaman/hungeroverhaul/json/HOJsonData.java b/src/main/java/iguanaman/hungeroverhaul/json/HOJsonData.java index c446bd7..fdab991 100644 --- a/src/main/java/iguanaman/hungeroverhaul/json/HOJsonData.java +++ b/src/main/java/iguanaman/hungeroverhaul/json/HOJsonData.java @@ -4,5 +4,5 @@ public class HOJsonData { //list of foods, and values they take being replaced - public static List foods; + public List foods; } diff --git a/src/main/java/iguanaman/hungeroverhaul/json/JsonModule.java b/src/main/java/iguanaman/hungeroverhaul/json/JsonModule.java index 471f163..07523ae 100644 --- a/src/main/java/iguanaman/hungeroverhaul/json/JsonModule.java +++ b/src/main/java/iguanaman/hungeroverhaul/json/JsonModule.java @@ -10,41 +10,50 @@ import java.io.FileReader; import java.util.List; -public class JsonModule { +public class JsonModule +{ private static Gson GSON; private static File[] hojsons; private static List hoData = Lists.newArrayList(); - public static void preinit(File configFolder) { + + public static void preinit(File configFolder) + { GsonBuilder builder = new GsonBuilder(); builder.enableComplexMapKeySerialization(); builder.setPrettyPrinting(); GSON = builder.create(); File hoFolder = new File(configFolder, "HungerOverhaul"); - if(!hoFolder.exists()) + if (!hoFolder.exists()) hoFolder.mkdirs(); hojsons = hoFolder.listFiles(); } - public static void init() { + + public static void init() + { HungerOverhaul.Log.info("Loading JSON Files"); HOJsonData hod; - for(File j:hojsons) { - try { + for (File j : hojsons) + { + try + { FileReader reader = new FileReader(j); hod = GSON.fromJson(reader, HOJsonData.class); reader.close(); hoData.add(hod); - } catch (Exception e) { + } + catch (Exception e) + { HungerOverhaul.Log.warn("Error Loading json files: ", e); } } HungerOverhaul.Log.info("Loading data from json"); - for (HOJsonData h : hoData) { - for (Food f: h.foods){ - FoodModifier.setModifiedFoodValues(f.toItemStack() , f.toFoodValues()); + for (HOJsonData h : hoData) + { + for (Food f : h.foods) + { + FoodModifier.setModifiedFoodValues(f.toItemStack(), f.toFoodValues()); } } HungerOverhaul.Log.info("Loaded all data from JSON"); } } - -