Auto-Equipping Armor help (PlayerInteractEvent)

Discussion in 'Plugin Development' started by AoH_Ruthless, Dec 31, 2013.

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

    AoH_Ruthless

    I am trying to code a plugin, where you right click (air or block), and you have a valid piece of armor, you autmatically equip it.
    Here is my code (Events are registered, so is permissions, etc.):
    Code:java
    1. @EventHandler
    2. public void onInteract(PlayerInteractEvent e) {
    3. Player p = e.getPlayer();
    4. if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    5. System.out.println("debug1");
    6. if (!p.hasPermission("pvp.autoequip")) return;
    7. System.out.println("debug2");
    8. if (isValidChestplate(p.getItemInHand().getType())) {
    9. System.out.println("debug3");
    10. p.getInventory().setChestplate(p.getItemInHand());
    11. System.out.println("debug4");
    12. p.setItemInHand(null);
    13. System.out.println("debug5");
    14. p.updateInventory();
    15. System.out.println("debug6");
    16. } else if (isValidBoots(p.getItemInHand().getType())) {
    17. p.getInventory().setBoots(p.getItemInHand());
    18. p.setItemInHand(null);
    19. p.updateInventory();
    20. } else if (isValidHelmet(p.getItemInHand().getType())) {
    21. p.getInventory().setHelmet(p.getItemInHand());
    22. p.setItemInHand(null);
    23. p.updateInventory();
    24. } else if (isValidLeggings(p.getItemInHand().getType())) {
    25. p.getInventory().setLeggings(p.getItemInHand());
    26. p.setItemInHand(null);
    27. p.updateInventory();
    28. }
    29. }
    30. }
    31.  
    32. private boolean isValidChestplate(Material m) {
    33. switch(m) {
    34. case DIAMOND_CHESTPLATE:
    35. case IRON_CHESTPLATE:
    36. case GOLD_CHESTPLATE:
    37. case CHAINMAIL_CHESTPLATE:
    38. return true;
    39. default:
    40. return false;
    41. }
    42. }
    43.  
    44. private boolean isValidHelmet(Material m) {
    45. switch(m) {
    46. case DIAMOND_HELMET:
    47. case IRON_HELMET:
    48. case GOLD_HELMET:
    49. case CHAINMAIL_HELMET:
    50. return true;
    51. default:
    52. return false;
    53. }
    54. }
    55.  
    56. private boolean isValidBoots(Material m) {
    57. switch(m) {
    58. case DIAMOND_BOOTS:
    59. case IRON_BOOTS:
    60. case GOLD_BOOTS:
    61. case CHAINMAIL_BOOTS:
    62. return true;
    63. default:
    64. return false;
    65. }
    66. }
    67.  
    68. private boolean isValidLeggings(Material m) {
    69. switch(m) {
    70. case DIAMOND_LEGGINGS:
    71. case IRON_LEGGINGS:
    72. case GOLD_LEGGINGS:
    73. case CHAINMAIL_LEGGINGS:
    74. return true;
    75. default:
    76. return false;
    77. }
    78. }


    So, when I have permission "pvp.autoequip" (default: op), it works fine. But when i remove op from myself and the permission, it still equips the armor and does not set my item in hand to null.
    When I have the permission, all 6 debug messages are sent. When I don't have permission, only the first debug is sent (like it is supposed to). How do I fix this?
    Also, there is no stacktrace.
     
  2. Offline

    WhippyCleric

    Hi

    Instead of using a return from a void method to exit you should put all the code in an if , else if block, so everything after your return put in an else if block.

    This could explain the behaviour you're seeing
     
  3. Offline

    AoH_Ruthless

  4. Offline

    WhippyCleric



    Can you test with a chestplate in hand and see if you receive the debug messages
     
  5. Offline

    AoH_Ruthless

    WhippyCleric
    I already said in the OP that I did... I even tested it with boots and leggings
     
  6. Offline

    AoH_Ruthless

    Bump? Does anyone have an idea?
     
  7. Offline

    AoH_Ruthless

  8. Do not bump twice within 24 hours.

    On-Topic: By default you can auto-equip armor since 1.5
     
  9. Offline

    AoH_Ruthless

    Sgt_Tailor
    Check the timings at which I bumped.

    And you can? Never knew that lol
     
Thread Status:
Not open for further replies.

Share This Page