Saving Inventory.

Discussion in 'Plugin Development' started by gjossep, Jun 5, 2012.

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

    gjossep

    Hello,
    Is there anyway that i can save a persons inventory to a hash map, and then latter give it back?

    I already tried with
    PHP:
        public Map<PlayerInventoryinventory = new HashMap<PlayerInventory>();
    Then i can save it, but then i can't restore it?

    Any other good ways i can do this? ItemStack?

    Thanks
    Gjosse
     
  2. Offline

    Chipmunk9998

    The way I save inventories is with ItemStack:

    public HashMap<Player, ItemStack[]> inventory = new HashMap<Player, ItemStack[]>();
     
  3. Offline

    gjossep

    But will this save all of the inventory?

    If yes, how do you save, and restore it?

    Thanks
     
  4. Offline

    Chipmunk9998

    To save to it, use this:

    Code:
    inventory.put(player, player.getInventory().getContents());
    To put the contents into someone's inventory, use this:

    Code:
    player.getInventory().setContents(inventory.get(player));
    Edit: And this won't work for armor, you need to do that separately with player.getInventory().setArmorContents()
     
    angelofdev likes this.
  5. Offline

    cdncampbell

    Here are some snips of code from my plugin that do exactly that:

    Code:
    //Equiped Armor
    private Map<Player, ItemStack[]> pArmor = new HashMap<Player, ItemStack[]>();
    //Inventory
    private Map<Player, ItemStack[]> pItems = new HashMap<Player, ItemStack[]>();
    //Item Currently In Hand
    private Map<Player, ItemStack> pHand = new HashMap<Player, ItemStack>();
    
    then you get the instance of a player (p), and insert the objects to the hashmap.

    Code:
    pArmor.put(p, p.getInventory().getArmorContents());
    pItems.put(p, p.getInventory().getContents());
    pHand.put(p, p.getItemInHand());
    
    then you want to give it back at a later time:

    Code:
    PlayerInventory pi = p.getInventory();
     
    /** Checking to see of the player has inventory or not; to avoid exceptions */
    if (pArmor.containsKey(p)) {
      pi.setArmorContents(pArmor.get(p));
    }
     
    if (pItems.containsKey(p)) {
      pi.setContents(pItems.get(p));
    }
     
    if (pHand.containsKey(p)) {
      pi.setItemInHand(pHand.get(p));
    }
     
    Badeye and hawkfalcon like this.
  6. Offline

    gjossep

    Thanks, that really helped, but how can i make it work with Enchanted items?
     
  7. Offline

    cdncampbell

    The enchanted item is an object within the respective itemstack. So if an enchanted item is in the player's inventory it will be part of the p.getInventory.getContents() list, or the p.getItemInHand() object.
     
  8. Offline

    gjossep

    Ok, Thanks, just a question, its not related to this topic, but in my plugin i teleport everyone to a location, but they spawn on the same place, how can i make it so its random but still around that location?

    Thanks
    Gjosse
     
  9. Offline

    cdncampbell

    You can use a random number between 1 and (number of players on server) and add the number to adjust the x/y coordinate. It should space most people out ok, some may still be stacked on top of each other :)
     
  10. Offline

    gjossep

    Yes thats what i though, but then i tried it and it kicked me of, saying Iliegle position....

    This is what i tried:
    Code:
     Random ran = new Random(20);
    Location spawnLocation = Bukkit.getWorld("World").getSpawnLocation();
     
    for (Player i : joined)
            {
                int blockX = spawnLocation.getBlockX()+ran.nextInt();
                int blockZ = spawnLocation.getBlockZ()+ran.nextInt();
                Location spawn = new Location(Bukkit.getWorld("HungerGames"), blockX, blockZ, spawnLocation.getZ());
                i.teleport(spawn);
    }
    Thanks
    Gjosse
     
  11. Offline

    cdncampbell

    From how the constructor looks like in the docs, you have an incorrect initialization of location:

    Code:
    Location (final World world, final double x, final double y, final double z)
    What I see there, you have:
    Code:
     new Location(World, X, Z, Z)
    not
    Code:
     new Location(World,X,Y,Z)
    Although, I do have to admit I haven't messed about too much with teleporting.
     
Thread Status:
Not open for further replies.

Share This Page