[PLUG-434] I want to simply have my custom recipes available in the recipe book Created: 21/Feb/21 Updated: 27/Feb/21 Resolved: 24/Feb/21 |
|
Status: | Resolved |
Project: | SpigotPlugins |
Component/s: | None |
Affects Version/s: | None |
Fix Version/s: | None |
Type: | Improvement | Priority: | Minor |
Reporter: | EndPlex | Assignee: | Unassigned |
Resolution: | Invalid | Votes: | 0 |
Labels: | RecipeBook, plugin, recipe | ||
Environment: |
I'm using IntelliJ IDEA CE for a 1.16.5 plugin. The software shouldn't matter for this issue. |
Version: | 1.16.5 |
Plugin: | My recipe plugin |
Guidelines Read: | Yes |
Description |
(This problem is resolved and you can see my solution in the comments)
This is my first time creating a plugin so please bear with me. I made a plugin that adds custom recipes. How do you get custom recipes to be unlocked by default in the 1.16.5 recipe book for all players? They only unlock when you craft them for the first time. Here is an example of one of my added recipes: // Protection 1 ItemStack item = new ItemStack(Material.ENCHANTED_BOOK); EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta(); if (meta != null) { meta.addStoredEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 1, false); item.setItemMeta(meta); NamespacedKey key = new NamespacedKey(this, "protection1"); ShapedRecipe recipe = new ShapedRecipe(key, item) .shape("CBC", "BAB", "CBC") .setIngredient('A', Material.BOOK) .setIngredient('B', Material.IRON_INGOT) .setIngredient('C', Material.IRON_NUGGET); Bukkit.addRecipe(recipe); }
Since this problem is resolved I finally created my first plugin |
Comments |
Comment by EndPlex [ 27/Feb/21 ] |
For those of you who want to see my solution:
I declared and initialized a static collection of NamespacedKey for all recipes. This is in the same class you add the recipes. You need to import Collection and ArrayList if you haven't done so.
static Collection<NamespacedKey> recipeKeysArr = new ArrayList<>();
Then after each recipe you add, you want to append the recipe's NamespacedKey. Here is an example:
NamespacedKey key = new NamespacedKey(this, "protection1"); recipeKeysArr.add(key);
Now you need a way to access this collection/array, so create a public static method.
public static Collection<NamespacedKey> getRecipeKeysArr() { return recipeKeysArr; }
Unfortunately, I haven't found a way to enable the recipes to be viewable by default. However, there is a workaround, which is to create an in-game command that any player can use. This command will be able to let players "discover" and "undiscover" your recipes. My command will be: /view {on/off}
If you don't know how to create a command, search a tutorial on youtube, this is what I have imported into my command class.
import org.bukkit.ChatColor; // For aesthetic purposes import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player;
If you do know how to create a command, you can utilize your previous method with player.discoverRecipes() and similarly player.undiscoverRecipes() to add/remove recipes a player has in their recipe book. It's that simple! For reference here are 2 cases for what comes after "/view" in my command: // "view on" command if (args[0].equalsIgnoreCase("on")) { player.discoverRecipes(Main.getRecipeKeysArr()); player.sendMessage(ChatColor.YELLOW + "Enchant book recipes are now " + ChatColor.RED + "viewable " + ChatColor.YELLOW + "via the recipe book!"); return true; } // "view off" command if (args[0].equalsIgnoreCase("off")) { player.undiscoverRecipes(Main.getRecipeKeysArr()); player.sendMessage(ChatColor.YELLOW + "Enchant book recipes are now " + ChatColor.RED + "not viewable " + ChatColor.YELLOW + "via the recipe book!"); return true; } (Remember to import Player and create a Player object, I named mine "player")
If you have questions please let me know. |