Solved Per-User .yml files

Discussion in 'Plugin Help/Development/Requests' started by redtsch, May 24, 2016.

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

    redtsch

    Hello all. Im currently working on a per-user .yml file plugin and have run into a brick wall. Code and traces below, thanks in advance =)

    Core.class:
    Code:
    public class Core extends JavaPlugin {
       
        Logger logger = Logger.getLogger("Minecraft");
        PluginDescriptionFile pdf = getDescription();
       
        private static Core core;
       
        public static Core getInstance() {
            return core;
        }
       
        public void onEnable() {
            logger.info("[" + pdf.getName() + "]" + " Enabled " + pdf.getName() + " v" + pdf.getVersion());
            registerEvents();
        }
       
        public void onDisable() {
            logger.info("[" + pdf.getName() + "]" + " Disabled " + pdf.getName() + " v" + pdf.getVersion());
        }
       
        public void registerEvents() {
            PluginManager pm = Bukkit.getServer().getPluginManager();
            pm.registerEvents(new CreatePlayerFiles(), this);
        }
    }
    
    CreatePlayerFiles.class:
    Code:
    public class CreatePlayerFiles implements Listener {
    
        Core core = Core.getInstance();
        FileConfiguration pConfig = null;
    
        @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
        public void onPlayerJoin(PlayerJoinEvent event) throws IOException {
            Player player = event.getPlayer();
            File pFile = new File(
                    core.getDataFolder() + File.separator + "userdata" + File.separator + player.getName() + ".yml");
            if (!pFile.exists()) {
                pFile.createNewFile();
                pConfig = YamlConfiguration.loadConfiguration(pFile);
                pConfig.set("path.to.key", false);
                pConfig.save(pFile);
                System.out.println("[ServerCore]" + " Created file: " + player.getName() + ".yml");
            }
        }
    }
    
    Trace:
    Code:
    [09:35:07 INFO]: UUID of player redtsch is 69d9ff9b-9485-4940-bb5b-c4fc0935a83a
    [09:35:07 ERROR]: Could not pass event PlayerJoinEvent to ServerCore v1.0.0a
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[bukkit.jar:git-Bukkit-f326992]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[bukkit.jar:git-Bukkit-f326992]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [bukkit.jar:git-Bukkit-f326992]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.PlayerList.onPlayerJoin(PlayerList.java:316) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.PlayerList.a(PlayerList.java:143) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.LoginListener.b(LoginListener.java:115) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.LoginListener.c(LoginListener.java:53) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.NetworkManager.a(NetworkManager.java:223) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.ServerConnection.c(SourceFile:187) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:751) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:361) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:635) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:539) [bukkit.jar:git-Bukkit-f326992]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_60]
    Caused by: java.lang.NullPointerException
        at com.redstsch.mcredcraft.ServerCore.main.playerfiles.CreatePlayerFiles.onPlayerJoins(CreatePlayerFiles.java:25) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_60]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_60]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_60]
        at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_60]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[bukkit.jar:git-Bukkit-f326992]
        ... 14 more
    [09:35:07 INFO]: redtsch[/127.0.0.1:59268] logged in with entity id 2424 at ([world]-21.63780956040712, 71.0, 133.65750486660943)
    If this look similar to code posted on another thread, it is. I'm just trying to find a temporary solution to my problem; then I will fine-tune it and make it my own.

    Again, thanks all. =)
     
  2. Offline

    WolfMage1

    your instance variable in your main class is null, never assigned. Assign it a value in your onEnable
     
    redtsch likes this.
  3. Offline

    redtsch

    @WolfMage1
    Ah it is, isn't it!

    Would I fix this like so:
    Code:
        public void onEnable() {
            logger.info("[" + pdf.getName() + "]" + " Enabled " + pdf.getName() + " v" + pdf.getVersion());
            registerEvents();
            core = this;
        }
    
    If it helps, I still get an error at CreatePlayerFiles.class, line 25 after I put core = this in my onEnable()...
     
    Last edited: May 24, 2016
  4. Offline

    WolfMage1

    I'd put core=this first so if anything else in your plugin wants to use it then they can, but yeah.
     
  5. Offline

    redtsch

    hmmm, I've put core = this first in my onEnable():
    Code:
        public void onEnable() {
            core = this;
            logger.info("[" + pdf.getName() + "]" + " Enabled " + pdf.getName() + " v" + pdf.getVersion());
            registerEvents();
        }
    and I get the same error, I believe it has to do with core.getDataFolder possibly.. maybe not =p
    EDIT:
    Ok, so I did some re-arranging and now its throwing an error at line 26:
    Code:
                pFile.createNewFile();
    
    @WolfMage1
    sorry if this seems like spam :oops:
    so theres actually a new error as it seems:
    Code:
    [10:29:17 INFO]: UUID of player redtsch is 69d9ff9b-9485-4940-bb5b-c4fc0935a83a
    [10:29:17 ERROR]: Could not pass event PlayerJoinEvent to ServerCore v1.0.0a
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[bukkit.jar:git-Bukkit-f326992]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[bukkit.jar:git-Bukkit-f326992]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [bukkit.jar:git-Bukkit-f326992]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.PlayerList.onPlayerJoin(PlayerList.java:316) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.PlayerList.a(PlayerList.java:143) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.LoginListener.b(LoginListener.java:115) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.LoginListener.c(LoginListener.java:53) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.NetworkManager.a(NetworkManager.java:223) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.ServerConnection.c(SourceFile:187) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:751) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:361) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:635) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:539) [bukkit.jar:git-Bukkit-f326992]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_60]
    Caused by: java.io.IOException: No such file or directory
        at java.io.UnixFileSystem.createFileExclusively(Native Method) ~[?:1.8.0_60]
        at java.io.File.createNewFile(File.java:1012) ~[?:1.8.0_60]
        at com.redstsch.mcredcraft.ServerCore.main.playerfiles.CreatePlayerFiles.onPlayerJoin(CreatePlayerFiles.java:26) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_60]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_60]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_60]
        at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_60]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[bukkit.jar:git-Bukkit-f326992]
        ... 14 more
    
    I believe i found the problem:
    Code:
    Caused by: java.io.IOException: No such file or directory
    And honestly idk how to fix this one.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.

    EDIT:
    so it turns out that i needed to correct my file directory, I've solved it so thanks for the help Wolfie =)
     
    Last edited: May 24, 2016
Thread Status:
Not open for further replies.

Share This Page