Search code examples
javapluginsminecraftbukkitbukkit-vault

Bukkit - ShapedRecipe is deprecated


I want to make custom item with custom recipe. I created class with 2 methods, item and customRecipe. My class looks like this

public class LifeCrystal implements Listener {

    private ItemStack item = new ItemStack(Material.DIAMOND);
    private ItemMeta meta = item.getItemMeta();

    private Plugin plugin = BelieveMe.getPlugin(BelieveMe.class);

    public void Item(Player player){

        meta.setDisplayName(ChatColor.GOLD + "Life Crystal");
        ArrayList<String> lores = new ArrayList<>();
        lores.add("Increase your life points");
        lores.add("...or revive someone");
        meta.setLore(lores);
        item.setItemMeta(meta);


    }
    public void customRecipe(){

        ShapedRecipe r = new ShapedRecipe(item);

        r.shape(" E ", "LAL", "DGD");
        r.setIngredient('E', Material.EMERALD);
        r.setIngredient('L', Material.LAPIS_LAZULI);
        r.setIngredient('A', Material.GOLDEN_APPLE);
        r.setIngredient('D', Material.DIAMOND);
        r.setIngredient('G', Material.GOLD_INGOT);

        plugin.getServer().addRecipe(r);

    }
}

"new ShapedRecipe(item)" is crossed and my error message is "ShapedRecipe is deprecated". I searched for that and find some information about NamespacedKey. I really don't know what to do now


Solution

  • It appears as if you just need to change your ShapedRecipe object constructor (according to the JavaDocs). Changing it to something like:

    NamespacedKey nsKey = new NamespacedKey(plugin, "unique_key_here");
    ShapedRecipe r = new ShapedRecipe(nsKey, item);
    

    should work on the latest version (1.14.4-R0.1-snapshot at time of writing). There is also a guide written on the official Spigot wiki, linked here.

    Quick note: From researching, seems the key HAS to be unique per 1.11 requirements, so make sure you have something that doesn't conflict with any vanilla recipes.