我会尽量做到这一点,基本上我会为每个玩家做一个配置。
所以基本上,当我打电话给用户时。CreateUser();如果用户不存在,它会在那里注册自己的配置,并将唯一的id作为yml的名称
我的问题是,当我尝试呼叫用户时。CreateUser();在PlayerJoinEvent上显示错误,我不确定
UUID u
不应该返回null,因为它在构造函数或其他变量中。
错误
[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侦听器类
public class CreateUser implements Listener {
@EventHandler
public void CreateUser(PlayerJoinEvent event){
Player player = event.getPlayer();
UserDataHandler user = new UserDataHandler(player.getUniqueId());
user.CreateUser();
}
}
UserDataHandler类
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();
}
}
}