Solved Errors/Warnings from ShapedRecipe - deprecated method

Discussion in 'Spigot Plugin Development' started by DoggyPlayss, Feb 3, 2020.

  1. Hello! I am here with a problem which causes errors/warnings in the console and is related to custom Item I made. I hope it's solvable :)

    Warnings/Errors:
    [14:09:09 WARN]: java.lang.Throwable: Warning: A plugin is creating a recipe using a Deprecated method. This will cause you to receive warnings stating 'Tried to load unrecognized recipe: bukkit:<ID>'. Please ask the author to give their recipe a static key using NamespacedKey.
    [14:09:09 WARN]: at org.bukkit.inventory.ShapedRecipe.<init>(ShapedRecipe.java:26)
    [14:09:09 WARN]: at net.hexilion.Basics.Items.SuperSword.customRecipe(SuperSword.java:28)
    [14:09:09 WARN]: at net.hexilion.Core.onEnable(Core.java:30)
    [14:09:09 WARN]: at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264)
    [14:09:09 WARN]: at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:316)
    [14:09:09 WARN]: at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405)
    [14:09:09 WARN]: at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:395)
    [14:09:09 WARN]: at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:344)
    [14:09:09 WARN]: at net.minecraft.server.v1_12_R1.MinecraftServer.t(MinecraftServer.java:442)
    [14:09:09 WARN]: at net.minecraft.server.v1_12_R1.MinecraftServer.l(MinecraftServer.java:403)
    [14:09:09 WARN]: at net.minecraft.server.v1_12_R1.MinecraftServer.a(MinecraftServer.java:341)
    [14:09:09 WARN]: at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:289)
    [14:09:09 WARN]: at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:616)
    [14:09:09 WARN]: at java.lang.Thread.run(Unknown Source)

    Code from the custom item class:

    Code (Java):
    package net.hexilion.Basics.Items;

    import net.hexilion.Core;
    import org.bukkit.Material;
    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 java.util.ArrayList;

    import static org.bukkit.ChatColor.*;

    public class SuperSword implements Listener
    {
        private static Plugin plugin = Core.getPlugin(Core.class);
        public void customRecipe()
        {
            ItemStack item = new ItemStack(Material.DIAMOND_SWORD, 1);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(BLUE + "" + BOLD + "SUPER SWORD");
            ArrayList<String> lore = new ArrayList<>();
            lore.add(WHITE + "Used by Hexilion himself!");
            meta.setLore(lore);
            meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
            item.setItemMeta(meta);
            ShapedRecipe supersword = new ShapedRecipe(item);
            supersword.shape(" D ","SDS"," L ");
            supersword.setIngredient('D',Material.DIAMOND_BLOCK);
            supersword.setIngredient('L',Material.LOG);
            supersword.setIngredient('S',Material.SLIME_BALL);
            plugin.getServer().addRecipe(supersword);
        }
    }
     
  2. NOTE: the code does work in-game: recipe is working how it should just I get these errors which kinda bothers me...
     
  3. As the stacktrace suggests, you need to register your ShapedRecipe with a NamespacedKey. It's essentially an identifier for your crafting recipe. Here's an example as to how to use it.
    Code (Java):
    // create a NamespacedKey for your recipe
    NamespacedKey key = new NamespacedKey(this, "emerald_sword");

    // Create our custom recipe variable
    ShapedRecipe recipe = new ShapedRecipe(key, item);
    (code snippet retained from the wiki)
     
  4. Now I changed the code how I understood BUT it's now disabling my plugin meaning there are errors!

    Code:
    Code (Java):
    package net.hexilion.Basics.Items;

    import net.hexilion.Core;
    import org.bukkit.Material;
    import org.bukkit.NamespacedKey;
    import org.bukkit.enchantments.Enchantment;
    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 java.util.ArrayList;

    import static org.bukkit.ChatColor.*;

    public class SuperSword implements Listener
    {
        private static Plugin plugin = Core.getPlugin(Core.class);
        public void customRecipe()
        {
            ItemStack item = new ItemStack(Material.DIAMOND_SWORD, 1);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(BLUE + "" + BOLD + "SUPER SWORD");
            ArrayList<String> lore = new ArrayList<>();
            lore.add(WHITE + "Used by Hexilion himself!");
            item.addEnchantment(Enchantment.DAMAGE_ALL, 5);
            meta.setLore(lore);
            meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
            item.setItemMeta(meta);
            NamespacedKey key = new NamespacedKey((Plugin) this, "super_sword");//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            ShapedRecipe supersword = new ShapedRecipe(key, item);
            supersword.shape(" D ","SDS"," L ");
            supersword.setIngredient('D',Material.DIAMOND_BLOCK);
            supersword.setIngredient('L',Material.LOG);
            supersword.setIngredient('S',Material.SLIME_BALL);
            plugin.getServer().addRecipe(supersword);
        }
    }
    Errors:
    [15:39:16 ERROR]: Error occurred while enabling HexilionCore v0.1.1 (Is it up to date?)
    java.lang.ClassCastException: net.hexilion.Basics.Items.SuperSword cannot be cast to org.bukkit.plugin.Plugin
    at net.hexilion.Basics.Items.SuperSword.customRecipe(SuperSword.java:31) ~[?:?]
    at net.hexilion.Core.onEnable(Core.java:32) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[patched_1.12.2.jar:git-Paper-1618]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:316) ~[patched_1.12.2.jar:git-Paper-1618]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) ~[patched_1.12.2.jar:git-Paper-1618]
    at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:395) ~[patched_1.12.2.jar:git-Paper-1618]
    at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:344) ~[patched_1.12.2.jar:git-Paper-1618]
    at org.bukkit.craftbukkit.v1_12_R1.CraftServer.reload(CraftServer.java:808) ~[patched_1.12.2.jar:git-Paper-1618]
    at org.bukkit.Bukkit.reload(Bukkit.java:559) ~[patched_1.12.2.jar:git-Paper-1618]
    at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:55) ~[patched_1.12.2.jar:git-Paper-1618]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:152) ~[patched_1.12.2.jar:git-Paper-1618]
    at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:685) ~[patched_1.12.2.jar:git-Paper-1618]
    at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1492) ~[patched_1.12.2.jar:git-Paper-1618]
    at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1297) ~[patched_1.12.2.jar:git-Paper-1618]
    at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) ~[patched_1.12.2.jar:git-Paper-1618]
    at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:5) ~[patched_1.12.2.jar:git-Paper-1618]
    at net.minecraft.server.v1_12_R1.PlayerConnectionUtils.lambda$ensureMainThread$0(PlayerConnectionUtils.java:14) ~[patched_1.12.2.jar:git-Paper-1618]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:1.8.0_241]
    at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:1.8.0_241]
    at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) ~[patched_1.12.2.jar:git-Paper-1618]
    at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:850) ~[patched_1.12.2.jar:git-Paper-1618]
    at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:423) ~[patched_1.12.2.jar:git-Paper-1618]
    at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:774) ~[patched_1.12.2.jar:git-Paper-1618]
    at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:666) ~[patched_1.12.2.jar:git-Paper-1618]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
     
  5. The first parameter for NamespacedKey is a JavaPlugin instance. You can not cast your class instance as a JavaPlugin since it doesn't extend the class. Instead, it seems you are storing the instance you need already in your static field, plugin.
    Also your stacktrace clearly outlines what I just described,
    net.hexilion.Basics.Items.SuperSword cannot be cast to org.bukkit.plugin.Plugin
     
  6. Ugh, I still don't really get what I need to change in my code...
    Code:

    Code (Java):
    package net.hexilion.Basics.Items;

    import net.hexilion.Core;
    import org.bukkit.Material;
    import org.bukkit.NamespacedKey;
    import org.bukkit.enchantments.Enchantment;
    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.java.JavaPlugin;

    import java.util.ArrayList;

    import static org.bukkit.ChatColor.*;

    public class SuperSword implements Listener
    {
        private static JavaPlugin plugin = Core.getPlugin(Core.class);
        public void customRecipe()
        {
            ItemStack item = new ItemStack(Material.DIAMOND_SWORD, 1);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(BLUE + "" + BOLD + "SUPER SWORD");
            ArrayList<String> lore = new ArrayList<>();
            lore.add(WHITE + "Used by Hexilion himself!");
            item.addEnchantment(Enchantment.DAMAGE_ALL, 5);
            meta.setLore(lore);
            meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
            item.setItemMeta(meta);
            NamespacedKey key = new NamespacedKey((JavaPlugin)this, "super_sword");//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            ShapedRecipe supersword = new ShapedRecipe(key, item);
            supersword.shape(" D ","SDS"," L ");
            supersword.setIngredient('D',Material.DIAMOND_BLOCK);
            supersword.setIngredient('L',Material.LOG);
            supersword.setIngredient('S',Material.SLIME_BALL);
            plugin.getServer().addRecipe(supersword);
        }
    }
     
  7. You can't cast SuperSword to JavaPlugin, you don't even need to since you have field of the type JavaPlugin (named "plugin") which can be used as the parameter for the NamespacedKey constructor.
     
    • Agree Agree x 1
  8. Like this?:
    NamespacedKey key = new NamespacedKey((plugin) this, "super_sword");

    If so that doesn't work.
    Also, I started developing half a week ago so sorry if I seem dumb on this...
     
  9. Please read a little bit about casting and "this" keyword. It's good to know Java before getting into Spigot.
    What you should do is
    NamespacedKey key = new NamespacedKey(plugin, "name");
     
    • Agree Agree x 1
  10. Thanks, It worked + I did learn java before starting but that's a thing I haven't met before so yea
    Also in my code, you can see I tried to put an enchantment on the sword but it doesn't appear in-game... why?