creating and using guis using lists

Discussion in 'Resources' started by Concurrent, Nov 9, 2014.

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

    Concurrent

    hello today i will show how to use lists to make functional guis. to start off you need to set up your main class, useful for kitpvp plugins and maybe hubs
    Code:java
    1. public class Main extends JavaPlugin {
    2. @Override
    3. public void onEnable() {
    4.  
    5. }
    6. @Override
    7. public void onDisable() {
    8. // im not using ondisable but you may
    9. }
    10. }
    11.  

    now we make a new class called "MenuItem", this will hold our methods for click events and such
    Code:java
    1. public class MenuItem {
    2. private String message;
    3. private ItemStack item;
    4.  
    5. public MenuItem(String message, ItemStack item) {
    6. this.message = message;
    7. this.item = item;
    8. }
    9.  
    10. public ItemStack getItem() {
    11. return item;
    12. }
    13.  
    14. public String getMessage() {
    15. return message;
    16. }
    17.  
    18. public void setItem(ItemStack item) {
    19. this.item = item;
    20. }
    21.  
    22. public void setMessage(String message) {
    23. this.message = message;
    24. }
    25. }
    26.  

    the method 'getItem()' will return the instance of the classes item which in this case will be in our selector
    the string 'getMessage()' will return a string which ill send to player on click, this can be method to teleport or give items
    now create a list for storing these classes
    Code:java
    1. private List<MenuItem> items = new ArrayList<MenuItem>();

    also add a getter for list
    Code:java
    1. public List<MenuItem> getItems() {
    2. return items;
    3. }

    now we create a listener to listen for click event i call it 'MenuListener' call it what you want
    Code:java
    1. public class MenuListener implements Listener {
    2. Main plugin;
    3.  
    4. public MenuListener(Main instance) {
    5. this.plugin = instance;
    6. }
    7.  
    8. public void show(Player p) {
    9.  
    10. }
    11.  
    12. @EventHandler
    13. public void onInventoryClick(InventoryClickEvent e) {
    14.  
    15. }
    16. }

    now in the show method we must create inventory and add items to do this follow my lead
    Code:java
    1. Inventory inv = Bukkit.createInventory(null, 9, "Selector");

    this creates inventory with name 'Selector'
    Code:java
    1. for (MenuItem menuItem : plugin.getItems()) {
    2.  
    3. }

    we iterate through these items to add their itemstack to inv
    Code:java
    1. inv.addItem(menuItem.getItem());

    now just open inventory for player
    Code:java
    1. p.openInventory(inv);


    now we must do our click event. firstly make sure they don't click outside the inventory otherwise get errors
    Code:java
    1. if (e.getSlotType() == SlotType.OUTSIDE)
    2. return;

    now check if inventory name is correct
    Code:java
    1. if (!e.getInventory().getName().equals("Selector"))
    2. return;

    finally check if item has item meta and is not null
    Code:java
    1. if (e.getCurrentItem() != null && e.getCurrentItem().hasItemMeta()) {
    2.  
    3. }

    now we will list through items and check if clicked item .equals() current item and if so send player message
    Code:java
    1. for (MenuItem menuItem : plugin.getItems()) {
    2. if (e.getCurrentItem().equals(menuItem.getItem())) {
    3. e.setCancelled(true);
    4. ((Player) e.getWhoClicked()).sendMessage(menuItem.getMessage());
    5. break;
    6. }
    7. }

    were almost done, back to main class in onEnable method register your listener and add some items
    final main class(after registering listener and adding items)
    Code:java
    1. public class Main extends JavaPlugin {
    2. private List<MenuItem> items = new ArrayList<MenuItem>();
    3. private MenuListener menu;
    4.  
    5. public void onEnable() {
    6. menu = new MenuListener(this);
    7. Bukkit.getPluginManager().registerEvents(menu, this);
    8. items.add(new MenuItem(ChatColor.GREEN + "First message!", createItemStack(Material.IRON_INGOT, ChatColor.GOLD + "First")));
    9. items.add(new MenuItem(ChatColor.YELLOW + "Second message!", createItemStack(Material.IRON_INGOT, ChatColor.LIGHT_PURPLE + "Second")));
    10. items.add(new MenuItem(ChatColor.RED + "Last message!", createItemStack(Material.IRON_INGOT, "Last")));
    11. }
    12.  
    13. public void onDisalbe() {
    14. // im not using ondisable but you may
    15. }
    16.  
    17. public List<MenuItem> getItems() {
    18. return items;
    19. }
    20.  
    21. private ItemStack createItemStack(Material material, String name) {
    22. ItemStack i = new ItemStack(material);
    23. ItemMeta im = i.getItemMeta();
    24. im.setDisplayName(name);
    25. i.setItemMeta(im);
    26. return i;
    27. }
    28.  
    29. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    30. if (sender instanceof Player) {
    31. Player p = (Player) sender;
    32. if (command.getName().equalsIgnoreCase("gui")) {
    33. menu.show(p);
    34. }
    35. }
    36. return false;
    37. }
    38. }

    thanks for reading, hopefully you didnt tl;dr have fun making easy-to-add-to guis
     
  2. Offline

    xTrollxDudex

    Good one, seems legit!
     
    teej107 likes this.
  3. Offline

    teej107

    Can I use the onDisalbe method? Or can you use @Override?
     
  4. If the onDisable() method is empty and you do not plan on putting anything in there, then just leave out the onDisable() method.

    Otherwise, you would correct the spelling and put @Override above it.
     
Thread Status:
Not open for further replies.

Share This Page