Solved How to restore inventory from a variable earlier set?

Discussion in 'Plugin Development' started by blue orange, Feb 15, 2021.

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

    blue orange

    I am making a plugin to add an economy to my friend's server, and I am making a trade command, but it requires the player to see the other person's inventory, so I am setting the player's inventory to the inventory of the player they want to trade with. Before I set their inventory, I have a variable that is set to their current inventory, and on InventoryCloseEvent I have a check to see if it is the right inventory being closed, and if so, to set the player's inventory to the variable inventory.
    Code:
    @SuppressWarnings("deprecation")
    public class coinconomy extends JavaPlugin implements Listener {
        public static Inventory trade = Bukkit.getServer().createInventory(null, 54, "Trade"); //the gui inventory
        Inventory restore = Bukkit.getServer().createInventory(null, InventoryType.PLAYER); //player's inventory variable
        //code
            if (cmd.getName().equalsIgnoreCase("cointrade"))
            {
                if(sender instanceof Player)
                {
                    if (args.length == 1)
                    {
                        if (!(Bukkit.getPlayerExact(args[0]) == null))
                        {
                        
                            Player p = Bukkit.getServer().getPlayer(sender.getName());
                            restore = p.getInventory();
            
                            ItemStack istack = new ItemStack(Material.YELLOW_STAINED_GLASS_PANE, 1);
                            ItemMeta imet = istack.getItemMeta();
                        
                            imet.setDisplayName(null);
                            istack.setItemMeta(imet);
            
            
                            trade.setItem(4, istack);
                            trade.setItem(13, istack);
                            trade.setItem(22, istack);
                            trade.setItem(31, istack);
                            trade.setItem(40, istack);
                            trade.setItem(49, istack);
                        
                            p.openInventory(trade);
                            p.getInventory().setContents(getServer().getPlayer(args[0]).getInventory().getContents());
                        }
                        else
                        {
                            sender.sendMessage("This player either does not exist, or is not online right now!");
                        }
                    }
                    else
                    {
                        if (args.length == 0) sender.sendMessage("You need to specify a player to trade with!");
                        if (args.length == 2) sender.sendMessage("Can't trade with multiple people at once!");
                    }
                }
            
                return true;
            }
        
                // If this hasn't happened the value of false will be returned.
            return false;
        }
        //more code...
        @EventHandler
        private void inventoryClose(InventoryCloseEvent event)
        {
            if (event.getInventory() == trade) {
                Bukkit.getServer().getPlayer(event.getPlayer().getName()).getInventory().clear();
            
            
            
            
                Inventory pinv = Bukkit.getServer().getPlayer(event.getPlayer().getName()).getInventory();
                pinv.setContents(restore.getContents());
                Bukkit.getServer().getPlayer(event.getPlayer().getName()).openInventory(pinv); //this is for debugging, so I can see the variable inventory
            
            }
        
        }
     
  2. Offline

    Kars

    You can set the players inventory to another players inventory already?
    So just save the players inventory and restore it afterward? What is the problem?
     
  3. Offline

    blue orange

    Well, it is not restoring the player's inventory, and I am asking why this is and how do i fix it
     
  4. Offline

    SootyHamster

    When we exit of the gui the inv just gets cleared, it doesn't set it to there old inv
     
  5. Offline

    blue orange

    I have done it! I used a hashmap.
     
  6. Offline

    Kars

    PHP:
    Inventory restore Bukkit.getServer().createInventory(nullInventoryType.PLAYER);

    Bukkit.getServer().getPlayer(event.getPlayer().getName()).getInventory().clear();
    Inventory pinv Bukkit.getServer().getPlayer(event.getPlayer().getName()).getInventory();
    pinv.setContents(restore.getContents());
    ^ maybe this here is why?
     
  7. Offline

    davidclue

    Why not just open a gui of a large chest and copy the players inventory into the chest? Like how openinv plugins work.
     
Thread Status:
Not open for further replies.

Share This Page