1.15.2 im probably stupid but

Discussion in 'Spigot Plugin Development' started by lwccb, Jan 29, 2020.

?

Was this a stupid question

Poll closed Jan 31, 2020.
  1. Yes obviously

    5 vote(s)
    100.0%
  2. No rare case

    0 vote(s)
    0.0%
  1. can someone explain the fix for this im tired and need a fix for it

    https://pastebin.com/JF5r0KEf

    its also running spigot 1.15.2
    thankyou in advance
     
  2. You have inserted a null-value for a material. Without any code, we can't say what the issue is...

    We need your "Utils" class, especially createItem()-method
     
    • Like Like x 1
  3. i forgot to make pastebins of all the code sorry
     
  4. Material#matchMaterial(String) is marked nullable and can, therefore, return a null value. I would suggest an approach like this:
    Code (Java):
    public static ItemStack createItem(Inventory inv, String materialString, int amount, int invSlot, String displayName, String... loreString) {
         
            List<String> lore = new ArrayList();
            final Material material = Material.matchMaterial(materialString);
            if (material == null) {
                Bukkit.getLogger().log(Level.SEVERE, "cannot match \"" + material + "\" to material (in Utils#createItem())");
                return null;
            }
            ItemStack item = new ItemStack(material, amount);
         
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(Utils.chat(displayName));
            for (String s : loreString){
                lore.add(Utils.chat(s));
            }
            meta.setLore(lore);
            item.setItemMeta(meta);
            inv.setItem(invSlot - 1, item);
            return item;
        }
    You will then see instantly whenever such a wrong material got passed to the method.
     
  5. Also, some things that will improve your code:
    1) You have two times the same code. This means you have majorly duplicate code fragments, which is not good (for several reasons like bad maintenance, e.t.c). You can do something like this, which is way better:
    Code (Java):
    public static ItemStack createItem(Inventory inv, String materialString, int amount, int invSlot, String displayName, String... loreString) {
        return this.createItemByte(inv, materialString, 0, amound, invSlot, displayName, loreString);
    }
     
    @SuppressWarnings("deprecation")
    public static ItemStack createItemByte(Inventory inv, String materialString, int byteId, int amount, int invSlot, String displayName, String... loreString) {
       
        ItemStack item;
        @SuppressWarnings({ "rawtypes", "unchecked" })
        List<String> lore = new ArrayList();
       
        item = new ItemStack(Material.matchMaterial(materialString), amount, (short) byteId);
       
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName(Utils.chat(displayName));
        for (String s : loreString){
            lore.add(Utils.chat(s));
        }
        meta.setLore(lore);
        item.setItemMeta(meta);
        inv.setItem(invSlot - 1, item);
        return item;
    }
    2) Listen to your IDE. sure, sometimes it's annoying, but this shouldn't be suppressed:
    Code (Java):
    @SuppressWarnings({ "rawtypes", "unchecked" })
    List<String> lore = new ArrayList();
    If you have are coding in Java 8 (which I hope you do), you can simply do this:
    Code (Java):
    List<String> lore = new ArrayList<>();
    and the compiler can infer the arguments.

    3) Array to String conversion is something Java can do natively, you don't have to do that yourself:
    Just noticed that some formatting should be done before the conversion, it that case you could (but don't have to, it's just a nice one-liner) do:
    Code (Java):
    final List<String> lore = Arrays.stream(strings).map(Utils::chat).collect(Collectors.toList());
     
    4) It is not necessary to declare ItemStack before you use it.

    All of these suggestions make your code easier, safer to use and are less prone to errors, so I highly recommend you apply these.
     
    #5 Schottky, Jan 29, 2020
    Last edited: Jan 29, 2020
  6. i used a vid from YT and thats how it worked from 1.8 - 1.12.2
     
  7. You should not watch a youtuber that recommends code like this...
    Edit: also you don‘t even need String to Material. You should replace that String by the material itself and then use the enum constants...
     
    #7 Schottky, Jan 30, 2020
    Last edited: Jan 30, 2020