Solved Grabbing player's heads, placing them in a gui and running commands when player head clicked.

Discussion in 'Spigot Plugin Development' started by Wilsoon, Mar 27, 2020.

Thread Status:
Not open for further replies.
  1. So, this time, with the GUI, I want to get all online players' heads and place them in a list like a selector menu in a gui. After that, I would get the player to select the player, and run commands with that player as the target.

    However, I don't know anything - Literally. I never attempted to get a player's head before, I also never attempted to name the head to the player's name or something to identify in InventoryClickEvent.
     
  2. https://hub.spigotmc.org/javadocs/spigot/org/bukkit/inventory/meta/SkullMeta.html

    It should be pretty self-explanatory.
    Just cast your ItemMeta that you received from the ItemStack to SkullMeta (but please not without Type checking first) and use it as you please. (Don't forget to set it onto the item after modifying it!)
    Also only use the players names if you don't have another choice - use their UUID's instead to get them as Player / OfflinePlayer.

    Edit: I was too slow again ^^ @Dexuby
     
  3. Yeah I would get this, but the main part would be adding each player's skull to the GUI itself. I'm going to have a single row GUI. But I don't know how to add it in one by one? I always used to use #setItem, but I doubt that would work if there's more than one player.
     
  4. #addItem
    /e read the javadocs, they have plenty of things for you
     
    • Agree Agree x 1
  5. Scale the GUI size based on the player count (you might also want to use a paging system depending on your estimated player count, there are a ton of resources for that out there) and as already mentioned add the skulls using the #addItem method
     
    • Informative Informative x 1
  6. I did something like this with a plugin I made a while ago. I didn't make a paging system like Dexuby suggested but I'm sure that it would be easy to add.
    Here is the method I used:
    Code (Java):

    Inventory inv = Bukkit.createInventory(null, 54, ChatColor.translateAlternateColorCodes('&', "&0Online Players));
                    for (Player plr : Bukkit.getServer().getOnlinePlayers()) {
                        ItemStack i = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
                        SkullMeta im = (SkullMeta) i.getItemMeta();
                   
                        List<String> il = new ArrayList<String>();
                        il.add(ChatColor.translateAlternateColorCodes('&', "
    &7Status: &aOnline"));
                        im.setOwner(plr.getName());
                        im.setLore(il);
                        im.setDisplayName(ChatColor.translateAlternateColorCodes('&', "
    &7" + plr.getName());
                        i.setItemMeta(im);
                   
                        inv.addItem(i);
    }
                        p.openInventory(inv);
    Note: This was made on 1.12.2. Some methods might be changed but for the most part you should be good.

    Also for getting the player clicked on the InventoryClickEvent, I'd recommend checking if the inventory's title matches with the one of the GUI and then I would check if Bukkit#getServer#getOnlinePlayers()#toString() contains the player name and then use Bukkit#getPlayer(String playername) to get the player using the displayname as the variable "playername" (might need to do ChatColor#stripColor() depending if you use color codes or not.
     
    • Useful Useful x 3
    • Like Like x 1
  7. Thank you everyone, I've completed this.
     
Thread Status:
Not open for further replies.