Inventory: Anvil Events

Discussion in 'Resources' started by Zelnehlun, Apr 22, 2013.

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

    Zelnehlun

    Hello,

    I do not know whether this already exists or if it is implemented in the latest builds, at least not in mine.
    For my plugin I needed a way to access Anvil events, such as item renaming or combining tools to repair them.

    Maybe someone finds this code useful:
    Item Renaming
    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.MONITOR)
    3. public void onInventoryClick(InventoryClickEvent e){
    4. // check if the event has been cancelled by another plugin
    5. if(!e.isCancelled()){
    6. HumanEntity ent = e.getWhoClicked();
    7.  
    8. // not really necessary
    9. if(ent instanceof Player){
    10. Player player = (Player)ent;
    11. Inventory inv = e.getInventory();
    12.  
    13. // see if the event is about an anvil
    14. if(inv instanceof AnvilInventory){
    15. InventoryView view = e.getView();
    16. int rawSlot = e.getRawSlot();
    17.  
    18. // compare the raw slot with the inventory view to make sure we are talking about the upper inventory
    19. if(rawSlot == view.convertSlot(rawSlot)){
    20. /*
    21. slot 0 = left item slot
    22. slot 1 = right item slot
    23. slot 2 = result item slot
    24.  
    25. see if the player clicked in the result item slot of the anvil inventory
    26. */
    27. if(rawSlot == 2){
    28. /*
    29. get the current item in the result slot
    30. I think inv.getItem(rawSlot) would be possible too
    31. */
    32. ItemStack item = e.getCurrentItem();
    33.  
    34. // check if there is an item in the result slot
    35. if(item != null){
    36. ItemMeta meta = item.getItemMeta();
    37.  
    38. // it is possible that the item does not have meta data
    39. if(meta != null){
    40. // see whether the item is beeing renamed
    41. if(meta.hasDisplayName()){
    42. String displayName = meta.getDisplayName();
    43.  
    44. // do something
    45. }
    46. }
    47. }
    48. }
    49. }
    50. }
    51. }
    52. }
    53. }
    54.  


    Item Repairing
    Code:java
    1.  
    2. @EventHandler
    3. public static void onInventoryClick(InventoryClickEvent e){
    4. // check whether the event has been cancelled by another plugin
    5. if(!e.isCancelled()){
    6. HumanEntity ent = e.getWhoClicked();
    7.  
    8. // not really necessary
    9. if(ent instanceof Player){
    10. Player player = (Player)ent;
    11. Inventory inv = e.getInventory();
    12.  
    13. // see if we are talking about an anvil here
    14. if(inv instanceof AnvilInventory){
    15. AnvilInventory anvil = (AnvilInventory)inv;
    16. InventoryView view = e.getView();
    17. int rawSlot = e.getRawSlot();
    18.  
    19. // compare raw slot to the inventory view to make sure we are in the upper inventory
    20. if(rawSlot == view.convertSlot(rawSlot)){
    21. // 2 = result slot
    22. if(rawSlot == 2){
    23. // all three items in the anvil inventory
    24. ItemStack[] items = anvil.getContents();
    25.  
    26. // item in the left slot
    27. ItemStack item1 = items[0];
    28.  
    29. // item in the right slot
    30. ItemStack item2 = items[1];
    31.  
    32. // I do not know if this is necessary
    33. if(item1 != null && item2 != null){
    34. int id1 = item1.getTypeId();
    35. int id2 = item2.getTypeId();
    36.  
    37. // if the player is repairing something the ids will be the same
    38. if(id1 != 0 && id1 == id2){
    39. // item in the result slot
    40. ItemStack item3 = e.getCurrentItem();
    41.  
    42. // check if there is an item in the result slot
    43. if(item3 != null){
    44. ItemMeta meta = item3.getItemMeta();
    45.  
    46. // meta data could be null
    47. if(meta != null){
    48. // get the repairable interface to obtain the repair cost
    49. if(meta instanceof Repairable){
    50. Repairable repairable = (Repairable)meta;
    51. int repairCost = repairable.getRepairCost();
    52.  
    53. // can the player afford to repair the item
    54. if(player.getLevel() >= repairCost){
    55. // success
    56. }else{
    57. // bugger
    58. }
    59. }
    60. }
    61. }
    62. }
    63. }
    64. }
    65. }
    66. }
    67. }
    68. }
    69. }
    70.  
     
    KerchooK, NukeDude, madtomic and 11 others like this.
  2. Offline

    eccentric_nz

    Very useful, thanks :)
     
  3. Offline

    rsod

    Tabs? Pastebin? naaah, for losers.
     
  4. Offline

    nuno1212sss

    Hi I would really like to know if like I could check what block they are renaming and if it is like mushroom soup it get's cancelled.
     
  5. Offline

    UltimateMC

    Perfect! Its working!
     
Thread Status:
Not open for further replies.

Share This Page