Solved ItemStack help

Discussion in 'Spigot Plugin Development' started by MarkIsCool, Apr 16, 2019.

  1. I'm trying to make my plugin work on both versions: 1.12 and 1.13.
    One problem is that my plugin uses Material.WHITE_STAINED_GLASS_PANE. This Material does not exist in 1.12 version. Instead, the item stack constructor is something like this where the (byte) is the special data id.
    Code (Text):

    ItemStack whitePane = new ItemStack(Material.STAINED_GLASS_PANE, 1, 0, (byte) 0);
     
    I added a part in the code where I check for the version and depending on the version, I initialize the objects differently. However, this does not work because I cannot build it at all because when I use the 1.13 Library, I get errors for a missing field from the 1.12 code, and the same with 1.12 library giving me missing fields from Material.WHITE_STAINED_GLASS_PANE and BLACK as well..
    Code (Text):

    static {
    if(KitBuilderPlugin.getVersion() == Version.v1_12_R1) {
    blank = generateItemStack(Material.STAINED_GLASS_PANE, 1, (byte) 0, "", Arrays.asList("&f-"));
    black = generateItemStack(Material.STAINED_GLASS_PANE, 1, (byte) 15, "", Arrays.asList("&l-x-"));
    } else if(KitBuilderPlugin.getVersion() == Version.v1_13_R1 || KitBuilderPlugin.getVersion() == Version.v1_13_R2) {
    blank = generateItemStack(Material.WHITE_STAINED_GLASS_PANE, 1, "", Arrays.asList("&f-"));
    black = generateItemStack(Material.BLACK_STAINED_GLASS_PANE, 1, "", Arrays.asList("&l-x-"));
    }
    }

    /**
    * Quickly create a new ItemStack object not having to go through the ItemMeta stuff
    * @param material - Material of the item
    * @param amount 1 to 64
    * @param name - DisplayName (use & for chat color)
    * @param lore - Use Arrays.asList() (use & for chat color)
    * @return New ItemStack object
    */
    public static ItemStack generateItemStack(Material material, int amount, String name, List<String> lore) {
    ItemStack item = new ItemStack(material, amount);
    ItemMeta meta = item.getItemMeta();
    meta.setDisplayName(Chat.colourize(name));
    if(lore != null) meta.setLore(Chat.colourize(lore));
    item.setItemMeta(meta);
    return item;
    }
    /**
    * Quickly create a new ItemStack object not having to go through the ItemMeta stuff
    * This method has a byte data parameter for special items (like glass panes)
    * @param material - Material of the item
    * @param amount - 1 to 64
    * @param data - Item data
    * @param name - DisplayName (use & for chat color)
    * @param lore - Use Arrays.asList() (use & for chat color)
    * @return New ItemStack object
    */
    public static ItemStack generateItemStack(Material material, int amount, byte data, String name, List<String> lore) {
    ItemStack item = new ItemStack(material, amount, (short) 0, data);
    ItemMeta meta = item.getItemMeta();
    meta.setDisplayName(Chat.colourize(name));
    if(lore != null) meta.setLore(Chat.colourize(lore));
    item.setItemMeta(meta);
    return item;
    }
     
    Any help is much appreciated!
     
  2. May I suggest XMaterial? You can then use: XMaterial.WHITE_STAINED_GLASS_PANE.parseItem() and it should return the correct itemstack in both 1.13 and 1.12
     
  3. Works like a charm! Thanks!
     

Share This Page