1.15.2 Custom class to create Items turns them into Legacy items somehow

Discussion in 'Spigot Plugin Development' started by Maxx_Qc, Feb 19, 2020.

  1. Basically I'm adding items to a custom menu GUI:
    Code (Text):
    ownedChunks.addOption(new ItemBuilder(Material.DIRT).toItemStack()); // Causes legacy items initialization
    ownedChunks.addOption(Material.DIRT, "", 2); // Works without legacy init
    The addOption method:
    Code (Text):
    public MenuGUI addOption(ItemStack is)
    {
       addOption(is, -1);
       return this;
    }

    public MenuGUI addOption(Material mat, String name, int position, String... desc)
    {
       ItemStack is = new ItemStack(mat, 1);
       ItemMeta im = is.getItemMeta();
       assert im != null;
       im.setDisplayName(Helper.colorize(name));
       if (desc != null)
          im.setLore(Arrays.asList(Helper.colorize(desc)));
       is.setItemMeta(im);
       addOption(is, position);
       return this;
    }

    public MenuGUI addOption(ItemStack is, int position)
    {

       if (Math.floor(position / 9) > 5)
          return this;
     
       if (position < 0)
          inv.addItem(is);
       else
          inv.setItem(position, is);

       return this;
    }
    My item builder class:
    Code (Text):
    package com.maxx.themcsurvival.util;

    import java.util.*;

    import net.minecraft.server.v1_15_R1.NBTTagCompound;
    import net.minecraft.server.v1_15_R1.NBTTagString;
    import org.bukkit.Material;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.inventory.meta.SkullMeta;

    /**
    * Easily create itemstacks, without messing your hands. <i>Note that if you do
    * use this in one of your projects, leave this notice.</i> <i>Please do credit
    * me if you do use this in one of your projects.</i>
    *
    * @author NonameSL
    */
    public class ItemBuilder {
       private ItemStack is;

       /**
        * Create a new ItemBuilder from scratch.
        *
        * @param m The material to create the ItemBuilder with.
        */
       public ItemBuilder(Material m) { this(m, 1); }

       /**
        * Create a new ItemBuilder over an existing itemstack.
        *
        * @param is The itemstack to create the ItemBuilder over.
        */
       public ItemBuilder(ItemStack is) { this.is = is; }

       /**
        * Create a new ItemBuilder from scratch.
        *
        * @param m      The material of the item.
        * @param amount The amount of the item.
        */
       public ItemBuilder(Material m, int amount) { is = new ItemStack(m, amount); }

       /**
        * Clone the ItemBuilder into a new one.
        *
        * @return The cloned instance.
        */
       @Override
       public ItemBuilder clone() { return new ItemBuilder(is); }

       /**
        * Set the displayname of the item.
        *
        * @param name The name to change it to.
        */
       public ItemBuilder setName(String name)
       {
          ItemMeta im = is.getItemMeta();
          assert im != null;
          im.setDisplayName(Helper.colorize(name));
          is.setItemMeta(im);
          return this;
       }

       /**
        * Set the skull owner for the item. Works on skulls only.
        *
        * @param owner The name of the skull's owner.
        */
       public ItemBuilder setSkullOwner(final OfflinePlayer owner)
       {
          try
          {
             SkullMeta im = (SkullMeta) is.getItemMeta();
             assert im != null;
             im.setOwningPlayer(owner);
             is.setItemMeta(im);
          } catch (ClassCastException ignored) {}
          return this;
       }

       /**
        * Sets infinity durability on the item by setting the durability to
        * Short.MAX_VALUE.
        */
       public ItemBuilder setInfinityDurability()
       {
          ItemMeta im = is.getItemMeta();
          assert im != null;
          im.setUnbreakable(true);
          is.setItemMeta(im);
          return this;
       }

       /**
        * Re-sets the lore.
        *
        * @param lore The lore to set it to.
        */
       public ItemBuilder setLore(String... lore)
       {
          ItemMeta im = is.getItemMeta();
          assert im != null;
          im.setLore(Arrays.asList(Helper.colorize(lore)));
          is.setItemMeta(im);
          return this;
       }

       public ItemBuilder addNBTTag(String id, String value)
       {
          net.minecraft.server.v1_15_R1.ItemStack nmsIs = CraftItemStack.asNMSCopy(is);
          NBTTagCompound nmsCompound = (nmsIs.hasTag()) ? nmsIs.getTag() : new NBTTagCompound();
          assert nmsCompound != null;
          nmsCompound.set(id, NBTTagString.a(value));
          nmsIs.setTag(nmsCompound);
          is = CraftItemStack.asBukkitCopy(nmsIs);
          return this;
       }

       /**
        * Remove a lore line.
        *
        * @param line The lore to remove.
        */
       public ItemBuilder removeLoreLine(String line)
       {
          ItemMeta im = is.getItemMeta();
          assert im != null;
          List<String> lore = new ArrayList<>(Objects.requireNonNull(im.getLore()));
          if (!lore.contains(line))
             return this;
          lore.remove(line);
          im.setLore(lore);
          is.setItemMeta(im);
          return this;
       }

       /**
        * Remove a lore line.
        *
        * @param index The index of the lore line to remove.
        */
       public ItemBuilder removeLoreLine(int index)
       {
          ItemMeta im = is.getItemMeta();
          assert im != null;
          List<String> lore = new ArrayList<>(Objects.requireNonNull(im.getLore()));
          if (index < 0 || index > lore.size())
             return this;
          lore.remove(index);
          im.setLore(lore);
          is.setItemMeta(im);
          return this;
       }

       /**
        * Add a lore line.
        *
        * @param line The lore line to add.
        */
       public ItemBuilder addLoreLine(String line)
       {
          ItemMeta im = is.getItemMeta();
          List<String> lore = new ArrayList<>();
          assert im != null;
          if (im.hasLore())
             lore = new ArrayList<>(Objects.requireNonNull(im.getLore()));
          lore.add(Helper.colorize(line));
          im.setLore(lore);
          is.setItemMeta(im);
          return this;
       }

       /**
        * Add a lore line.
        *
        * @param line The lore line to add.
        * @param pos  The index of where to put it.
        */
       public ItemBuilder addLoreLine(String line, int pos)
       {
          ItemMeta im = is.getItemMeta();
          assert im != null;
          List<String> lore = new ArrayList<>(Objects.requireNonNull(im.getLore()));
          lore.set(pos, Helper.colorize(line));
          im.setLore(lore);
          is.setItemMeta(im);
          return this;
       }

       /**
        * Retrieves the itemstack from the ItemBuilder.
        *
        * @return The itemstack created/modified by the ItemBuilder instance.
        */
       public ItemStack toItemStack() { return is; }

       public void clearLoreLines()
       {
          ItemMeta im = is.getItemMeta();
          assert im != null;
          im.setLore(new ArrayList<>());
          is.setItemMeta(im);
       }
    }
    I'm lost cause basically it's just returning a new ItemStack(Material.DIRT)?? What's wrong?
     
  2. Set your api-version in your plugin.yml
     
  3. It’s set to 1.15, but it still says "initializing legacy support"... yet I don’t have any legacy item
     
  4. show the plugin.yml
    there is a possible bug within spigot that initializes legacy support when it shouldnt be, so this could be the issue
     
  5. Code (Text):
    name: TheMCSurvival
    main: com.maxx.themcsurvival.TheMCSurvival
    version: '1.0'
    author: Maxx_Qc
    description: TheMCSurvival's main plugin.
    website: www.themcsurvival.net
    api-version: '1.15'
    commands:
      chunk:
        usage: /chunk
      setrank:
        usage: /setrank <player> <rank>
      setspawn:
        usage: /setspawn
      spawn:
        usage: /spawn [player]
      kick:
        usage: /kick <player> <reason>
      freeze:
        usage: /freeze <player>
    I made a bug report but they said that NMS isn't supported (even though I do not use it, it's just in the library for the item builder).
    https://hub.spigotmc.org/jira/browse/SPIGOT-5588
     
    #5 Maxx_Qc, Feb 20, 2020
    Last edited: Feb 20, 2020

  6. Why do you have ''s around your version and api-version variables? I dont know if that would cause the error, but you might try anyways to change it to

    Code (Text):
    name: TheMCSurvival
    main: com.maxx.themcsurvival.TheMCSurvival
    version: 1.0
    author: Maxx_Qc
    description: TheMCSurvival's main plugin.
    website: www.themcsurvival.net
    api-version: 1.15
    commands:
      chunk:
        usage: /chunk
      setrank:
        usage: /setrank <player> <rank>
      setspawn:
        usage: /setspawn
      spawn:
        usage: /spawn [player]
      kick:
        usage: /kick <player> <reason>
      freeze:
        usage: /freeze <player>[/code
     
  7. Putting quotes around the 1.15 is actually "recommended" since api-version expects a string according to regex. Sure, it'll convert 1.15 to a string just as good as "1.15", it's just cleaner to do it.

    As for the "enabling legacy support" message, are you sure your plugin is causing that? Because it will list the causing plugins below that message.
     
  8. There’s only my plugin on the server and as mentioned in the OP if I use the second line it works fine but as soon as I use the first line it is legacy support.