Getting DisplayName from a Renamed item

Discussion in 'Spigot Plugin Development' started by x313, Apr 27, 2017.

  1. Hello, I am trying to make a BanManeger where you can type a custem time in an anvil.
    It works like this:
    • You click on "select time"
    • There openes a virtual anvil
    • You type the time in the rename bar
    • You click the renamed item and it will send you back. It will also save your typed time in a file
    virtual Anvil:
    Code (Text):
        public void SelectTimeBan(Player player, String name, boolean online){
           
            Inventory inv = Bukkit.createInventory(null, InventoryType.ANVIL);
           
            ItemStack Time = new ItemStack(Material.WATCH);
            ItemMeta TimeMeta = Time.getItemMeta();
            TimeMeta.setDisplayName(" ");
             ArrayList<String> TimeLore = new ArrayList<String>();
             TimeLore.add(ChatColor.BLACK + "" + ChatColor.MAGIC + name);
             TimeLore.add(ChatColor.RED + "Go Back!");
            TimeMeta.setLore(TimeLore);
            Time.setItemMeta(TimeMeta);
            inv.setItem(0, Time);
           
            player.openInventory(inv);
           
           
        }
    Anvil click event:
    Code (Text):
        @EventHandler
           public void onTimeClick(InventoryClickEvent event) {

               Player player = (Player) event.getWhoClicked();
               if(event.getInventory().getType() == InventoryType.ANVIL){
                if(event.getCurrentItem().getType() == Material.WATCH){
                        String DisplayName = ChatColor.stripColor(event.getCurrentItem().getItemMeta().getDisplayName());
                        String naam = ChatColor.stripColor(event.getCurrentItem().getItemMeta().getLore().get(0));
                        Boolean online = getServer().getOfflinePlayer(naam).isOnline();
                        if(DisplayName.equalsIgnoreCase(" ")){
                            openBan(player, naam, online);
                            event.setCancelled(true);
                            return;
                        }
                            times.remove(naam);
                            times.put(naam, DisplayName);
                            openBan(player, naam, online);
                            event.setCancelled(true);
                            return;
                   
                    }
                    }
            }
    Code (Text):
    27.04 08:20:26 [Server] INFO ... 15 more
    27.04 08:20:26 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[Spigot.jar:git-Spigot-d276ab1-d219213]
    27.04 08:20:26 [Server] INFO at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_121]
    27.04 08:20:26 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_121]
    27.04 08:20:26 [Server] INFO at sun.reflect.GeneratedMethodAccessor136.invoke(Unknown Source) ~[?:?]
    27.04 08:20:26 [Server] INFO at me.x313.PlayerStats.PlayerStats.onTimeClick(PlayerStats.java:2102) ~[?:?]
    27.04 08:20:26 [Server] INFO Caused by: java.lang.NullPointerException
    27.04 08:20:26 [Server] INFO at java.lang.Thread.run(Unknown Source) [?:1.8.0_121]
    27.04 08:20:26 [Server] INFO at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [Spigot.jar:git-Spigot-d276ab1-d219213]
    27.04 08:20:26 [Server] INFO at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [Spigot.jar:git-Spigot-d276ab1-d219213]
    27.04 08:20:26 [Server] INFO at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [Spigot.jar:git-Spigot-d276ab1-d219213]
    27.04 08:20:26 [Server] INFO at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:747) [Spigot.jar:git-Spigot-d276ab1-d219213]
    27.04 08:20:26 [Server] INFO at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [Spigot.jar:git-Spigot-d276ab1-d219213]
    27.04 08:20:26 [Server] INFO at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_121]
    27.04 08:20:26 [Server] INFO at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_121]
    27.04 08:20:26 [Server] INFO at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [Spigot.jar:git-Spigot-d276ab1-d219213]
    27.04 08:20:26 [Server] INFO at net.minecraft.server.v1_11_R1.PacketPlayInWindowClick.a(SourceFile:10) [Spigot.jar:git-Spigot-d276ab1-d219213]
    27.04 08:20:26 [Server] INFO at net.minecraft.server.v1_11_R1.PacketPlayInWindowClick.a(SourceFile:33) [Spigot.jar:git-Spigot-d276ab1-d219213]
    27.04 08:20:26 [Server] INFO at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1848) [Spigot.jar:git-Spigot-d276ab1-d219213]
    27.04 08:20:26 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [Spigot.jar:git-Spigot-d276ab1-d219213]
    27.04 08:20:26 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [Spigot.jar:git-Spigot-d276ab1-d219213]
    27.04 08:20:26 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[Spigot.jar:git-Spigot-d276ab1-d219213]
    27.04 08:20:26 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[Spigot.jar:git-Spigot-d276ab1-d219213]
    27.04 08:20:26 [Server] INFO org.bukkit.event.EventException
    27.04 08:20:26 [Server] ERROR Could not pass event InventoryClickEvent to PlayerStats v1.0

    If you click the renamed item it will give you this error and it wil open the virtual anvil again.
    Can someone please say whats going wrong?
     
  2. What kinda of material is a WATCH (Material.WATCH) ?
     
  3. It's... it's a watch... I know it sounds shocking, but trust me

    It's a bloody watch
     
    • Winner Winner x 2
    • Like Like x 1
  4. What is on line 2102?
     
  5. This is your error, so you need to show us line 2102 of your code (I hope you're using more than one class).
    Code (Text):
    27.04 08:20:26 [Server] INFO at me.x313.PlayerStats.PlayerStats.onTimeClick(PlayerStats.java:2102) ~[?:?]
    27.04 08:20:26 [Server] INFO Caused by: java.lang.NullPointerException


    Material.WATCH is a "clock" item in-game.
     
  6. Code (Text):
     2102           if(event.getCurrentItem().getType() == Material.WATCH){
    it is also in my first code
     
  7. try do this at line 2102:
    Code (Text):
    if(event.getCurrentItem().getType().equals(Material.WATCH)){
     
  8. Enums should be compared with ==, because they refer to the same object. A more detailed description is here: http://stackoverflow.com/a/7520464

    Did you check if event.getCurrentItem() is not null?
     
  9. I tryed it but it didn't work. it gives the same error as before
     
  10. no I didn't but when i check if the item is a watch it will automaticly check if the item is not null, will it?
     
  11. If you are trying to compare a null object to a watch, it will throw a NPE.
     
  12. try it like this maybe:
    event.getCurrentItem() == Material.WATCH
     
  13. I added it to my code and it gives the same error again :(
     
  14. did you try the one i suggested a few mins ago?
     
  15. BananaPuncher714

    Supporter

    That would always be false because an item stack is not a material.
    It would have to be:
    Code (Text):
    if ( e.getCurrentItem() != null && e.getCurrentItem().getType() == Material.WATCH ) {
        \\ do stuff here
     
  16. that is my first code and it doen't work :(
     
  17. BananaPuncher714

    Supporter

    Did you check to see if it's not null?
     
  18. Print in console e, then getCurrentItem, then getType to see what is null.
     
  19. WAS

    WAS

    You probably should always check to make sure an ItemStack#hasItemMeta() ** before going ahead and trying to access it, as well. Can't imagine when you do get to retrieving the displayname it would go over very well if there is no displayname set for the item.
     
  20. Try saving the clicked item as an ItemStack then checking if its the correct material:

    Code (Text):
    ItemStack clicked = new ItemStack(event.getCurrentItem());
    if (clicked.getType() == Material.WATCH) {
    //do stuff here
    }
    or at least that's how i do it.