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<Player, Inventory> inventory = new HashMap<Player, Inventory>(); Then i can save it, but then i can't restore it? Any other good ways i can do this? ItemStack? Thanks Gjosse
The way I save inventories is with ItemStack: public HashMap<Player, ItemStack[]> inventory = new HashMap<Player, ItemStack[]>();
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()
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)); }
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.
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
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
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
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.