Solved Help with adding Custom item to a custom recipe.

Discussion in 'Plugin Development' started by JackJupper, May 2, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    JackJupper

    I'm trying to make a certain plugin where you can go to a different planet and I'm having trouble trying to use a custom item in a crafting recipe

    Here is the code:
    Code:
    ItemStack engine = new ItemStack(Material.NOTE_BLOCK, 1, (short) 3);
            ItemMeta engineMeta = engine.getItemMeta();
            engineMeta.setDisplayName (ChatColor.AQUA + "Engine");
            engine.setItemMeta(engineMeta);
            ShapedRecipe Engine = new ShapedRecipe(engine);
           
            Engine.shape("IOG", "ONO", "DOE");
           
            Engine.setIngredient('N', Material.NETHER_STAR);
            Engine.setIngredient('O', Material.OBSIDIAN);
            Engine.setIngredient('D', Material.DIAMOND_BLOCK);
            Engine.setIngredient('E', Material.EMERALD_BLOCK);
            Engine.setIngredient('I', Material.IRON_BLOCK);
            Engine.setIngredient('G', Material.GOLD_BLOCK);
           
            getServer().addRecipe(Engine);
           
            ItemStack procket = new ItemStack(Material.IRON_BLOCK);
            ItemMeta procketMeta = procket.getItemMeta();
            procketMeta.setDisplayName (ChatColor.AQUA + "Procket");
            procket.setItemMeta(engineMeta);
            ShapedRecipe Procket = new ShapedRecipe(procket);
           
            Procket.shape("SIS", "RER", "BPB");
           
            Procket.setIngredient('S', Material.STONE_SLAB2);
            Procket.setIngredient('R', Material.BLAZE_ROD);
            Procket.setIngredient('P', Material.BLAZE_POWDER);
            Procket.setIngredient('E', Material.END_CRYSTAL);
            Procket.setIngredient('I', Material.IRON_BLOCK);
            Procket.setIngredient('B', Material.NOTE_BLOCK, (short) 3);
           
            getServer().addRecipe(Procket);
            
    Anyone know how to do this?
     
  2. Offline

    MrGeneralQ

    Do you register the recipe in onEnable?
     
  3. Offline

    Goldenfire

    In procket.setitemMeta(enginemeta) change the engine meta to procketMeta
     
  4. Offline

    JackJupper

    Yes it is in onEnable, i just didn't think i needed to show that part
    Code:
    @SuppressWarnings("deprecation")
        @Override
        public void onEnable() {
           
            ItemStack engine = new ItemStack(Material.NOTE_BLOCK, 1, (short) 3);
            ItemMeta engineMeta = engine.getItemMeta();
            engineMeta.setDisplayName (ChatColor.AQUA + "Engine");
            engine.setItemMeta(engineMeta);
            ShapedRecipe Engine = new ShapedRecipe(engine);
           
            Engine.shape("IOG", "ONO", "DOE");
           
            Engine.setIngredient('N', Material.NETHER_STAR);
            Engine.setIngredient('O', Material.OBSIDIAN);
            Engine.setIngredient('D', Material.DIAMOND_BLOCK);
            Engine.setIngredient('E', Material.EMERALD_BLOCK);
            Engine.setIngredient('I', Material.IRON_BLOCK);
            Engine.setIngredient('G', Material.GOLD_BLOCK);
           
            getServer().addRecipe(Engine);
           
            ItemStack procket = new ItemStack(Material.IRON_BLOCK);
            ItemMeta procketMeta = procket.getItemMeta();
            procketMeta.setDisplayName (ChatColor.AQUA + "Procket");
            procket.setItemMeta(procketMeta);
            ShapedRecipe Procket = new ShapedRecipe(procket);
           
            Procket.shape("SIS", "RER", "BPB");
           
            Procket.setIngredient('S', Material.STONE_SLAB2);
            Procket.setIngredient('R', Material.BLAZE_ROD);
            Procket.setIngredient('P', Material.BLAZE_POWDER);
            Procket.setIngredient('E', Material.END_CRYSTAL);
            Procket.setIngredient('I', Material.IRON_BLOCK);
            Procket.setIngredient('B', Material.NOTE_BLOCK, (short) 3);
           
            getServer().addRecipe(Procket);
           
       
        }
    That didn't make it work
     
  5. Offline

    Goldenfire

    Can u tell me where the problem occurs in Minecraft
     
    Last edited: May 3, 2017
  6. Offline

    JackJupper

    The procket doesn't craft....

    im trying to make it where you need to use the engine to craft the procket
     
  7. Offline

    Zombie_Striker

    Are you sure you are using the correct type of stone slab? Are you sure you have a noteblock with a durability of 3?
     
  8. Offline

    JackJupper

    well Stone_Slab2 is the slab made out of stone, then yes


    and tell me if I am using a note_block with a durability of 3 by looking at my Engine....
    because if you read what I said, I am trying to make it so i can use the Engine in the Procket recipe
     
  9. Offline

    Goldenfire

    So I'm guessing that the "B" is the engine

    Try setting the procket.setIngredient(B,
    engine)
     
  10. Offline

    JackJupper

    that doesn't work.......
    I've tried that a while ago
     
  11. Offline

    Goldenfire

    Try removing the (short) 3 in the engine itemstack and also in the setIngridient() idk if this will work or not
     
  12. Offline

    Caderape2

  13. Offline

    timtower Administrator Administrator Moderator

    How would that help for a recipe that is designed to be shaped?
     
  14. Offline

    JackJupper

    That will also make it to where you can use normal noteblocks though....
     
  15. Offline

    Goldenfire

    Wont the display name make a difference ?
     
  16. Offline

    JackJupper

    you are telling me to remove the (short)
    which will make it

    Procket.setIngredient('B', Material.NOTE_BLOCK);

    which is just making it read "craft it with a note block"
     
  17. Offline

    Goldenfire

    What about setting the setIngridient(B, engine) and also remove the short 3
     
  18. Offline

    JackJupper

    i already said that doesn't work
     
    Christian210x likes this.
  19. Offline

    JackJupper

  20. STONE_SLAB2 is numeric id 182 which is the Red Sandstone Slab.
    See here for a full slab-overview: http://minecraft.gamepedia.com/Slab#ID

    With red sandstone slabs the recipe works.
     
  21. Offline

    JackJupper

    the problem is that it won't let me do STONE_SLAB

    s
    so i changed it from a stone slab to stone and now i have the right crafting recipe for it

    now the problem is that you are still able to use normal noteblocks to craft it and not just the engine

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 7, 2017
  22. 1. stone slabs are called 'STEP' (block id 44). If you want to allow any stone slab variant (Stone, Sandstone, (the old, non-burnable) Wooden, Cobblestone, Bricks, Stone Brick, Nether Brick and Quartz Slab):
    Code:
    Engine.setIngredient('S', Material.STEP);
    If you only want for example sandstone steps:
    Code:
    Engine.setIngredient('S', Material.STEP, 1);
    The list of stone slabs is here: http://minecraft.gamepedia.com/Slab#Stone_slabs

    2. there is no noteblock with data value 3, so your first recipe returns a 'normal' noteblock with a custom name.
    the setIngredient only accepts a material and a data/damage value. You could for example only allow orange wool to be used; but to detect custom names, you'd probably have to listen for the PrepareItemCraftEvent.

    You can then use event.getInventory().getMatrix() to iterate through all the blocks in the recipe; you can check if the containing noteblock has the correct custom name. If it hasn't, set the result to 'AIR'.
     
    Zombie_Striker likes this.
  23. Offline

    PhantomUnicorns

    @JackJupper You understand that you initialized it as lowercase and you used it in uppercase right (For both recipes)?
    Code:
    ItemStack engine = new ItemStack(Material.NOTE_BLOCK, 1, (short) 3); // <--- Spelled engine
    Engine.setIngredient('G', Material.GOLD_BLOCK); // <--- Spelled Engine
    getServer().addRecipe(Engine);
    
    Your IDE (If you have an error finding one) should have caught that, sorry for your inconvenience
     
  24. Offline

    JackJupper

    that's on purpose....
    and it works

    but that isn't my problem

    Example of how to make this? I can't really find any.
     
    Last edited: May 8, 2017
  25. Offline

    PhantomUnicorns

    @JackJupper You would hook in to the PrepareItemCraftEvent (Obviously setting it as an eventhandler) then create an iterator which contains all the blocks for the recipe, then run through the iterator and check if the material equals the noteblock and if the name is equal to the appropriate name (And don't forget the null checks for itemmeta!) and if none of them (is a noteblock with the name) then do nothing

    P.S: It's a wonder how that actually works because it should give you a variable not found exception/error (Did you edit an option or something for it? I'm not really bugged about it but it may be useful)
     
  26. Offline

    crysis992

    I've made something like that a while back, not sure if it still works.
    It allows you to register custom crafting recipes that accepts itemsstacks as ingredients
    (Might need some tweaks/improvements but it "should" work)

    The MyRecipe class, that handles the most part.
    Show Spoiler
    Code:
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.event.inventory.PrepareItemCraftEvent;
    import org.bukkit.inventory.CraftingInventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.material.MaterialData;
    
    public class MyRecipe extends ShapedRecipe {
      
        public static Map<ItemStack, List<MyRecipe>> recipes = new HashMap<ItemStack, List<MyRecipe>>();
    
        private Map<Character, ItemStack> exactIntegrients = new HashMap<Character, ItemStack>();
      
        public MyRecipe(ItemStack result) {
            super(result);
        }
      
        @Override
        public MyRecipe setIngredient(char key, Material ingredient) {
            super.setIngredient(key, ingredient);
            this.exactIntegrients.put(key, new ItemStack(ingredient));
            return this;
        }
    
        @Override
        public MyRecipe setIngredient(char key, MaterialData ingredient) {
            super.setIngredient(key, ingredient);
            this.exactIntegrients.put(key, new ItemStack(ingredient.getItemType(), 1, ingredient.getData()));
            return this;
        }
    
        @Override
        public MyRecipe setIngredient(char key, Material ingredient, int raw) {
            super.setIngredient(key, ingredient, raw);
            this.exactIntegrients.put(key, new ItemStack(ingredient, 1, (short) raw));
            return this;
        }
    
        public MyRecipe setIngredient(char key, ItemStack item) {
            super.setIngredient(key, item.getType(), item.getDurability());
            this.exactIntegrients.put(key, item);
            return this;
        }
      
        public boolean equals(ItemStack[] matrix) {
            String[] shape = super.getShape();
            for (int y = 0; y < shape.length; y++) {
                String line = shape[y];
                for (int x = 0; x < line.length(); x++) {
                    char c = line.charAt(x);
                    int i = y * line.length() + x;      
                    ItemStack item0 = matrix[i];
                    ItemStack item1 = this.exactIntegrients.get(c);
                    if (item0 != null && item1 != null) {
                        if (item0.getTypeId() != item1.getTypeId() || item0.getDurability() != item1.getDurability() || !item0.getItemMeta().equals(item1.getItemMeta())) {
                            return false;
                        }
                    }
                }
            }
            return true;
        }
      
        public void register() {
            Bukkit.addRecipe(this);
            ItemStack result = super.getResult();
            List<MyRecipe> list = MyRecipe.recipes.get(result);
            if (list == null) {
                list = new ArrayList<MyRecipe>();
            }
            if (!list.contains(this)) {
                list.add(this);
                MyRecipe.recipes.put(result, list);
            }
        }
      
        public static List<MyRecipe> getRecipes(ItemStack item) {
            return MyRecipe.recipes.get(item);
        }
      
        public static MyRecipe process(PrepareItemCraftEvent event) {
            CraftingInventory inv = event.getInventory();
            ItemStack result = inv.getResult();
            List<MyRecipe> recipes = MyRecipe.getRecipes(result);
            if (recipes != null) {
                inv.setResult(null);
                for (MyRecipe recipe : recipes) {
                    if (recipe.equals(inv.getMatrix())) {
                        inv.setResult(recipe.getResult());
                        return recipe;
                    }
                }
            }
            return null;
        }
    }
    


    The main class with an example:

    Show Spoiler
    Code:
    import java.util.Arrays;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.PrepareItemCraftEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Test extends JavaPlugin implements Listener {
      
        @Override
        public void onEnable() {
            PluginManager pm = this.getServer().getPluginManager();
          
            pm.registerEvents(this, this);
          
            ItemStack item0 = new ItemStack(Material.EXP_BOTTLE, 1);
           ItemMeta meta0 = item0.getItemMeta();
           meta0.setDisplayName("§3Magical essence");
           meta0.setLore(Arrays.asList("bla bla", "a discription"));
           item0.setItemMeta(meta0);
           MyRecipe recipe0 = new MyRecipe(item0);
           recipe0.shape("GGG","GPG","GGG");
           recipe0.setIngredient('G', Material.GLOWSTONE_DUST);
           recipe0.setIngredient('P', Material.EXP_BOTTLE);
           recipe0.register();
          
           ItemStack item1 = new ItemStack(Material.STICK, 1);
           ItemMeta meta1 = item1.getItemMeta();
           meta1.setDisplayName("§3Staff of light");
           item1.setItemMeta(meta1);
           MyRecipe recipe1 = new MyRecipe(item1);
           recipe1.shape("GZG","ZSZ","GZG");
           recipe1.setIngredient('G', Material.GLOWSTONE_DUST);
           recipe1.setIngredient('Z', item0);
           recipe1.setIngredient('S', Material.STICK);
           recipe1.register();
        }
      
        @EventHandler
        public void onCraft(PrepareItemCraftEvent event) {
            MyRecipe.process(event);
        }
    
     
  27. Maybe it's because your plugin doesn't start, make sure your plugin is enabled ( with /pl or /plugins ).
    If you don't see your plugin, check the console and give us the error message.
     
  28. Offline

    PhantomUnicorns

    @aGuy_ According to the oc, one recipe works and one doesn't work the exact way he wants it.... We have established that it works like he wants except he has a certain displayname he wants to check....
     
  29. Offline

    Horsey

    This seems to be the correct solution.
    Why does it not work? Does it return an error in-game, or does it not craft at all?

    Edit: just found out Bukkit doesn't accept itemmeta in crafting recipes. Refer this thread: https://bukkit.org/threads/making-c...ng-matrix-contains-custom-named-items.452420/. For future reference, please Google if a thread concerning your problem already exists, and only then make a new thread.
     
    Last edited: May 12, 2017
Thread Status:
Not open for further replies.

Share This Page