Solved Crafting Event

Discussion in 'Plugin Development' started by HelGod, Apr 4, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    HelGod

    Code:java
    1.  
    2. @EventHandler
    3. public void Craft(CraftItemEvent event) {
    4. Player player = (Player) event.getWhoClicked();
    5. if (event.getRecipe().getResult().equals(SFBlaze)) {
    6. player.sendMessage("HI");
    7. }
    8. }
    9. }
    10.  


    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.
     
  2. Offline

    CreeperShift

    HelGod

    99.9999% of these errors come from people forgetting to properly register your listener in your main class. Code plox :p

    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?
     
  3. Offline

    HelGod

    CreeperShift
    Code:java
    1.  
    2. public class SFC extends JavaPlugin implements Listener {
    3.  
    4. ItemStack blazerod = new ItemStack(Material.BLAZE_ROD);
    5. int blazerodLevel = 7;
    6. Enchantment sharpness = Enchantment.DAMAGE_ALL;
    7. ShapedRecipe SFBlaze = new ShapedRecipe(new ItemStack(blazerod));
    8.  
    9. public void onEnable() {
    10. blazerod.addUnsafeEnchantment(sharpness, blazerodLevel);
    11. ShapedRecipe SFBlaze = new ShapedRecipe(new ItemStack(blazerod)).shape( " ", " b ", " ").setIngredient('b', Material.BLAZE_ROD);
    12. getServer().addRecipe(SFBlaze);
    13. getServer().getPluginManager().registerEvents(this, this);
    14. }
    15.  
    16. public void onDisable() {
    17.  
    18. }
    19.  
    20. @EventHandler
    21. public void Craft(CraftItemEvent event) {
    22. Player player = (Player) event.getWhoClicked();
    23. String name = player.getName();
    24. if (player.getLevel() >= 30) {
    25. if (event.getRecipe().getResult().equals(SFBlaze)) {
    26. player.sendMessage("Hi");
    27. } else if (player.getLevel() < 30) {
    28. player.sendMessage(ChatColor.DARK_RED
    29. + "You need 30 levels to craft this item");
    30. event.setCancelled(true);
    31. }
    32. }
    33. }
    34. }
    35.  

    What do you mean by registering the recipe and here is my entire code
     
  4. Offline

    CreeperShift

    HelGod
    Nope you did register the recipe, from your listener class it looked like you were trying to register a recipe in the CraftItemEvent :p (Don't ask, nothing I haven't seen in this section before :pPP)

    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.
     
  5. Offline

    HelGod

    CreeperShift
    I tried a debug message, but it didn't say anything in game and there were no errors in the console at all
     
  6. Offline

    CreeperShift

    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.
     
  7. Offline

    HelGod

  8. Offline

    CreeperShift

    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.
     
  9. Offline

    HelGod

  10. Offline

    CreeperShift

    If you aren't willing to answer my questions then I wont bother to help you anymore.
     
  11. Offline

    HelGod

    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)
     
  12. 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.
     
    Cupcakes69 likes this.
  13. Offline

    HelGod

    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
     
  14. 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.
     
  15. Offline

    HelGod

    Digi
    How would I compare the enchantments. The only time I compared something was inventory
     
  16. 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.
     
  17. Offline

    CreeperShift

    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 :p) 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.
     
  18. Offline

    HelGod

    CreeperShift
    I did attach a custom display name and a lore in my code. I just sisnt add it when I posted this
     
  19. Offline

    CreeperShift

    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.
     
  20. CreeperShift
    equals() or isSimilar() already checks for all of them.

     
Thread Status:
Not open for further replies.

Share This Page