Help With Custom Shaped Recipes

Discussion in 'Plugin Development' started by StanRadner, Dec 10, 2014.

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

    StanRadner

    Hello. I am trying to code it so that I can craft 3 water bottles named "Swamp Water Bottle" by placing them around another custom item named "Swamp Water Bucket" but I cannot seem how to set a custom item as an ingredient. I am using CraftBukkit 1.7.9 R0.2.

    Code:
    package me.StanRadner.RadnerCraft;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    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
    {
        public void onEnable()
        {
            getLogger().info("RadnerCraft has been enabled!");
           
            ItemStack swampWaterBucketIS = new ItemStack(Material.WATER_BUCKET);
            ItemMeta swampWaterBucketMeta = swampWaterBucketIS.getItemMeta();
            swampWaterBucketMeta.setDisplayName(ChatColor.YELLOW + "Swamp Water Bucket");
           
            ItemStack swampWaterBottleIS = new ItemStack(Material.GLASS_BOTTLE);
            ItemMeta swampWaterBottleMeta = swampWaterBottleIS.getItemMeta();
            swampWaterBottleMeta.setDisplayName(ChatColor.YELLOW + "Swamp Water Bottle");
           
            ShapedRecipe swampWaterBottle = new ShapedRecipe(swampWaterBottleIS);
            swampWaterBottle.shape(new String[] {"#* ", "** ", "   "}).setIngredient('#', ItemStack.swampWaterBucketIS).setIngredient('*', Material.GLASS_BOTTLE);
            Bukkit.getServer().addRecipe(swampWaterBottle);
        }
       
        public void onDisable()
        {
            getLogger().info("RadnerCraft has been disabled!");
           
            Bukkit.getServer().clearRecipes();
        }
    }
    
     
  2. Offline

    Funergy

    @StanRadner "ItemStack.swampWaterBucketIS" this will not work
     
  3. Offline

    StanRadner

    I know that. I have been trying to find a way to be able to use custom items in a custom crafting recipe for a few days now.
     
  4. Offline

    Funergy

    @StanRadner I think its not possible to create custom crafting recipes with custom item stacks in Bukkit.addRecipe();
    You can try using NMS
     
  5. Offline

    StanRadner

    @Funergy Sadly I am relatively new to coding for Bukkit but I have been coding with java for about 3 years now. I was thinking that maybe using nested loops (starting with a for loop and going into nested if's) would work, but I am clueless as how to perform this for Bukkit.

    Maybe something to check if a water bucket with the name "Swamp Water Bucket" in yellow is in slot # whatever then produce the item. I do not know though.
     
  6. Offline

    Funergy

    Then you should know you don't create a package with capitals
    "me.StanRadner.RadnerCraft" --> "me.stanradner.radnercraft"
     
    Skionz likes this.
  7. Offline

    StanRadner

    @Funergy Oops! That would be my OCD kicking in. Good thing you caught that or it would possibly have interfered with names of classes or interfaces. Do you have any idea how I can solve my problem?
     
  8. Offline

    StanRadner

    I checked the forum for the rules and read them all and have waited the necessary 24 hour period for this... so I am bumping this thread.
     
  9. Offline

    97WaterPolo

    @StanRadner
    Been awhile since I messed with item meta, but if you get the item meta and modify it, don't you have to then set the item's meta to the new meta?

    ItemStack is = ...;
    ItemMeta im = is.getItemMeta();
    im.setDisplayName(...);
    is.setItemMeta(im);
     
    StanRadner likes this.
  10. Offline

    StanRadner

    @97WaterPolo Yeah, I would have to set the new ItemStack's meta, but would I then be able to use that new item in a custom crafting recipe?
     
  11. Offline

    FabeGabeMC

  12. Offline

    97WaterPolo

    @StanRadner
    You should be able to.


    @FabeGabeMC
    He wants to add new recipes, which is a lot easier than modifying each time they start to craft it.
     
    StanRadner likes this.
  13. Offline

    StanRadner

    @FabeGabeMC Thanks! I'll start coding with it in a few minutes.

    @FabeGabeMC I can't figure out how to do it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  14. Offline

    FabeGabeMC

    @StanRadner
    You could first check if the inventory is an instance of a CraftingInventory and check if the size is bigger than 4 (crafting grid on player inventory). Then loop through the CraftingInventory's matrix.
    More reference here.
     
  15. Offline

    StanRadner

  16. Offline

    hugo156

    Try this code
    public void onEnable()
    {
    ItemStack item = new ItemStack(material you want from crafting table);
    ItemMeta meta = item.getItemMeta();
    meta.setDisplayName(name of the new crafted item);
    item.setItemMeta(meta);

    ShapedRecipe recipe = new ShapedRecipe(recipe);
    recipe.shape(new String[] {
    "!!!",
    "[email protected]!",
    "!!!" }); // You can change this in the shape you want this is just an example
    recipe.setIngredient('!', Material.TNT);
    recipe.setIngredient('@', Material.REDSTONE_BLOCK);
    Bukkit.addRecipe(recipe);
    }
     
  17. Offline

    StanRadner

    I know how to create custom crafting recipes, it's using already created custom crafting recipes in another one that I'm having trouble for.

    For example, if someone puts a chest in the middle and surrounds it by buckets it would come out as "Bucket Chest" (it sounds stupid but it's just for the example). Then if they add that item, with that specific itemMeta in another custom crafting recipe, it will produce another item with custom itemMeta.
     
  18. Offline

    hugo156

    Im going to eclipse and right a new code for you

    I dont know if this is gonna work but its worth a shot isnt it
    Code:
    public class Main extends JavaPlugin {
        public void onEnable() {
            ItemStack item = new ItemStack(Material.CHEST);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.RED + "BukkitChest");
            item.setItemMeta(meta);
           
            ShapedRecipe recipe = new ShapedRecipe(item);
            recipe.shape(new String[] {
            "!!!",
            "[email protected]!",
            "!!!" });
            recipe.setIngredient('!', Material.BUCKET);
            recipe.setIngredient('@', Material.CHEST);
            Bukkit.addRecipe(recipe);
            ItemStack obsidian = new ItemStack(Material.OBSIDIAN);
            ShapedRecipe oRecipe = new ShapedRecipe(obsidian);
            oRecipe.shape(new String[] {
                "!!!",
                "[email protected]!",
                "!!!"
            });
            MaterialData nitem = new MaterialData(Material.CHEST);
            ItemMeta nmeta = nitem.toItemStack().getItemMeta();
            nmeta.setDisplayName(ChatColor.RED + "BukkitChest");
            nitem.toItemStack().setItemMeta(nmeta);
            oRecipe.setIngredient('@', nitem);
            oRecipe.setIngredient('@', Material.EYE_OF_ENDER);
            // Its just an example you can change it but i think you can see where im getting at
        }
    
    }
    EDIT by Timtower: merged posts and fixed formatting
     
    Last edited by a moderator: Dec 16, 2014
  19. Offline

    ColonelHedgehog

    That's not necessarily true. According to Oracle (creators of the Java language), the reason why it's best to write your package names in lowercase letters is to avoid any conflicts with class names. Obviously, this will depend on the classes you have and if they match your package name.

    https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html
     
  20. Offline

    hugo156

    I know but i just have my own style of coding i know ita not necessarily to do it but i just what i like and its more about the code im just trying to help him
     
  21. Offline

    leon3001

    Doing so is possible with MaterialData.

    @StanRadner Check the the link above and the variables the ShapedRecipe#setIngredients method can take.
     
  22. Offline

    Fuzzybear04

    for adding custom items ->

    recipename.setIngredient('symbol', customitemstack.getData());
     
  23. Offline

    hugo156

  24. Offline

    StanRadner

    @hugo156 @leon3001 @Fuzzybear04 I apologize for the lack of responses by me. I have been busy with work. I really do appreciate all of your help and I will get to coding it using everyone's valued help when I get off of my shift today.
     
Thread Status:
Not open for further replies.

Share This Page