Solved Players in a Chest GUI

Discussion in 'Plugin Development' started by maestro34, May 7, 2014.

Thread Status:
Not open for further replies.
  1. I'm making a Spectator plugin and I would like to make a chest GUI with all players in it, I don't know how to make it :/

    Is anyone have an idea ?

    I already made this but it's not working :/
    Code:java
    1. @EventHandler
    2. public void onInteract(PlayerInteractEvent e)
    3. {
    4. Player p = e.getPlayer();
    5. ItemStack i = p.getInventory().getItemInHand();
    6. ItemStack player = null;
    7.  
    8. if(i.getType().equals(Material.NETHER_STAR)
    9. && i.hasItemMeta()
    10. && i.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "Joueurs connectés " + ChatColor.GRAY + "(Clique Droit)"))
    11. {
    12.  
    13. Inventory players = Bukkit.getServer().createInventory(null, 54, ChatColor.GREEN + "Joueurs Connectés");
    14.  
    15. int op = Bukkit.getOnlinePlayers().length;
    16.  
    17. for(Player pl : Bukkit.getOnlinePlayers())
    18. {
    19. player = new ItemStack(Material.SKULL);
    20. ItemMeta im = player.getItemMeta();
    21. im.setDisplayName(ChatColor.AQUA + pl.getName());
    22. player.setItemMeta(im);
    23.  
    24. for(int in = 0; in <= op; in++)
    25. {
    26. players.setItem(0, player);
    27. }
    28. }
    29.  
    30. p.openInventory(players);
    31. }
    32. }
     
  2. Offline

    DoctorDark

    You need to the cast the ItemMeta as a SkullMeta in this case:
    Code:
    SkullMeta im = (SkullMeta) player.getItemMeta();
    This would be similar to ItemMeta, except with a set owner method, so in your case:
    im.setOwner(pl.getName());

    Also you may want to use better naming conventions as your ItemStack is named 'player', which can be confused for an actual player, the same with your inventory name.

    You are also looping through a bunch of players replacing the ItemStack at slot 0 each time, so if you got this to work, there would only be one skullmeta in the "players" inventory.


    And one more thing; you should replace your inventory variable with this, assigning the InventoryHolder to the player, otherwise it is returning null; preventing the player from opening the inventory
    Inventory players = Bukkit.getServer().createInventory(p, 54, ChatColor.GREEN + "Joueurs Connectés");
     
  3. DoctorDark
    First, thanks for your answer :)

    I tried this but I get an error :/
    Code:java
    1. @EventHandler
    2. public void onInteract(PlayerInteractEvent e)
    3. {
    4. Player p = e.getPlayer();
    5. ItemStack item = p.getInventory().getItemInHand();
    6. ItemStack pHead = null;
    7.  
    8. if(item.getType().equals(Material.NETHER_STAR)
    9. && item.hasItemMeta()
    10. && item.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "Joueurs connectés " + ChatColor.GRAY + "(Clique Droit)"))
    11. {
    12.  
    13. Inventory onlinePlayers = Bukkit.getServer().createInventory(p, 54, ChatColor.GREEN + "Joueurs Connectés");
    14.  
    15. int op = Bukkit.getOnlinePlayers().length;
    16.  
    17. for(Player pl : Bukkit.getOnlinePlayers())
    18. {
    19. pHead = new ItemStack(Material.SKULL);
    20. SkullMeta im = (SkullMeta) pHead.getItemMeta();
    21. im.setOwner(pl.getName());
    22. pHead.setItemMeta(im);
    23.  
    24. for(int i = 0; i <= op; i++)
    25. {
    26. onlinePlayers.setItem(i, pHead);
    27. }
    28. }
    29.  
    30. p.openInventory(onlinePlayers);
    31. }
    32. }


    Error: http://hastebin.com/efagevipiz.avrasm
     
  4. Offline

    DoctorDark

    maestro34

    pHead =new ItemStack(Material.SKULL);

    should be

    pHead = new ItemStack(Material.SKULL_ITEM);
     
  5. DoctorDark Thanks It's working :) But I have two heads in the inventory :/
     
  6. Offline

    DoctorDark

    Then make use of google, add debug lines to see what's going on in the loop etc.
     
  7. DoctorDark I tried this, It's working but in the inventory when two people are online, the two skulls have the same name :/

    Code:java
    1. @EventHandler
    2. public void onInteract(PlayerInteractEvent e)
    3. {
    4. Player p = e.getPlayer();
    5. ItemStack item = p.getInventory().getItemInHand();
    6. ItemStack pHead = null;
    7.  
    8. if(item.getType().equals(Material.NETHER_STAR)
    9. && item.hasItemMeta()
    10. && item.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "Joueurs connectés " + ChatColor.GRAY + "(Clique Droit)"))
    11. {
    12.  
    13. Inventory onlinePlayers = Bukkit.getServer().createInventory(p, 54, ChatColor.GREEN + "Joueurs Connectés " + ChatColor.GRAY + "(" + Bukkit.getOnlinePlayers().length + "/" + Bukkit.getServer().getMaxPlayers() + ")");
    14.  
    15. int op = Bukkit.getOnlinePlayers().length;
    16.  
    17. for(Player pl : Bukkit.getOnlinePlayers())
    18. {
    19. pHead = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
    20. SkullMeta im = (SkullMeta) pHead.getItemMeta();
    21. im.setOwner(pl.getName());
    22. im.setDisplayName(ChatColor.AQUA + im.getOwner());
    23. pHead.setItemMeta(im);
    24. }
    25.  
    26. for(int i = 1; i <= op; i++)
    27. {
    28. onlinePlayers.setItem(i - 1, pHead);
    29. }
    30.  
    31. p.openInventory(onlinePlayers);
    32. }
    33. }
     
  8. Offline

    tryy3

    remove the for loop and do something like

    Code:java
    1. int i = 0;
    2. for (Player pl : Bukkit.getOnlinePlayers())
    3. {
    4. ... //code
    5. onlinePlayers.setItem(i, pHead);
    6. i++;
    7. }


    If you have the for loop outside it will use the last pHead that was set, if you have it inside you will pretty much spam the same head several times, so its better to just set a variable outside the for each loop, and then use that in the foreach loop and at the end just increase it (same logic as a for loop but inside a foreach loop)

    I am sure there is other ways to do this but, this should work atleast :)
     
  9. tryy3 Tanks it's working :)
     
Thread Status:
Not open for further replies.

Share This Page