"Random" errors with Soup PvP Plugin

Discussion in 'Plugin Development' started by Paradrakor, Jul 18, 2014.

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

    Paradrakor

    I'm coding a basic kitpvp server with instant soup healing. However, there are a few unresolved variables even though I used craftbukkit and bukkit in my plugin. The bolded and underlined variables are the ones that "cannot be resolved to a variable": food, health, and bowl:


    Code:java
    1. package eliteKits.events;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.block.Action;
    8. import org.bukkit.event.player.PlayerInteractEvent;
    9. import org.bukkit.inventory.ItemStack;
    10.  
    11. public class InventoryListener implements Listener {
    12. @EventHandler
    13. public void onPlayerInteract (PlayerInteractEvent event) {
    14. Player player = event.getPlayer();
    15. Material mat = player.getItemInHand().getType();
    16.  
    17. if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    18. if (mat == Material.MUSHROOM_SOUP) {
    19. ItemStack bowl = new ItemStack(Material.BOWL);
    20. double health = player.getHealth();
    21. if (health == 20) {
    22. int food = player.getFoodLevel();
    23. if (food == 20);
    24. return;
    25. }
    26. int nfood = [U][B]food[/B][/U] + 7;
    27. if (nfood > 20) {
    28. player.setFoodLevel(20);
    29. }
    30. else {
    31. player.setFoodLevel(nfood);
    32. }
    33. player.getInventory().setItem(player.getInventory().getHeldItemSlot(), bowl);
    34. event.setCancelled(true);
    35. return;
    36. } else {
    37. double nhealth = [U][B]health[/B][/U] + 7;
    38. if (nhealth > 20) {
    39. player.setHealth(20);
    40. } else {
    41. player.setHealth(nhealth);
    42. }
    43. event.setCancelled(true);
    44. player.getInventory().setItem(player.getInventory().getHeldItemSlot(), [U][B]bowl[/B][/U]);
    45. return;
    46. }
    47.  
    48. }
    49. }
    50. }
    51.  



    I'm pretty new to Java, please help! I find it strange that there is an exact same instance of this line of code:
    Code:text
    1. player.getInventory().setItem(player.getInventory().getHeldItemSlot(), [U][B]bowl[/B][/U]);


    but it has no errors.

    Thanks in advance!
     
  2. Offline

    teej107

    Initialize your health variable outside of your if statement. Since you are initializing the health variable inside the if statement, your else statement will never have an instance of the health variable. nhealth is trying to access a variable that hasn't been declared or initialized.
     
    Paradrakor likes this.
  3. Offline

    Traks

    On line 23 you used a semicolon instead of a opening curly bracket, replace it with one! Also, the code at the end of your if-block and else-block are identical, you could remove them and include one copy behind the else-block. The return statement is excessive by the way
     
  4. Offline

    Paradrakor


    Thanks, but I'm really new and am not sure how to fix this, because I used many if statements and I'm not sure where to initialize it.

    Nevermind, thank you guys so much!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page