Brick Oven main JSON loader done

master
sealedinterface 2016-05-19 23:34:17 -07:00
parent 53c89cafdf
commit 63d3c648c3
8 changed files with 338 additions and 7 deletions

View File

@ -1,9 +1,6 @@
package net.einsteinsci.betterbeginnings.config;
import net.einsteinsci.betterbeginnings.config.json.BBJsonLoader;
import net.einsteinsci.betterbeginnings.config.json.IJsonConfig;
import net.einsteinsci.betterbeginnings.config.json.KilnConfig;
import net.einsteinsci.betterbeginnings.config.json.SmelterConfig;
import net.einsteinsci.betterbeginnings.config.json.*;
import net.einsteinsci.betterbeginnings.util.LogUtil;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
@ -34,6 +31,7 @@ public class BBConfigFolderLoader
loadJsonConfig(e, KilnConfig.INSTANCE);
loadJsonConfig(e, SmelterConfig.INSTANCE);
loadJsonConfig(e, BrickOvenConfig.INSTANCE);
}
public static void loadJsonConfig(FMLInitializationEvent e, IJsonConfig config)

View File

@ -0,0 +1,102 @@
package net.einsteinsci.betterbeginnings.config.json;
import net.einsteinsci.betterbeginnings.util.LogUtil;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import org.apache.logging.log4j.Level;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
public class BrickOvenConfig implements IJsonConfig
{
public static final BrickOvenConfig INSTANCE = new BrickOvenConfig();
private JsonBrickOvenRecipeHandler mainRecipes = new JsonBrickOvenRecipeHandler();
@Override
public String getSubFolder()
{
return "brickoven";
}
@Override
public String getMainJson(File subfolder)
{
File mainf = new File(subfolder, "main.json");
if (!mainf.exists())
{
return "{}";
}
try
{
return new String(Files.readAllBytes(mainf.toPath()));
}
catch (IOException e)
{
LogUtil.log(Level.ERROR, "IOException occurred opening config/betterbeginnings/brickoven/main.json!");
LogUtil.log("");
LogUtil.log(Level.ERROR, e.toString());
return "{}";
}
}
@Override
public String getAutoJson(File subfolder)
{
return "{}";
}
@Override
public String getCustomJson(File subfolder)
{
return "{}";
}
@Override
public boolean isOnlyMain()
{
return true;
}
@Override
public void loadJsonConfig(FMLInitializationEvent e, String mainJson, String autoJson, String customJson)
{
mainRecipes = BBJsonLoader.deserializeObject(mainJson, JsonBrickOvenRecipeHandler.class);
for (JsonBrickOvenShapedRecipe j : mainRecipes.getShaped())
{
j.register();
}
for (JsonBrickOvenShapelessRecipe j : mainRecipes.getShapeless())
{
j.register();
}
}
@Override
public void savePostLoad(File subfolder)
{
String json = BBJsonLoader.serializeObject(mainRecipes);
try
{
File mainf = new File(subfolder, "main.json");
Files.write(mainf.toPath(), json.getBytes(), StandardOpenOption.CREATE);
}
catch (IOException e)
{
LogUtil.log(Level.ERROR, "IOException occurred saving config/betterbeginnings/brickoven/main.json!");
LogUtil.log("");
LogUtil.log(Level.ERROR, e.toString());
}
}
public JsonBrickOvenRecipeHandler getMainRecipes()
{
return mainRecipes;
}
}

View File

@ -0,0 +1,36 @@
package net.einsteinsci.betterbeginnings.config.json;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import java.util.ArrayList;
import java.util.List;
public class JsonBrickOvenRecipeHandler
{
private List<JsonBrickOvenShapedRecipe> shaped;
private List<JsonBrickOvenShapelessRecipe> shapeless;
public JsonBrickOvenRecipeHandler()
{
shaped = new ArrayList<>();
shapeless = new ArrayList<>();
// TESTING ONLY
shaped.add(new JsonBrickOvenShapedRecipe(new ItemStack(Items.beef), "x ", "ox", 'x',
Blocks.bedrock, 'o', Items.chainmail_chestplate));
shapeless.add(new JsonBrickOvenShapelessRecipe(new ItemStack(Items.porkchop), Blocks.bedrock, Items.chainmail_boots));
}
public List<JsonBrickOvenShapedRecipe> getShaped()
{
return shaped;
}
public List<JsonBrickOvenShapelessRecipe> getShapeless()
{
return shapeless;
}
}

View File

