gpt4 book ai didi

java - files.getPlayersConfig().contains() 抛出 NullPointerException

转载 作者:行者123 更新时间:2023-11-30 03:36:39 24 4
gpt4 key购买 nike

所以我目前正在使用 Bukkit(技术上的 Spigot 插件),我希望它在玩家第一次加入时在文件 (players.yml) 中创建一个字符串。

我目前有:

public void onPlayerJoin(PlayerJoinEvent e){
String player = e.getPlayer().getName().toString();

if(!files.getPlayersConfig().contains(player)) {
files.getPlayersConfig().set(player + ".enabled", true);
files.getPlayersConfig().set(player + ".money", 100);
files.savePlayersConfig();
files.reloadPlayersConfig();
}
}

我基本上想做的是在每个玩家第一次加入时在文件中创建一个字符串“player”。

例如,如果玩家名为bob第一次加入,则会生成该文件

bob:
enabled: true
money: 100

但是,我不希望它在每次玩家加入时重置,所以这就是为什么我试图让它检查文件是否包含“bob”,如果它返回 null,它将创建它。我的问题是它显示为错误,而不是仅仅创建它,因此它不为空。

(抱歉措辞不好,但很难解释。)

谢谢。

日志:

PlayerJoinEvent to McEconPlus v1.0
org.bukkit.event.EventException
at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:305) ~[spigot.jar:git-Spigot-8dc4297-fbda3a7]
at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-8dc4297-fbda3a7]
at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-8dc4297-fbda3a7]
at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-8dc4297-fbda3a7]
at net.minecraft.server.v1_8_R1.PlayerList.onPlayerJoin(PlayerList.java:262) [spigot.jar:git-Spigot-8dc4297-fbda3a7]
at net.minecraft.server.v1_8_R1.PlayerList.a(PlayerList.java:147) [spigot.jar:git-Spigot-8dc4297-fbda3a7]
at net.minecraft.server.v1_8_R1.LoginListener.b(LoginListener.java:109) [spigot.jar:git-Spigot-8dc4297-fbda3a7]
at net.minecraft.server.v1_8_R1.LoginListener.c(LoginListener.java:41) [spigot.jar:git-Spigot-8dc4297-fbda3a7]
at net.minecraft.server.v1_8_R1.NetworkManager.a(NetworkManager.java:159) [spigot.jar:git-Spigot-8dc4297-fbda3a7]
at net.minecraft.server.v1_8_R1.ServerConnection.c(ServerConnection.java:82) [spigot.jar:git-Spigot-8dc4297-fbda3a7]
at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:785) [spigot.jar:git-Spigot-8dc4297-fbda3a7]
at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot.jar:git-Spigot-8dc4297-fbda3a7]
at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:623) [spigot.jar:git-Spigot-8dc4297-fbda3a7]
at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:526) [spigot.jar:git-Spigot-8dc4297-fbda3a7]
at java.lang.Thread.run(Unknown Source) [?:1.7.0_67]
**Caused by: java.lang.NullPointerException**
at me.mrson.McEconPlus.Events.MCEP_CreatePlayers.onPlayerJoin(MCEP_CreatePlayers.java:21) ~[?:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_67]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_67]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_67]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_67]
at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[spigot.jar:git-Spigot-8dc4297-fbda3a7]
... 14 more

请求的主类:

公共(public)类 MCEP_Base 扩展 JavaPlugin{ 私有(private)静态Plugin插件;

public File configFile = new File(this.getDataFolder(), "config.yml");
public FileConfiguration config = YamlConfiguration.loadConfiguration(configFile);

public File playersFile = new File(this.getDataFolder(), "players.yml");
public FileConfiguration players = YamlConfiguration.loadConfiguration(playersFile);


public void onEnable(){
plugin = this;

//config

config.options().copyDefaults(true);
config.addDefault("Enabled", "true");
this.saveCustomConfig();

players.options().copyDefaults(true);
players.addDefault("Enabled", true);
this.savePlayersConfig();

//Logger
this.getLogger().log(Level.FINEST, "[McEcon+] Enabling.");

//Commands
getCommand("econ").setExecutor(new MCEP_Econ());
getCommand("pay").setExecutor(new MCEP_Pay());
getCommand("sell").setExecutor(new MCEP_Sell());

//Events
registerEvents(this, new MCEP_CreatePlayers());


}

public void onDisable(){
plugin = null;
}

public static void registerEvent(Plugin plugin, Listener... listeners){
for(Listener listener : listeners){
Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
}
}

public static Plugin getPlugin() {
return plugin;
}

public void saveCustomConfig(){
try{
config.save(configFile);
}catch(IOException ex){
ex.printStackTrace();
}
}

public void savePlayersConfig(){
try{
players.save(playersFile);
}catch(IOException ex){
ex.printStackTrace();
}
}

public FileConfiguration getCustomConfig(){
if(config == null){
saveCustomConfig();
}
return config;
}

public FileConfiguration getPlayersConfig() {
if (players == null) {
savePlayersConfig();
}
return players;
}

public void reloadCustomConfig(){
if(configFile == null){
configFile = new File(getDataFolder(), "config.yml");
}
config = YamlConfiguration.loadConfiguration(configFile);
config.set("Enabled", true);
}

public void reloadPlayersConfig(){
if(playersFile == null){
playersFile = new File(getDataFolder(), "players.yml");
}
players = YamlConfiguration.loadConfiguration(playersFile);
players.set("Enabled", true);
}

public static void registerEvents(org.bukkit.plugin.Plugin plugin, Listener... listeners) {
for (Listener listener : listeners) {
Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
}
}

}

以及我的 Listener 类:

公共(public)类 MCEP_CreatePlayers 实现 Listener{ MCEP_Base 文件;

@EventHandler()
public void onPlayerJoin(PlayerJoinEvent e){
String player = e.getPlayer().getName().toString();
if(files == null){
MCEP_Base.getPlugin().getLogger().warning("Test");
}else if(files.getPlayersConfig() == null){
MCEP_Base.getPlugin().getLogger().warning("Test1");
}
else if(!files.players.contains(player)) {
files.getPlayersConfig().set(player + ".enabled", true);
files.getPlayersConfig().set(player + ".money", 100);
files.savePlayersConfig();
files.reloadPlayersConfig();
}
}

}

最佳答案

您可以添加另一个检查来查看 getPlayersConfig() 是否返回 null :

if(files.getPlayersConfig() == null) {
// handle this case
}
else if(!files.getPlayersConfig().contains(player)) {
// continue with rest of your code
}

关于java - files.getPlayersConfig().contains() 抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27751694/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com