1.15.2 Creating a custom crafting recipe.

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

  1. Hello all,
    I am creating a plugin which implements a custom crafting recipe. I have created it in a different class to the main class and I implement it into the main class onEnable(). However, it doesn't work. It can only work as static but the code doesn't allow it to be static because of the Namespacedkey. How should I fix this?
    Crafting recipe class:
    Code (Java):
    public abstract class Flare implements Plugin
    {
      public static void flareRecipe() {
        ItemStack item = new ItemStack(Material.REDSTONE_TORCH);
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName("§cNuke Flare");
        meta.setLore(Arrays.asList(new String[] { "§7Right-Click to call in", "§7a nuke strike at your", "§7location" }));
        item.setItemMeta(meta);
        NamespacedKey key = new NamespacedKey(this, "nuke_flare");
        ShapedRecipe recipe = new ShapedRecipe(key, item);
        recipe.shape(" F ", " R ", " S ");
        recipe.setIngredient('F', Material.FIREWORK_ROCKET);
        recipe.setIngredient('R', Material.REDSTONE);
        recipe.setIngredient('S', Material.STICK);
     
      }
    }
    Main class: (see line 34)
    Code (Text):
    /*    */ public class Main extends JavaPlugin {
    /* 13 */   public static String prefix = "§e§lBH §7§l» §r§e";
    /*    */
    /*    */
    /*    */   public static Main instance;
    /*    */
    /*    */
    /*    */   public void onEnable() {
    /* 20 */     getConfig().options().copyDefaults(true);
    /*    */  
    /* 22 */     instance = this;
    /*    */  
    /* 24 */     PluginManager pm = Bukkit.getServer().getPluginManager();
    /* 25 */     PluginDescriptionFile pdf = getDescription();
    /*    */  
    /* 27 */     pm.registerEvents((Listener)new FlareThrow(), (Plugin)this);
    /* 28 */     pm.registerEvents((Listener)new NukeHit(), (Plugin)this);
    /*    */  
    /* 30 */     getCommand("giveflare").setExecutor((CommandExecutor) new GiveFlare());
    /*    */  
    /* 32 */     Bukkit.getConsoleSender().sendMessage(String.valueOf(prefix) + "§aNuke Strike §cv" + pdf.getVersion() + " §aenabled!");
    /*    */  
    /* 34 */     Flare.flareRecipe();
    /*    */
    /* 36 */     saveConfig();
    /*    */   }
    /*    */
    /*    */   public static Main getInstance() {
    /* 40 */     return instance;
    /*    */   }
    /*    */ }
     
  2. md_5

    Administrator Developer

  3. Choco

    Moderator

    Looks to me like OP tried to create a NamespacedKey by implementing Plugin and supplying "this", IDE complained that methods were not implemented so it suggested abstracting the class. So, yes. A bit more understanding of how the language works would definitely help here. And please, don't blindly listen to your IDE. A lot of times it's very, very wrong.
     
    #3 Choco, Mar 30, 2020
    Last edited: Mar 30, 2020