CraftItemEvent won't update CraftingInventory

Discussion in 'Plugin Development' started by zerodevice, Sep 8, 2014.

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

    zerodevice

    I made a custom recipe added to the server onEnable().
    I managed to craft the item.
    When i tries to craft and pickup just 1 resulted item, it has no problem at all.

    But when i tries to do quantity pickup (InventoryAction.MOVE_TO_OTHER_INVENTORY), the TopInventory will not update correctly and leaves a ghostly recipe behind.
    BottomInventory will not update to the right amount.

    I've tried making the player to UpdateInventory but its not working.
    Anyone can help me?
     
  2. Offline

    TheMintyMate

    Please post your main java file (containing above code).
    - Minty
     
  3. Offline

    zerodevice

    Code:java
    1. public class Main extends JavaPlugin implements Listener
    2. {
    3. private Echo gEcho = new Echo();
    4. private HomeFeather gHomeFeather = new HomeFeather();
    5.  
    6. public void onEnable()
    7. {
    8. //Register plugin to plugin manager
    9. EnchantGlow glow = new EnchantGlow(128);
    10. this.getServer().getPluginManager().registerEvents(this, this);
    11. this.getServer().addRecipe(gHomeFeather.HomeFeatherRecipe());
    12.  
    13. try
    14. {
    15. try
    16. {
    17. Field f = Enchantment.class.getDeclaredField("acceptingNew");
    18. f.setAccessible(true);
    19. f.set(null, true);
    20. }
    21. catch (Exception e)
    22. {
    23. e.printStackTrace();
    24. }
    25.  
    26. try
    27. {
    28. Enchantment.registerEnchantment(glow);
    29. }
    30. {
    31. //if this is thrown it means the id is already taken.
    32. }
    33. }catch(Exception e)
    34. {
    35. e.printStackTrace();
    36. }
    37. }
    38.  
    39. public void onDisable()
    40. {
    41. this.getServer().resetRecipes();
    42. HandlerList.unregisterAll();
    43. }
    44.  
    45. //@SuppressWarnings("deprecation")
    46. @EventHandler
    47. public void onCraftItem(CraftItemEvent e)
    48. {
    49. //i stop here, some problems when the player collects the result in bulk will
    50. //make the craft inventory not updated.
    51.  
    52.  
    53.  
    54. }
    55.  
    56. @EventHandler
    57. public void onInteract(PlayerInteractEvent e)
    58. {
    59. try
    60. {
    61. if(e.getAction() == Action.RIGHT_CLICK_BLOCK)
    62. {
    63. if(e.getPlayer().getItemInHand() != null)
    64. {
    65. if(e.getPlayer().getItemInHand() instanceof ItemStack)
    66. {
    67. if(e.getPlayer().getItemInHand().getType().equals(gHomeFeather.Item_HomeFeather().getType()))
    68. {
    69. ItemStack itemStack = e.getPlayer().getItemInHand();
    70. if(gHomeFeather.Exist(itemStack))
    71. {
    72. e.setCancelled(true);
    73. if(e.getPlayer().teleport(e.getPlayer().getBedSpawnLocation(),TeleportCause.PLUGIN))
    74. {
    75. gEcho.Msg(e.getPlayer(), ChatColor.YELLOW + "Welcome home.");
    76. if(e.getPlayer().getItemInHand().getAmount() > 1)
    77. {
    78. e.getPlayer().getItemInHand().setAmount(e.getPlayer().getItemInHand().getAmount()-1);
    79. }else
    80. {
    81. e.getPlayer().setItemInHand(null);
    82. }
    83. }else
    84. {
    85. gEcho.Msg(e.getPlayer(), ChatColor.YELLOW + "Sorry. Unable to bring you home.");
    86. }
    87. }
    88. }
    89. }
    90. }
    91. }
    92. }catch(Exception ex)
    93. {
    94. gEcho.Trace("Home Feather :: onInteract() :: " + ex.getMessage());
    95. gEcho.Msg(e.getPlayer(), ChatColor.YELLOW + "Sorry. Unable to bring you home.");
    96. }
    97. }
    98.  
    99. }


    Thanks in advance.

    I got no idea where to fix this.
    Can't seem to find any "CraftingInventory.Update()" in the API.

    ** I had taken away the syntax for the InventoryAction, coz i cant find any methods to update it, so i assume it was something else...

    And the HomeFeather Class code goes below.

    Code:java
    1. public class HomeFeather
    2. {
    3. private String _ItemName = "HOME FEATHER";
    4. private String _ItemCode = "123456";
    5. private Short _ItemSubID = 999;
    6. private Material _ItemType = Material.FEATHER;
    7.  
    8.  
    9. public HomeFeather()
    10. {
    11.  
    12. }
    13.  
    14. public ShapedRecipe HomeFeatherRecipe()
    15. {
    16. ShapedRecipe recipe = new ShapedRecipe(Item_HomeFeather());
    17. recipe.shape("FFF", "F F", "FFF");
    18. recipe.setIngredient('F', Material.FEATHER);
    19. return recipe;
    20. }
    21.  
    22. public ItemStack Item_HomeFeather()
    23. {
    24. EnchantGlow glow = new EnchantGlow(128);
    25. ItemStack iStack = new ItemStack(_ItemType, 1, _ItemSubID);
    26. ItemMeta iMeta = iStack.getItemMeta();
    27. iMeta.setDisplayName(ChatColor.AQUA + _ItemName);
    28. iMeta.setLore(Arrays.asList(ChatColor.MAGIC + _ItemCode, ChatColor.GRAY + "Right-Click this to go home."));
    29. iStack.setItemMeta(iMeta);
    30. iStack.addUnsafeEnchantment(glow, 1);
    31.  
    32. return iStack;
    33. }
    34.  
    35. public boolean Exist(ItemStack itemStack)
    36. {
    37. if(itemStack.getItemMeta().getDisplayName() != null)
    38. {
    39. if(itemStack.getItemMeta().getDisplayName().equalsIgnoreCase("null") == false)
    40. {
    41. if((itemStack.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.AQUA + _ItemName)))
    42. {
    43. if(itemStack.getType() == _ItemType)
    44. {
    45. if(itemStack.getDurability() == _ItemSubID)
    46. {
    47. if(itemStack.getItemMeta().getLore().toArray()[0].toString().contains(_ItemCode))
    48. {
    49. return true;
    50. }
    51. }
    52. }
    53. }
    54. }
    55. }
    56. return false;
    57. }
    58.  
    59. public void Create(Player player)
    60. {
    61. player.getInventory().addItem(Item_HomeFeather());
    62. }
    63.  
    64. }


    This is the class i made for the HomeFeather Item.
    As you can see, the ShapedRecipe should be very simple.
    Just not sure why it has problems.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  4. Offline

    zerodevice

    Hi, I've posted the code.
    Your help would be very much appreciated.
    Thank you.
     
  5. Offline

    TheMintyMate

    zerodevice
    Have you tried just using this? :
    Code:
    Player player = /*get player*/;
    player.updateInventory();
    
    If this doesn't seem to work, you could always look at this thread. However, try and avoid his method ( I hate BukkitRunnables , with great passion :p - mainly because so many people - me included once upon a time - use it for a cool down, when you don't need to).

    Hope this helped,
    - Minty
     
  6. Offline

    zerodevice


    I did tried the updateInventory() but doesn't change anything and its updating the players inventory and not the crafting inventory.
     
  7. Offline

    TheMintyMate

    zerodevice
    I see you commented out your CraftItemEvent. Please could you post this too?

    Thanks,
    - Minty
     
  8. Offline

    zerodevice


    I didn't really commented out the CraftItemEvent, it was just there blank because i've tested several attempts including the updateinventory and other methods and it's not working so im leaving it blank until i can figure it out.

    do you know of any methods to actually update the CraftingInventory or im actually coding a wrong approach from the beginning?

    My main target is to create my own custom recipe and then players can craft them with 1 or quantity.
    i've seen other plugins made it but just not sure why i couldn't do it.

    Thanks.
     
  9. Offline

    TheMintyMate

    zerodevice
    After looking through some of the javadocs, I have come to the conclusion that your coding from the wrong approach. Here (for CraftingInventory) you GET the items being crafted using these (here for better documentation):
    Code:
    itemstack.getResult();
    //Check what item is in the resulting slot of the crafting inventory - gets recipe formed
    itemstack.getMatrix();
    //get crafting matrix (where user crafts - applies items for crafting) - gets contents, if any
    setResult(ItemStack, newResult);
    //set the result (what the player gets from the resulting slot)
    setMatrix(ItemStack[], contents);
    //replace the items placed into the matrix (crafting area)
    Recipe recipe = event.getRecipe();
    //get the recipe formed inside the crafting box, if any
     
    /*extra paremeters : */
    newResult
    
    I hope this helped considerably more,
    - Minty
     
  10. Offline

    zerodevice

    H
    Hmm.... Ok Thanks Minty, I've going to give it a try.
     
    TheMintyMate likes this.
Thread Status:
Not open for further replies.

Share This Page