Open A GUI W/ Player's Inv?

Discussion in 'Spigot Plugin Development' started by DireLegendDev, Jun 7, 2015.

  1. I am trying to open a gui with the contents being the player's inventory. How can I do this? My current code is below.

    Code (Text):
    public class GUI implements Listener {

        public void openGUI(Player player) {
            Inventory inv = Bukkit.createInventory(null, 27, ChatColor.BLACK + "Enchant");
            inv.addItem(player.getInventory().getContents());
           
            player.openInventory(inv);
        }  
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            openGUI(e.getPlayer());
        }
    }
     
  2. Do player.openInventory(player.getInventory());
     
    • Useful Useful x 1
  3. Thanks! Is there any easy way to open an inventory with all the for example- swords in a player's inventory?
     
    • Like Like x 1
    • Friendly Friendly x 1
  4. No problem man
     
    • Friendly Friendly x 1
  5. What about opening the inv with all the swords in a player's inv? Is there any easy way, or just use a bunch of if statements?
     
  6. Could I just use a for loop to cycle through all the player's items in their inventory, and put them in the GUI that is being opened? Do you have an example of this?
     
  7. Uhm Ive never tried but you can try this


    (off top of my head)

    for(ItemStack items : p.getInventory().getContents()) {

    }
     
    • Useful Useful x 1
  8. Tux

    Tux

    Yes.

    Code (Text):
    for (ItemStack item : player.getInventory()) {
      if (isSword(item)) {
        // ...
        newInv.addItem(item);
      }
    }
     
    • Like Like x 1
    • Agree Agree x 1
  9. You don't define what is in the inventory and if you want to define a material that open the inventory is if(ItemStack = Material.(name)
    openGui(event.getPlayer());
     
    • Informative Informative x 1
  10. if that doesnt work pm me on here and give me your skype ill help you there
     
  11. Check if the inventory contains any swords. Then if it does create an inventory of those swords.
    Code (Text):
    if (ItemStack == Material.(material) openGUI(event.getPlayer());
     
    • Informative Informative x 1
  12. I have another idea. Would it be possible to use the code below but get an array of the items that are just swords/something else?
    All right awesome thanks!
     
    • Friendly Friendly x 1
  13. Hey I messaged you. I can't figure it out :(
     
  14. What can't you figure out? I'll help you on Spigot, or if you want Skype.
    Skype: mr.mansionwill
     
  15. ItemStack isn't castable to material, iirc. Also, it might be better to check the instance, not use "==".