Icon Menu

Discussion in 'Resources' started by nisovin, Oct 30, 2012.

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

    bobacadodl


    Coming soon :)
     
    chasechocolate and makertim like this.
  2. Offline

    makertim

    why not using this?
    public static Main plugin;
    public IconMenu(Main instance){
    plugin = instance;
    }

    I get error's at line 34 that plugin cant be null
     
  3. Offline

    chaseoes

    You gave us 4 lines of code.. how are we supposed to tell you why line 34 is giving you an error?
     
  4. Offline

    bobacadodl

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

    Added an onClose() listener and made the inventory destroy itself when the player closes it without selecting an option.
     
  5. Offline

    makertim

    I used clean code, but i fixt it already :D
    but, instad of using
    private Plugin plugin;

    this is deBugd, and used by many plugins and is recommanded, so i like to see when you use this
    public static Main plugin;
    public IconMenu(Main instance){
    plugin = instance;
    }
     
  6. Offline

    nisovin

    I'm not sure what you're going on about here. You're supposed to pass your plugin as the third parameter to the constructor. There's no need to make the plugin variable public and static.
     
  7. Offline

    chasechocolate

    bobacadodl
    Awww you stole my idea. I am using something similar on my server.
     
    bobacadodl likes this.
  8. Offline

    ftbastler

  9. Offline

    desht

  10. Offline

    AmoebaMan

    Fifth. People seem to have a much easier time understanding "click item" than "type command".
     
    chasechocolate likes this.
  11. Offline

    s0undx

    I love this, really nice! Thank you
     
  12. Offline

    ZeusAllMighty11

    The method setItem(int, ItemStack) in the type Inventory is not applicable for the arguments (int, ItemStack[])

    Figured it out, forgot to put int :p
     
  13. Offline

    xXSilentYoshiXx

    ftbastler
    nisovin

    Oh ha, ha, ha. I just realized something. The Class name should be IconMenu. xD I'm so stupid. Resolved!

    EDIT: Removed problems, got stupider after reading this forum.
     
  14. Offline

    AstramG

    When I try to use this, it returns this error in the server console.
    Code:
    Caused by: java.lang.NullPointerException
        at me.AstramG.TestPlugin.IconMenu.<init>(IconMenu.java:34)
        at me.AstramG.TestPlugin.Main.<init>(Main.java:202)
    At line 34 in the IconMenu class it says:
    Code:
    plugin.getServer().getPluginManager().registerEvents(this, plugin);
    And in line 202 in Main, it says this:
    Code:
    IconMenu menu = new IconMenu("My Fancy Menu", 9, new IconMenu.OptionClickEventHandler() {
    I can't seem to find a solution to this, and I've been looking for awhile, so I thought my best bet would be to come here.
     
  15. Offline

    xXSilentYoshiXx

    AstramG

    I know why.

    Here's the code to enlighten you.

    Main Class:
    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin {
    public static IconMenu im;
     
    @Override
    public void onEnable() {
    PluginManager pm = Bukkit.getPluginManager();
    pm.registerEvents(im, this);
    }
     
    public boolean onCommand(CommandSender s, Command cmd, String cl, String[] args) {
    Player p = (Player) s;
     
    if (cl.equalsIgnoreCase("imtest")) {
       
        IconMenu menu = new IconMenu("My Fancy Menu", 9, new IconMenu.OptionClickEventHandler() {
            @Override
            public void onOptionClick(IconMenu.OptionClickEvent event) {
                event.getPlayer().sendMessage("You have chosen " + event.getName());
                event.setWillClose(true);
            }
        }, plugin)
        .setOption(3, new ItemStack(Material.APPLE, 1), "Food", "The food is delicious")
        .setOption(4, new ItemStack(Material.IRON_SWORD, 1), "Weapon", "Weapons are for awesome people")
        .setOption(5, new ItemStack(Material.EMERALD, 1), "Money", "Money brings happiness");
     
    menu.open(p);
    }
    return false;
    }
    }
    IconMenu Class:
    Code:
    import java.util.Arrays;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.HandlerList;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;
     
    public class IconMenu implements Listener {
     
        private String name;
        private int size;
        private OptionClickEventHandler handler;
        private Plugin plugin;
     
        private String[] optionNames;
        private ItemStack[] optionIcons;
     
        public IconMenu(String name, int size, OptionClickEventHandler handler, Plugin plugin) {
            this.name = name;
            this.size = size;
            this.handler = handler;
            this.plugin = plugin;
            this.optionNames = new String[size];
            this.optionIcons = new ItemStack[size];
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
     
        public IconMenu setOption(int position, ItemStack icon, String name, String... info) {
            optionNames[position] = name;
            optionIcons[position] = setItemNameAndLore(icon, name, info);
            return this;
        }
     
        public void open(Player player) {
            Inventory inventory = Bukkit.createInventory(player, size, name);
            for (int i = 0; i < optionIcons.length; i++) {
                if (optionIcons[i] != null) {
                    inventory.setItem(i, optionIcons[i]);
                }
            }
            player.openInventory(inventory);
        }
     
        public void destroy() {
            HandlerList.unregisterAll(this);
            handler = null;
            plugin = null;
            optionNames = null;
            optionIcons = null;
        }
     
        @EventHandler(priority=EventPriority.MONITOR)
        void onInventoryClick(InventoryClickEvent event) {
            if (event.getInventory().getTitle().equals(name)) {
                event.setCancelled(true);
                int slot = event.getRawSlot();
                if (slot >= 0 && slot < size && optionNames[slot] != null) {
                    Plugin plugin = this.plugin;
                    OptionClickEvent e = new OptionClickEvent((Player)event.getWhoClicked(), slot, optionNames[slot]);
                    handler.onOptionClick(e);
                    if (e.willClose()) {
                        final Player p = (Player)event.getWhoClicked();
                        Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                            public void run() {
                                p.closeInventory();
                            }
                        }, 1);
                    }
                    if (e.willDestroy()) {
                        destroy();
                    }
                }
            }
        }
       
        public interface OptionClickEventHandler {
            public void onOptionClick(OptionClickEvent event);     
        }
       
        public class OptionClickEvent {
            private Player player;
            private int position;
            private String name;
            private boolean close;
            private boolean destroy;
         
            public OptionClickEvent(Player player, int position, String name) {
                this.player = player;
                this.position = position;
                this.name = name;
                this.close = true;
                this.destroy = false;
            }
         
            public Player getPlayer() {
                return player;
            }
         
            public int getPosition() {
                return position;
            }
         
            public String getName() {
                return name;
            }
         
            public boolean willClose() {
                return close;
            }
         
            public boolean willDestroy() {
                return destroy;
            }
         
            public void setWillClose(boolean close) {
                this.close = close;
            }
         
            public void setWillDestroy(boolean destroy) {
                this.destroy = destroy;
            }
        }
     
        private ItemStack setItemNameAndLore(ItemStack item, String name, String[] lore) {
            ItemMeta im = item.getItemMeta();
                im.setDisplayName(name);
                im.setLore(Arrays.asList(lore));
            item.setItemMeta(im);
            return item;
        }
    }
     
  16. Offline

    AstramG

    They are no errors in eclipse, but when I tried this but it says when I use the command that there is an Internal server error, on line 44 which is:
    Code:
    im.menu.open(player);
    I defined im previously, like you did, however it still errors :(.
     
  17. Offline

    xXSilentYoshiXx

    AstramG

    Yeah, I just remembered. I fixed up the code. If it doesn't work, I'll just do some troubleshooting. :)
     
  18. Offline

    AstramG

    This creates the same error as my first error.
     
  19. Offline

    xXSilentYoshiXx

    AstramG

    Time to troubleshoot...
     
  20. Offline

    AstramG

    Did you find anything?

    Anybody care to help? I'm trying to understand, I feel like I'm just missing how to do it :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  21. Offline

    bobacadodl

    This means that you're not passing the right arguments to the icon menu. I'm guessing that the "plugin" that you're passing to the ItemMenu class is null.

    EDIT: do some debugging to check if it is null & post your entire class so we can help
     
  22. Offline

    chasechocolate

    Are you using this? I believe the OP is outdated...
    Code:java
    1. IconMenu menu = new IconMenu("Name", <size>, new IconMenu.OptionClickEventHandler(){
    2. @Override
    3. public void onOptionClick(OptionClickEvent event){
    4. //Do whatever
    5. }
    6. }, <plugin instance>);
     
  23. Offline

    chasechocolate

    A better example on usage:
    Code:java
    1. IconMenu menu = new IconMenu(<title>, <size>, new IconMenu.OptionClickEventHandler(){
    2. @Override
    3. public void onOptionClick(OptionClickEvent event){
    4. Player clicker = event.getPlayer(); //Get the player who clicked
    5. String name = event.getName(); //Returns the display name of the item the player clicked
    6. event.setWillClose(true); //Will close the inventory when they click on an item
    7. }
    8. }, <plugin instance>)
    9. .setOption(<slot, 0 is the first>, <ItemStack>, <item display name>, <item lore>); //You can add as much lore as you want
    10. menu.open(player); //Open the IconMenu for a player
     
    bobacadodl likes this.
  24. Offline

    ShadowRanger

    What would the plugin instance be?
     
  25. Offline

    chasechocolate

    The instance of your main class (the one that extends JavaPlugin).
     
  26. Offline

    ShadowRanger

    It doesn't seem to be working... Keeps getting underlined in red.

    Wait don't worry I fixed it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  27. Offline

    ShadowRanger

    Okay well i'm trying to first of all get the base of the plugin to work, then edit it to my liking afterwards. At the moment i'm using the code that xXSilentYoshiXx pasted in a previous reply to this thread. For some reason, when I load the plugin on my server I get an error of 'java.lang.IllegalArgumentException: Listener can not be null'. I'm starting to think I need some sort of listener, is that true? If so, what code would I put in the Listener? If it's not the requirement of a listener then please let me know what the problem is. As a side note, i'm getting a red underlined error under the plugin instance of 'plugin' in the main class stating it cannot be resolved to a variable. Just in case it's asked for, this is the code i'm using (I have purposely censored out part of the package name):

    Main Class:
    Code:java
    1. package ---.-------------.iconmenu;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Material;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.inventory.ItemStack;
    9. import org.bukkit.plugin.PluginManager;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Main extends JavaPlugin {
    13. public static IconMenu im;
    14.  
    15. @Override
    16. public void onEnable() {
    17. PluginManager pm = Bukkit.getPluginManager();
    18. pm.registerEvents(im, this);
    19. }
    20.  
    21. public boolean onCommand(CommandSender s, Command cmd, String cl, String[] args) {
    22. Player p = (Player) s;
    23.  
    24. if (cl.equalsIgnoreCase("imtest")) {
    25.  
    26. IconMenu menu = new IconMenu("My Fancy Menu", 9, new IconMenu.OptionClickEventHandler() {
    27. @Override
    28. public void onOptionClick(IconMenu.OptionClickEvent event) {
    29. event.getPlayer().sendMessage("You have chosen " + event.getName());
    30. event.setWillClose(true);
    31. }
    32. }, plugin)
    33. .setOption(3, new ItemStack(Material.APPLE, 1), "Food", "The food is delicious")
    34. .setOption(4, new ItemStack(Material.IRON_SWORD, 1), "Weapon", "Weapons are for awesome people")
    35. .setOption(5, new ItemStack(Material.EMERALD, 1), "Money", "Money brings happiness");
    36.  
    37. menu.open(p);
    38. }
    39. return false;
    40. }
    41. }


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


    So yeah, that's basically it, some help would be much appreciated, thanks in advance.
     
  28. Offline

    Rprrr

    Phoenix_Mawson1
    Eh.. have you ever made a plugin? Do you actually (globally) know how this thing works? In general, an icon menu is a simple custom inventory combined with a listener that listens for the InventoryClickEvents done in that inventory. If a certain icon (ItemStack) is clicked in the inventory, you execute some code.
     
  29. Offline

    ShadowRanger

    I'm kind of a beginner to coding although I know the basics... As soon as I get this working i'll probably be able to do the rest myself, but yeah.
     
  30. Offline

    iZanax

    How can I open an IconMenu when someone is opening his regular menu (InventoryOpenEvent)

    Because when I open an IconMenu on the InventoryOpenEvent nothing happening and on Command it does.
    So is there a possibility to do this?

    Thanks in advance!
    Zanax
     
Thread Status:
Not open for further replies.

Share This Page