Solved Getting players Inventory contents and adding them to a GUI?

Discussion in 'Plugin Development' started by krizzdawg, May 3, 2016.

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

    krizzdawg

    So basically i am not sure how to get the players inventory contents and add them to a GUI for example.

    Code:
      @EventHandler
        public void clicked(PlayerInteractEntityEvent e) {
            Player rightclick = (Player) e.getRightClicked();
            Player p = e.getPlayer();
    
    
    
            if(rightclick instanceof Player){
            
                
                    ItemStack[] iv = rightclick.getInventory().getContents();
                    Inventory rightCcon = Bukkit.createInventory(null, 54, "§a" + rightclick.getName() + "'s Inventory");
                    p.sendMessage(pre + "§cYou have opened §7" + rightclick.getName() + "'s §cinventory.");
                    rightCcon.addItem(iv);
                    p.openInventory(rightCcon);
    
    ---------
    The Inventory does not open.
    but if I try this

    ---------
    Code:
        @EventHandler
        public void clicked(PlayerInteractEntityEvent e) {
            Player rightclick = (Player) e.getRightClicked();
            Player p = e.getPlayer();
    
    
    
            if(rightclick instanceof Player){
             
                 
                   ItemStack iv = rightclick.getInventory().getItemInHand();
                    Inventory rightCcon = Bukkit.createInventory(null, 54, "§a" + rightclick.getName() + "'s Inventory");
                    p.sendMessage(pre + "§cYou have opened §7" + rightclick.getName() + "'s §cinventory.");
                    rightCcon.addItem(iv);
                    p.openInventory(rightCcon); 
    
    It works just fine please help! (On getting the player's item in hand).
     
    Last edited: May 3, 2016
  2. Offline

    timtower Administrator Administrator Moderator

    @krizzdawg Please use [code] <code here> [/code]
    Makes it easier to read.
     
  3. Offline

    krizzdawg

    Done. Please help.
     
  4. Offline

    timtower Administrator Administrator Moderator

    @krizzdawg Try looping through the contents instead of adding the entire array in 1 go.
     
  5. Offline

    mine-care

    First check, then cast.
     
  6. Offline

    krizzdawg

    Example code please.
     
  7. Offline

    timtower Administrator Administrator Moderator

    I don't spoonfeed, certainly not a simple for loop.
     
  8. Offline

    krizzdawg

    Code:
    for(ItemStack i : rightclick.getInventory().getContents()){
    
                    Inventory rightCcon = Bukkit.createInventory(null, 54, "§a" + rightclick.getName() + "'s Inventory");
                    p.sendMessage(pre + "§cYou have opened §7" + rightclick.getName() + "'s §cinventory.");
                    rightCcon.addItem(i);
                    p.openInventory(rightCcon);
    
    Doesnt work timmy.
     
    Last edited: May 3, 2016
  9. Offline

    timtower Administrator Administrator Moderator

    @krizzdawg You do realize that you create a load of inventories?
     
  10. Offline

    Firestar311

    I was about to post how to do it without giving code, but then your post about that. Please be considerate when posting.
     
Thread Status:
Not open for further replies.

Share This Page