Solved I tried to make a custom item and recipe but it doesn't work when I load it up.

Discussion in 'Spigot Plugin Development' started by Rgos, Mar 30, 2020.

  1. I'm relatively new to Java so I've been following a Bukkit Coding Series by {The Source Code} and haven't had much trouble loading it up, but when I try to use the recipe it doesn't work. This is the code:
    Code (Text):
    package xyz.squadcraft.FactionRPG.Items;

    import java.util.ArrayList;

    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.md_5.bungee.api.ChatColor;
    import xyz.squadcraft.FactionRPG.Core;

    public class CustomItems extends JavaPlugin implements Listener{

        public ItemStack item = new ItemStack(Material.OBSIDIAN);
        private ItemMeta meta = item.getItemMeta();
       
        private Plugin plugin = Core.getPlugin(Core.class);
        public void giveItems(Player player) {
            meta.setDisplayName(ChatColor.DARK_PURPLE + "Enchanted Obsidian");
            ArrayList<String> lore = new ArrayList<String>();
            lore.add(ChatColor.WHITE + "Yes");
            meta.setLore(lore);
            meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
            item.setItemMeta(meta);
           
            player.getInventory().addItem(item);
        }
       
        public void customRecipe() {
            ShapedRecipe enchobsidian = new ShapedRecipe(item);
           
            enchobsidian.shape("###", "###", "###");
            enchobsidian.setIngredient('#', Material.OBSIDIAN);
           
            plugin.getServer().addRecipe(enchobsidian);
        }
    }
     
     
  2. When are these being called? I suggest you make a method to create your item, and call that method in both of the above methods. You should also call customRecipe() your onEnable(). Assuming this is your main class, it would be ike this:
    Code (Java):

            @Override
            public void onEnable()
            {
                customRecipe();
            }
            public ItemStack createItem(){
                ItemStack item = new ItemStack(Material.OBSIDIAN);
                ItemMeta meta = item.getItemMeta();
                //Setup the ItemMeta
                return item;
            }
         
            public void giveItems(Player player) {
                ItemStack item = createItem();
                player.getInventory().addItem(item);
            }
       
            public void customRecipe() {
                ItemStack item = createItem();
                ShapedRecipe enchobsidian = new ShapedRecipe(item);
           
                enchobsidian.shape("###", "###", "###");
                enchobsidian.setIngredient('#', Material.OBSIDIAN);
           
                getServer().addRecipe(enchobsidian);
            }
        }
     
    • Agree Agree x 2
  3. 1. Hello1231 is correct, you have to call it on onEnable for the recipe to be added to the server
    2. You're using the wrong chat color import, remove the bungee one and use: import org.bukkit.ChatColor;
    3. The constructor ShapedRecipe(ItemStack) is deprecated, make one with a NamespacedKey, like this:
    ShapedRecipe(NamespacedKey key, ItemStack result)
    Click here for the Javadoc
     
  4. I attempted to research about NamespacedKey and how to use it but I just can't seem to find how it works this is my code:
    Code (Java):
     
        public void customRecipe() {
            NamespacedKey key = new NamespacedKey(plugin, key);
            ItemStack item = createItem();
            ShapedRecipe enchobsidian = new ShapedRecipe(key, item);
     
            enchobsidian.shape("###", "###", "###");
            enchobsidian.setIngredient('#', Material.OBSIDIAN);
     
            getServer().addRecipe(enchobsidian);
        }
     
    My error is that the new NamespacedKey is not defined.
     
  5. Pay attention to the NamespacedKey args:
    NamespacedKey key = new NamespacedKey(<plugin instance>, <string>);

    If you are creating the recipe in your main class the should be something like this:
    Code (Text):
    public void customRecipe() {
            NamespacedKey key = new NamespacedKey(this, "craft1");
            ItemStack item = createItem();
            ShapedRecipe enchobsidian = new ShapedRecipe(key, item);
            enchobsidian.shape("###", "###", "###");
            enchobsidian.setIngredient('#', Material.OBSIDIAN);
            getServer().addRecipe(enchobsidian);
        }
     
  6. Why have you called it craft1? Is there a reason
     
  7. No, you can name it whatever you want
     
  8. I changed up my code, no errors are showing but I can't seem to craft anything.
    Code:
    Code (Text):
    package xyz.squadcraft.FactionRPG;

    import java.util.ArrayList;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.NamespacedKey;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;

    public final class Core extends JavaPlugin implements Listener {
       
        public void OnEnable(Plugin plugin) {
            getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "\n\nFactionRPG enabled\n\n");
            Bukkit.getServer().getPluginManager();
           
            customRecipe(plugin);
        }
       
        public ItemStack createItem(){
            ItemStack item = new ItemStack(Material.OBSIDIAN);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName("Enchanted Obsidian");
            meta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 1, true);
            ArrayList<String> lore = new ArrayList<String>();
            lore.add("Yes");
            meta.setLore(lore);
            meta.setUnbreakable(true);
            item.setItemMeta(meta);
            return item;
        }
        public void giveItems(Player player) {
            ItemStack item = createItem();
            player.getInventory().addItem(item);
        }
       
        public void customRecipe(Plugin plugin) {
            NamespacedKey key = new NamespacedKey(plugin, "enchobsidian");
            ItemStack item = createItem();
            ShapedRecipe enchobsidian = new ShapedRecipe(key, item);
     
            enchobsidian.shape("###", "###", "###");
            enchobsidian.setIngredient('#', Material.OBSIDIAN);
     
            getServer().addRecipe(enchobsidian);
        }
    }
     
     
  9. I don't have anything to add to this but I like the idea of the FactionRPG (if that's what you meant)!
     
  10. It is, thanks
     
  11. Is that your main class?
    If so, the onEnable method you wrote is not right, should be like this:
    Code (Text):

    @Override
    public void onEnable() {
    // instructions
    customRecipe(); // <-- dont need that plugin parameter (explained below)
    }
     
    Then the customRecipe method does not need a Plugin type parameter, you just need to write something like this:
    Code (Text):

    public void customRecipe() {
            NamespacedKey key = new NamespacedKey(this, "enchobsidian");
            ItemStack item = createItem();
            ShapedRecipe enchobsidian = new ShapedRecipe(key, item);
     
            enchobsidian.shape("###", "###", "###");
            enchobsidian.setIngredient('#', Material.OBSIDIAN);
     
            getServer().addRecipe(enchobsidian);
        }

     
     
  12. It worked thanks! :)
     
    • Like Like x 1
    • Friendly Friendly x 1
  13. It isn’t the wrong import, ChatColor API from bungee is more complete than the bukkit one and many people prefer using that one over bukkit’s.