What is causing an internal error here?

Discussion in 'Plugin Development' started by niehoelzz, May 14, 2017.

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

    niehoelzz

    Code:
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class Rename implements CommandExecutor {
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmnd, String cmndlabel, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("You must be a player to perform this command!");
                return false;
            }
            Player player = (Player) sender;
            ItemMeta meta = player.getInventory().getItemInMainHand().getItemMeta();
            meta.setDisplayName(args[0]);
            ItemStack stack = (ItemStack) meta;
            player.setItemInHand(stack);
            return true;
        }
    
    }
    
     
  2. Offline

    timtower Administrator Administrator Moderator

    @niehoelzz
    ItemMeta meta = player.getInventory().getItemInMainHand().getItemMeta();
    My guess would be that line.
    Might be no item in their hand.
    Item might not have item meta.
    And if not that line:
    args[0] might not exists.
    You can't cast ItemMeta to an itemstack.
     
  3. Offline

    Zombie_Striker

    @niehoelzz
    1. The Item may not exist.
    2. The item meta may not exist.
    3. You are not checking the args length. Make sure there are args before you get the first one.
    4. You cannot convert ItemMeta to an Itemstack. Create a new ItemStack or just get the item in the main and and re-set its item meta.
     
Thread Status:
Not open for further replies.

Share This Page