PrepareItemCraftEvent

Discussion in 'Plugin Development' started by LucaForever, Nov 11, 2020.

  1. Offline

    LucaForever

    I know that there are a lot of posts about this, but I still don't know how to resolve this:
    Code:
    package org.lucaforever.betterredstone.main;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.event.inventory.PrepareItemCraftEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin
    {
        @Override
        public void onEnable()
        {
            //energy block
            ItemStack energy_block = new ItemStack(Material.HOPPER);
            ItemMeta itemMeta_eb = energy_block.getItemMeta();
    
            itemMeta_eb.setDisplayName(ChatColor.RED + "Energy Block");
    
            energy_block.setItemMeta(itemMeta_eb);
    
            ShapedRecipe recipe_eb = new ShapedRecipe(energy_block);
    
            recipe_eb.shape("RRR", "RRR", "RRR");
            recipe_eb.setIngredient('R', Material.HOPPER);
            Bukkit.addRecipe(recipe_eb);
          
            //plane engine
            ItemStack plane_engine = new ItemStack(Material.DROPPER);
            ItemMeta itemMeta_pg = plane_engine.getItemMeta();
    
            itemMeta_pg.setDisplayName(ChatColor.RED + "Plane Engine");
    
            plane_engine.setItemMeta(itemMeta_pg);
    
            ShapedRecipe recipe_pg = new ShapedRecipe(plane_engine);
    
            recipe_pg.shape("RPR", "PRP", "RPR");
            PrepareItemCraftEvent.recipe_pg.('R', energy_block);
            recipe_pg.setIngredient('P', Material.PISTON_BASE);
            Bukkit.addRecipe(recipe_eb);
          
        } 
    }
    
    I am trying to make a recipe (plane engine) with the energy block, but I don't know how to do it. I saw that I need to use PrepareItemCraftEvent but, i can't understand how to do it. Thanks
     
  2. Offline

    xelatercero

    If i am not wrong , when you make recipe for example a ShapedRecipe , you can use recipe#setIngridient('your char here', your custom itemstack).
     
  3. Offline

    LucaForever

    ok, but i question, what do i put in the "your char here" thing? (like what char)

    Edit: i just noticed what the meant, and no, you cant use set a custom ingredient, you can only put "Material.-"
     
    Last edited: Nov 11, 2020
  4. Offline

    xelatercero

    Well the you can try to use the event you mentioned , and #getInventory() then #getMatrix(). Now loop through the matrix and check if the items used and the position on the grid matches what you want. Finally use #getInventory()#setResult(your itemstack here)

    Edit: I don't know if this is the best way , but here you have a working example:

    https://pastebin.com/Q03hWVNF
     
  5. Offline

    LucaForever

    well i understand the code, but i dont get if this
    Code:
    if(matrix[0] != null && matrix[0].getType().equals(Material.TARGET)) {
                e.getInventory().setResult(new ItemStack(Material.ACACIA_FENCE_GATE));
            }
    
    means that if the recipe is equal to a target block, it replaces it with a acacia fence gate?
    im sorry, but im confuesd (and mods pls dont ban for bumping)
     
  6. Offline

    xelatercero

    Well its quiet simple , i get the matrix or the crafting grid whatever you want to call it, then for example i check if the item in the 1rst position of the grid is a target block , could be whatever you want even a custom item, and then, if it is, just set the result of the crafting grid to whatever i want , in this case is just an example so i used acacia fence gates. Obviously you have to check if its null or it will throw a NPE, if you still confused we can hop in a discord session: xelatercero#7083
     

Share This Page