Hello, so I would like to create a custom recipe that if the player points a block of gold with his cursor, here is my code, but I do not know what to do next ... Thank you in advance! Code (Java): @EventHandler(priority=EventPriority.LOWEST) public void Core(PrepareItemCraftEvent e){ @SuppressWarnings("deprecation") Block block = e.getView().getPlayer().getTargetBlock((HashSet<Byte>) null, 6).getLocation().getBlock(); if(block.getType() != null && block.getType() == Material.GOLD_BLOCK){
What you're asking doesn't really make sense, nor does your code. You mean you want the gold block to open a crafting GUI, and then have a special recipe only in that crafting GUI opened from the gold block?
Here I found a good guide on how to make recipes in Bukkit/Spigot. It's important that you understand what you're doing rather than copy-pasting.
Code (Java): // Here's an example of making a custom recipe ItemStack result = new ItemStack(Material.CHEST); // making item which the below recipe crafts ItemMeta resultMeta = result.getItemMeta(); resultMeta.setDisplayName(ChatColor.GREEN + "Emerald Chest"); result.setItemMeta(resultMeta); ShapedRecipe shapedRecipe = new ShapedRecipe(result); // creating recipe object shapedRecipe.shape(new String[] { EEE", "ECE", "EEE" }); // E is for emerald and C is for chest. this makes it so the top row is all emeralds, middle is emerald, chest, emerald, and bottom is all emeralds shapedRecipe.setIngredient('E', Material.EMERALD); // specifics what E is shapedRecipe.setIngredient('C', Material.CHEST); // specifies what C is plugin.getServer().addRecipe(shapedRecipe); // adds recipe to server
Code (Text): Inventory inv = Bukkit.createInventory(player, InventoryType.WORKBENCH, "Your Title"); player.openInventory(inv);
When you right click a gold block, add a player UUID to a list and open crafting GUI. On Inventory close, remove the uuid. Inside PrepareItemCraftEvent, check if the player's uuid is in the list. That will verify if the crafting GUI was opened from a gold block or not.
Instead of adding a new one everytime, add one directly onEnable, and manipulate PrepareItemCraftEvent to only show the crafted item when the player is in the list.
Ok, thx, but I don't understand how to create recipe with items who have custom ItemMeta (displayname, lore, etc)
Like in this code example from Fizmo: You have to change the ItemMeta resultMeta object and then set it to the ItemStack result afterwards. Here are all methods you can use: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/inventory/meta/ItemMeta.html If you want to get meta values make sure to check if the current meta has something before getting the value. For example: If you want to get the Lore use: if(resultMeta#hasLore()) --> resultMeta#getLore()
So I don't understand why, but the recipes from the GUI don't work... : / (All recipes are disabled) My code: Code (Java): @EventHandler public void SpecialTable(PlayerInteractEvent e){ if(e.getClickedBlock() != null && e.getAction() == Action.RIGHT_CLICK_BLOCK){ if(e.getClickedBlock().getType() == Material.STONE && e.getClickedBlock().getData() == 0){ e.getPlayer().openInventory(Bukkit.createInventory(e.getPlayer(), InventoryType.WORKBENCH, "§bSpecial table")); } } }