Solved Store players inventory as an item stack in config

Discussion in 'Plugin Development' started by Josh014, Jan 16, 2014.

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

    Josh014

    Hey,
    How can I store a players inventory with the enchantments and the item state in the config?
    I tried to do it by my self but I didn't come that far...

    My code:
    Code:java
    1. public static ItemStack[] getWholeInventory(Player player) {
    2. player.getInventory().getContents();
    3. player.getInventory().getHelmet();
    4. player.getInventory().getChestplate();
    5. player.getInventory().getLeggings();
    6. player.getInventory().getBoots();
    7. return null;
    8. }


    Thank you,
     
  2. Offline

    Garris0n

    ItemStack is ConfigurationSerializable, that takes care of most of the work. Simply save each item in the inventory with the key of the number it's in and the value of ItemStack.serialize(). Then save the armor with just "helmet, chestplate, leggings, boots". When it's time to load them up, use config.getItemStack().
     
  3. Offline

    metalhedd

  4. Offline

    Josh014

    metalhedd
    Hmm... I get it a bit but I don't see the part that he saves an inventory into the config.

    metalhedd
    Alright I got this now for storing the items in the config. I didn't test it but I want to have a second look to it.

    Code:
    Code:java
    1. public static ItemStack[] storeWholeInventoryInConfig(Player player, String name) {
    2.  
    3. List<ItemStack> inventoryContents = new ArrayList<ItemStack>();
    4. List<ItemStack> armourContents = new ArrayList<ItemStack>();
    5.  
    6. for (ItemStack inventory : player.getInventory().getContents())
    7. if ((inventory != null) && (inventory.getType() != Material.AIR)) inventoryContents.add(inventory);
    8. plugin.getConfig().set("Inventory." + name + ".InventoryContents", inventoryContents);
    9.  
    10. for (ItemStack armour : player.getInventory().getArmorContents())
    11. if ((armour != null) && (armour.getType() != Material.AIR)) armourContents.add(armour);
    12. plugin.getConfig().set("Inventory." + name + ".ArmourContents", armourContents);
    13.  
    14. plugin.saveConfig();
    15. return null;
    16. }


    metalhedd
    Alright I did that and I got an error in the console that says that there is an error at the line:
    Code:java
    1. plugin.getConfig().set("Inventory." + name + ".InventoryContents", inventoryContents);
    2.  


    Error:
    Code:
    SEVERE: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'test
    ' in plugin Teams v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:19
    2)
            at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServe
    r.java:543)
            at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerCon
    nection.java:923)
            at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java
    :803)
            at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java
    :28)
            at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat
    .java:47)
            at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146
    )
            at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134)
            at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    45)
            at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    43)
            at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    35)
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :447)
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17)
    Caused by: java.lang.NullPointerException
            at teams.main.Inventory.storeWholeInventoryInConfig(Inventory.java:53)
            at teams.commands.CopyThis.onCommand(CopyThis.java:23)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
            ... 13 more
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  5. Offline

    metalhedd

    looks to me like 'name' is null.
     
  6. Offline

    Josh014

  7. Offline

    metalhedd

    maybe 'plugin' ? those are the only 2 variables on that line which could possibly be null, so you'd have to figure out which one it is, add some logging at the top your method to be sure.
     
  8. Offline

    Josh014

    metalhedd
    The plugin. is necessary because this is in an other class file. But what I think I have to do is something with config list to store the itemstack in...?
     
  9. Offline

    metalhedd


    No, everything you have there is fine, if that error message is accurate and that is really line 53 that you've pasted, then 'plugin' or 'name' is null. those are the only 2 possibilities that I see. I know that its necessary to have a reference to your plugin, I'm just saying that you probably haven't set it yet, and it's still null.
     
  10. Offline

    Josh014

    metalhedd
    Hmm I send the whole code of my main class and my Inventory class

    Inventory:
    Code:java
    1. package teams.main;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Color;
    7. import org.bukkit.Material;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.inventory.ItemStack;
    10. import org.bukkit.inventory.meta.ItemMeta;
    11. import org.bukkit.inventory.meta.LeatherArmorMeta;
    12.  
    13. public class Inventory {
    14. static main plugin;
    15.  
    16. public Inventory(main instance){
    17. plugin = instance;
    18. }
    19.  
    20.  
    21. public ItemStack dyeLeatherArmour(Material material, int r, int g, int b){
    22. ItemStack armour = new ItemStack(material);
    23. LeatherArmorMeta lam = (LeatherArmorMeta) armour.getItemMeta();
    24.  
    25. lam.setColor(Color.fromBGR(r,g,b));
    26. armour.setItemMeta(lam);
    27.  
    28. return armour;
    29. }
    30.  
    31. public ItemStack setNameLore(ItemStack material, String name, List<String> lore){
    32. if (((material == null) || material.getType() == Material.AIR)
    33. || (name == null && lore == null))
    34. return null;
    35.  
    36. ItemMeta im = material.getItemMeta();
    37. if(name != null)
    38. im.setDisplayName(name);
    39. if(lore != null)
    40. im.setLore(lore);
    41.  
    42. material.setItemMeta(im);
    43. return material;
    44. }
    45.  
    46. public static ItemStack[] storeWholeInventoryInConfig(Player player, String name) {
    47.  
    48. List<ItemStack> inventoryContents = new ArrayList<ItemStack>();
    49. List<ItemStack> armourContents = new ArrayList<ItemStack>();
    50.  
    51. for (ItemStack inventory : player.getInventory().getContents())
    52. if ((inventory != null) && (inventory.getType() != Material.AIR)) inventoryContents.add(inventory);
    53. plugin.getConfig().set("Inventory." + name + ".InventoryContents", inventoryContents);
    54.  
    55. for (ItemStack armour : player.getInventory().getArmorContents())
    56. if ((armour != null) && (armour.getType() != Material.AIR)) armourContents.add(armour);
    57. plugin.getConfig().set("Inventory." + name + ".ArmourContents", armourContents);
    58.  
    59. player.sendMessage("You have added your inventory to the config");
    60. plugin.saveConfig();
    61. return null;
    62. }
    63.  
    64. }
    65.  


    Main:
    Code:java
    1. package teams.main;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.plugin.PluginDescriptionFile;
    10. import org.bukkit.plugin.PluginManager;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. import teams.commands.CopyThis;
    14. import teams.commands.assignall;
    15. import teams.commands.check;
    16. import teams.commands.leave;
    17. import teams.commands.removeall;
    18. import teams.commands.spawnset_blueTeam;
    19. import teams.commands.spawnset_lobby;
    20. import teams.commands.spawnset_redTeam;
    21.  
    22. public class main extends JavaPlugin implements Listener{
    23. public final Logger logger = Logger.getLogger("minecraft");
    24. public static main plugin;
    25.  
    26.  
    27. public void onEnable() {
    28.  
    29. PluginDescriptionFile pdfFile = this.getDescription();
    30. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled");
    31. PluginManager pm = this.getServer().getPluginManager();
    32. getServer().getPluginManager().registerEvents(this, this);
    33. getConfig().options().copyDefaults(true);
    34. Team.clearTeams();
    35. saveConfig();
    36.  
    37. // Register Events
    38.  
    39. // Register Commands:
    40. getCommand("test").setExecutor(new CopyThis (this));
    41. getCommand("assignall").setExecutor(new assignall (this));
    42. getCommand("check").setExecutor(new check (this));
    43. getCommand("leave").setExecutor(new leave (this));
    44. getCommand("removeall").setExecutor(new removeall (this));
    45. getCommand("setspawnblue").setExecutor(new spawnset_blueTeam (this));
    46. getCommand("setspawnred").setExecutor(new spawnset_redTeam (this));
    47. getCommand("setlobby").setExecutor(new spawnset_lobby (this));
    48.  
    49. // Config register
    50. CopyThis.plugin = this;
    51. assignall.plugin = this;
    52. check.plugin = this;
    53. leave.plugin = this;
    54. removeall.plugin = this;
    55. Teleport.plugin = this;
    56. Inventory.plugin = this;
    57. spawnset_blueTeam.plugin = this;
    58. spawnset_redTeam.plugin = this;
    59. spawnset_lobby.plugin = this;
    60.  
    61. }
    62.  
    63. public void onDisable() {
    64.  
    65. PluginDescriptionFile pdfFile = this.getDescription();
    66. this.logger.info(pdfFile.getName() + " Has Been Disabled");
    67. Team.clearTeams();
    68. saveConfig();
    69.  
    70. }
    71.  
    72. public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
    73. Player player = (Player)sender;
    74. if(commandLabel.equalsIgnoreCase("savelocation")){
    75. Teleport.storeLocationInConfig(player.getLocation(), "test", "x", "y", "z", "yaw", "pitch");
    76. }
    77. if(commandLabel.equalsIgnoreCase("gotolocation")){
    78. Teleport.teleportToLocation(player, player.getWorld(), "test", "x", "y", "z", "yaw", "pitch");
    79. }
    80.  
    81. return false;
    82. }
    83.  
    84. }
    85.  


    Edit:
    Don't mind the 2 commands in the main class it was for testing something.
     
  11. Offline

    Drkmaster83

    A whole paste of the code would be great in further determining your issue. Perhaps your think you're setting it correctly and you aren't... We could identify the issue.
     
  12. Offline

    Josh014

  13. Offline

    metalhedd

    well, 'plugin' looks ok, but we never see the code which calls storeWholeInventoryInConfig, the problem is probably passing null in as 'name' there.
     
  14. Offline

    Josh014

    metalhedd
    Alright I'm gonna remove the "name" and I'll say if it worked or not.

    metalhedd
    I got the same error as before so it seems like there is no problem with the 'name'?

    Code:
    Code:java
    1. public static ItemStack[] storeWholeInventoryInConfig(Player player) {
    2.  
    3. List<ItemStack> inventoryContents = new ArrayList<ItemStack>();
    4. List<ItemStack> armourContents = new ArrayList<ItemStack>();
    5.  
    6. for (ItemStack inventory : player.getInventory().getContents())
    7. if ((inventory != null) && (inventory.getType() != Material.AIR)) inventoryContents.add(inventory);
    8. plugin.getConfig().set("Inventory." + ".InventoryContents", inventoryContents);
    9.  
    10. for (ItemStack armour : player.getInventory().getArmorContents())
    11. if ((armour != null) && (armour.getType() != Material.AIR)) armourContents.add(armour);
    12. plugin.getConfig().set("Inventory." + ".ArmourContents", armourContents);
    13.  
    14. player.sendMessage("You have added your inventory to the config");
    15. plugin.saveConfig();
    16. return null;
    17. }


    The problem is probably something stupid ._.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  15. Offline

    metalhedd

    paste the full stack trace and which line number it matches up with above
     
  16. Offline

    Josh014

    metalhedd

    Line 53 is line 8 in the last post I sent.

    Code:
    SEVERE: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'test
    ' in plugin Teams v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:19
    2)
            at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServe
    r.java:543)
            at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerCon
    nection.java:923)
            at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java
    :803)
            at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java
    :28)
            at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat
    .java:47)
            at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146
    )
            at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134)
            at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    45)
            at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    43)
            at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    35)
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :447)
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17)
    Caused by: java.lang.NullPointerException
            at teams.main.Inventory.storeWholeInventoryInConfig(Inventory.java:53)
            at teams.commands.CopyThis.onCommand(CopyThis.java:23)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
            ... 13 more
     
     
    >
     
  17. Offline

    metalhedd

    Hmm... well it REALLY looks like it's 'plugin' that's null then.. but I can't see why... The way you're setting it is really wierd and probably not 'good' but it looks like it's set. try adding:

    Bukkit.getLogger().info("Plugin is: " + plugin);

    at the very top of that method and see what it prints out
     
  18. Offline

    Josh014

    metalhedd
    Plugin is: null

    So... how to fix this?
     
  19. Offline

    metalhedd

    I'm not sure why it's not set to be honest, something somewhere else in your code could be un-setting it, I dunno. The only way to really 'fix' it is to rewrite a bunch of code. The part where you set each classes 'plugin' property to 'this' is particularly troubling... You should be instantiating those classes, saving a reference to the instance, and calling the methods on that.
     
  20. Offline

    Josh014

    metalhedd
    And if I add this method to my main class and look what happens?
     
  21. Offline

    metalhedd

    It'd probably solve your immediate problem, but you've still gotta learn how to work with classes, and instances of classes, properly. Your code seems to demonstrate that you don't really understand how all of that works yet, so you'd be better off taking this opportunity to sort it out now before you get too far into your plugin and have to rewrite even more.
     
  22. Offline

    Josh014

    metalhedd Hmm but it is still weird because in my teleport class the plugin.getConfig etc does still work. And I've put the code in my main class and it still gives the error at the line:
    Code:java
    1. getConfig().set("Inventory." + name + ".InventoryContents", inventoryContents);
     
  23. Offline

    metalhedd

    now it's gotta be 'name', use the logger ;)
     
  24. Offline

    Josh014

    metalhedd
    Nvm haha I forgot to remove plugin at another line... Stupid me... But yea this works :)
    Now the next thing is to get the itemstack out of the config. ;o

    metalhedd I have a question if I get the itemstack back out of the config does it know where the items belong in my inventory and places armour things at the armour places etc?

    Sorry because of lagg I posted it twice :/ I thought it wasn't loading :3

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  25. Offline

    Josh014

    metalhedd how can I get the items back out of the config and getting it back in the inventory of the player where the items belongs to be?
     
  26. Offline

    metalhedd

    It's all in the original thread I linked to. I posted code to retrieve the list as well.
     
  27. Offline

    Josh014

    metalhedd
    Alright I got it working now. Only there is one problem, it doesn't place the items on the exact place where they need to be when I saved my inventory to the config. Like the armour isn't on the armour places etc.
     
  28. Offline

    Josh014

    metalhedd
    How can I do it like the items will be placed where they stood when I saved my inventory?

    Code:
    Code:java
    1. @SuppressWarnings("unchecked")
    2. public static ItemStack[] getWholeInventoryOutConfig(Player player, String name, Boolean clear) {
    3.  
    4. List<ItemStack> InventoryContents = (List<ItemStack>) plugin.getConfig().getList("Inventory." + name + ".InventoryContents");
    5. List<ItemStack> ArmourContents = (List<ItemStack>) plugin.getConfig().getList("Inventory." + name + ".ArmourContents");
    6.  
    7. if(plugin.getConfig().get("Inventory." + name) == null) {
    8. player.sendMessage("Unknown name: '" + name + "'");
    9. return null;
    10. }
    11.  
    12. if(clear == true) {
    13. player.getInventory().clear();
    14. player.getInventory().setArmorContents(null);
    15. }
    16.  
    17. for (ItemStack inventory : InventoryContents) {
    18. player.getInventory().addItem(inventory);
    19. }
    20.  
    21. for (ItemStack armour : ArmourContents) {
    22. player.getInventory().addItem(armour);
    23. }
    24.  
    25. return null;
    26. }
     
  29. Offline

    metalhedd

    the positions aren't saved because empty slots are simply removed, you will have to store the empty slots, maybe as Material.AIR stacks. in order to retain the positions.
     
  30. Offline

    Josh014

    metalhedd likes this.
Thread Status:
Not open for further replies.

Share This Page