Skip to content

Commit

Permalink
Fixed JSON-defined foods incorrectly using static variables
Browse files Browse the repository at this point in the history
 * The variables being static would mean that a maximum of one food could ever be loaded
  • Loading branch information
squeek502 committed Sep 19, 2014
1 parent 6a6df4b commit 3db24ea
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 31 deletions.
45 changes: 27 additions & 18 deletions src/main/java/iguanaman/hungeroverhaul/json/Food.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

public class HOJsonData {
//list of foods, and values they take being replaced
public static List<Food> foods;
public List<Food> foods;
}
33 changes: 21 additions & 12 deletions src/main/java/iguanaman/hungeroverhaul/json/JsonModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<HOJsonData> 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");
}
}


0 comments on commit 3db24ea

Please sign in to comment.