Problem in my Bukkit plugin

Discussion in 'Plugin Development' started by CoolCreeper001, Dec 1, 2018.

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

    CoolCreeper001

    I'm trying to create a command that heals you but you have to pay 1 gold nugget.

    Ingame it works fine when you have a gold nugget, but if you have not a gold nugget it's an internal error.
    How can i fix that?

    Code:
    if (cmd.getName().equalsIgnoreCase("heal")) {
    
                for(int i = 0; i<p.getInventory().getSize()-1; ++i) {
                    if(p.getInventory().getItem(i).getType() == Material.GOLD_NUGGET){
                        p.getInventory().remove(new ItemStack(org.bukkit.Material.GOLD_NUGGET, 1));
                    p.sendMessage("§4Healed");
                    p.playSound(p.getLocation(), Sound.VILLAGER_YES, 10, 1);
                    p.setHealth(20);
                    p.setFoodLevel(20);
                    p.setSaturation(20);
                    return true;
                } else {
                    p.sendMessage("§6You don't have a gold nugget");
                    p.playSound(p.getLocation(),Sound.WITHER_HURT, 10 ,1);
                    return true;
    
                    }
    
                }
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    CoolCreeper001

    @timtower What do you mean (sorry im new in programming plugins)
     
  4. Offline

    timtower Administrator Administrator Moderator

    @CoolCreeper001 If there is an empty slot in the inventory then getInventory().getItem(slot) will return null.
    If you call getType on null then you get a nullpointerexception
     
  5. Offline

    CoolCreeper001

    so i don't know how to edit the code because im such a noob

    Sorry im confused
     
  6. Offline

    timtower Administrator Administrator Moderator

    @CoolCreeper001 Add a null check on the item, if it is null the continue.
     
  7. Offline

    CoolCreeper001

    give me 5 minutes to test

    I did this:

    Code:
    for (int i = 0; i < p.getInventory().getSize() - 1; ++i) {
                    if (p.getInventory().getItem(i).getType() == Material.AIR) {
                        p.sendMessage("§6You don't have a gold nugget");
                        p.playSound(p.getLocation(), Sound.WITHER_HURT, 10, 1);
                        return true;
                    } else {
                        p.getInventory().remove(new ItemStack(Material.GOLD_NUGGET, 1));
                        p.sendMessage("§4Healed");
                        p.playSound(p.getLocation(), Sound.VILLAGER_YES, 10, 1);
                        p.setHealth(20);
                        p.setFoodLevel(20);
                        p.setSaturation(20);
                        return true;
    I think i didn't understand it correct because it's the same error ingame when i have no nugget

    there's also an error in console. Im getting null error: org.bukkit.command.CommandException: Unhandled exception executing command 'heal' in plugin test v1.0

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 1, 2018
  8. Offline

    timtower Administrator Administrator Moderator

  9. Offline

    CoolCreeper001

    yea i know but how can i do a null check

    i tried something like that : Playerinventory inv = p.getInventory().getItem(i).getType() == MATERIAL.GOLDNUGGET
    if(inv == null)

    but this didn't work either

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  10. Offline

    timtower Administrator Administrator Moderator

    Did you search for it.
     
  11. Offline

    CoolCreeper001

    yes, im only getting things that my program don't like
     
  12. Offline

    timtower Administrator Administrator Moderator

    Why aren't you checking the item itself?
     
  13. Offline

    CoolCreeper001

    I don't know how

    I fixed it with
    if(p.getInventory().contains(Material.GOLD_NUGGET))

    TY for your time

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

Share This Page