use of com.builtbroken.mc.lib.json.processors.recipe.crafting.JsonCraftingRecipeData in project Engine by VoltzEngine-Project.
the class CommandJsonRecipe method handleConsoleCommand.
@Override
public boolean handleConsoleCommand(ICommandSender sender, String[] args) {
if (args != null && args.length > 0 && !"help".equalsIgnoreCase(args[0])) {
if (args[0].equals("generate") || args[0].equals("gen")) {
if (args.length > 1) {
String entryID = args[1];
ItemStack stack = new JsonCraftingRecipeData(null, null, null, false, false).toStack(entryID);
if (stack != null) {
List<IRecipe> recipes = entryID.contains("#") ? InventoryUtility.getRecipesWithOutput(stack) : InventoryUtility.getRecipesWithOutput(stack.getItem());
if (recipes != null) {
sender.addChatMessage(new ChatComponentText("Found " + recipes.size() + " for '" + entryID + "' saving to external json file"));
File writeFile = new File(JsonContentLoader.INSTANCE.externalContentFolder.getParent(), "json-gen/" + (entryID + "-recipes.json").replace(":", "_"));
if (!writeFile.getParentFile().exists()) {
writeFile.getParentFile().mkdirs();
}
try {
JsonObject object = new JsonObject();
int index = 0;
for (IRecipe recipe : recipes) {
try {
if (recipe instanceof ShapedOreRecipe) {
int width = 0;
int height = 0;
Object[] recipeItems = null;
Field field = ShapedOreRecipe.class.getDeclaredField("input");
field.setAccessible(true);
recipeItems = (Object[]) field.get(recipe);
field = ShapedOreRecipe.class.getDeclaredField("width");
field.setAccessible(true);
width = field.getInt(recipe);
field = ShapedOreRecipe.class.getDeclaredField("height");
field.setAccessible(true);
height = field.getInt(recipe);
Pair<String, HashMap<String, JsonElement>> itemSet = generateItemData(recipeItems, width, height);
//Build data
if (itemSet != null) {
JsonObject recipeObject = new JsonObject();
recipeObject.add("type", new JsonPrimitive("shaped"));
recipeObject.add("output", toItemJson(recipe.getRecipeOutput()));
recipeObject.add("grid", new JsonPrimitive(itemSet.left()));
JsonObject itemEntry = new JsonObject();
for (Map.Entry<String, JsonElement> entry : itemSet.right().entrySet()) {
itemEntry.add(entry.getKey(), entry.getValue());
}
recipeObject.add("items", itemEntry);
object.add("craftingGridRecipe:" + (index++), recipeObject);
} else {
sender.addChatMessage(new ChatComponentText("Failed to map recipe items for '" + recipe + "'"));
}
} else if (recipe instanceof ShapedRecipes) {
Pair<String, HashMap<String, JsonElement>> itemSet = generateItemData(((ShapedRecipes) recipe).recipeItems, ((ShapedRecipes) recipe).recipeWidth, ((ShapedRecipes) recipe).recipeHeight);
//Build data
if (itemSet != null) {
JsonObject recipeObject = new JsonObject();
recipeObject.add("type", new JsonPrimitive("shaped"));
recipeObject.add("output", toItemJson(recipe.getRecipeOutput()));
recipeObject.add("grid", new JsonPrimitive(itemSet.left()));
JsonObject itemEntry = new JsonObject();
for (Map.Entry<String, JsonElement> entry : itemSet.right().entrySet()) {
itemEntry.add(entry.getKey(), entry.getValue());
}
recipeObject.add("items", itemEntry);
object.add("craftingGridRecipe:" + (index++), recipeObject);
} else {
sender.addChatMessage(new ChatComponentText("Failed to map recipe items for '" + recipe + "'"));
}
} else {
sender.addChatMessage(new ChatComponentText("Failed to ID recipe type of '" + recipe + "'"));
}
} catch (Exception e) {
sender.addChatMessage(new ChatComponentText("Error processing recipe '" + recipe + "', see logs for details."));
e.printStackTrace();
}
}
if (object.entrySet().size() > 0) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
try (FileWriter file = new FileWriter(writeFile)) {
file.write(gson.toJson(object));
}
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
sender.addChatMessage(new ChatComponentText("Failed to locate recipes for '" + entryID + "'"));
}
} else {
sender.addChatMessage(new ChatComponentText("Failed to locate entry for '" + entryID + "'"));
}
return true;
}
}
}
return handleHelp(sender, args);
}
Aggregations