It doesn´t set item in hand

Discussion in 'Plugin Development' started by PlayerNerd, Nov 8, 2014.

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

    PlayerNerd

    It doesn´t set the bowl in the hand.

    Code:java
    1. @EventHandler
    2.  
    3. public void onPotion(PlayerInteractEvent e)
    4.  
    5. {
    6.  
    7. ItemStack potion = new ItemStack(Material.MUSHROOM_SOUP);
    8.  
    9. ItemMeta potionN = potion.getItemMeta();
    10.  
    11. ArrayList<String> potionL = new ArrayList<String>();
    12.  
    13. potionN.setDisplayName(Main.nserv + ChatColor.AQUA + " Sopa de " + ChatColor.DARK_RED + "s2");
    14.  
    15. potionL.add(ChatColor.RED + "Clique com direito e recupere vida.");
    16.  
    17. potionN.setLore(potionL);
    18.  
    19. potion.setItemMeta(potionN);
    20.  
    21.  
    22.  
    23. Player p = e.getPlayer();
    24.  
    25.  
    26.  
    27. if(p.getItemInHand().equals(potion))
    28.  
    29. {
    30.  
    31. if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK))
    32.  
    33. {
    34.  
    35.  
    36.  
    37. p.setItemInHand(new ItemStack(Material.BOWL));
    38.  
    39.  
    40.  
    41. double f = p.getFoodLevel();
    42.  
    43. double h = p.getHealth();
    44.  
    45.  
    46.  
    47. switch(p.getFoodLevel())
    48.  
    49. {
    50.  
    51.  
    52.  
    53. case 20:
    54.  
    55.  
    56.  
    57. switch((int)p.getHealth())
    58.  
    59. {
    60.  
    61.  
    62.  
    63. case 20:
    64.  
    65. break;
    66.  
    67. case 19:
    68.  
    69. p.setHealth(h + 1);
    70.  
    71. break;
    72.  
    73. case 18:
    74.  
    75. p.setHealth(h + 2);
    76.  
    77. break;
    78.  
    79. case 17:
    80.  
    81. p.setHealth(h + 3);
    82.  
    83. break;
    84.  
    85. case 16:
    86.  
    87. p.setHealth(h + 4);
    88.  
    89. break;
    90.  
    91. case 15:
    92.  
    93. p.setHealth(h + 5);
    94.  
    95. break;
    96.  
    97. case 14:
    98.  
    99. p.setHealth(h + 6);
    100.  
    101. break;
    102.  
    103. case 13:
    104.  
    105. p.setHealth(h + 7);
    106.  
    107. break;
    108.  
    109. case 12:
    110.  
    111. p.setHealth(h + 8);
    112.  
    113. break;
    114.  
    115. default:
    116.  
    117. p.setHealth(h + 8);
    118.  
    119. break;
    120.  
    121. }
    122.  
    123.  
    124.  
    125. break;
    126.  
    127. case 19:
    128.  
    129. p.setFoodLevel((int) (f + 1));
    130.  
    131. break;
    132.  
    133. case 18:
    134.  
    135. p.setFoodLevel((int) (f + 2));
    136.  
    137. break;
    138.  
    139. case 17:
    140.  
    141. p.setFoodLevel((int) (f + 3));
    142.  
    143. break;
    144.  
    145. case 16:
    146.  
    147. p.setFoodLevel((int) (f + 4));
    148.  
    149. break;
    150.  
    151. case 15:
    152.  
    153. p.setFoodLevel((int) (f + 5));
    154.  
    155. break;
    156.  
    157. case 14:
    158.  
    159. p.setFoodLevel((int) (f + 6));
    160.  
    161. break;
    162.  
    163. case 13:
    164.  
    165. p.setFoodLevel((int) (f + 7));
    166.  
    167. break;
    168.  
    169. case 12:
    170.  
    171. p.setFoodLevel((int) (f + 8));
    172.  
    173. break;
    174.  
    175. default:
    176.  
    177. p.setFoodLevel((int) (f + 8));
    178.  
    179. break;
    180.  
    181. }
    182.  
    183.  
    184.  
    185.  
    186.  
    187. }
    188.  
    189. }
    190.  
    191.  
    192.  
    193. if(!(p.getItemInHand().equals(potion))) return;
    194.  
    195. }
     
  2. Offline

    Watto

    PlayerNerd

    Please use the code wrapper provided by the Bukkit Forums
    Code:java
    1. Like this!


    I don't even want to attempt to read that monstrosity.
     
  3. Offline

    PlayerNerd

    Watto Sorry, i forgot
     
  4. Offline

    teej107

    Register event. Your big switch statement could be condensed into a for loop.
     
  5. Offline

    PlayerNerd

    teej107 Could you give me an example?
     
  6. Offline

    teej107

    PlayerNerd Actually you don't even need a for loop.

    Just set the food and health to 20.
     
  7. Offline

    PlayerNerd

    teej107 But i need set firstly the food and when it is full set the healh.
     
  8. Offline

    teej107

    PlayerNerd if food level is 20, set health to 20. Else, set food level to 20. That is what your are basically doing.
     
  9. Offline

    PlayerNerd

    teej107 But when it right click for set food/health de item in hand isn´t set to a bowl.
     
  10. Offline

    teej107

  11. Offline

    PlayerNerd

  12. Offline

    PlayerNerd

  13. Offline

    MajorSkillage

    are you right clicking air with the potion or a block? In your code it says that if you're right clicking a block as well :p
     
  14. Offline

    Dudemister1999

    PlayerNerd
    1. You need null checks for the item
    2. You should just check if it's a mushroom stew, not check if it's a specific ItemStack
    3. After that, you should check for meta (Then whatever lore / name you want)
    4. p.updateInventory()
    5. Profit...? (Not sure if you can profit from these.)
     
Thread Status:
Not open for further replies.

Share This Page