Helmet and AIR?

Discussion in 'Plugin Development' started by trinc, Sep 22, 2012.

  1. Offline

    trinc

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I got a problem. I'm developing a "hat"-plugin, which allows you to add the ItemInHand to the your helmet. Problem was, that an internal server error occures :/

    Can someone help me?

    My code

    [IMG]

    Console

    [IMG]
  2. Offline

    XbannisherX

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    You my friend got a nullpointer exeption, so something in your code returns null, let me have a look...
  3. Offline

    XbannisherX

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    line 33 and 40 and 35 prob return null
  4. Offline

    XbannisherX

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    You should do a null check on it, like
    if(item != null){
    if(item == what you want{//Your code here}
    }
  5. Offline

    trinc

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
  6. Offline

    MCForger

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Here I had an old hat command and I updated to your needs.
    Code:
        private boolean handleHat(CommandSender sender, String[] args)
        {
            Player player = (Player) sender;
     
            if (player.getItemInHand().getType() != Material.AIR)
            {
                ItemStack itemHand = player.getItemInHand();
                PlayerInventory inventory = player.getInventory();
                ItemStack itemHead = inventory.getHelmet();
                inventory.removeItem(itemHand);
                inventory.setHelmet(itemHand);
                inventory.setItemInHand(itemHead);
                player.sendMessage(ChatColor.GREEN
                        + "Item successfuly put on your head.");
                player.sendMessage(ChatColor.AQUA
                        + "Remember to check your helment slot to make sure you didnt put more then 1 of that item!");
                return true;
            }
            else
            {
                player.sendMessage(ChatColor.RED
                        + "You must have something to put on your head!");
                return true;
            }
        }
    Remember to check if it is a player by checking if instanceof Player. If you try do to it in the console you will get an error saying you cant cast entity player to the console.

    This post has been edited 2 times. It was last edited by MCForger Sep 22, 2012.
  7. Offline

    CarPet

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    There are so many of these, you should try to make yours cooler than others.

Share This Page