Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm going to try to make this quick basically i'm trying to make ONE config per player.

So basically when ever I call user.CreateUser(); if the user doesn't exist it registers there own config with there unique id as the name of the yml

My problem is that when ever I try to call user.CreateUser(); on the PlayerJoinEvent it shows a error and i'm not sure the UUID u shouldn't be returning null because it's inside a constructor or the other variables.

Error

[23:57:52 ERROR]: Could not pass event PlayerJoinEvent to MCEnhanced v1.0
org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
va:310) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
a:62) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
ava:502) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
ava:487) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.PlayerList.onPlayerJoin(PlayerList.java:
296) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.PlayerList.a(PlayerList.java:156) [spigo
t-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.LoginListener.b(LoginListener.java:144)
[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.LoginListener.c(LoginListener.java:54) [
spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.NetworkManager.a(NetworkManager.java:231
) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.ServerConnection.c(ServerConnection.java
:148) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:8
17) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:3
67) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:6
57) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
:560) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
Caused by: java.lang.NullPointerException
        at crypted.mcenhanced.Handlers.UserDataHandler.CreateUser(UserDataHandle
r.java:46) ~[?:?]
        at crypted.mcenhanced.Mechanics.ConfigMechanics.CreateUser.CreateUser(Cr
eateUser.java:19) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
_45]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
_45]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
.8.0_45]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_45]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
va:306) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        ... 14 more

CreateUser Listener Class

public class CreateUser implements Listener {

@EventHandler
public void CreateUser(PlayerJoinEvent event){
    Player player = event.getPlayer();
    UserDataHandler user = new UserDataHandler(player.getUniqueId());
    user.CreateUser();

}
}

UserDataHandler Class

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.Listener;

import java.io.File;
import java.util.UUID;

public class UserDataHandler implements Listener {


     UUID u;
     File UserFile;
     FileConfiguration UserConfig;

    //UserDataHandler user = new UserDataHandler(player.getUniqueId()); // Make sure that you have the player.getUniqueId()

    public UserDataHandler(UUID u){

        this.u = u;

        File UserFile = new File("plugins/MCEnhanced/data/" + u + ".yml");

        YamlConfiguration UserConfig = YamlConfiguration.loadConfiguration(UserFile);

    }

    public void CreateUser(){

        if ( !(UserFile.exists()) ) {
            try {

                //Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[MCEnhanced] Created a new File for " + player.getName() + "(" + player.getUniqueId() + ")");

                YamlConfiguration UserConfig = YamlConfiguration.loadConfiguration(UserFile);

                UserConfig.save(UserFile);


            } catch (Exception e) {

                e.printStackTrace();

               // Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[MCEnhanced] Could not create a new File for " + player.getName() + "(" + player.getUniqueId() + ")");

                //u.kickPlayer(ChatColor.RED + "We could not create a file for your account!"); // THE PLAYERS CONFIG NEEDS TO BE CREATED!!!!!!!!

            }
        }

    }


    public FileConfiguration getUserFile(){

        return UserConfig;

    }

    public void setDefaultUserFile(){

        getUserFile().set("MCEnhanced.Info.IsInfected", false);

    }

    public void saveUserFile(){

        try {

            getUserFile().save(UserFile);

        } catch(Exception e) {

            e.printStackTrace();

        }

    }





}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
191 views
Welcome To Ask or Share your Answers For Others

1 Answer

In the CreateUser method, you are referencing the UserFile field or instance variable of the current UserDataHandler object which is by default null. Your constructor for the UserDataHandler class does not instantiate this UserFile field and instead creates a local File variable inside that constructor which is then never used. The line...

File UserFile = new File("plugins/MCEnhanced/data/" + u + ".yml");

should be...

UserFile = new File("plugins/MCEnhanced/data/" + u + ".yml");

so that the File field is instantiated for that instance.

I would recommend using the lowerCamelCase naming convention to name your variables, fields and methods as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...