Help with calling items with names

Discussion in 'Plugin Development' started by KaiPol, Dec 21, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    KaiPol

    Hello! I have a small problem while trying to call an item with a name
    Here's my code checking it:
    Code:java
    1. ItemStack diamond = new ItemStack(Material.DIAMOND);
    2. if(diamond.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GOLD + "Upgrade Amour") || diamond.getItemMeta().getLore().equals(ChatColor.DARK_RED + "Upgrade level 1")){
    3. player.sendMessage("hi");

    and here's my code setting the display name and adding it to the gui(The gui shouldn't be important)
    Code:java
    1. ItemStack diamond = new ItemStack(Material.DIAMOND);
    2. ItemMeta diamondMeta = diamond.getItemMeta();
    3. diamondMeta.setDisplayName(ChatColor.GOLD + "Upgrade Amour");
    4. diamondMeta.setLore(Arrays.asList(ChatColor.DARK_RED + "Upgrade level 1"));
    5. diamond.setItemMeta(diamondMeta);
    6. inventory.addItem(diamond);

    This is the error:
    Code:java
    1. [07:52:34 ERROR]: Could not pass event InventoryClickEvent to Hunter v1.7.2
    2. org.bukkit.event.EventException
    3. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    4. va:427) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    5. at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    6. a:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    7. at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    8. ava:481) [craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    9. at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    10. ava:466) [craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    11. at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java
    12. :1348) [craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    13. at net.minecraft.server.v1_7_R1.PacketPlayInWindowClick.a(SourceFile:32)
    14. [craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    15. at net.minecraft.server.v1_7_R1.PacketPlayInWindowClick.handle(SourceFil
    16. e:10) [craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    17. at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146
    18. ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    19. at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craf
    20. tbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    21. at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    22. 55) [craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    23. at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    24. 50) [craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    25. at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    26. 45) [craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    27. at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    28. :457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    29. at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    30. 17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    31. Caused by: java.lang.NullPointerException
    32. at me.KaiPol.Hunter.Hunter.inventoryclick(Hunter.java:282) ~[?:?]
    33. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    34. _12-ea]
    35. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    36. _12-ea]
    37. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    38. .7.0_12-ea]
    39. at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_12-ea]
    40. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    41. va:425) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    42. ... 13 more
    43. [07:52:40 ERROR]: Could not pass event InventoryClickEvent to Hunter v1.7.2
    44. org.bukkit.event.EventException
    45. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    46. va:427) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    47. at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    48. a:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    49. at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    50. ava:481) [craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    51. at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    52. ava:466) [craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    53. at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java
    54. :1348) [craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    55. at net.minecraft.server.v1_7_R1.PacketPlayInWindowClick.a(SourceFile:32)
    56. [craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    57. at net.minecraft.server.v1_7_R1.PacketPlayInWindowClick.handle(SourceFil
    58. e:10) [craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    59. at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146
    60. ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    61. at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craf
    62. tbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    63. at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    64. 55) [craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    65. at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    66. 50) [craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    67. at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    68. 45) [craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    69. at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    70. :457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    71. at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    72. 17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    73. Caused by: java.lang.NullPointerException
    74. at me.KaiPol.Hunter.Hunter.inventoryclick(Hunter.java:282) ~[?:?]
    75. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    76. _12-ea]
    77. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    78. _12-ea]
    79. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    80. .7.0_12-ea]
    81. at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_12-ea]
    82. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    83. va:425) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.1-b2969jnks]
    84. ... 13 more
     
  2. Offline

    XvBaseballkidvX

    Do this when checking:
    Code:java
    1. if(player.getItemInHand().getItemMeta == null) return;
    2. if(player.getItemInHand().getItemMeta().equals(diamond.getItemMeta()){
    3. //Do stuff
    4. }
     
  3. Offline

    KaiPol

    XvBaseballkidvX
    I don't think this will work because I applied the ItemMeta in another Event.
     
  4. Offline

    XvBaseballkidvX

    Hmm, if you are going to need to access items in other classes, you might want to make an Items Class:
    Code:java
    1. public class Items {
    2.  
    3. //Creating an ItemStack
    4. public static ItemStack example;
    5.  
    6. //IMPORTANT: Make sure on your onEnable method you do this: Items.registerItems(); or else the items wont be created
    7. public static void registerItems(){
    8.  
    9. example = setData(new ItemStack(Material.SNOW_BALL), "Example", Arrays.asList(
    10. "Merry Christmas!"));
    11.  
    12. }
    13.  
    14. //A method for setting ItemMeta
    15.  
    16. public static ItemStack setData(ItemStack item, String name, List<String> lore){
    17. ItemMeta m = item.getItemMeta();
    18. m.setDisplayName(name);
    19. m.setLore(lore);
    20. item.setItemMeta(m);
    21.  
    22. return item;
    23.  
    24. }
    25. }


    Then in other classes you will be able to do this:
    Code:java
    1. if(player.getItemInHand().getItemMeta() == null) return;
    2. if(player.getItemInHand().getItemMeta().equals(Items.example.getItemMeta()){
    3. player.sendMessage("Have a nice Christmas Break!"); ^^Right here we get 'examples' item meta :D
    4. }
     
  5. Offline

    KaiPol

    Thank you for this, but after posting my post I started thinking WHY it wasn't in the same class lol.
     
    XvBaseballkidvX likes this.
Thread Status:
Not open for further replies.

Share This Page