Solved How to create an inventory with player heads

Discussion in 'Plugin Development' started by sgavster, Aug 15, 2013.

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

    sgavster

    Hi everyone. I've been looking for a while now, but can't find anything. I was wondering how I would make an inventory, put a list of players heads in it, and have it teleport you to them?
    Thanks for any help in advance :)
     
  2. Offline

    FlxiFlix

    Where are you from?
    Because of tutorials
     
  3. Offline

    sgavster

    FlxiFlix I am not exactly sure what you mean.. I am english if that's what you're asking.
     
  4. Offline

    FlxiFlix

    Yes, that's what I ment. I know a very good German tutorial about the exactly answer of your question.
    It may help you. Just copy the code.
     
  5. Offline

    sgavster

    FlxiFlix Could I have a link? I don't know German but the code will help
     
  6. Offline

    FlxiFlix

    Code:
    http://www.youtube.com/watch?v=9smc9YMAOZY
     
    NoteInBlock likes this.
  7. Offline

    sgavster

    FlxiFlix hmm, k. I was hoping the code was in the description or something- this is gonna be a lot harder than I thought, as I do not understand german at all. :/
     
  8. Offline

    tamajpm

    sgavster
    If you doesn't take the time for your project, don't begin.
     
  9. Offline

    sgavster

    tamajpm
    How am I supposed to take time on something I have no idea how to do? I don't speak german, so it's not 'easy' for me to know what it does.. >_>
     
  10. Offline

    tamajpm

    sgavster
    Create a second project with the same code but edit it with the code from that video. If you don't know what something means you can come back, i take care of my problems with this plan ;).
     
  11. Offline

    Deleted user

    sgavster
    Copy the code...Just copy ALL of it
    Anything in German is most likely a player.sendMessage thing
    Just replace everything in German...worst comes to worst...maybe try putting the code (or a word in the code) in Google Translate :p
     
  12. Offline

    sgavster

    JHG0 FlxiFlix I did the code, it didn't work.. This is the code:
    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onClick(InventoryClickEvent event){
    4. if(event.getSlot() == event.getRawSlot()){
    5. Player player = (Player) event.getWhoClicked();
    6. if(this.inv.containsKey(player.getName())){
    7. if(this.inv.get(player.getName()).equals(ChatColor.GOLD + "Teleport")){
    8. event.setCancelled(true);
    9. player.updateInventory();
    10. ItemStack item = event.getCurrentItem();
    11. if(item != null){
    12. String name = ChatColor.stripColor(item.getItemMeta().getDisplayName());
    13. Player p_ = Bukkit.getPlayerExact(name);
    14. if(p_ != null){
    15. player.teleport(p_);
    16. player.sendMessage(this.prefix + ChatColor.GOLD + " You teleported to " + ChatColor.DARK_RED + p_.getName() + ChatColor.GOLD + "!");
    17. }else{
    18. player.sendMessage(this.prefix + ChatColor.DARK_RED + " Failed to teleport.");
    19. }
    20. this.closeInv(player);
    21.  
    22. }
    23. }
    24.  
    25. }
    26. }
    27. }
    28.  
    29.  
    30.  
    31.  
    32. @EventHandler
    33. public void inventoryClose(InventoryCloseEvent event){
    34. this.closeInv((Player) event.getPlayer());
    35. }
    36.  
    37. @EventHandler
    38. public void onKick(PlayerKickEvent event){
    39. this.closeInv((Player) event.getPlayer());
    40. }
    41.  
    42. @EventHandler
    43. public void onQuit(PlayerQuitEvent event){
    44. this.closeInv((Player) event.getPlayer());
    45. }
    46.  
    47. @EventHandler
    48. public void playerInteract1(PlayerInteractEvent event){
    49. Player player1 = event.getPlayer();
    50. if(event.getAction() == Action.RIGHT_CLICK_AIR && player1.getItemInHand().getType() == Material.COMPASS){
    51. this.openInv(player1);
    52. }
    53. }
    54.  
    55. public void openInv(Player player){
    56. int lines = 0;
    57. Player[] players = Bukkit.getOnlinePlayers();
    58. while (lines * 9 < players.length - 1) {
    59. lines++;
    60. }
    61.  
    62. if(lines > 6){
    63. lines = 6;
    64. }
    65.  
    66. Inventory inv = Bukkit.createInventory(null, lines * 9, ChatColor.GOLD + "Choose a player to teleport to.");
    67. int slot = 0;
    68. for (int i = 0; i < players.length; i++){
    69. Player player_ = players[1];
    70. if (player_ != player){
    71. ItemStack item = new ItemStack(Material.SKULL_ITEM, 1, (short)3);
    72. ItemMeta meta = item.getItemMeta();
    73. meta.setDisplayName(ChatColor.AQUA + player_.getName());
    74. item.setItemMeta(meta);
    75. inv.setItem(slot, item);
    76. slot++;
    77. }
    78. }
    79. player.openInventory(inv);
    80. this.inv.put(player.getName(), ChatColor.GOLD + "Teleport");
    81. }
    82.  
    83. public void closeInv(Player player){
    84. if(this.inv.containsKey(player.getName())){
    85. player.closeInventory();
    86. this.inv.remove(player.getName());
    87. }
    88. }
    89. }

    But, it doesn't work. When I right click nothing helps :/ Any help?
     
  13. Offline

    Deleted user

    sgavster

    I'm not that farmiliary with inventories...sorry
     
  14. Offline

    sgavster

    JHG0 Oh okay.. :/
     
  15. Offline

    xTrollxDudex

    sgavster
    Use Bukkit.createInventory(Player, int slots, String name);, loop through all the players, add a head to the inventory and open it
     
  16. Offline

    sgavster

    xTrollxDudex I know that. If you look up I have the code; it doesn't work, and I am looking for how to fix it..
     
  17. Offline

    xTrollxDudex

    sgavster
    You need to assign a value to null when you create the inventory
     
  18. Offline

    flaaghara

    You see that deprecation warning that you are trying to suppress? That is there for a reason. How about you add debug messages?
     
  19. Offline

    sgavster

    xTrollxDudex What would I put there? I'm not exactly sure what I would put there..

    flaaghara Yes, it is. The reason being player.updateInventory();

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  20. Offline

    xTrollxDudex

    flaaghara
    Just because it's deprecated doesn't mean it is unusable.
    sgavster
    I don't think you need player.updateInventory(), set null to the player you want to open the inventory to
     
    Samthelord1 likes this.
  21. Offline

    Xx_LeetGamer_xX

    What I do in my Survival Games plugin (IP: SSundee-mc.com for an example try spectating)

    Code:
    public class PlayerInteract implements Listener{
        @EventHandler(priority = EventPriority.LOWEST)
        public void onPlayerInteract(PlayerInteractEvent e){
            if(SpectateHandler.spectators.contains(p)){
                if(p.getItemInHand().getTypeId() == 0){
                    e.setCancelled(true);
                    return;
                }else if(p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.GREEN + "Spectator Teleporter")){
                    if(e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
                        int arena = SpectateHandler.GetArena(p);
                        Inventory inv = Bukkit.createInventory(p, GetSize(arena), ChatColor.BOLD + "Spectator Teleporter");
                        for(Player px: SurvivalGames.getInstance().games[arena].players){
                            ItemStack skull = new ItemStack(397, 1, (short) 3);
                            SkullMeta meta = (SkullMeta) skull.getItemMeta();
                            meta.setDisplayName(px.getName());
                            skull.setItemMeta(meta);
                            inv.addItem(skull);
                        }
                        p.openInventory(inv);
                    }
                }
                e.setCancelled(true);
            }
        }
       
        private static int GetSize(int arena){
            int size = SurvivalGames.getInstance().games[arena].players.size();
            while(!(size % 9 == 0)){
                ++size;
            }
            return (size > 54) ? 54 : size;
        }
    }
    Code:
    public class InventoryClick implements Listener{
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e){
            if(e.getWhoClicked() instanceof Player){
                Player p = (Player) e.getWhoClicked();
                if(SpectateHandler.spectators.contains(p)){
                    e.setCancelled(true);
                    if(e.getInventory().getName().equals(ChatColor.BOLD + "Spectator Teleporter")){
                        if(e.getCurrentItem() == null){
                            e.setCancelled(true);
                            return;
                        }
                       
                        String name = e.getCurrentItem().getItemMeta().getDisplayName();
                        if(name == null){
                            return;
                        }
                       
                        Player px = Bukkit.getPlayer(name);;
                        if(px != null){
                            if(SurvivalGames.getInstance().games[SpectateHandler.GetArena(p)].players.contains(px)){
                                p.teleport(px);
                            }
                        }else{
                            p.closeInventory();
                        }
                    }
                }
            }
        }
    }
     
  22. Offline

    flaaghara

    xTrollxDudex
    Deprecation's only purpose is to serve as a form of backwards compatibility. Once something is deprecated there's a better way to use it than the current implementation.
     
  23. Offline

    sgavster

    xTrollxDudex After playing around for a couple hours, I changed the code a tiny bit (probably not at all, moved it around in some classes) and I have this:
    Code:java
    1. private HashMap<String, String> inv = new HashMap<String, String>();
    2.  
    3. @SuppressWarnings("deprecation")
    4. @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled=true)
    5. public void onClick(InventoryClickEvent event){
    6. if(event.getSlot() == event.getRawSlot()){
    7. Player player = (Player) event.getWhoClicked();
    8. if(this.inv.containsKey(player.getName())){
    9. if(this.inv.get(player.getName()).equals(ChatColor.GOLD + "Teleport")){
    10. event.setCancelled(true);
    11. player.updateInventory();
    12. ItemStack item = event.getCurrentItem();
    13. if(item != null){
    14. String name = ChatColor.stripColor(item.getItemMeta().getDisplayName());
    15. Player p_ = Bukkit.getPlayerExact(name);
    16. if(p_ != null){
    17. player.teleport(p_);
    18. player.sendMessage(this.prefix + ChatColor.GOLD + " You teleported to " + ChatColor.DARK_RED + p_.getName() + ChatColor.GOLD + "!");
    19. }else{
    20. player.sendMessage(this.prefix + ChatColor.DARK_RED + " Failed to teleport.");
    21. }
    22. this.closeInv(player);
    23.  
    24. }
    25. }
    26.  
    27. }
    28. }
    29. }
    30.  
    31. @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled=true)
    32. public void inventoryClose(InventoryCloseEvent event){
    33. this.closeInv((Player) event.getPlayer());
    34. }
    35.  
    36. @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled=true)
    37. public void onKick(PlayerKickEvent event){
    38. this.closeInv((Player) event.getPlayer());
    39. }
    40.  
    41. @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled=true)
    42. public void onQuit(PlayerQuitEvent event){
    43. this.closeInv((Player) event.getPlayer());
    44. }
    45.  
    46. @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled=true)
    47. public void playerInteract1(PlayerInteractEvent event){
    48. Player player = event.getPlayer();
    49. if(player.getItemInHand().getType() == Material.COMPASS){
    50. if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
    51. player.chat("shiz");
    52. this.openInv(player);
    53. }
    54. }
    55. }
    56.  
    57. public void openInv(Player player){
    58. int lines = 0;
    59. Player[] players = Bukkit.getOnlinePlayers();
    60. while (lines * 9 < players.length - 1) {
    61. lines++;
    62. }
    63.  
    64. if(lines > 6){
    65. lines = 6;
    66. }
    67.  
    68. Inventory inv = Bukkit.createInventory(null, lines * 9, ChatColor.GOLD + "Choose a player to teleport to.");
    69. int slot = 0;
    70. for (int i = 0; i < players.length; i++){
    71. Player player_ = players[1];
    72. if (player_ != player){
    73. ItemStack item = new ItemStack(Material.SKULL_ITEM, 1, (short)3);
    74. ItemMeta meta = item.getItemMeta();
    75. meta.setDisplayName(ChatColor.AQUA + player_.getName());
    76. item.setItemMeta(meta);
    77. inv.setItem(slot, item);
    78. slot++;
    79. }
    80. }
    81. player.openInventory(inv);
    82. this.inv.put(player.getName(), ChatColor.GOLD + "Teleport");
    83. }
    84.  
    85. public void closeInv(Player player){
    86. if(this.inv.containsKey(player.getName())){
    87. player.closeInventory();
    88. this.inv.remove(player.getName());
    89. }
    90. }
    91. }
    92.  

    The "player.chat("chiz");" is a debug message, which only works when I right click a block.
    I don't care about that now. All I care about is the inv. I get an error in the console. The error is:
    [​IMG]
    thanks for any help. I've been trying to fix this, and I can't, so any help is great
     
  24. Offline

    Xx_LeetGamer_xX

    Did you try what I posted?
     
  25. Offline

    sgavster

    Xx_LeetGamer_xX Actually, didn't see the post until now :p
    I'll try it later (it's 4 am for me)
    I'll get back to you tomorrow (i'll tahg you.) thanks! :D

    Xx_LeetGamer_xX I'm not sure how I would use some of your code... Something like the arena and things, I don't know how to replace.. any help?..

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  26. Offline

    Xx_LeetGamer_xX

    The arena variable is just an integer that holds what arena that player is in. This is for a Survival Games plugin. You could just make this for loop loop through all the players you want to add to the chest GUI:

    Code:
    for(Player px: SurvivalGames.getInstance().games[arena].players){
     
  27. Offline

    xTrollxDudex

    flaaghara
    Doesn't mean you can't use it, because unless it says "do not use" like the door class, I'm still enough of a rebel to still use it :p
     
  28. Offline

    sgavster

    Xx_LeetGamer_xX IS it too much to ask what I have to change in my code to get it to work?
    The things that I have to change in yours is confusing me.. I'll try to change it, I'll play around with it..
     
  29. Offline

    Xx_LeetGamer_xX

    Do you want every player on the server to be added into the inventory GUI for the teleporter? Or do you have an array list of player objects?
     
  30. Offline

    sgavster

    Xx_LeetGamer_xX I still need to make an arraylist for it, but I want it to be players in an arraylist.
     
Thread Status:
Not open for further replies.

Share This Page