[Util] Specate GUI - Create a GUI to for spectating players!

Discussion in 'Resources' started by sgavster, Dec 7, 2013.

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


    I've noticed the amount of minigame plugins is.. alot!

    I made this util so that you can easily create a GUI that will have items in it (that you can customize) with player names. When you click on them you will teleport to the player that you clicked on!

    The class:

    public class SpectateGUI implements Listener
    Inventory spectate;
    void openSpectateGUI(Player pPlayer[] playersint rowsItemStack item)
    ItemStack i item;
    ItemMeta m i.getItemMeta();
    spectate Bukkit.createInventory(prows*9"Click on a name");
    int z 0players.lengthz++)
    public void onClick(InventoryClickEvent e)
    e.getInventory().getTitle().equalsIgnoreCase("Click on a name"))
    e.getCurrentItem() != null && e.getCurrentItem().getType() != null)
    Player p = (Playere.getWhoClicked();
    e.getCurrentItem().hasItemMeta() && e.getCurrentItem().getItemMeta().hasDisplayName())
    Player c Bukkit.getPlayer(e.getCurrentItem().getItemMeta().getDisplayName().trim());
    How to use:
    To use it, you'd simply do this:
    private SpectateGUI spectategui = new SpectateGUI();
    spectategui.openSpectateGUI(playerBukkit.getOnlinePlayers(), 1, new ItemStack(Material.SKULL_ITEM1, (byte3));
    * The first thing, 'player' is the player that you want to open the GUI for.
    * The second thing, 'Bukkit.getOnlinePlayers()' are the players you want in the GUI. This needs to be an array.
    * The third thing, '1', is the amount of ROWS in the inventory.
    * The fourth thing, 'new ItemStack(Material.SKULL_ITEM, 1, (byte) 3)', is the item that the players name are on.
    * What the code I posted would do it get all the online players, put their head on a player skull.
    You'll also need to register the evens in your onEnable, because of the Listener. This is how you would do it:
    Bukkit.getPluginManager().registerEvents(new SpectateGUI()), this);
    • If you want to use this in a plugin, feel free! Credit would be great, but is not needed!
    • If this was useful, let me know!
    • Have a question? Ask it!
    • Have a suggestion? Tell me!
    Hope this was useful! :D
  2. Offline


    This would be a nice edition to my plugin. Thanks! :)
  3. sgavster
    Pretty neat. Gonna find a use for this on my server :)
  4. Offline


  5. Offline


    I thought I'd post this - I modified what you made a little bit for my own personal use and just though I'd share for anyone else who wants to use it.

    There are really only three things I changed with mine rather than yours:
    1. I registered the listener inside the class. (I just don't like 50 event listeners on my onEnable :p).
    2. Instead of the developer specifying the amount of rows, it will automatically calculate how many rows are necessary to fit all of the players.
    3. One of the bigger changes, I guess, is it allows the developer to set their own action to be done when someone clicks on the inventory. This is just if a developer wants to put their player in creative and then teleport them, or if the developer wants to put the player in creative, teleport them, and then send them a message.

    To setup what I have, just import the class SpectatorGUI and then create a new instance of it like I've demonstrated below.

    The SpectatorGUI Class:
    Show Spoiler

    1. public class SpectatorGUI implements Listener
    2. {
    3. private Plugin plugin;
    4. private Player player;
    5. private Player[] players;
    6. private SpectatorGUIHandler handler;
    8. public SpectatorGUI(Plugin plugin, Player player, Player[] players, SpectatorGUIHandler handler)
    9. {
    10. this.plugin = plugin;
    11. this.player = player;
    12. this.players = players;
    13. this.handler = handler;
    15. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    16. }
    18. public SpectatorGUI openGUI()
    19. {
    20. int invSize = ((players.length + 8) / 9) * 9;
    22. Inventory inventory = Bukkit.createInventory(null, invSize, "Select a player...");
    24. for(int i = 0; i < players.length; i++)
    25. {
    26. ItemStack item = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
    27. SkullMeta meta = (SkullMeta) item.getItemMeta();
    29. meta.setOwner(players[i].getName());
    31. item.setItemMeta(meta);
    33. inventory.setItem(i, item);
    34. }
    36. player.openInventory(inventory);
    37. return this;
    38. }
    40. @EventHandler
    41. public void onClick(InventoryClickEvent event)
    42. {
    43. Inventory inv = event.getInventory();
    45. if(inv.getTitle().equalsIgnoreCase("Select a player..."))
    46. {
    47. if(event.getCurrentItem() != null && event.getCurrentItem().getType() != null)
    48. {
    49. handler.onItemClicked(event);
    50. event.setCancelled(true);
    51. }
    52. }
    53. }
    55. public interface SpectatorGUIHandler
    56. {
    57. public void onItemClicked(InventoryClickEvent event);
    58. }
    59. }[/i]

    To use the class, you'll just put this code wherever in your project you want the GUI to be executed (command?):
    Show Spoiler

    1. new SpectatorGUI(plugin, getPlayer(), Bukkit.getOnlinePlayers(), new SpectatorGUIHandler(){
    3. @Override
    4. public void onItemClicked(InventoryClickEvent event)
    5. {
    6. Player player = (Player) event.getWhoClicked();
    7. Player playerBeingClicked = Bukkit.getPlayer(((SkullMeta) event.getCurrentItem().getItemMeta()).getOwner());
    9. if(player.getName() == playerBeingClicked.getName()) return;
    11. if(playerBeingClicked != null)
    12. {
    13. //Do stuff...
    14. }
    15. }
    17. }).openGUI();

    The parameters should be pretty self-explanatory; however, if they aren't, the first one is the instance of your plugin, the second one is instance of the player you want to show the inventory to, the third one is the array of players you are wanting to list in the GUI (all players online, or modified to all players in an arena or whatever), and the last one is the instance of the SpectatorGUIHandler.
    JBoss925, Jozeth and Aqua like this.
  6. Offline



    • Fixed player names
  7. Offline


    You beat me to this, I just had it all ready and tested. Was just about to create the thread, nice job anyway.
    KingFaris11 likes this.
  8. Offline


    How can i set the inventory to show display name colours too and not just plain names on the item?
  9. Offline


    change m.setDisplayName(players[z].getName());
    to m.setDisplayName(players[z].getDisplayName());
  10. Would this code work? I am trying to do this and I just eddited it now, before this it just shows the amount of players online's heads and the name would be the player who opened it. Just wondering would this work?

        public static void openGUI(Player p, Player[] players) {
            Inventory i = Bukkit.createInventory(null, 54, UHC.replaceVar("&b&lSpectater &c&lMenu"));
                for(int it = 0; it < Bukkit.getOnlinePlayers().length; it++){
                if (players[it].hasPermission("uhc.alive")) {
                    ItemStack skull = new ItemStack(Material.SKULL_ITEM);
                    SkullMeta sM = (SkullMeta) skull.getItemMeta();
                    i.setItem(it, skull);
                } else{
  11. Offline


    OfficerDeveloper likes this.
  12. @sgavster it does! Thank you! I was trying to debug this for a while now!

    Do you know how you would do it with like a grey border around the inventory? So that the skull place doesn't override the grey border?
  13. Offline


    @OfficerDeveloper Hmm, probably in my class create the inventory beforehand, then set the gray items that you want in the places you want and check if there is nothing in the slot and then add the heads? Not exactly sure if that would work but I don't see why not.
  14. Also, if I was doing it my way with the permissions, I found a bug :p. It makes it so that if a OP is dead, but still OP it will show them to since they have every permission :/. For now I just make it so noone can spectate OP's :p.
  15. Offline


    Having a single inventory would be better than creating a new one for each player, it would also be easier to update it for all players viewing it. (someone dying, etc)
Thread Status:
Not open for further replies.

Share This Page