@ -0,0 +1,131 @@
package net.einsteinsci.betterbeginnings.config.json;
import net.einsteinsci.betterbeginnings.register.recipe.BrickOvenRecipeHandler;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JsonBrickOvenShapedRecipe
{
private JsonLoadedItemStack output;
private List<String> recipe = new ArrayList<>();
private Map<Character, JsonLoadedItem> ingredients = new HashMap<>();
public JsonBrickOvenShapedRecipe(ItemStack output, Object... params)
{
this.output = new JsonLoadedItemStack(output);
char active = '\0';
boolean recipeFinished = false;
for (Object obj : params)
{
if (!recipeFinished)
{
if (obj instanceof String)
{
String str = (String)obj;
recipe.add(str);
}
else if (obj instanceof Character)
{
active = (Character)obj;
recipeFinished = true;
}
else
{
throw new IllegalArgumentException("Invalid type for first phase of recipe: " +
obj.getClass().getName());
}
}
else
{
if (obj instanceof Character)
{
char c = (Character)obj;
if (c != ' ')
{
active = c;
}
}
else if (obj instanceof ItemStack)
{
ItemStack stack = (ItemStack)obj;
JsonLoadedItem ing = new JsonLoadedItem(stack);
ingredients.put(active, ing);
}
else if (obj instanceof Item)
{
Item item = (Item)obj;
JsonLoadedItem ing = new JsonLoadedItem(new ItemStack(item));
ingredients.put(active, ing);
}
else if (obj instanceof Block)
{
Block block = (Block)obj;
JsonLoadedItem ing = new JsonLoadedItem(new ItemStack(block));
ingredients.put(active, ing);
}
}
}
}
public JsonBrickOvenShapedRecipe(JsonLoadedItemStack output, List<String> recipe, Map<Character, JsonLoadedItem> ingredients)
{
this.output = output;
this.recipe = recipe;
this.ingredients = ingredients;
}
public void register()
{
List<Object> res = new ArrayList<>();
for (String s : recipe)
{
res.add(s);
}
boolean oreDict = false;
for (Map.Entry<Character, JsonLoadedItem> entry : ingredients.entrySet())
{
res.add(entry.getKey());
JsonLoadedItem jli = entry.getValue();
ItemStack stack = jli.getFirstItemStackOrNull();
if (stack != null)
{
res.add(stack);
}
else
{
ItemStack invalid = new ItemStack(Blocks.barrier);
invalid.setStackDisplayName("ERROR IN LOADING JSON RECIPE. MISSING INGREDIENT.");
res.add(invalid);
}
}
Object[] params = res.toArray();
BrickOvenRecipeHandler.addShapedRecipe(output.getFirstItemStackOrNull(), params);
}
public JsonLoadedItemStack getOutput()
{
return output;
}
public List<String> getRecipe()
{
return recipe;
}
public Map<Character, JsonLoadedItem> getIngredients()
{
return ingredients;
}
}

View File

@ -0,0 +1,66 @@
package net.einsteinsci.betterbeginnings.config.json;
import net.einsteinsci.betterbeginnings.register.recipe.BrickOvenRecipeHandler;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import java.util.ArrayList;
import java.util.List;
public class JsonBrickOvenShapelessRecipe
{
private JsonLoadedItemStack output;
private List<JsonLoadedItem> inputs;
public JsonBrickOvenShapelessRecipe(ItemStack output, Object... params)
{
this.output = new JsonLoadedItemStack(output);
inputs = new ArrayList<>();
for (Object obj : params)
{
if (obj instanceof JsonLoadedItem)
{
inputs.add((JsonLoadedItem)obj);
}
else if (obj instanceof ItemStack)
{
ItemStack stack = (ItemStack)obj;
inputs.add(new JsonLoadedItem(stack));
}
else if (obj instanceof Item)
{
Item item = (Item)obj;
inputs.add(new JsonLoadedItem(new ItemStack(item)));
}
else if (obj instanceof Block)
{
Block block = (Block)obj;
inputs.add(new JsonLoadedItem(new ItemStack(block)));
}
}
}
public JsonBrickOvenShapelessRecipe(JsonLoadedItemStack output, List<JsonLoadedItem> inputs)
{
this.output = output;
this.inputs = inputs;
}
public void register()
{
List<Object> res = new ArrayList<>();
for (JsonLoadedItem jli : inputs)
{
ItemStack stack = jli.getFirstItemStackOrNull();
if (stack != null)
{
res.add(stack);
}
}
Object[] params = res.toArray();
BrickOvenRecipeHandler.addShapelessRecipe(output.getFirstItemStackOrNull(), params);
}
}

View File

@ -1,6 +1,5 @@
package net.einsteinsci.betterbeginnings.config.json;
import net.einsteinsci.betterbeginnings.register.recipe.KilnRecipeHandler;
import net.einsteinsci.betterbeginnings.register.recipe.SmelterRecipeHandler;
import net.einsteinsci.betterbeginnings.util.LogUtil;
import net.minecraft.item.ItemStack;

View File

@ -15,7 +15,7 @@ public class JsonSmelterRecipeHandler
{
recipes = new ArrayList<>();
// TESTING ONLY
// TESTING ONLY //
recipes.add(new JsonSmelterRecipe(new ItemStack(Blocks.bedrock), new ItemStack(Items.golden_hoe), 0.5f, 1, 0));
}

View File

@ -77,7 +77,6 @@ public class SmelterConfig implements IJsonConfig
public void savePostLoad(File subfolder)
{
String json = BBJsonLoader.serializeObject(mainRecipes);
try
{
File mainf = new File(subfolder, "main.json");