1.15.2 Custom Item Object Question

Discussion in 'Spigot Plugin Development' started by cosmic1132, May 19, 2020.

  1. Hello Spigot community,

    I'm developing a plugin that has gadgets.

    Here is my code for the Gadget Item Class:

    Code (Java):
    public class GadgetItem {
        public ClassTypes ClassType;
        public String displayName;
        public ItemStack itemStack;

        public GadgetItem(ClassTypes ClassType, String displayName, ItemStack itemStack) {
            this.ClassType = ClassType;
            this.displayName = displayName;
            this.itemStack = itemStack;

            // actually set item meta name
            ItemMeta im = this.itemStack.getItemMeta();

        public ItemStack getItemStack() {
            return itemStack;

        public String getDisplayName() {
            return displayName;
    I now want to implement events. For starters, I would like a simple message of "hello world" to be printed to console if the user right-clicks while holding a compass with a display name of "Radar". Currently, this is my code:

    Code (Java):
    public void onGadgetUse(PlayerInteractEvent e){
        Player p = e.getPlayer();

        if(p.getInventory().getItemInMainHand().hasItemMeta() && ChatColor.stripColor(p.getInventory().getItemInMainHand().getItemMeta().getDisplayName()).equals("Radar")) {
            System.out.println("hello world");
    You can see that in order to test whether the interacted item is the radar, I have to check whether it has an item meta and then get its display name. Is there a way to use the
    Code (Text):
    keyword to somehow determine whether the item stack clicked is a GadgetItem?

    Is there a way to use inheritance or something like that? Because then my code would be a lot more efficient/clean.

  2. What if you make your GadgetItem object extend ItemStack? might be a stupid idea lol, just a random suggestion, I should probably go to sleep.
  3. That won't work. Inventories don't store ItemStacks directly, but instead delegates them to an internal NMS inventory, which stores the item's NMS counterpart.

    Whenever you query an item from an inventory, it is first retrieved from the internal NMS inventory, and used to create an equivalent Bukkit's item stack (CraftItemStack to be precise).

    So, even if you implement your own GadgetItem and add it to the inventory, it will just be converted back to a regular CraftItemStack once you retrieve it.
    • Informative Informative x 2
  4. But what if I retrieve the gadget by running a command that creates the GadgetItem and uses GadgetItem.getItemStack(); to give the player the item?
  5. That would give the player an ordinary ItemStack, and if you verify it by name, it will work just fine.
    What won't work would be creating a new ItemStack subclass and verifying it with instanceof, as you asked in your original question.
  6. You could give your gadget items a lore entry such as 'Gadget' and simply check whether the item contains this specific lore entry when it is being right clicked.