IconMenu problems

Discussion in 'Plugin Development' started by Fhbgsdhkfbl, Jul 16, 2014.

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

    Fhbgsdhkfbl

    Hey bukkit forums, I'm making a kit selector for my kitpvp plugin with IconMenu, I got it all set up, but when they click an item, it doesn't perform the command that it's supposed to, it just lets you put the item in your inventory
    IconMenu class

    Code:java
    1. package me.Fhbgsdhkfbl;
    2.  
    3. import java.util.Arrays;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.EventPriority;
    9. import org.bukkit.event.HandlerList;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.inventory.InventoryClickEvent;
    12. import org.bukkit.inventory.Inventory;
    13. import org.bukkit.inventory.ItemStack;
    14. import org.bukkit.inventory.meta.ItemMeta;
    15. import org.bukkit.plugin.Plugin;
    16.  
    17. public class IconMenu implements Listener {
    18. private me.Fhbgsdhkfbl.Main plugin;
    19. public IconMenu(me.Fhbgsdhkfbl.Main plugin){
    20. this.plugin = plugin;
    21. }
    22.  
    23. private String name;
    24. private int size;
    25. private OptionClickEventHandler handler;
    26.  
    27. private String[] optionNames;
    28. private ItemStack[] optionIcons;
    29.  
    30. public IconMenu(String name, int size, OptionClickEventHandler handler, Plugin plugin) {
    31. this.name = name;
    32. this.size = size;
    33. this.handler = handler;
    34. this.optionNames = new String[size];
    35. this.optionIcons = new ItemStack[size];
    36. }
    37.  
    38. public IconMenu setOption(int position, ItemStack icon, String name, String... info) {
    39. optionNames[position] = name;
    40. optionIcons[position] = setItemNameAndLore(icon, name, info);
    41. return this;
    42. }
    43.  
    44. public void open(Player player) {
    45. Inventory inventory = Bukkit.createInventory(player, size, name);
    46. for (int i = 0; i < optionIcons.length; i++) {
    47. if (optionIcons[i] != null) {
    48. inventory.setItem(i, optionIcons[i]);
    49. }
    50. }
    51. player.openInventory(inventory);
    52. }
    53.  
    54. public void destroy() {
    55. HandlerList.unregisterAll(this);
    56. handler = null;
    57. plugin = null;
    58. optionNames = null;
    59. optionIcons = null;
    60. }
    61.  
    62. @EventHandler(priority=EventPriority.MONITOR)
    63. void onInventoryClick(InventoryClickEvent event) {
    64. if (event.getInventory().getTitle().equals(name)) {
    65. event.setCancelled(true);
    66. int slot = event.getRawSlot();
    67. if (slot >= 0 && slot < size && optionNames[slot] != null) {
    68. Plugin plugin = this.plugin;
    69. OptionClickEvent e = new OptionClickEvent((Player)event.getWhoClicked(), slot, optionNames[slot]);
    70. handler.onOptionClick(e);
    71. if (e.willClose()) {
    72. final Player p = (Player)event.getWhoClicked();
    73. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    74. public void run() {
    75. p.closeInventory();
    76. }
    77. }, 1);
    78. }
    79. if (e.willDestroy()) {
    80. destroy();
    81. }
    82. }
    83. }
    84. }
    85.  
    86. public interface OptionClickEventHandler {
    87. public void onOptionClick(OptionClickEvent event);
    88. }
    89.  
    90. public class OptionClickEvent {
    91. private Player player;
    92. private int position;
    93. private String name;
    94. private boolean close;
    95. private boolean destroy;
    96.  
    97. public OptionClickEvent(Player player, int position, String name) {
    98. this.player = player;
    99. this.position = position;
    100. this.name = name;
    101. this.close = true;
    102. this.destroy = false;
    103. }
    104.  
    105. public Player getPlayer() {
    106. return player;
    107. }
    108.  
    109. public int getPosition() {
    110. return position;
    111. }
    112.  
    113. public String getName() {
    114. return name;
    115. }
    116.  
    117. public boolean willClose() {
    118. return close;
    119. }
    120.  
    121. public boolean willDestroy() {
    122. return destroy;
    123. }
    124.  
    125. public void setWillClose(boolean close) {
    126. this.close = close;
    127. }
    128.  
    129. public void setWillDestroy(boolean destroy) {
    130. this.destroy = destroy;
    131. }
    132. }
    133.  
    134. private ItemStack setItemNameAndLore(ItemStack item, String name, String[] lore) {
    135. ItemMeta im = item.getItemMeta();
    136. im.setDisplayName(name);
    137. im.setLore(Arrays.asList(lore));
    138. item.setItemMeta(im);
    139. return item;
    140. }
    141.  
    142. }[/i][/i]


    interact event and iconmenu event

    Code:java
    1. IconMenu menu = new IconMenu("Kit Selector", 27, new IconMenu.OptionClickEventHandler() {
    2. @Override
    3. public void onOptionClick(IconMenu.OptionClickEvent event) {
    4. Player p = event.getPlayer();
    5. switch (event.getPosition()) {
    6. case 0:
    7. p.performCommand("pvp");
    8. break;
    9. case 1:
    10. p.performCommand("archer");
    11. break;
    12. case 2:
    13. Bukkit.dispatchCommand(p, "soldier");
    14. break;
    15. }
    16.  
    17. event.setWillClose(true);
    18. }
    19. }, plugin)
    20. .setOption(0, new ItemStack(Material.DIAMOND_SWORD, 1), "Knight", "Click me for the Knight kit!", "Default Kit")
    21. .setOption(1, new ItemStack(Material.BOW, 1), "Archer", "Click me for the Archer kit!")
    22. .setOption(2, new ItemStack(Material.IRON_SWORD, 1), "Soldier", "Click me for the Soldier kit!");
    23. @EventHandler
    24. public void onKitGui(PlayerInteractEvent e) {
    25. Player player = e.getPlayer();
    26. Action a = e.getAction();
    27. if (a.equals(Action.RIGHT_CLICK_BLOCK) || (a.equals(Action.RIGHT_CLICK_AIR)) && !player.getItemInHand().getType().equals(Material.DIAMOND)) {
    28. return;
    29. }
    30. if (a.equals(Action.RIGHT_CLICK_BLOCK) || (a.equals(Action.RIGHT_CLICK_AIR)) && player.getItemInHand().getType().equals(Material.DIAMOND)) {
    31. menu.open(player);
    32. return;
    33. }
    34. }
     
  2. Offline

    themuteoneS

    Even though it lets you put the item in your inventory, does it still give you the items you wanted? If so the problem may be that the associated event was not cancelled properly. I'm guessing you want the inventory to be closed directly after the player clicks on the item so how about cancelling the InventoryClickEvent and then running
    Code:java
    1. e.getPlayer().closeInventory();


    Source: https://forums.bukkit.org/threads/closing-custom-inventories-by-clicking-on-items.164825/

    Sidenote:
    Code:java
    1. private me.Fhbgsdhkfbl.Main plugin;

    *shudder*
    I hope that's just rudimentary obfuscation for posting purposes.
     
Thread Status:
Not open for further replies.

Share This Page