0

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;
    }
}

2 Answers2

0

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.

Samuel Marchant
  • 331
  • 2
  • 6
  • so would i make my own method for it in the ShopCommand class? – Jimmy Graham Aug 20 '22 at 12:14
  • Hello im very confused about what you said i dont think it was an answer i just think it was random java knowledge unless im missing something about your answer. – Jimmy Graham Aug 20 '22 at 12:57
  • No doubt a constructor does not use a method type declaration, THE interface did not appear to be "implemented" on either class. For interface methods to operate they must have code in a class that implements the interface, usually "a class that implements is either default or private" (or has protected methods) not public so the only access can be made to the method code is from an interface (public). While a class that is public can have an interface it is not required because of public. – Samuel Marchant Aug 30 '22 at 16:12
0

In BluntKnife.java I did the constructor wrong.

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

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 24 '22 at 20:57