Solved Inventory not opening

Discussion in 'Plugin Development' started by PieMan456, Nov 17, 2013.

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

    PieMan456

    the_merciless
    Command works but not right clicking custom book.

    the_merciless
    This error pops up
    Code:
    2013-11-21 17:01:08 [SEVERE] Could not pass event PlayerInteractEvent to CustomInv v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
        at org.bukkit.craftbukkit.v1_6_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:190)
        at org.bukkit.craftbukkit.v1_6_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:160)
        at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:1008)
        at net.minecraft.server.v1_6_R3.Packet18ArmAnimation.handle(SourceFile:41)
        at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
        at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
        at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
        at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
        at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
        at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
        at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
        at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.NullPointerException
        at me.pieman.custominventory.CustomInventory.onPlayerInteract(CustomInventory.java:112)
        at sun.reflect.GeneratedMethodAccessor5.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
        ... 16 more
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  2. Offline

    the_merciless

    Caused by: java.lang.NullPointerException
    at me.pieman.custominventory.CustomInventory.onPlayerInteract(CustomInventory.java:112)

    What do you have on line 112?

    Are you trying to open the inventory if the player is holding the book and clicking?

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

    PieMan456

    I replaced
    Code:java
    1. if (e.getClickedBlock().hasMetadata("Worlds")) return;
    to
    Code:java
    1. if (!(e.getClickedBlock().hasMetadata(ChatColor.BLUE + "" + ChatColor.BOLD + "Worlds")))
    There is no error but it still does not work.
     
  4. Offline

    the_merciless

    show me your whole interact event
     
  5. Offline

    PieMan456

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e) {
    3. if (!(e.getAction() == Action.LEFT_CLICK_AIR)
    4. && !(e.getAction() == Action.LEFT_CLICK_BLOCK))
    5. return;
    6. if (!(e.getClickedBlock().hasMetadata(ChatColor.BLUE + "" + ChatColor.BOLD + "Worlds")))
    7. return;
    8. if (e.getClickedBlock().hasMetadata("Worlds")) {
    9. openCustomInventory(e.getPlayer());
    10. }
    11. }
     
  6. Offline

    the_merciless

     
    PieMan456 likes this.
  7. Offline

    PieMan456

  8. Offline

    the_merciless

    Code:
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
           
            if (e.getAction() == Action.RIGHT_CLICK_AIR){
                if (e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("Worlds")){
                    openCustomInventory(e.getPlayer());
                }
            }   
        }
     
  9. Offline

    PieMan456

    the_merciless
    Ok so I have this but it still does not work.
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e) {
    3. if(e.getPlayer().getItemInHand() == null) return;
    4. if(e.getPlayer().getItemInHand() != new ItemStack(Material.ENCHANTED_BOOK)) return;
    5. if(e.getAction() == Action.RIGHT_CLICK_AIR && e.getAction() == Action.RIGHT_CLICK_BLOCK){
    6. if(e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("World Teleporter")){
    7. openCustomInventory(e.getPlayer());
    8. }
    9. }
    10. }
     
  10. Offline

    JRL1004

    PieMan456 You are cancelling out of the event when the player right clicks
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e) {
    3. if (!(e.getAction() == Action.LEFT_CLICK_AIR)
    4. && !(e.getAction() == Action.LEFT_CLICK_BLOCK))
    5. return;

    If you want to make it right-click only it should be like this
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e) {
    3. if (e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK)
    4. return;


    EDIT: Now you are checking to see the the player is right clicking air and a block. replace the && with || in the check
     
  11. Offline

    PieMan456

    JRL1004
    This is what I have will this work?
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e) {
    3. if(e.getPlayer().getItemInHand() == null) return;
    4. if(e.getPlayer().getItemInHand() != new ItemStack(Material.ENCHANTED_BOOK)) return;
    5. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
    6. if(e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("World Teleporter")){
    7. openCustomInventory(e.getPlayer());
    8. }
    9. }
    10. }
     
  12. Offline

    JRL1004

    PieMan456 That should work assuming the player is holding an enchanted book named "World Teleporter" but keep in mind that if you anvil the object the name becomes italicized and it may not work
     
  13. Offline

    PieMan456

    JRL1004
    Does it matter if the DisplayName is blue and bold or do I have to put that in so it recongnizes it because it will not work.

    Edit: I tried it putting in the bold and blue and it still does not work.
     
  14. Offline

    JRL1004

    PieMan456 The DisplayName is very sensitive. If you plan to have the item using a colored name you will have to use the colored name (as a string) in the method. For example if the item were Purple_Pickaxe, you would have to check for "§5Purple_Pickaxe" since § plus another character creates a color.
     
  15. Offline

    PieMan456

    JRL1004
    Can I use the ChatColor.Blue thing or does that nor work?
     
  16. Offline

    JRL1004

    PieMan456 I have not tried to use ChatColor in names so I could not tell you that. Maybe the_merciless knows.
     
  17. Offline

    PieMan456

    JRL1004
    When I try and put the weird thing in it comes up red
     
  18. Offline

    JRL1004

    PieMan456 The weird thing? I assume you are referring to the "§" and that you mean the you are most likely not using it in the name of the item as a string. The § symbol is not different from any other (for the most part) and should be treated as all other symbols (like a, b, c, etc...) in a string.
     
  19. Offline

    PieMan456

    JRL1004
    It still does not work. What am I doing wrong?
    Whole Class:
    Code:java
    1. package me.pieman.custominventory;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Location;
    8. import org.bukkit.Material;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.block.Action;
    15. import org.bukkit.event.inventory.InventoryClickEvent;
    16. import org.bukkit.event.player.PlayerInteractEvent;
    17. import org.bukkit.event.player.PlayerJoinEvent;
    18. import org.bukkit.inventory.Inventory;
    19. import org.bukkit.inventory.ItemStack;
    20. import org.bukkit.inventory.meta.ItemMeta;
    21. import org.bukkit.plugin.java.JavaPlugin;
    22.  
    23. public class CustomInventory extends JavaPlugin implements Listener {
    24. public void onEnable() {
    25. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    26. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    27. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    28. }
    29.  
    30. public void onDisable() {
    31.  
    32. }
    33.  
    34. @SuppressWarnings("deprecation")
    35. public void openCustomInventory(Player p) {
    36. Inventory CustomInv = Bukkit.createInventory(null, 9, "Worlds");
    37.  
    38. ItemStack pvp = new ItemStack(Material.DIAMOND_SWORD);
    39. ItemMeta pvpMeta = pvp.getItemMeta();
    40. ArrayList<String> pworld = new ArrayList<String>();
    41. pvpMeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD
    42. + "PVPArena World");
    43. pworld.add(ChatColor.DARK_PURPLE
    44. + "Click this to teleport to the PVPArena World!");
    45. pvpMeta.setLore(pworld);
    46. pvp.setItemMeta(pvpMeta);
    47.  
    48. ItemStack survival = new ItemStack(Material.GRASS);
    49. ItemMeta survivalMeta = survival.getItemMeta();
    50. ArrayList<String> sworld = new ArrayList<String>();
    51. survivalMeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD
    52. + "Survival World");
    53. sworld.add(ChatColor.DARK_PURPLE
    54. + "Click this to teleport to the Survival World!");
    55. survivalMeta.setLore(sworld);
    56. survival.setItemMeta(survivalMeta);
    57.  
    58. ItemStack mg = new ItemStack(Material.REDSTONE_BLOCK);
    59. ItemMeta mgMeta = mg.getItemMeta();
    60. ArrayList<String> mgworld = new ArrayList<String>();
    61. mgMeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD
    62. + "Minigame World");
    63. mgworld.add(ChatColor.DARK_PURPLE
    64. + "Click this to teleport to the Minigame World!");
    65. mgMeta.setLore(mgworld);
    66. mg.setItemMeta(mgMeta);
    67.  
    68. CustomInv.clear();
    69. CustomInv.setItem(1, new ItemStack(pvp));
    70. CustomInv.setItem(4, new ItemStack(mg));
    71. CustomInv.setItem(7, new ItemStack(survival));
    72.  
    73. p.openInventory(CustomInv);
    74.  
    75. p.updateInventory();
    76. }
    77.  
    78. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    79. if(!(sender instanceof Player)) return true;
    80. Player p = (Player) sender;
    81. if(label.equalsIgnoreCase("worldtp")){
    82. if(p.hasPermission("ci.open")){
    83. openCustomInventory(p);
    84. }
    85. }
    86.  
    87. return true;
    88. }
    89.  
    90. @EventHandler
    91. public void onPlayerJoin(PlayerJoinEvent e) {
    92. ItemStack book = new ItemStack(Material.ENCHANTED_BOOK);
    93. ItemMeta bookMeta = book.getItemMeta();
    94. ArrayList<String> book1 = new ArrayList<String>();
    95. book1.add(ChatColor.RED + "Click this to open the world teleporter!");
    96. bookMeta.setLore(book1);
    97. bookMeta.setDisplayName("§1" + "§l" + "World Teleporter");
    98. book.setItemMeta(bookMeta);
    99. if (e.getPlayer().getInventory().firstEmpty() == -1) {
    100. return;
    101. } else {
    102. Player p = (Player) e.getPlayer();
    103. p.getInventory().setItem(8, new ItemStack(book));
    104. }
    105. }
    106.  
    107. @EventHandler
    108. public void onPlayerInteract(PlayerInteractEvent e) {
    109. if(e.getPlayer().getItemInHand() == null) return;
    110. if(e.getPlayer().getItemInHand() != new ItemStack(Material.ENCHANTED_BOOK)) return;
    111. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
    112. if(e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("§1" + "§l" + "World Teleporter")){
    113. openCustomInventory(e.getPlayer());
    114. }
    115. }
    116. }
    117.  
    118. @EventHandler
    119. public void onInventoryClick(InventoryClickEvent e) {
    120. if (!(e.getInventory().getName().equalsIgnoreCase("World Teleporter")))
    121. return;
    122. if (e.getCurrentItem().getItemMeta().getDisplayName()
    123. .contains("PVPArena World")) {
    124. e.setCancelled(true);
    125. e.getWhoClicked().teleport(
    126. new Location(Bukkit.getWorld("world"), 49, 88, -206));
    127. }
    128. if (e.getCurrentItem().getItemMeta().getDisplayName()
    129. .contains("Survival World")) {
    130. e.setCancelled(true);
    131. e.getWhoClicked().teleport(
    132. new Location(Bukkit.getWorld("Hub"), 0, 66, 0));
    133. }
    134. if (e.getCurrentItem().getItemMeta().getDisplayName()
    135. .contains("Minigame World")) {
    136. e.setCancelled(true);
    137. e.getWhoClicked().teleport(
    138. new Location(Bukkit.getWorld("minigames"), -0.5, 66, -6.5));
    139. }
    140. }
    141. }
    142.  
     
  20. Offline

    JRL1004

    PieMan456 You are making an inventory with the name of "Worlds" and checking if it's name matches "World Teleporter"
     
  21. Offline

    PieMan456

    JRL1004
    Yeah I fixed that but it still does not open.
     
  22. Offline

    JRL1004

    PieMan456 Give me a moment while I start Eclipse since reading the code:java boxes is a pain. I'll edit this in a moment.

    Edit here is your class after I cut it down to the base problem and how I went about doing it:
    Code:java
    1. public class CustomInventory extends JavaPlugin implements Listener {
    2. String title = "World Teleporter";
    3. Inventory CustomInv = Bukkit.createInventory(null, 9, title);
    4.  
    5. // Defined the inventory and its name early //
    6.  
    7. public void onEnable() {
    8. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    9. // Removed unnecessary registerEvents //
    10. }
    11.  
    12. public void openCustomInventory(Player p) {
    13. CustomInv = Bukkit.createInventory(null, 9, "Worlds");
    14. // Cropped method down to just remaking the inventory //
    15. }
    16.  
    17. // Removed onCommand as it was not the problem //
    18.  
    19. // Removed PlayerJoinEvent as it was not the problem //
    20.  
    21. // Removed PlayerInteractEvent as it was not the problem //
    22.  
    23. @EventHandler
    24. public void onInventoryClick(InventoryClickEvent e) {
    25. if (!(e.getInventory().getName().equalsIgnoreCase(CustomInv.getName())))
    26. return;
    27. // Changed check so it does not have to have a string input but instead
    28. // will call the inventory //
    29. }
    30. }
    31.  

    This is just barebones. I am not by any means asking you to delete your entire class
     
  23. Offline

    PieMan456

    JRL1004
    The command works fine but the book does not open at all.
     
  24. Offline

    JRL1004

    PieMan456 I wasn't aware of the book issue. I'll go and look into that as well. I also posted a few changes (some may not be needed) that could help you as an edit to my last post.

    PieMan456 Okay, I've gone through your inventory method and I couldn't fine much that would cause a problem. Here is what I have done to (I forgot what I changed so I can't explain the edits):
    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerInteract(PlayerInteractEvent e) {
    4. Player p = e.getPlayer();
    5. if(p.getItemInHand() == null) return;
    6. if(p.getItemInHand() != new ItemStack(Material.ENCHANTED_BOOK)) return;
    7. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
    8. if(p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("§1§lWorld Teleporter")){
    9. // Player right clicked with an Enchanted Book called "World Teleporter" (the name will be bold and blue)
    10. // Do stuff
    11. }
    12. }
    13. }


    EDIT: I'm was being an idiot and tahg'd myself, whoops

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

    PieMan456

    JRL1004
    Well it still does not work. Man what the heck do I need to do?
     
  26. Offline

    JRL1004

    PieMan456 Backtrack trough the code. If the code wants an enchanted book, you have to make sure that is what you are holding. If the book is named (with or without colors) you need to make sure the names match exactly. I would recommend adding a few debug lines to tell you what is or is not calling then removing or commenting them out once you have isolated the issue.
     
  27. Offline

    PieMan456

    JRL1004
    Ok so I have this:
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e) {
    3. Player p = e.getPlayer();
    4. if(p.getItemInHand() == null) return;
    5. System.out.println("Works");
    6. if(p.getItemInHand() != new ItemStack(Material.ENCHANTED_BOOK)) return;
    7. System.out.println("Working");
    8. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
    9. System.out.println("Still Working");
    10. if(p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("§1§lWorld Teleporter")){
    11. System.out.println("Works!");
    12. openCustomInventory(e.getPlayer());
    13. }
    14. }
    15. }


    And in the console it prints out "Works" 3 times which is weird.
     
  28. Offline

    JRL1004

    PieMan456 If it works all three times that means your openCustomInventory method is the issue. Also, I would change this: if(p.getItemInHand()!=new ItemStack(Material.ENCHANTED_BOOK))return;
    to be this: if(p.getItemInHand().getType()!=Material.ENCHANTED_BOOK)return; so that it supports stacks of enchant books.
     
  29. Offline

    PieMan456

    JRL1004
    No. It prints out the first Works 3 times. Not the first 3. Just Works 3 times.
     
  30. Offline

    JRL1004

    PieMan456 That would be because you are registering the event three times so your going to get three times the output...
     
Thread Status:
Not open for further replies.

Share This Page