Jump to content

Set inventory of offline player


vemacs

Recommended Posts

Suppose I have 2 ItemStack[] s, and want to write them to the inventory of an offline player.

 

How would I do that, given the player name?

 

Specifically, something like:

 

public void setOfflineInventory(String playername, ItemStack[] contents, ItemStack[] armor) {
//do stuff here
}

Would I have to go into an NBT editor? And if so, are there any readily available NBT libraries for Forge?

 

Thanks in advance.

Link to comment
Share on other sites

Suppose I have 2 ItemStack[] s, and want to write them to the inventory of an offline player.

 

How would I do that, given the player name?

You can use something like this:

// modify playerNbt
try {
    File playersDirectory = new File(saveHandler.getSaveDirectory(), "players");

    File temp = new File(playersDirectory, username + ".dat.tmp");
    File playerFile = new File(playersDirectory, username + ".dat");
    CompressedStreamTools.writeCompressed(playerNbt, new FileOutputStream(temp));

   if (playerFile.exists()) {
        playerFile.delete();
    }
    temp.renameTo(playerFile);
} catch (Exception e) {
    logger.warning("Failed to save player data for " + username);
}

Untested, but should work ;)

Would I have to go into an NBT editor? And if so, are there any readily available NBT libraries for Forge?

There is already an NBT library built in Vanilla Minecraft, obviously :P

 

Thanks, but forgive my noobishness, would I use something like this (haven't had a chance to test it yet) :

 

  public void setOfflineInventory(String username, net.minecraft.entity.player.InventoryPlayer
inventory) {
    SaveHandler saveHandler =(SaveHandler)MinecraftServer.getServer().getConfigurationManager().worldServers[0].getSaveHandler();
    NBTTagCompound playerNbt = saveHandler.getPlayerData(username);
    //set inventory
    this.playerNBT.set("Inventory", ((CraftInventoryPlayer)inventory).getInventory().a(new NBTTagList()));
try {
    File playersDirectory = new File(saveHandler.getSaveDirectory(), "players");

    File temp = new File(playersDirectory, username + ".dat.tmp");
    File playerFile = new File(playersDirectory, username + ".dat");
    CompressedStreamTools.writeCompressed(playerNbt, new FileOutputStream(temp));

   if (playerFile.exists()) {
        playerFile.delete();
    }
    temp.renameTo(playerFile);
} catch (Exception e) {
    logger.warning("Failed to save player data for " + username);
}
  }

 

Link to comment
Share on other sites

CraftInventoryPlayer? You are using Forge, not bukkit :P

You can use something like this:

InventoryPlayer inventory = new InventoryPlayer(null);
inventory.readFromNBT(playerNbt.getTagList("Inventory"));
// modify the inventory
playerNbt.setTagList("Inventory", inventory.writeToNbt(new NBTTagList());

 

Use with caution, it might cause NPE's since the Player instance in the Inventory is null. E.g. addItemStackToInventory will not work.

 

Erm, right. That was cobbled together from copypasted code.

 

Could you provide a basic example for the loading and saving of the inventory? I can't seem to find any detailed documentation of that.

I'm not overly familiar with the way forge works, but assuming I have the InventoryPlayer loaded, would I do:

 

public void setOfflineInventory(String username, ItemStack[] contents, ItemStack[] armor) {
    //load username's inventory as an InventoryPlayer called inventory
    inventory.armorInventory = armor;
    inventory.mainInventory = contents;
    //save username's inventory and cleanup
}

 

Thanks in advance!

 

Link to comment
Share on other sites

Combine the first I posted with the second.

The first loads / saves the Player NBT, the second reads the inventory from it and saves the modified inventory to it.

Here's my class:

	public void setOfflineInventory(String username, ItemStack[] contents, ItemStack[] armor) {
	SaveHandler saveHandler = (SaveHandler)DimensionManager.getWorld(0).getSaveHandler();
	NBTTagCompound playerNbt = saveHandler.getPlayerData(username);
	InventoryPlayer inventory = new InventoryPlayer(null);
	inventory.readFromNBT(playerNbt.getTagList("Inventory"));
	inventory.armorInventory = armor;
	inventory.mainInventory = contents;
	playerNbt.setTag("Inventory", inventory.writeToNBT(new NBTTagList()));
	try {
		File playersDirectory = new File(saveHandler.getSaveDirectory(), "players");

		File temp = new File(playersDirectory, username + ".dat.tmp");
		File playerFile = new File(playersDirectory, username + ".dat");
		CompressedStreamTools.writeCompressed(playerNbt, new FileOutputStream(temp));

		if (playerFile.exists()) {
			playerFile.delete();
		}
		temp.renameTo(playerFile);
	} catch (Exception e) {
		logger.warning("Data failed to save for " + username);
	}
}

 

I've fixed some minor typos, but I can't seem to find those methods for NBTTagCompound and SaveHandlerMP, respectively.

 

I'm trying setTag instead of setTagList for NBTTagCompound, I'm not sure about how it would work, but it doesn't report any errors.

 

I've also removed getConfigurationManager() from the 1st line of the class, and used DimensionManager instead.

 

For further stability, I probably should get the last logged in world of the player, and add another parameter to the method.

 

As this is obviously going to be part of a SMP mod, should I use SaveHandlerMP instead of SaveHandler?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.