Home > Blockchain >  getItemMeta is always returning null in my spigot plugin
getItemMeta is always returning null in my spigot plugin

Time:08-21

Hi fellow coders I have been trying to make a custom sword and put it into a shop gui but when I try to change the swords meta using ItemStack.getitemmeta() and then i try to set the display name or something it says the sword meta is null. Also I am changing My actual name in irl to my name and the server name to server name

Error:

Caused by: java.lang.NullPointerException: Cannot invoke "org.bukkit.inventory.meta.ItemMeta.setDisplayName(String)" because "weaponMeta" is null

Blunt Knife.java

package me.myname.servername.items.weapons;

import me.myname.servername.Utils.Weapon;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;

public class BluntKnife extends Weapon
{

    public void BluntKnife()
    {
        tradable = true;
        damage = 12;
        rarity = "COMMON";
        name = "Blunt Knife";
        ArrayList<String> lore = new ArrayList<String>();
        lore.add("Damage:  12");
        lore.add("");
        lore.add("Can be upgraded with /upgrade for 500 coins!");
        lore.add("");
        lore.add("⚔ COMMON SWORD ⚔");
        item = new ItemStack(Material.AIR, 1);

    }
}

MakeItem.java

package me.myname.servername.Utils;

import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

public interface MakeItem
{
    public default ItemStack makeItemFromWeapon(Weapon wpn)
    {
        ItemStack weapon = wpn.item;
        ItemMeta weaponMeta = weapon.getItemMeta();

        weaponMeta.setDisplayName(wpn.name);
        weaponMeta.setLore(wpn.lore);
        weapon.setItemMeta(weaponMeta);
        
        return weapon;
    }
}

Weapon.java

package me.myname.servername.Utils;

import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;

public class Weapon
{
    protected boolean tradable = true;
    protected int damage = 5;
    protected String rarity = null;
    protected String name = null;
    protected ArrayList<String> lore = new ArrayList<>();
    protected ItemStack item = new ItemStack(Material.AIR, 1);
}

ShopCommand.java

package me.myname.servername.commands;

import me.myname.servername.Utils.MakeItem;
import me.myname.servername.Utils.Weapon;
import me.myname.servername.items.weapons.BluntKnife;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

public class ShopCommand implements CommandExecutor, MakeItem {

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
    {
        if(sender instanceof Player){
            Player player = (Player) sender;
            Inventory shopinv = Bukkit.createInventory(player, 9, ChatColor.AQUA   "Shop");
            BluntKnife item1 = new BluntKnife();


            ItemStack[] menu_items = {makeItemFromWeapon(item1)};


            shopinv.setContents(menu_items);
            player.openInventory(shopinv);
        }

        return true;
    }
}

CodePudding user response:

Of public interface MakeItem. An interface in Java is always public and static and All fields (variables) must be static constants and All methods are an abstract signature only , no body code You must "implement" the coded methods inside a class that uses the implement keyword and the interface name.

CodePudding user response:

In BluntKnife.java I did the constructor wrong.

So I changed it from public void BluntKnife() too public BluntKnife()

  • Related