Solved Inventories, need help!

Discussion in 'Plugin Development' started by DoggyCode™, Jul 4, 2015.

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

    DoggyCode™

    Hi,

    I was wondering, how do I edit the name of an already existing inventory?

    As you can see on the attached photo, it displays the inventory of a player, but the name is somehow editied. How can I do that?

    Edit:
    Ok, so I found out that you can't edit an already created inventory. Anyone else has any idea on how to make it like it is on the picture? With the name and everything?

    Screenshot_1.png
     
    Last edited: Jul 4, 2015
  2. Offline

    Tecno_Wizard

    @DoggyCode™, the thing is, that isn't the player's inventory. Rather, it is more likely a copy of it. Editing the name of an inventory is impossible once it has been created.
     
    DoggyCode™ likes this.
  3. Offline

    Synapz

    @DoggyCode™,
    Bukkit.getServer().createInventory(InventoryHolder owner, int size, String title)

    [Edit] ninja'd
    What @Tecno_Wizard said, didn't see the 'existing' inventory part in your post. But, you can get the contents of the inventory and remove the inventory then create another inventory with the same contents but with a new inventory name.
     
  4. Offline

    DoggyCode™

  5. Offline

    ShadowWizardMC

    By creating a Bukkit Inventory you can set the title of it. The inventory in the picture is obviously custom (the layout)

    Code:
        public Inventory InspectPlayerInventory(Player p){
            //Defining the Player interface above
            //Creating a Player Inventory and setting the name            
            Inventory inv = Bukkit.createInventory(p, InventoryType.PLAYER,"Inspecting: " + p.getName());
            //Filling the Inventory with the contents of the player's inventory
            inv.setContents(p.getInventory().getContents());
          
            //Return the filled Inventory to the player that called the function
            return inv;
        }
    And here's and example:

    Code:
     //EXAMPLE
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String cl, String[] args){
            if(cmd.getName().equalsIgnoreCase("inspect")){
                if(!(sender instanceof Player)){
                    sender.sendMessage("Only Players can execute this command.");
                    return true;
                }
                Player p = (Player) sender;
                if(args.length == 0){
                    p.sendMessage(ChatColor.RED + "Uh oh! You forgot to define a player's name!");
                    p.sendMessage(ChatColor.RED + "Correct Usage: /inspect <name>.");
                }else if (args.length == 1){
                    String name = args[0];
                    if(Bukkit.getPlayer(name) != null){
                        p.openInventory(InspectPlayerInventory(Bukkit.getPlayer(name)));
                    }else{
                        p.sendMessage(ChatColor.RED + "Player cannot be found.");
                    }
                }
            }
            return false;
        }
    Ps I just realized that other people have assisted you while I was typing this but that's OK the more help the better :).
    @Tecno_Wizard @Synapz @DoggyCode™
     
    Synapz likes this.
  6. Offline

    Synapz

    Last edited: Jul 4, 2015
  7. Offline

    DoggyCode™

    Thank you very much, @ShadowWizardMC !

    @ShadowWizardMC Ok, but after adding all the contents from p.getInventory() to that created inventory, how do I add seperate items after that? Like using the format (inventory).setItem(ItemStack); I tried just doing it under "inv.setContents(p.getInventory().getContents());", but I got an error. please halp! :D

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

    ShadowWizardMC

    @DoggyCode™

    Code:
        public Inventory InspectPlayerInventory(Player p){
            //Defining the Player interface above
            //Creating a Player Inventory and setting the name
    
            //EDIT: Making a custom inventory to support extra items
            Inventory inv = Bukkit.createInventory(p, 54,"Inspecting: " + p.getName());
            //Filling the Inventory with the contents of the player's inventory
            inv.setContents(p.getInventory().getContents());
    
    
            //Making and editing properties of custom item
            //You Can Copy paste this part as much items as you want and set each item to a different slot in the inventory
            ItemStack stack = new ItemStack(Material.BOOK);
            ItemMeta meta = stack.getItemMeta();
            meta.setDisplayName("Custom Item");
            meta.setLore(Arrays.asList("New Test Lore", "Supports Multiple", "Lines!!!"));
            stack.setItemMeta(meta);
    
            //Setting the custom item in the inventory
            inv.setItem(53, stack);
    
            //Return the filled Inventory to the player that called the function
            return inv;
        }
     
  9. Offline

    DoggyCode™

    @ShadowWizardMC , you see, that's what I'm trying, but it only gives me an error whenever I try to open the inventory.

    My code:
    Code:
      public Inventory InspectPlayerInventory(Player p){
      //Defining the Player interface above
      //Creating a Player Inventory and setting the name   
      Inventory inv = Bukkit.createInventory(p, 54,"§6Inspecting: §e" + p.getName());
      //Filling the Inventory with the contents of the player's inventory
      inv.setContents(p.getInventory().getContents());
       
      //Making and editing properties of custom item
      //You Can Copy paste this part as much items as you want and set each item to a different slot in the inventory
      ItemStack stack = new ItemStack(Material.BOOK);
      ItemMeta meta = stack.getItemMeta();
      meta.setDisplayName("Custom Item");
      meta.setLore(Arrays.asList("New Test Lore", "Supports Multiple", "Lines!!!"));
      stack.setItemMeta(meta);
      //Setting the custom item in the inventory
      inv.setItem(53, stack);
       
      //Return the filled Inventory to the player that called the function
      return inv;
       }
    Error message:
     

    Attached Files:

  10. Offline

    Tecno_Wizard

    WesJD likes this.
  11. Offline

    DoggyCode™

    Erm, I debugged the whole thing... I found out the solution..
     
    ShadowWizardMC likes this.
  12. Synapz likes this.
  13. Offline

    DoggyCode™

    Lol, of course. But I wanted the title of the inventory to be Player#getName(), what you wrote there is not something that could've been just used.
     
  14. Offline

    ShadowWizardMC

    @DoggyCode™ Many apologies for the faulty code. Coded this off my head. Glad you found your solution by debugging mind telling so I know exactly what I did wrong? We're only as strong as our weakest mistake.
     
  15. Offline

    DoggyCode™

    Haha, @ShadowWizardMC , I love you soo much man.. You've helped me a lot xD There's only one thing more I need to figure out. I want it in code like whenever a player opens a chest, I want the event to be cancelled, and then I want to open a new inventory for the player with the contents of that chest. Like a copy, like I did with a player's inventory:

    Code:
      public Inventory ViewPlayerInventory(Player p) {
      //Defining the Player interface above
      //Creating a Player Inventory and setting the name   
      Inventory inv = Bukkit.createInventory(p, 54,"§6Viewing: §e" + p.getName());
      //Filling the Inventory with the contents of the player's inventory
      inv.setContents(p.getInventory().getContents());
       
      //Making and editing properties of custom item
      //You Can Copy paste this part as much items as you want and set each item to a different slot in the inventory
      ItemStack stack = new ItemStack(Material.STAINED_GLASS_PANE);
      ItemMeta meta = stack.getItemMeta();
      meta.setDisplayName("§6Player's Armor:");
      meta.setLore(Arrays.asList("§a" + p.getName() + "§e's Armor Contents"));
      stack.setItemMeta(meta);
       
      ItemStack stackB = new ItemStack(Material.STAINED_GLASS_PANE);
      ItemMeta metaB = stackB.getItemMeta();
      metaB.setDisplayName("§e|");
      stackB.setItemMeta(metaB);
      //Setting the custom item in the inventory
      inv.setItem(45, stack);
      inv.setItem(46, p.getInventory().getHelmet());
      inv.setItem(47, p.getInventory().getChestplate());
      inv.setItem(48, p.getInventory().getLeggings());
      inv.setItem(49, p.getInventory().getBoots());
      inv.setItem(50, stackB);
       
      //Return the filled Inventory to the player that called the function
      return inv;
       }
    
     
  16. Offline

    SuperOriginal

    Have you tried researching any of this... At all?
     
  17. Offline

    DoggyCode™

    Yeh, I tried...
     
  18. Offline

    ShadowWizardMC

    @DoggyCode™

    Code:
        @EventHandler
        public void onInteract(PlayerInteractEvent e){
            if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
                Block b = e.getClickedBlock();
                if(b.getType() == Material.CHEST){
                    Inventory inv;
                    e.setCancelled(true);
                    Chest chest = (Chest) b.getState();
                    if(chest.getInventory().getSize() == 27) {
                        inv = Bukkit.createInventory(null,27,"Chest");
                        inv.setContents(chest.getInventory().getContents());
                        e.getPlayer().openInventory(inv);
                    }else if(chest.getInventory().getSize() == 54) {
                        inv = Bukkit.createInventory(null,54,"Chest");
                        inv.setContents(chest.getInventory().getContents());
                        e.getPlayer().openInventory(inv);
                    }
                }
            }
        }
     
  19. Offline

    DoggyCode™

    Yeah, I figured it out xD Thanks anyways man.
     
  20. @ShadowWizardMC
    You shouldn't spoonfeed so much, because usually players will just copy the code without knowing what they're doing. Also, InspectPlayerInventory goes against java's naming convention, so you gave a bit of a bad example :/
     
  21. Offline

    ShadowWizardMC

    @megamichiel I understand what you mean by spoon-feeding but I included some comments about exactly what I was doing. Copy pasting coding is like cheating, you're not benefiting yourself in the long run and if anyone is doing that chances are you'll probably find them back here looking for help again. I know InspectPlayerInventory goes against naming conventions (inspectPlayerInventory) but I had a motive behind it (if you didn't realize yet :) ).
     
  22. Offline

    DoggyCode™

    Btw, by copying that code, I know exactly what I was doing.

    Firstly, it checks if the action is a right click of on a block; then it says that b is equal to the block that was clicked (right clicked in this case); then it checks if the block is equal to a chest, and if it is, it cancells the event; then it checks if the chest's inventory size is equal to 27, and if it is, it creates a new inventory titled "Chest"- with 27 slots (because it's a non-double chest), then it put's all the contents of the clicked chest into this new created inventory; then it opens it (the new inventory) for the player; and the same with the next lines, just with a double chest.

    I just didn't exactly know how to put it in code, but I understand it now as I see it. And I'm not that kind of person that just copies code and doesn't read over it and tries to not understand it.

    @ShadowWizardMC @megamichiel

    Well, it's solved now.. Could anyone lock the thread?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
Thread Status:
Not open for further replies.

Share This Page