所以我现在正在使用Bukkit(Technically 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
要求的主要课程:
公共类MCEP_Base扩展JavaPlugin { private static 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 class 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();
}
}
}
答案 0 :(得分:2)
您可以添加另一项检查以查看getPlayersConfig()是否返回null:
if(files.getPlayersConfig() == null) {
// handle this case
}
else if(!files.getPlayersConfig().contains(player)) {
// continue with rest of your code
}