Solved Get armor slots

Discussion in 'Plugin Development' started by mehboss, Jan 25, 2017.

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

    mehboss

    Alright so I have it so this copies the inventory and then restores it. How would I save the armor slots to restore also?

    Code:
                        if (!plugin.inmod.contains(p)) {
                            plugin.inmod.add(p);
    
                            this.Inventory.put(p.getName(), copyInventory(p.getInventory()));
    Code:
        private ItemStack[] copyInventory(Inventory inv) {
            ItemStack[] original = inv.getContents();
            ItemStack[] copy = new ItemStack[original.length];
            for (int i = 0; i < original.length; ++i)
                if (original != null)
                    copy = original;
            return copy;
        }
    
        private void restoreInventory(Player p, ItemStack[] inventory) {
            p.getInventory().setContents(inventory);
        }
    }
    
     
  2. Offline

    Zombie_Striker

    @mehboss
    Get the armor using Inventory#getArmorContents. After that, either create it's own itemstack array or combine those items with 'copy.'
     
  3. Offline

    mehboss

    I tried:
    Code:
    Inventory armor = inv.getArmorContents();
    it just says:
    Code:
    The method getArmorContents(null) is undefined for the type Inventory
    I am probably doing the variable wrong, what would the correct variable be?
     
  4. Offline

    Zombie_Striker

    @mehboss
    It is specifically for "PlayerInventories". If your inventory came from a player, just change the variable type to PlayerInventory.

    Also, getArmorContents does not return an inventory, but an itemstack[].
     
  5. Offline

    mehboss

    @Zombie_Striker
    Like this?
    Code:
            ItemStack[] armor = ((PlayerInventory) inv).getArmorContents();
    UPDATE: It doesn't work
    Code:
                        if (!plugin.inmod.contains(p)) {
                            plugin.inmod.add(p);
    
                            this.Inventory.put(p.getName(), copyInventory(p.getInventory()));
    Code:
                                else {
                                restoreInventory(p, savedInventory);
                                plugin.inmod.remove(p);
                                p.sendMessage(plugin.prefixcolored + "§7ModMode §cdisabled!");
                                return true;
    Code:
        private ItemStack[] copyInventory(Inventory inv) {
            ItemStack[] original = inv.getContents();
            ItemStack[] copy = new ItemStack[original.length];
            ItemStack[] armor = ((PlayerInventory) inv).getArmorContents();
            ItemStack[] armorcopy = new ItemStack[armor.length];
            for (int i = 0; i < original.length; ++i)
                if (original != null && armor != null) {
                    copy = original;
                    armorcopy = armor;
                }
                else if (original != null && armor == null) {
                    copy = original;
                }
            return copy;
        }
    
        private void restoreInventory(Player p, ItemStack[] inventory) {
            p.getInventory().setContents(inventory);
        }
    }
     
    Last edited: Jan 25, 2017
Thread Status:
Not open for further replies.

Share This Page