Problems with InventoryClickEvent

Discussion in 'Plugin Development' started by bitbox, Aug 27, 2014.

Thread Status:
Not open for further replies.
  1. Hi Bukkit.
    I am coding a plugin where so far there are two inventories. The first is named main, and the second gameModeSelector
    Inside the main inventory, there is a book that on click, takes the user to the gameModeSelector inventory, however when the player clicks on any of the wool representing Creative, Survival or Adventure, nothing seems to happen.
    Here is the code:
    MainClass.java
    Code:java
    1. package com.gmail.bitboxgaming.admincontrol;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.GameMode;
    8. import org.bukkit.Material;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.HumanEntity;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.inventory.InventoryClickEvent;
    16. import org.bukkit.inventory.Inventory;
    17. import org.bukkit.inventory.ItemStack;
    18. import org.bukkit.inventory.meta.ItemMeta;
    19. import org.bukkit.plugin.java.JavaPlugin;
    20.  
    21. public class MainClass extends JavaPlugin implements Listener{
    22.  
    23. //Defining the target
    24. public Player target;
    25.  
    26. //Creating the Gamemode Selector Inventory
    27. Inventory gameModeSelector = Bukkit.createInventory(null, 9, "Gamemode Selector");
    28.  
    29. //Creating the Main Menu Inventory
    30. Inventory main = Bukkit.createInventory(null, 54, "Main Menu");
    31.  
    32. //When plugin is enabled.
    33. public void onEnable() {
    34. getServer().getPluginManager().registerEvents(this, this);
    35. }
    36.  
    37. //Open Main Menu
    38. private void openGuiMain(Player player) {
    39. //Gamemode Book
    40. ItemStack gameModeItem = new ItemStack(Material.BOOK);
    41. ItemMeta gameModeItemMeta = gameModeItem.getItemMeta();
    42. //Gamemode Book, lores and metas.
    43. gameModeItemMeta.setDisplayName(ChatColor.RED + "Gamemode Selector");
    44. ArrayList < String > gameModeItemLore = new ArrayList < String > ();
    45. gameModeItemLore.add(ChatColor.GOLD + "Set the Gamemode of your Target.");
    46. gameModeItemMeta.setLore(gameModeItemLore);
    47. gameModeItem.setItemMeta(gameModeItemMeta);
    48. //Setting the slot for Gamemode Book
    49. main.setItem(0, gameModeItem);
    50. player.openInventory(main);
    51. }
    52. //Open Gamemode Menu
    53. private void openGuiGamemodeSelector(HumanEntity humanEntity){
    54. //Creative Wool
    55. ItemStack gameModeCreative = new ItemStack(Material.WOOL, 1, (byte)14);
    56. ItemMeta gameModeCreativeMeta = gameModeCreative.getItemMeta();
    57. //Creative Wool, lores and metas.
    58. gameModeCreativeMeta.setDisplayName(ChatColor.RED + "Creative");
    59. ArrayList < String > gameModeCreativeLore = new ArrayList < String > ();
    60. gameModeCreativeLore.add(ChatColor.GOLD + "Set your Target into Creative");
    61. gameModeCreativeMeta.setLore(gameModeCreativeLore);
    62. gameModeCreative.setItemMeta(gameModeCreativeMeta);
    63. //Setting the slot for Creative Wool
    64. gameModeSelector.setItem(2, gameModeCreative);
    65. humanEntity.openInventory(gameModeSelector);
    66.  
    67. //Survival Wool
    68. ItemStack gameModeSurvival = new ItemStack(Material.WOOL, 1, (byte)5);
    69. ItemMeta gameModeSurvivalMeta = gameModeSurvival.getItemMeta();
    70. //Survival Wool, lores and metas.
    71. gameModeSurvivalMeta.setDisplayName(ChatColor.GREEN + "Survival");
    72. ArrayList < String > gameModeSurvivalLore = new ArrayList < String > ();
    73. gameModeSurvivalLore.add(ChatColor.GOLD + "Set your Target into Survival");
    74. gameModeSurvivalMeta.setLore(gameModeSurvivalLore);
    75. gameModeSurvival.setItemMeta(gameModeSurvivalMeta);
    76. //Setting the slot for Survival Wool
    77. gameModeSelector.setItem(4, gameModeSurvival);
    78. humanEntity.openInventory(gameModeSelector);
    79.  
    80. //Adventure Wool
    81. ItemStack gameModeAdventure = new ItemStack(Material.WOOL, 1, (byte)1);
    82. ItemMeta gameModeAdventureMeta = gameModeAdventure.getItemMeta();
    83. //Adventure Wool, lores and metas.
    84. gameModeAdventureMeta.setDisplayName(ChatColor.GOLD + "Adventure");
    85. ArrayList < String > gameModeAdventureLore = new ArrayList < String > ();
    86. gameModeAdventureLore.add(ChatColor.GOLD + "Set your Target into Adventure");
    87. gameModeAdventureMeta.setLore(gameModeAdventureLore);
    88. gameModeAdventure.setItemMeta(gameModeAdventureMeta);
    89. //Setting the slot for Adventure Wool
    90. gameModeSelector.setItem(6, gameModeAdventure);
    91. humanEntity.openInventory(gameModeSelector);
    92. }
    93. ///Admin Gui Command
    94. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    95. Player player = (Player) sender;
    96. if (cmd.getName().equalsIgnoreCase("admingui")) {
    97. //Testing for a target
    98. if (args.length == 0) {
    99. sender.sendMessage(ChatColor.RED + "Please specify your Target");
    100. return true;
    101. }
    102. //If the player has selected a target
    103. if (args.length == 1) {
    104. target = getServer().getPlayerExact(args[0]);
    105. //If the target is offline
    106. if (target != null) {
    107. sender.sendMessage("You have targeted: " + target.getDisplayName());
    108. //Open the Main Gui
    109. openGuiMain(player);
    110. } else {
    111. player.sendMessage(ChatColor.RED + "The specified player is not Online.");
    112. }
    113.  
    114. }
    115.  
    116.  
    117. }
    118. return true;
    119.  
    120.  
    121. }
    122. @EventHandler
    123. public void onInventoryClickMain(InventoryClickEvent e) {
    124. if (e.getCurrentItem().getItemMeta() == null) return;
    125. if (e.getCurrentItem().getItemMeta().getDisplayName().contains(ChatColor.RED + "Gamemode Selector")){
    126. openGuiGamemodeSelector(e.getWhoClicked());
    127. }
    128. if (e.getCurrentItem().getItemMeta().getDisplayName().contains(ChatColor.RED + "Creative")){
    129. target.setGameMode(GameMode.CREATIVE);
    130. e.setCancelled(true);
    131. e.getWhoClicked().closeInventory();
    132. }
    133. if (e.getCurrentItem().getItemMeta().getDisplayName().contains(ChatColor.GREEN + "Survival")){
    134. target.setGameMode(GameMode.SURVIVAL);
    135. e.setCancelled(true);
    136. e.getWhoClicked().closeInventory();
    137. }
    138. if (e.getCurrentItem().getItemMeta().getDisplayName().contains(ChatColor.GOLD + "Adventure")){
    139. target.setGameMode(GameMode.ADVENTURE);
    140. e.setCancelled(true);
    141. e.getWhoClicked().closeInventory();
    142. }
    143.  
    144.  
    145. }
    146.  
    147. }
    148.  

    The InventoryClickEvent seems to work properly with the Gamemode Selector book, however not with anything else.

    Thanks!
    -BitBox
     
  2. Unfortunately I know nothing of inventories so I can't help you. What I think would be helpful for debugging this is having a few debug messages printed out to the player so you can try to understand what piece of code is not getting called properly. I suggest you setup a few like this:

    Code:java
    1. target.setGameMode(GameMode.ADVENTURE);
    2. target.sendMessage("Mode Set Successful");
    3.  


    and so forth. This way you can at least see up until which part everything works fine and what doesn't get called whn you expect it to.
     
  3. Offline

    TheOddPuff

    In the openGuiGamemodeSelector method, add this:
    Code:java
    1. target = (Player)humanEntity;
     
  4. Offline

    fireblast709

    TheOddPuff Why?
    bitbox You open your inventory thrice, this causes issues with the click listener. Just open it once (i.e. remove all but the last openInventory call) and it should work as intended.
     
  5. Where do I open my inventory 3 times? I close it three times with the different wools, but I don't think I open it three times
     
  6. Offline

    mine-care

    Ugh why!
    Player player = (Player) sender;
    Why!!???! Read my signature...
     
  7. Offline

    TheOddPuff

    bitbox In your openGuiGamemodeSelector method, you have this 3 times:
    Code:java
    1. humanEntity.openInventory(gameModeSelector);

    Remove the first 2 and only keep the last one
     
  8. I see what you are saying, but how is anything else apart from a player going to execute a command?

    Haha! Thanks very much, I forgot about that when I copied and pasted.

    Cheers

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

    TheOddPuff

    Code:java
    1. Player player = (sender instanceof Player) ? (Player)sender : null;
     
  10. Even after making the changes, the click event still doesn't seem to be working.

    Code:java
    1. package com.gmail.bitboxgaming.admincontrol;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.GameMode;
    8. import org.bukkit.Material;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.HumanEntity;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.inventory.InventoryClickEvent;
    16. import org.bukkit.inventory.Inventory;
    17. import org.bukkit.inventory.ItemStack;
    18. import org.bukkit.inventory.meta.ItemMeta;
    19. import org.bukkit.plugin.java.JavaPlugin;
    20.  
    21. public class MainClass extends JavaPlugin implements Listener{
    22.  
    23. //Defining the target
    24. public Player target;
    25.  
    26. //Creating the Gamemode Selector Inventory
    27. Inventory gameModeSelector = Bukkit.createInventory(null, 9, "Gamemode Selector");
    28.  
    29. //Creating the Main Menu Inventory
    30. Inventory main = Bukkit.createInventory(null, 54, "Main Menu");
    31.  
    32. //When plugin is enabled.
    33. public void onEnable() {
    34. getServer().getPluginManager().registerEvents(this, this);
    35. }
    36.  
    37. //Open Main Menu
    38. private void openGuiMain(Player player) {
    39. //Gamemode Book
    40. ItemStack gameModeItem = new ItemStack(Material.BOOK);
    41. ItemMeta gameModeItemMeta = gameModeItem.getItemMeta();
    42. //Gamemode Book, lores and metas.
    43. gameModeItemMeta.setDisplayName(ChatColor.RED + "Gamemode Selector");
    44. ArrayList < String > gameModeItemLore = new ArrayList < String > ();
    45. gameModeItemLore.add(ChatColor.GOLD + "Set the Gamemode of your Target.");
    46. gameModeItemMeta.setLore(gameModeItemLore);
    47. gameModeItem.setItemMeta(gameModeItemMeta);
    48. //Setting the slot for Gamemode Book
    49. main.setItem(0, gameModeItem);
    50. player.openInventory(main);
    51. }
    52. //Open Gamemode Menu
    53. private void openGuiGamemodeSelector(HumanEntity humanEntity){
    54. //Creative Wool
    55. ItemStack gameModeCreative = new ItemStack(Material.WOOL, 1, (byte)14);
    56. ItemMeta gameModeCreativeMeta = gameModeCreative.getItemMeta();
    57. //Creative Wool, lores and metas.
    58. gameModeCreativeMeta.setDisplayName(ChatColor.RED + "Creative");
    59. ArrayList < String > gameModeCreativeLore = new ArrayList < String > ();
    60. gameModeCreativeLore.add(ChatColor.GOLD + "Set your Target into Creative");
    61. gameModeCreativeMeta.setLore(gameModeCreativeLore);
    62. gameModeCreative.setItemMeta(gameModeCreativeMeta);
    63. //Setting the slot for Creative Wool
    64. gameModeSelector.setItem(2, gameModeCreative);
    65.  
    66. //Survival Wool
    67. ItemStack gameModeSurvival = new ItemStack(Material.WOOL, 1, (byte)5);
    68. ItemMeta gameModeSurvivalMeta = gameModeSurvival.getItemMeta();
    69. //Survival Wool, lores and metas.
    70. gameModeSurvivalMeta.setDisplayName(ChatColor.GREEN + "Survival");
    71. ArrayList < String > gameModeSurvivalLore = new ArrayList < String > ();
    72. gameModeSurvivalLore.add(ChatColor.GOLD + "Set your Target into Survival");
    73. gameModeSurvivalMeta.setLore(gameModeSurvivalLore);
    74. gameModeSurvival.setItemMeta(gameModeSurvivalMeta);
    75. //Setting the slot for Survival Wool
    76. gameModeSelector.setItem(4, gameModeSurvival);
    77.  
    78. //Adventure Wool
    79. ItemStack gameModeAdventure = new ItemStack(Material.WOOL, 1, (byte)1);
    80. ItemMeta gameModeAdventureMeta = gameModeAdventure.getItemMeta();
    81. //Adventure Wool, lores and metas.
    82. gameModeAdventureMeta.setDisplayName(ChatColor.GOLD + "Adventure");
    83. ArrayList < String > gameModeAdventureLore = new ArrayList < String > ();
    84. gameModeAdventureLore.add(ChatColor.GOLD + "Set your Target into Adventure");
    85. gameModeAdventureMeta.setLore(gameModeAdventureLore);
    86. gameModeAdventure.setItemMeta(gameModeAdventureMeta);
    87. //Setting the slot for Adventure Wool
    88. gameModeSelector.setItem(6, gameModeAdventure);
    89. humanEntity.openInventory(gameModeSelector);
    90. }
    91. ///Admin Gui Command
    92. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    93. Player player = (Player) sender;
    94. if (cmd.getName().equalsIgnoreCase("admingui")) {
    95. //Testing for a target
    96. if (args.length == 0) {
    97. sender.sendMessage(ChatColor.RED + "Please specify your Target");
    98. return true;
    99. }
    100. //If the player has selected a target
    101. if (args.length == 1) {
    102. target = getServer().getPlayer(args[0]);
    103. //If the target is offline
    104. if (target != null) {
    105. sender.sendMessage("You have targeted: " + target.getDisplayName());
    106. //Open the Main Gui
    107. openGuiMain(player);
    108. } else {
    109. player.sendMessage(ChatColor.RED + "The specified player is not Online.");
    110. }
    111.  
    112. }
    113.  
    114.  
    115. }
    116. return true;
    117.  
    118.  
    119. }
    120. @EventHandler
    121. public void onInventoryClickMain(InventoryClickEvent e) {
    122. if (e.getCurrentItem().getItemMeta() == null) return;
    123. if (e.getCurrentItem().getItemMeta().getDisplayName().contains(ChatColor.RED + "Creative")){
    124. target.setGameMode(GameMode.CREATIVE);
    125. e.setCancelled(true);
    126. e.getWhoClicked().closeInventory();
    127. }
    128. if (e.getCurrentItem().getItemMeta().getDisplayName().contains(ChatColor.GREEN + "Survival")){
    129. target.setGameMode(GameMode.SURVIVAL);
    130. e.setCancelled(true);
    131. e.getWhoClicked().closeInventory();
    132. }
    133. if (e.getCurrentItem().getItemMeta().getDisplayName().contains(ChatColor.GOLD + "Adventure")){
    134. target.setGameMode(GameMode.ADVENTURE);
    135. e.setCancelled(true);
    136. e.getWhoClicked().closeInventory();
    137. }
    138. if (e.getCurrentItem().getItemMeta().getDisplayName().contains(ChatColor.RED + "Gamemode Selector")){
    139. openGuiGamemodeSelector(e.getWhoClicked());
    140. }
    141.  
    142.  
    143. }
    144.  
    145. }
    146.  
     
  11. Offline

    Necrodoom

    Console.
    Rcon.
    Commandblock.
    Commandblock on a minecart.
     
  12. Oops, my bad :/ It's early....
    How does this make any difference to the problem though? I'm sure I will change it later, but for right now, I doubt it will fix the problem I am having.
     
  13. Offline

    fireblast709

    bitbox debug your event handler
     
  14. Offline

    mine-care

    TheOddPuff that works too :) single line if. But most people are not aware of the use of ? In if's so I added the simplest format of if :)
     
  15. I have just worked out how to debug, what am I looking for?
     
    mine-care likes this.
  16. Offline

    mine-care

    Exactly what Necrodoom said. If you want to see that happening go in your console and type the command :)
    About your problem do as fireblast709 suggested, print out messages when a action in code is executed.
     
  17. Offline

    fireblast709

  18. I'm gonna cry now ;'(
     
Thread Status:
Not open for further replies.

Share This Page