Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am working on a bukkit plugin. One thing I want this plugin to do is teleport you to other worlds on the server. However, I don't want the player's inventory to be saved.

Right now, I have this code:

File playerInvConfigFile = new File(plugin.getDataFolder() + File.separator + "players" + File.separator + sender.getName(), "inventory.yml");
FileConfiguration pInv = YamlConfiguration.loadConfiguration(playerInvConfigFile);

        Player user = (Player)sender;
        PlayerInventory inv = user.getInventory();

        String worldName = user.getWorld().getName();
        int i = 0;
        for (ItemStack stack : inv.getContents()){
            i++;
            String start = worldName + "." + Integer.toString(i);

            pInv.set(start + ".amount", stack.getAmount());
            pInv.set(start + ".durability", stack.getDurability());
            pInv.set(start + ".type", stack.getTypeId());
        }
        inv.clear();

This works great, but there is a big problem: enchantments are now saved! This is a big problem. I want to know how to both save enchantments to a file, and then read them back in and set them!

Thank you all!

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

There are two methods on ItemStack that you will find of use:

The first is getEnchantments, which returns a Map, which you will need to use an iterator to go through and save off the keys and values.

Map<Enchantment,Integer> enchantments = itemStack.getEnchantments();
Iterator<Entry<Enchantment,Integer>> iter = enchantments.entrySet().iterator();
while(iter.hasNext()){
    Entry<Enchantment,Integer> entry = iter.next();
    //do something with the entry
}

When you restore the inventory, you need to create a new HashMap, restore the values within the original map, and restore them to the ItemStack, using addEnchantments, which takes as its sole parameter a Map, so you deserialize whatever you serialized before, and then add it to the ItemStack like so:

itemStack.addEnchantments(enchantments);
share|improve this answer
    
thank you very much, but one thing that troubles me is do i just call all of them from the file i am saving them to? and then do the addEnchantments? –  HcgRandon Mar 14 '12 at 12:50
    
After saving them off to file, when you load them in, there is an Enchantment.getByName function, and an Integer.parseInt (or Integer.decode if you prever) function to bring them back into memory, and then use the Map's "put" function to load up the Map. –  PlayDeezGames Mar 14 '12 at 13:52
    
I'm facing the same problem right now. The concept is clear, but the HashMap is not working. Do you have any example code? I would really appreciate it! Thanks! –  Lars Petersen May 16 '12 at 13:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.