Saving player inventory

Working on a command allowing to transfer worlds, I’m trying to save the player inventory.

public void savePlayerInv(Player p, World w){
    File playerInvConfigFile = new File(plugin.getDataFolder() + File.separator + "players" + File.separator + p.getName(), "inventory.yml");
    FileConfiguration pInv = YamlConfiguration.loadConfiguration(playerInvConfigFile);
    PlayerInventory inv = p.getInventory();
    int i = 0;

    for (ItemStack stack : inv.getContents()) {
        //increment integer
        String startInventory = w.getName() + ".inv." + Integer.toString(i);

        //save inv
        pInv.set(startInventory + ".amount", stack.getAmount());
        pInv.set(startInventory + ".durability", Short.toString(stack.getDurability()));
        pInv.set(startInventory + ".type", stack.getTypeId());
        //pInv.set(startInventory + ".enchantment", stack.getEnchantments());
        //TODO add enchant saveing

    i = 0;
    for (ItemStack armor : inv.getArmorContents()){
            String startArmor = w.getName() + ".armor." + Integer.toString(i);

            //save armor
            pInv.set(startArmor + ".amount", armor.getAmount());
            pInv.set(startArmor + ".durability", armor.getDurability());
            pInv.set(startArmor + ".type", armor.getTypeId());
            //pInv.set(startArmor + ".enchantment", armor.getEnchantments());

    //save exp
    if (p.getExp() != 0) {
            pInv.set(w.getName() + ".exp", p.getExp());

The problem is on line 130:

pInv.set(startInventory + ".amount", stack.getAmount());

I get this stacktrace:

2012-03-21 13:23:25 [SEVERE] null
org.bukkit.command.CommandException: Unhandled exception executing command 'wtp' in plugin Needs v1.0
at org.bukkit.command.PluginCommand.execute(
at org.bukkit.command.SimpleCommandMap.dispatch(
at org.bukkit.craftbukkit.CraftServer.dispatchCommand(
at net.minecraft.server.NetServerHandler.handleCommand(
at net.minecraft.server.NetServerHandler.a(
at net.minecraft.server.Packet3Chat.handle(
at net.minecraft.server.NetworkManager.b(
at net.minecraft.server.NetServerHandler.a(
at net.minecraft.server.NetworkListenThread.a(
at net.minecraft.server.MinecraftServer.w(
    Caused by: java.lang.NullPointerException
at com.devoverflow.improved.needs.commands.CommandWorldtp.savePlayerInv(
at com.devoverflow.improved.needs.commands.CommandWorldtp.onCommand(
at org.bukkit.command.PluginCommand.execute(
... 12 more


When you do inv.getContents(), you retrieve every slot in the player inventory, including the empty slots. These empty slots return null because there is nothing in them.

To fix, add an if statement right after you start the for loop:

for (ItemStack stack : inv.getContents()) {
    if (stack == null) continue;
    // And keep everything else the same

The problem you might encounter later would be replacing everything in the exact same slot it came from. I suggest using a HashMap or something to keep track of which slot the item came from.

Leave a Reply

Your email address will not be published. Required fields are marked *