Code:java @EventHandlerpublic void Craft(CraftItemEvent event) {Player player = (Player) event.getWhoClicked();if (event.getRecipe().getResult().equals(SFBlaze)) {player.sendMessage("HI");}}} I registered the event like this getServer().getPluginManager().registerEvents(this, this); The event is in my main class. There are no errors in the console and SFBlaze is a shaped recipe. I am not even sure what I did wrong.
HelGod 99.9999% of these errors come from people forgetting to properly register your listener in your main class. Code plox Also, try moving the debug message out of ALL if statements and see if it triggers for the crafting even at all EDIT: Wait, so SFBlaze is a recipe? Are you trying to register a recipe?
CreeperShift Code:java public class SFC extends JavaPlugin implements Listener { ItemStack blazerod = new ItemStack(Material.BLAZE_ROD); int blazerodLevel = 7; Enchantment sharpness = Enchantment.DAMAGE_ALL; ShapedRecipe SFBlaze = new ShapedRecipe(new ItemStack(blazerod)); public void onEnable() { blazerod.addUnsafeEnchantment(sharpness, blazerodLevel); ShapedRecipe SFBlaze = new ShapedRecipe(new ItemStack(blazerod)).shape( " ", " b ", " ").setIngredient('b', Material.BLAZE_ROD); getServer().addRecipe(SFBlaze); getServer().getPluginManager().registerEvents(this, this); } public void onDisable() { } @EventHandler public void Craft(CraftItemEvent event) { Player player = (Player) event.getWhoClicked(); String name = player.getName(); if (player.getLevel() >= 30) { if (event.getRecipe().getResult().equals(SFBlaze)) { player.sendMessage("Hi"); } else if (player.getLevel() < 30) { player.sendMessage(ChatColor.DARK_RED + "You need 30 levels to craft this item"); event.setCancelled(true); } } }} What do you mean by registering the recipe and here is my entire code
HelGod Nope you did register the recipe, from your listener class it looked like you were trying to register a recipe in the CraftItemEvent (Don't ask, nothing I haven't seen in this section before PP) Now to your problem, does the event fire? Did you test it with the debug message? If yes, something that looks weird is this like: Code: if (event.getRecipe().getResult().equals(SFBlaze)) I'm sorry if I'm wrong (I've not done much with CraftItemEvent) but should getResult() not by the name of it give you something like an itemstack? instead of a recipe? You should probably add a debug message like player.getServer().broadcastMessage("Result is " + event.getRecipe().getResult() ); and see what it says ingame. By the name of it I would assume it gives you the item in the output field, not the recipe. Again, I might be wrong, but easiest way to test it would be to try it ingame.
CreeperShift I tried a debug message, but it didn't say anything in game and there were no errors in the console at all
Code: @EventHandler public void Craft(CraftItemEvent event) { player.getServer().broadcastMessage("Result is " + event.getRecipe().getResult() ); } this exact code does nothing? Then either your event isn't even firing, or you didn't register it properly. I never put the listener in the same class so I'm not sure if you did actually register it properly.
Well that at least means that your stuff is setup correctly. And what did it tell you for the "recipe" part of the debug message? When you tried it with your custom recipe. You should probably try something like: Code: if (event.getRecipe().equals("whateverRecipe output was) ) { //do your stuff } Again, untested.
CreeperShift I didn't notice the question. It broacasted the item stack the display name and the enchantment Also, I am pretty sure the problem is with the event.getRecipe().equals(SFBlaze)
Stop wasting your time with equals() on recipes, that won't work since it's not properly implemented. You'll have to manually check if it's your recipe... there are many ways, the most reliable one is to check ingredients and result. Checking ingredients is very tricky because when you get the recipe in the event it's re-converted by bukkit, see: https://github.com/Bukkit/CraftBukk...a/net/minecraft/server/ShapedRecipes.java#L26 It basically builds a shape from "a" to "abc/def/ghi" depending on width/height of the shape and associates ingredients accordingly... However, in your case, you just have one ingredient, if you're not really going to add other recipes, checking this one would be easy, just see if there's one entry in the ingredient map, and if it is then get the first one (map.get('a')) and see if it's a blaze rod, then check for your result and then you can almost be sure it's your recipe. You need to do all those checks because your plugin is not the only plugin adding recipes and you might do stuff with other plugin's recipes. And also, I suggest you use PrepareCraftItemEvent instead and use event.getInventory().setResult(null) to cancel it, that event triggers when the recipe is matched so people know they can't use it when they place the ingredient and they won't see the result.
Digi Ok, but what would I do if there is more than 1 ingredient? I just put 1 ingredient to test if the crafting works
Well, you could build your recipe exacly after that style and then compare shape's length and shape index 0's length() - a fast check to skip recipes of other sizes. After that you can just loop through the characters and compare ingredients from both recipes' ingredient maps. EDIT: Acutally, I noticed your result is pretty unique with that enchantment, you can just compare that, it won't be 100% reliable when it comes to other plugins but it would do for now. Also, mind that you have 2 SFBlaze variables in your class, one is a field that's basically a blank recipe with only result and the other one is local for onEnable() method.
ItemStack's equals() is properly implemented, it will check type, data, amount and metadata (enchantments, name, lore, and other special stuff like leather color, book pages, etc). The isSimilar() method does the same thing except checking for amount.
I went to bed but what digi says will work. I was hoping it would actually return an item stack (but I wasn't sure!, that's why I asked you ) and personally I would just attach some lore or a custom displayname as that tends to look the best and isn't too hard to check for.
CreeperShift I did attach a custom display name and a lore in my code. I just sisnt add it when I posted this
Then try to get the itemstack of the result, and check for either one of them. Then you can still disallow it if the level is too low.