Saving player-data doesn't work.

Discussion in 'Spigot Plugin Development' started by BurstyCodes, May 20, 2016.

  1. Hey guys,
    I'm trying to create a plugin that saves some player data (name, ip, uuid..).
    But when I start my server it doesn't work. Can you guys check my code and my error and maybe tell me how to fix this error?

    Here is my code:
    Code (Text):
    package me.bursty.lobby.main;

    import java.io.File;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;


    public class Main extends JavaPlugin implements Listener {
       
        static Main plugin;
       
        @Override
        public void onEnable(){
            getLogger().info("rpCore enabled..!");
            getServer().getPluginManager().registerEvents(this, this);
        }
       
        @Override
        public void onDisable(){
            getLogger().info("rpCore disabled..!");
        }

        static File cfile;
        static FileConfiguration config;
        static File folder = new File(Main.plugin.getDataFolder(), "playerdata" + File.separator);
        static File df = Main.plugin.getDataFolder();
     
        public static void create(Player p){
            cfile = new File(df, "playerdata" + File.separator + p.getUniqueId() + ".yml");
            if (!df.exists()) df.mkdir();
            if (!cfile.exists()) {
                try {
                    cfile.createNewFile();
                    config.createSection("Info");
                    config.set("Info.Name", p.getName());
                    config.set("Info.IP", p.getAddress().getAddress().getAddress());
                    config.set("Info.UUID", p.getUniqueId());
                    config.set("Info.Health", p.getHealthScale());
                    config.set("Info.Food-level", p.getFoodLevel());
                } catch(Exception e) {
                    Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Error creating " + cfile.getName() + "!");
                }
            }
            config = YamlConfiguration.loadConfiguration(cfile);
        }
     
        public static File getfolder() {
            return folder;
        }
     
        public static File getfile() {
            return cfile;
        }
     
        public static void load(Player p) {
            cfile = new File(df, "playerdata" + File.separator + p.getUniqueId() + ".yml");
            config = YamlConfiguration.loadConfiguration(cfile);
        }
     
        public static FileConfiguration get() {
            return config;
        }
     
        public static void save() {
            try {
                config.save(cfile);
            } catch(Exception e) {
                Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Error saving " + cfile.getName() + "!");
            }
        }

    }
     
    Here is my error:
    Code (Text):
    [21:25:57 ERROR]: Could not load 'plugins\rpLobby.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.ExceptionInInitializerError
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:133) ~[spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:341) ~[spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:263) [spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.loadPlugins(CraftServer.ja
    va:369) [spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.reload(CraftServer.java:86
    4) [spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at org.bukkit.Bukkit.reload(Bukkit.java:301) [spigot-1.7.10-R0.1-SNAPSHO
    TBuild1646.jar:git-Spigot-1646]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:
    23) [spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    1) [spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServe
    r.java:767) [spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchServerCommand(Craf
    tServer.java:753) [spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at net.minecraft.server.v1_7_R4.DedicatedServer.aB(DedicatedServer.java:
    326) [spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:2
    90) [spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:5
    84) [spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
    :490) [spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
    28) [spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
    Caused by: java.lang.ExceptionInInitializerError
            at java.lang.Class.forName0(Native Method) ~[?:1.8.0_77]
            at java.lang.Class.forName(Unknown Source) ~[?:1.8.0_77]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.jav
    a:64) ~[spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:129) ~[spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            ... 14 more
    Caused by: java.lang.NullPointerException
            at me.bursty.lobby.main.Main.<clinit>(Main.java:32) ~[?:?]
            at java.lang.Class.forName0(Native Method) ~[?:1.8.0_77]
            at java.lang.Class.forName(Unknown Source) ~[?:1.8.0_77]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.jav
    a:64) ~[spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:129) ~[spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            ... 14 more
     
  2. Caused by: java.lang.NullPointerException
    at me.bursty.lobby.main.Main.<clinit>(Main.java:32) ~[?:?]

    There's a null pointer exception on line 32 in your Main class. Something on line 32 is null
     
  3. This is line 32 in my code: static File df = Main.plugin.getDataFolder();
    What do I need to change on that?
     
  4. plugin is null. You never initialized it. Just say:

    plugin = this;

    in your onEnable()
     
    • Agree Agree x 1
  5. When I do that I get an error:
    Code (Text):
    [21:45:43 ERROR]: Could not load 'plugins\rpLobby.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.ExceptionInInitializerError
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:133) ~[spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:341) ~[spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:263) [spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.loadPlugins(CraftServer.ja
    va:369) [spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.reload(CraftServer.java:86
    4) [spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at org.bukkit.Bukkit.reload(Bukkit.java:301) [spigot-1.7.10-R0.1-SNAPSHO
    TBuild1646.jar:git-Spigot-1646]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:
    23) [spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    1) [spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServe
    r.java:767) [spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchServerCommand(Craf
    tServer.java:753) [spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at net.minecraft.server.v1_7_R4.DedicatedServer.aB(DedicatedServer.java:
    326) [spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:2
    90) [spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:5
    84) [spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
    :490) [spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
    28) [spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
    Caused by: java.lang.ExceptionInInitializerError
            at java.lang.Class.forName0(Native Method) ~[?:1.8.0_77]
            at java.lang.Class.forName(Unknown Source) ~[?:1.8.0_77]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.jav
    a:64) ~[spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:129) ~[spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            ... 14 more
    Caused by: java.lang.NullPointerException
            at me.bursty.lobby.main.Main.<clinit>(Main.java:32) ~[?:?]
            at java.lang.Class.forName0(Native Method) ~[?:1.8.0_77]
            at java.lang.Class.forName(Unknown Source) ~[?:1.8.0_77]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.jav
    a:64) ~[spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:129) ~[spigot-1.7.10-R0.1-SNAPSHOTBuild1646.jar:git-Spigot-1646]
            ... 14 more
     
  6. Has line 32 changed now that you added that line in? What's line 32 now?
     
  7. Also could be just an issue with compiling, cause that's the same error as before. I might try a recompile and reload the plugin and see if that helps it.
     
  8. Nope. Whats happening is that it is in an intitializer, so line 32's code will be called well before the onEnable. Move
    = Main.plugin.getDataFolder();
    and
    new File(Main.plugin.getDataFolder(), "playerdata" + File.separator);
    into your onenable and the issue should be solved.
     
    • Agree Agree x 1
  9. Ahhh didn't even notice that. Yup. Didn't look carefully. I'm not really used to people doing that
     
  10. Still getting errors mate. Here is my full code maybe you can solve the problem?

    Code (Text):
    package me.bursty.lobby.main;

    import java.io.File;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;


    public class Main extends JavaPlugin implements Listener {
       
        static Main plugin;
       
        @Override
        public void onEnable(){
            getLogger().info("rpCore enabled..!");
            getServer().getPluginManager().registerEvents(this, this);
            new File(Main.plugin.getDataFolder(), "playerdata" + File.separator);
            Main.plugin.getDataFolder();
            plugin = this;
        }
       
        @Override
        public void onDisable(){
            getLogger().info("rpCore disabled..!");
        }

        static File cfile;
        static FileConfiguration config;
        static File folder = new File(Main.plugin.getDataFolder(), "playerdata" + File.separator);
        static File df = Main.plugin.getDataFolder();
     
        public static void create(Player p){
            cfile = new File(df, "playerdata" + File.separator + p.getUniqueId() + ".yml");
            if (!df.exists()) df.mkdir();
            if (!cfile.exists()) {
                try {
                    cfile.createNewFile();
                    config.createSection("Info");
                    config.set("Info.Name", p.getName());
                    config.set("Info.IP", p.getAddress().getAddress().getAddress());
                    config.set("Info.UUID", p.getUniqueId());
                    config.set("Info.Health", p.getHealthScale());
                    config.set("Info.Food-level", p.getFoodLevel());
                } catch(Exception e) {
                    Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Error creating " + cfile.getName() + "!");
                }
            }
            config = YamlConfiguration.loadConfiguration(cfile);
        }
     
        public static File getfolder() {
            return folder;
        }
     
        public static File getfile() {
            return cfile;
        }
     
        public static void load(Player p) {
            cfile = new File(df, "playerdata" + File.separator + p.getUniqueId() + ".yml");
            config = YamlConfiguration.loadConfiguration(cfile);
        }
     
        public static FileConfiguration get() {
            return config;
        }
     
        public static void save() {
            try {
                config.save(cfile);
            } catch(Exception e) {
                Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Error saving " + cfile.getName() + "!");
            }
        }

    }
     
     
  11. This problem is this:

    static File cfile;
    static FileConfiguration config;
    static File folder = new File(Main.plugin.getDataFolder(), "playerdata" + File.separator);
    static File df = Main.plugin.getDataFolder();

    It's just out there in open space, not in a function or anything. That code gets run first. That entire section needs to be moved into your onEnable. It's ok to declare you static variables out there, but you need to init them in your onEnable after you've done

    plugin = this;
     
  12. But when I put in my onEnable the hole code gets errors. (sorry i'm not that good in coding.. i'm trying to learn it). Can you make it in my code?
     
  13. Code (Text):
    package me.bursty.lobby.main;

    import java.io.File;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;


    public class Main extends JavaPlugin implements Listener {
     
        static JavaPluplugin;
        static File cfile;
        static FileConfiguration config;
        static File folder;
        static File df;
     
        @Override
        public void onEnable(){
            plugin = this;

            folder = new File(plugin.getDataFolder(), "playerdata" + File.separator);
            df = plugin.getDataFolder();

            getLogger().info("rpCore enabled..!");
            getServer().getPluginManager().registerEvents(this, this);          
        }
     
        @Override
        public void onDisable(){
            getLogger().info("rpCore disabled..!");
        }

     
        public static void create(Player p){
            cfile = new File(df, "playerdata" + File.separator + p.getUniqueId() + ".yml");
            if (!df.exists()) df.mkdir();
            if (!cfile.exists()) {
                try {
                    cfile.createNewFile();
                    config.createSection("Info");
                    config.set("Info.Name", p.getName());
                    config.set("Info.IP", p.getAddress().getAddress().getAddress());
                    config.set("Info.UUID", p.getUniqueId());
                    config.set("Info.Health", p.getHealthScale());
                    config.set("Info.Food-level", p.getFoodLevel());
                } catch(Exception e) {
                    Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Error creating " + cfile.getName() + "!");
                }
            }
            config = YamlConfiguration.loadConfiguration(cfile);
        }
        public static File getfolder() {
            return folder;
        }
        public static File getfile() {
            return cfile;
        }
        public static void load(Player p) {
            cfile = new File(df, "playerdata" + File.separator + p.getUniqueId() + ".yml");
            config = YamlConfiguration.loadConfiguration(cfile);
        }
        public static FileConfiguration get() {
            return config;
        }
        public static void save() {
            try {
                config.save(cfile);
            } catch(Exception e) {
                Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Error saving " + cfile.getName() + "!");
            }
        }

    }
     
  14. Wow dude.. you are a-m-a-z-i-n-g THANKS!
     
    • Like Like x 1
  15. np, just updated it. Got rid of some lines you don't need
     
  16. Sorry but can you help me with one more thing? Normally it need to create a fily with my uuid (<uuid>.yml). But when I join the server it doesn't create that file.. Can you check-out my code and check if there is something wrong?
     
  17. Sorry but can you help me with one more thing? Normally it need to create a fily with my uuid (<uuid>.yml). But when I join the server it doesn't create that file.. Can you check-out my code and check if there is something wrong?