Solved Custom Inventory Error

Discussion in 'Plugin Development' started by ActivisionDevReloaded, Jul 26, 2014.

Thread Status:
Not open for further replies.
  1. Ok when you open the inventory and get a item it works perfectly but it someone is looking in the inventory and moves stuff in their own inventory it gives errors in the console. Also players can put items in the custom inventory anyone know a fix?

    Here's some of the code:

    Code:java
    1. @EventHandler
    2. public void onInventoryClick(InventoryClickEvent e) {
    3. Player player = (Player) e.getWhoClicked();
    4. this.cfg = Core.config;
    5. if (!e.getInventory().getName().equalsIgnoreCase(this.inv.getTitle())) {
    6. return;
    7. }
    8. if (e.getCurrentItem().getItemMeta() == null) {
    9. return;
    10. }
    11. if (e.getCurrentItem().getItemMeta().getDisplayName()
    12. .contains("Diamond Helmet")) {
    13. e.setCancelled(true);
    14. if(cfg.getInt("players." + player.getName() + ".points") < 10) {
    15. player.playSound(player.getLocation(), Sound.ANVIL_LAND, 1, 1);
    16. player.sendMessage("§cYou can't afford this item!");
    17. } else {
    18. this.cfg.set("players." + player.getName() + ".points", Integer.valueOf(this.cfg.getInt("players." + player.getName() + ".points") - 10));
    19. ItemStack helm = new ItemStack(Material.DIAMOND_HELMET);
    20. Enchantment enchantment = new EnchantmentWrapper(0);
    21. helm.addEnchantment(enchantment, 2);
    22. player.getInventory().addItem(helm);
    23. player.playSound(player.getLocation(), Sound.NOTE_PLING,
    24. 1.0F, 1.0F);
    25. player.sendMessage("§aYou have just purchased a Diamond Helmet!");
    26. Core.savePointsConfig();
    27. }
    28. }
    29. }


    Also heres the errors in the console:
    Code:
    [01:53:23 ERROR]: Could not pass event InventoryClickEvent to ProdigyShops v1.0 org.bukkit.event.EventException                                                        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:294) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                  at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                  at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                  at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:1364) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                    at net.minecraft.server.v1_7_R3.PacketPlayInWindowClick.a(SourceFile:32) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                          at net.minecraft.server.v1_7_R3.PacketPlayInWindowClick.handle(SourceFile:10) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                    at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                        at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                                at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                      at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                      at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                      at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                    at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]              Caused by: java.lang.NullPointerException                                              at com.nickyc27.prodigyshops.shops.StarterShop.onInventoryClick(StarterShop.java:206) ~[?:?]                                                                    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_65]                                                                                    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_65]                                                                                    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_65]                                                                                at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_65]                at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:292) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                  ... 13 more                                                            >                                                                              
     
  2. Offline

    Niknea

  3. Offline

    SkyleTyler1337

    InventoryDragEvent?
     
  4. Offline

    fireblast709

    ActivisionDevReloaded cfg can be null, getDisplayName can be null, why the useless Integer wrapping, why the EnchantmentWrapper
     
  5. fireblast709

    The cfg is not null, I initialized it on line 4. It says getDisplayName.contains("Diamond Helmet"), And the enchantment things is for the player to get the item they bought with the enchantment... I don't see any of what you said useful. Not to be mean but your answers are all in the code. I'm asking for a fix for the errors and im pretty sure those things aren't thing problem since they should be all initialized.

    That is the entire stacktrace .

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  6. Offline

    Niknea

  7. Niknea
    Move the little gray box on the botttom to see more.
     
  8. Offline

    fireblast709

    ActivisionDevReloaded you assign Core.config to it, which can be null at that point. As I said before, getDisplayName() can be null, thus calling contains() will throw a NullPointerException if so. You should -not- use the EnchantmentWrapper class, use the constants found in Enchantment. Lastly, that is not the full stacktrace. Check your logs, you will find the full stacktrace there. Especially the 'Caused by...', which is likely to be present, will tell you a lot about the error in combination with the lines below it.

    The only things that do not help you solve the issues would be the remarks about the Integer wrapping and the Enchantments (though they are things you should change)
     
  9. Offline

    xTigerRebornx

  10. Offline

    Konkz

    What is line 206?
    Code:
    Caused by: java.lang.NullPointerException
    at com.nickyc27.prodigyshops.shops.StarterShop.onInventoryClick(StarterShop.java:206) ~[?:?]
     


  11. Here is your pastebin with the stacktrace:

    http://pastebin.com/u4KXR71K


    Integer wrapping isn't the issue since its been happening before that. The integer wrapping works fine so leave it at that. Core.config is initialized in the class and the main class (if you need the Core class just ask). Explain more on the getDisplayName().

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  12. Offline

    fireblast709

    ActivisionDevReloaded getDisplayName() returns null if the ItemStack doesn't have a display name (which is set by a call to ItemMeta#setDisplayName(String) followed by a ItemStack#setItemMeta(ItemMeta)).

    The Integer wrap is just redundant (since you already have an int) and looks a bit messy imo.

    xTigerRebornx didn't notice the small grey scrolling bar xD (I'm on my phone atm)
     
  13. fireblast709

    I'm pretty sure the error is coming from the getDisplayName() do you have a fix?
     
  14. Offline

    fireblast709

Thread Status:
Not open for further replies.

Share This Page