Solved How do I set a player's inventory to a new Inventory?

Discussion in 'Plugin Development' started by MaxFireIce, Nov 7, 2016.

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

    MaxFireIce

    I'm working on a plugin that saves the players old inventory and gives them a new one. However, nothing I have tried is working. Is there some method that sets the players inventory to a new inventory?
     
  2. Offline

    Zombie_Striker

    @MaxFireIce
    1. Instead of saying that what you tried doesn't work, why not post it so we can tell you why it does not work?
    2. Use .setContents() to set the contents of an inventory.
     
  3. Offline

    MaxFireIce

    @Zombie_Striker

    umm, ok..

    Code:
    FileConfiguration config = Main.plugin.getConfig();
    
    Code:
        @EventHandler
        public void onPlayerTeleport(PlayerTeleportEvent event){
            String oldWorld = event.getFrom().getWorld().toString();
            String newWorld = event.getTo().getWorld().toString();

    Code:
            if (!config.contains(playerName + ".Worlds." + newWorld + ".Inventory")){
                player.getInventory().clear();
                PlayerInventory playerNewInv = player.getInventory();
                config.addDefault(playerName + ".Worlds." + newWorld + ".Inventory", playerNewInv);
                Main.plugin.saveConfig();
            } else {
                player.getInventory().setContents((ItemStack[]) config.get(playerName + ".Worlds." + newWorld + ".Inventory"));
            }
    And then it says it cant convert from player inventory to itemstack, so what do i do?

    I tried set contents but that is where the error is because it requires an itemstack parameter
     
  4. Offline

    Zombie_Striker

    Please do not abuse static. Just pass the main class's instance through this other class's constructor.

    Main problem: The ".setContents" method requires an ItemStack[], not an inventory instance. Instead of saving the whole inventory instance to the config, just save the contents of the inventory. You can get the contents by using "Inventory#getContents()".
     
  5. Offline

    MaxFireIce

    @Zombie_Striker

    What is static abuse? Does it really matter? Im curious, im not exactly familiar with static, i just know that it allows the use of something without instantiation.

    Btw, thank you for that :)

    To save inventory:

    Code:
    ItemStack[] playerInv = player.getInventory().getContents();
    
    Saving it to config:

    Code:
    if (!config.contains(playerName + ".Worlds." + oldWorld + ".Inventory" + playerInv)){
    config.addDefault(playerName + ".Worlds." + oldWorld + ".Inventory", playerInv);
    Main.plugin.saveConfig();
    }
    
    To set the inventory of the player to the one in the config:

    Code:
    player.getInventory().setContents((ItemStack[]) config.get(playerName + ".Worlds." + newWorld + ".Inventory"));
    
     
    Last edited: Nov 7, 2016
Thread Status:
Not open for further replies.

Share This Page