Search in sources :

Example 1 with Pair

use of com.builtbroken.jlib.type.Pair 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);
}
Also used : ShapedRecipes(net.minecraft.item.crafting.ShapedRecipes) HashMap(java.util.HashMap) FileWriter(java.io.FileWriter) Field(java.lang.reflect.Field) JsonCraftingRecipeData(com.builtbroken.mc.lib.json.processors.recipe.crafting.JsonCraftingRecipeData) ChatComponentText(net.minecraft.util.ChatComponentText) Pair(com.builtbroken.jlib.type.Pair) IRecipe(net.minecraft.item.crafting.IRecipe) ShapedOreRecipe(net.minecraftforge.oredict.ShapedOreRecipe) ItemStack(net.minecraft.item.ItemStack) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Pair

use of com.builtbroken.jlib.type.Pair in project Engine by VoltzEngine-Project.

the class AutoCraftingManager method getIdealRecipe.

/**
 * Does this player's inventory contain the required resources to craft this item?
 *
 * @return Required items to make the desired item.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public Pair<ItemStack, ItemStack[]> getIdealRecipe(ItemStack outputItem) {
    this.printDebug("IdealRecipe", outputItem.toString());
    for (IRecipe object : RecipeUtility.getRecipesByOutput(outputItem)) {
        if (AutoCraftingManager.areStacksEqual(outputItem, object.getRecipeOutput())) {
            this.printDebug("IdealRecipe", "Output Match Found");
            if (object instanceof ShapedRecipes) {
                if (this.hasResource(((ShapedRecipes) object).recipeItems) != null) {
                    this.printDebug("IdealRecipe", "Shaped Recipe Found");
                    return new Pair<>(object.getRecipeOutput().copy(), ((ShapedRecipes) object).recipeItems);
                }
            } else if (object instanceof ShapelessRecipes) {
                if (this.hasResource(((ShapelessRecipes) object).recipeItems.toArray(new ItemStack[1])) != null) {
                    this.printDebug("IdealRecipe", "Shapeless Recipe Found");
                    return new Pair<>(object.getRecipeOutput().copy(), (ItemStack[]) ((ShapelessRecipes) object).recipeItems.toArray(new ItemStack[1]));
                }
            } else if (object instanceof ShapedOreRecipe) {
                ShapedOreRecipe oreRecipe = (ShapedOreRecipe) object;
                Object[] oreRecipeInput = ReflectionHelper.getPrivateValue(ShapedOreRecipe.class, oreRecipe, "input");
                ArrayList<ItemStack> hasResources = this.hasResource(oreRecipeInput);
                if (hasResources != null) {
                    this.printDebug("IdealRecipe", "ShapedOre Recipe Found");
                    return new Pair<>(object.getRecipeOutput().copy(), hasResources.toArray(new ItemStack[1]));
                }
            } else if (object instanceof ShapelessOreRecipe) {
                ShapelessOreRecipe oreRecipe = (ShapelessOreRecipe) object;
                ArrayList oreRecipeInput = ReflectionHelper.getPrivateValue(ShapelessOreRecipe.class, oreRecipe, "input");
                List<ItemStack> hasResources = this.hasResource(oreRecipeInput.toArray());
                if (hasResources != null) {
                    this.printDebug("IdealRecipe", "ShapelessOre Recipe Found");
                    return new Pair<>(object.getRecipeOutput().copy(), hasResources.toArray(new ItemStack[1]));
                }
            }
        }
    }
    return null;
}
Also used : ShapedRecipes(net.minecraft.item.crafting.ShapedRecipes) IRecipe(net.minecraft.item.crafting.IRecipe) ShapedOreRecipe(net.minecraftforge.oredict.ShapedOreRecipe) ArrayList(java.util.ArrayList) ShapelessOreRecipe(net.minecraftforge.oredict.ShapelessOreRecipe) ArrayList(java.util.ArrayList) List(java.util.List) ShapelessRecipes(net.minecraft.item.crafting.ShapelessRecipes) ItemStack(net.minecraft.item.ItemStack) Pair(com.builtbroken.jlib.type.Pair)

Example 3 with Pair

use of com.builtbroken.jlib.type.Pair in project Engine by VoltzEngine-Project.

the class Schematic method getBox.

/**
 * Creates a map of vectors in the shape of a square
 *
 * @param center - center to create the box around, controls offset for later if needed
 * @param block  - block to make the box out of
 * @param meta   - meta value of the block for placement
 * @param sizeX  - size from the center to the edge, half of the side
 * @param sizeZ  - size from the center to the edge, half of the side
 * @return hash map of vectors to placement data
 */
