Save player inventory

Discussion in 'Plugin Development' started by FireCube, Jan 5, 2016.

Thread Status:
Not open for further replies.
  1. hello, i'm trying to save a player inventory to a file (yml or whatever).
    i tried to create a class that implements ConfigurationSerializable, as i saw on the wiki, to save it :
    Code:
    package org.firecube.KitPvp;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.bukkit.configuration.serialization.ConfigurationSerializable;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    
    public class SerializedInventory implements ConfigurationSerializable {
    
        private ItemStack[] stuff = new ItemStack[36];
        private ItemStack[] armor = new ItemStack[4];
    
        @Override
        public Map<String, Object> serialize() {
            Map<String, Object> ret = new HashMap<String, Object>();
            ret.put("stuff", stuff);
            ret.put("armor", armor);
          
            return null;
        }
      
        public SerializedInventory(Map<String, Object> map)
        {
            this.stuff = (ItemStack[]) map.get("stuff");
            this.armor = (ItemStack[]) map.get("armor");
        }
    
        public SerializedInventory(PlayerInventory inventory) {
            stuff = inventory.getContents();
            armor = inventory.getContents();
        }
    
        public ItemStack[] getStuff() {
            return stuff;
        }
    
        public ItemStack[] getArmor() {
            return armor;
        }
    }
    
    
    there's the error :
    Code:
    [21:50:49 ERROR]: Error occurred while disabling KitPvp v1.0 (Is it up to date?)
    java.lang.NullPointerException
            at java.util.HashMap.putMapEntries(Unknown Source) ~[?:1.8.0_65]
            at java.util.HashMap.putAll(Unknown Source) ~[?:1.8.0_65]
            at org.bukkit.configuration.file.YamlRepresenter$RepresentConfigurationSerializable.representData(YamlRepresenter.java:33) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.yaml.snakeyaml.representer.BaseRepresenter.representData(BaseRepresenter.java:94) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.yaml.snakeyaml.representer.BaseRepresenter.representMapping(BaseRepresenter.java:156) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.yaml.snakeyaml.representer.SafeRepresenter$RepresentMap.representData(SafeRepresenter.java:304) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.configuration.file.YamlRepresenter$RepresentConfigurationSection.representData(YamlRepresenter.java:23) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.yaml.snakeyaml.representer.BaseRepresenter.representData(BaseRepresenter.java:94) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.yaml.snakeyaml.representer.BaseRepresenter.representMapping(BaseRepresenter.java:156) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.yaml.snakeyaml.representer.SafeRepresenter$RepresentMap.representData(SafeRepresenter.java:304) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.configuration.file.YamlRepresenter$RepresentConfigurationSection.representData(YamlRepresenter.java:23) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.yaml.snakeyaml.representer.BaseRepresenter.representData(BaseRepresenter.java:94) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.yaml.snakeyaml.representer.BaseRepresenter.representMapping(BaseRepresenter.java:156) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.yaml.snakeyaml.representer.SafeRepresenter$RepresentMap.representData(SafeRepresenter.java:304) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.yaml.snakeyaml.representer.BaseRepresenter.representData(BaseRepresenter.java:94) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.yaml.snakeyaml.representer.BaseRepresenter.represent(BaseRepresenter.java:64) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.yaml.snakeyaml.Yaml.dumpAll(Yaml.java:242) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.yaml.snakeyaml.Yaml.dumpAll(Yaml.java:206) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.yaml.snakeyaml.Yaml.dump(Yaml.java:181) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.configuration.file.YamlConfiguration.saveToString(YamlConfiguration.java:40) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.configuration.file.FileConfiguration.save(FileConfiguration.java:103) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.firecube.Utils.ConfigFile.SaveConfig(ConfigFile.java:44) ~[?:?]
            at org.firecube.KitPvp.KitPvp.onDisable(KitPvp.java:44) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:323) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin(JavaPluginLoader.java:364) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.disablePlugin(SimplePluginManager.java:424) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.disablePlugins(SimplePluginManager.java:417) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.clearPlugins(SimplePluginManager.java:458) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:708) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.Bukkit.reload(Bukkit.java:535) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_65]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_65]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_65]
    
    there's the configFile class :
    Code:
    package org.firecube.Utils;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class ConfigFile {
    
      
      
        private JavaPlugin plugin;
      
        private String fileName;
      
        private File configFile;
      
        public FileConfiguration config;
      
        public ConfigFile(JavaPlugin plugin, String fileName)
        {
            this.plugin = plugin;
            this.fileName = fileName;
            ReloadConfig();
        }
      
        public void ReloadConfig()
        {
            if (configFile == null) {
                configFile = new File(plugin.getDataFolder(), fileName);
            }
            config = YamlConfiguration.loadConfiguration(configFile);
        }
      
        public void SaveConfig()
        {
            if (config == null || configFile == null) {
               return;
           }
           try {
               getConfig().save(configFile);
           } catch (IOException ex) {
               plugin.getLogger().log(Level.SEVERE, "Could not save config to " + configFile, ex);
           }
    
        }
      
        public FileConfiguration getConfig() {
           if (config == null) {
               ReloadConfig();
           }
           return config;
        }
    }
    
    
    and in KitPvp.java, i just call the SaveConfig

    also, i never had this error before the config file contains a SerializedInventory object.


    thanks,

    EDIT by Timtower: changed spoilers into code blocks.
     
    Last edited by a moderator: Jan 5, 2016
  2. Offline

    Zombie_Striker

    First, please stick to JavaNamingConventions: All packagenames should be lower case. All methods should start with a lower case.

    Okay, this tells me something is null (it says on line 44, but since it's just a "try" method, most likely it is wrong).

    Are you sure "GetConfig" does not return anything null? Are you sure you are saving the object properly?
     
    mine-care likes this.
  3. Until i add the SerializedInvemtory, it used to work, so the problem should be with it, but i don't see why...

    bump?

    ok, this class have a problem, i'll just save itemstacks one by one, thanks Zombie, if someone want to see the code i'll use, just ask

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

    Zombie_Striker

    @FireCube
    Only bump after every 24 hours.

    The only way you can solve this problem is if you null check. Just make sure nothing is equal to null.
     
  5. but the problem is that the null error is not in one of my classes, but in java.util.HashMap.putMapEntries(), if i read the error message well
     
  6. Offline

    Zombie_Striker

    @FireCube
    But you see, Java is something that doesn't break often. Because of that, I assume that the problem isn't with java, but the way you are handling the hashmap.

    Again, please null check everything to make sure there nothing equal to null.
     
  7. i found a solution, by a method to save all itemstacks one by one, and it's working, so it's good now, but thanks, and i'll try to respect java naming convention @Zombie_Striker ;)
     
Thread Status:
Not open for further replies.

Share This Page