ItemMeta Null in 1.11.2

Discussion in 'Spigot Plugin Development' started by woarth, Apr 21, 2017.

  1. Hi people, i have been doing my best trying to know why I have this error but I cant make it work. I have checked spigot forums, but couldnt find a solution for me. I am making a plugin which you have a custom item which makes special things in your hotbar, and i am having problems with the Drop listener.

    This error is only happening in spigot version 1.11.2, in other versions it works perfect. When i try to drop that item, it throws this error:

    Code (Text):
    [00:20:01 ERROR]: Could not pass event PlayerDropItemEvent to PingPlugin v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-54ec0b8-be9ef98]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-54ec0b8-be9ef98]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-54ec0b8-be9ef98]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-54ec0b8-be9ef98]
            at net.minecraft.server.v1_11_R1.EntityHuman.a(EntityHuman.java:581) [spigot.jar:git-Spigot-54ec0b8-be9ef98]
            at net.minecraft.server.v1_11_R1.EntityHuman.a(EntityHuman.java:534) [spigot.jar:git-Spigot-54ec0b8-be9ef98]
            at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:805) [spigot.jar:git-Spigot-54ec0b8-be9ef98]
            at net.minecraft.server.v1_11_R1.PacketPlayInBlockDig.a(SourceFile:40) [spigot.jar:git-Spigot-54ec0b8-be9ef98]
            at net.minecraft.server.v1_11_R1.PacketPlayInBlockDig.a(SourceFile:10) [spigot.jar:git-Spigot-54ec0b8-be9ef98]
            at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-54ec0b8-be9ef98]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_121]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_121]
            at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [spigot.jar:git-Spigot-54ec0b8-be9ef98]
            at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:747) [spigot.jar:git-Spigot-54ec0b8-be9ef98]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-54ec0b8-be9ef98]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot.jar:git-Spigot-54ec0b8-be9ef98]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot.jar:git-Spigot-54ec0b8-be9ef98]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_121]
    Caused by: java.lang.NullPointerException
            at me.woarth.PingPlugin.Listeners.DropListener.onPlayerDrop(DropListener.java:207) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_121]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_121]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_121]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_121]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot.jar:git-Spigot-54ec0b8-be9ef98]
            ... 17 more

    And this is the code:

    Code (Text):
          @SuppressWarnings("deprecation")
        @EventHandler
          public void onPlayerDrop(PlayerDropItemEvent e) {
                Player p = e.getPlayer();
                if (p.getItemInHand().getItemMeta().hasDisplayName()) {
                if (p.getItemInHand().getItemMeta().getDisplayName().equals(this.plugin.colorize(this.plugin.getConfig().getString("Pingitemname")))) {
                      e.setCancelled(true);
                  return;
                }
                }
          }
    Code (Text):
                if (p.getItemInHand().getItemMeta().hasDisplayName()) {

    Code (Text):
      public void PingItem (Player p) {
        ItemStack PingItem = new ItemStack(Material.getMaterial(Main.plugin.getConfig().getString("Pingitemmaterial").toUpperCase()));
        ItemMeta PingItemMeta = PingItem.getItemMeta();
        PingItemMeta.setDisplayName(Main.plugin.colorize(Main.plugin.getConfig().getString("Pingitemname")));
        PingItemMeta.addEnchant(Enchantment.DURABILITY, 1, true);
        ArrayList<String> PingItemItemLore = new ArrayList<String>();
        PingItemItemLore.add("§7Ping Item");
        PingItemMeta.setLore(PingItemItemLore);
        PingItem.setItemMeta(PingItemMeta);
        p.getInventory().setItem(0, PingItem);
        p.updateInventory();
      }

    I would really appreciate your help. For those who will recommend me this, if i add:
    Code (Text):
                    if (p.getItemInHand() == null || p.getItemInHand().getType() == Material.AIR)
                          return;
    to the code, everything will return false, because i dont know why in the 1.11.2 that item meta is null.

    Thanks a lot! If you dont understand something, please tell me!
     
  2. What is line 207 of DropListener.java



    Sent from my iPhone using Tapatalk
     
  3. Try to use player.getInventory().getItemInMainHand()
     
  4. electronicboy

    IRC Staff

    if that is returning instead of doing anything, that is your issue with your current code: If the item in the players hand is null, or if the material is Air (which is impossible), that means that the player has nothing in their hand. (Air is also the only instance where ItemMeta itself will be null (last I checked))
    This event is also fired from the inventory screen, so you're assuming that the player actually has an item in their hand at the time this is fired, which might not be the case. The Item entity for the drop in question is also exposed in the event, no need to access the players inventory directly.
     
  5. But the player has an item, and its the ping item i showed you in the code. So that is why it has no sense. This just happens in 1.11.2, it says the item is null when it is not and it is not air, its the same item used in old minecraft versions.
     
  6. Same error. Item is null, i dont know why
     
  7. You should be getting the item they drop from e.getItem(), not from item in hand. That will be null because they're dropping it and technically at this point in the code the item has been dropped.

    If you cancel the event the item would be back in their hand.

    And furthermore you need to check if e.getItem().hasMetaData BEFORE you check hasDisplayName() otherwise you'll get a null pointer exception because items that have not had their display name set (every item naturally spawned) will have null for itemMeta

    Sent from my iPhone using Tapatalk
     
    #7 dNiym, Apr 21, 2017
    Last edited: Apr 21, 2017
  8. I did what you say, and the item is still null. Nothing happens, and if i remove the hasItemMeta, i have the null error in console.

    Code (Text):
                    if (e.getItemDrop().getItemStack().hasItemMeta())
                          return;
                        if (e.getItemDrop().getItemStack().getItemMeta().hasDisplayName()) {
                        if (e.getItemDrop().getItemStack().getItemMeta().getDisplayName().equals(this.plugin.colorize(this.plugin.getConfig().getString("Pingitemname")))) {
                            e.setCancelled(true);
                          return;
                        }
                        }
     
  9. Then there is no item being dropped, or that item has no meta.

    An item will ONLY have meta if something sets it, meaning a

    ItemStack is = new ItemStack(Material.DIAMOND,1)

    Is will have no meta at all. Unless you do ItemMeta Im = is.getItemMeta();
    Im.setDisplayName("test item");
    Is.setItemMeta(im);

    THEN has ItemMeta will return true and getDisplayName() will return the display name and not null.



    Sent from my iPhone using Tapatalk
     
  10. The null check should probably never fire unless something else is removing the item before it fires and not cancelling the event.


    Sent from my iPhone using Tapatalk
     
  11. Yes but the problem is that the item has ItemMeta:

    Code (Text):
      public void PingItem (Player p) {
        ItemStack PingItem = new ItemStack(Material.getMaterial(Main.plugin.getConfig().getString("Pingitemmaterial").toUpperCase()));
        ItemMeta PingItemMeta = PingItem.getItemMeta();
        PingItemMeta.setDisplayName(Main.plugin.colorize(Main.plugin.getConfig().getString("Pingitemname")));
        PingItemMeta.addEnchant(Enchantment.DURABILITY, 1, true);
        ArrayList<String> PingItemItemLore = new ArrayList<String>();
        PingItemItemLore.add("§7Ping Item");
        PingItemMeta.setLore(PingItemItemLore);
        PingItem.setItemMeta(PingItemMeta);
        p.getInventory().setItem(0, PingItem);
        p.updateInventory();
      }
    And the strange thing is that it only happens in 1.11.2
     
  12. Have you verified that the data is actually getting set? As in read from the config?

    Add a couple lines to output the display name and item type to confirm it.

    You need to add some debug statements to your code, then drop some items and post the output, the only things I see wrong are you not checking if the item is null and if the meta exists.


    Sent from my iPhone using Tapatalk
     
  13. Yes the data is set, because that item generated has the display name, the enchantment and the lore. And again, it just happens in 1.11.2 so the problem is not the code :( I should add the checkers to see if its meta is null, but in this case, the meta shouldnt be null and it is.
     
  14. electronicboy

    IRC Staff

    try printing out information from the itemstack to the logger, if hasItemMeta (and is not throwing an NPE) is causing your code to not be executed, than it seems clear that the ItemStack you're playing with apparently doesn't have it's IM attached.

    You should be working out *WHY* a method like hasItemMeta isn't returning true when you want it to return true, instead of just trying to dismiss it all together.
     
  15. And how can i do that? Because the same system works well in a PlayerInteractEvent, but not in the PlayerDropItemEvent
     
  16. I agree with eb here just switching to 1.11 shouldn't cause much issues with this type of thing but if you were getting the dropped item from player in hand instead of event.getItem() I can see why that could cause you an issue.


    Sent from my iPhone using Tapatalk
     
  17. getItem gives me erorrs, and getItemDrop or getItemInHand trows me null. So i dont know what to do. I have tried everything you told me but keeps happening the same