[Solved] Inventory to file

Discussion in 'Plugin Development' started by Aza24, Apr 15, 2012.

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

    Aza24

    What is the best way to convert a ItemStack[] to a string and back so it can be used in a configuration file?
     
  2. Just write the itemstack into your config. You will be amazed that that works ;)

    PS: It's because itemstack has the ConfigurationSerializable implemented
     
  3. Offline

    Aza24

    I know how to do that but I want to save an entire inventory, so how do you save an array of ItemStacks?
     
  4. Offline

    r0306

    You could write them one by one into a text file using a for loop. And then call them line by line when you need to use them again.
     
  5. Offline

    Neodork

    Yeah, or you could just print it into a list...

    Code:java
    1.  
    2. plugin.youryamlfile.set("your.path",Arrays.asList(something));
    3.  
     
  6. Offline

    Aza24

    I get this error though when retrieving it:

    Code:java
    1. 13:23:20 [SEVERE] null
    2. org.bukkit.command.CommandException: Unhandled exception executing command 'bn'
    3. in plugin BattleNight v2.0...
    4. Caused by: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [
    5. Lorg.bukkit.inventory.ItemStack;
    6. at me.limebyte.battlenight.core.Configuration.PlayerData.restore(PlayerD
    7. ata.java:106)
    8. at me.limebyte.battlenight.core.Util.restorePlayer(Util.java:74)
    9. at me.limebyte.battlenight.core.Commands.onCommand(Commands.java:31)
    10. at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
    11. ... 14 more
    12.  


    Source:
    me.limebyte.battlenight.core.Configuration.PlayerData.restore(PlayerData.java:105)
     
  7. Offline

    Njol

    Use .toArray(new ItemStack[0]) and you won't need the cast.
     
  8. Offline

    Aza24

    Don't I need to set a size for the Array?
     
  9. Offline

    Njol

    No, read the javadoc of the function. It creates a new array of the same type if it's too small, thus passing a zero size array is common.
     
Thread Status:
Not open for further replies.

Share This Page