public HashMap<Pos, Pair<Block, Integer>> getBox(final Pos center, Block block, int meta, int sizeX, int sizeZ) {
    HashMap<Pos, Pair<Block, Integer>> returnMap = new HashMap();
    // zero zero corner of the square
    Pos start = new Pos(-sizeX, 0, -sizeZ).add(center);
    if (sizeX != sizeZ) {
        // X sides
        for (int x = 0; x <= sizeX * 2; x++) {
            returnMap.put(new Pos(x, 0, 0).add(start), new Pair<>(block, meta));
            returnMap.put(new Pos(x, 0, sizeZ * 2).add(start), new Pair<>(block, meta));
        }
        // Z sides
        for (int z = 0; z <= sizeZ * 2; z++) {
            returnMap.put(new Pos(0, 0, z).add(start), new Pair<>(block, meta));
            returnMap.put(new Pos(sizeX * 2, 0, z).add(start), new Pair<>(block, meta));
        }
    } else {
        // All sides, used verses the other way as it cuts the time in half
        for (int s = 0; s <= sizeX * 2; s++) {
            returnMap.put(new Pos(s, 0, 0).add(start), new Pair<>(block, meta));
            returnMap.put(new Pos(s, 0, sizeZ * 2).add(start), new Pair<>(block, meta));
            returnMap.put(new Pos(0, 0, s).add(start), new Pair<>(block, meta));
            returnMap.put(new Pos(sizeZ * 2, 0, s).add(start), new Pair<>(block, meta));
        }
    }
    return returnMap;
}
Also used : Pos(com.builtbroken.mc.lib.transform.vector.Pos) HashMap(java.util.HashMap) Pair(com.builtbroken.jlib.type.Pair)

Example 4 with Pair

use of com.builtbroken.jlib.type.Pair in project Engine by VoltzEngine-Project.

the class HeatingDictionary method getSpecificHeat.

/**
 * Grabs the specific heating point of a block at the location
 */
public static float getSpecificHeat(World world, int x, int y, int z) {
    Block block = world.getBlock(x, y, z);
    int meta = world.getBlockMetadata(x, y, z);
    if (block != null) {
        if (blockToHeatMap.containsKey(block)) {
            return blockToHeatMap.get(block);
        } else if (idMetaToHeatMap.containsKey(new Pair(block, meta))) {
            return idMetaToHeatMap.get(new Pair(block, meta));
        } else if (materialToHeatMap.containsKey(block.getMaterial())) {
            return materialToHeatMap.get(block.getMaterial());
        }
    }
    return 5;
}
Also used : Block(net.minecraft.block.Block) Pair(com.builtbroken.jlib.type.Pair)

Example 5 with Pair

use of com.builtbroken.jlib.type.Pair in project Engine by VoltzEngine-Project.

the class FluidUtility method isFillableBlock.

/**
 * Checks to see if a non-fluid block is able to be filled with fluid
 */
public static boolean isFillableBlock(World world, Pos node) {
    if (world == null || node == null) {
        return false;
    }
    Block block = node.getBlock(world);
    int meta = node.getBlockMetadata(world);
    if (drainBlock(world, node, false) != null) {
        return false;
    } else if (block.isAir(world, node.xi(), node.yi(), node.zi())) {
        return true;
    } else if (!(block instanceof IFluidBlock || block instanceof BlockLiquid) && block.isReplaceable(world, node.xi(), node.yi(), node.zi()) || replacableBlockMeta.contains(new Pair(block, meta)) || replacableBlocks.contains(block)) {
        return true;
    }
    return false;
}
Also used : BlockLiquid(net.minecraft.block.BlockLiquid) Block(net.minecraft.block.Block) Pair(com.builtbroken.jlib.type.Pair)

Aggregations

Pair (com.builtbroken.jlib.type.Pair)9 Block (net.minecraft.block.Block)4 Pos (com.builtbroken.mc.lib.transform.vector.Pos)3 HashMap (java.util.HashMap)2 ItemStack (net.minecraft.item.ItemStack)2 IRecipe (net.minecraft.item.crafting.IRecipe)2 ShapedRecipes (net.minecraft.item.crafting.ShapedRecipes)2 ShapedOreRecipe (net.minecraftforge.oredict.ShapedOreRecipe)2 JsonCraftingRecipeData (com.builtbroken.mc.lib.json.processors.recipe.crafting.JsonCraftingRecipeData)1 BlockEdit (com.builtbroken.mc.lib.world.edit.BlockEdit)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 BlockLiquid (net.minecraft.block.BlockLiquid)1 ISidedInventory (net.minecraft.inventory.ISidedInventory)1 ShapelessRecipes (net.minecraft.item.crafting.ShapelessRecipes)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1