Minecraft每个配置一名玩家 [英] Minecraft One Player Per Config

查看:95
本文介绍了Minecraft每个配置一名玩家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我将尝试使每个玩家配置一个。

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

所以基本上,每当我打电话给user.CreateUser( );如果用户不存在,它将在其中注册具有唯一ID的配置作为yml的名称。

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

我的问题是,每当我尝试调用user.CreateUser ();在PlayerJoinEvent上显示错误,并且我不确定 UUID u 是否不应返回null,因为它位于构造函数或其他变量中。

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.

错误

[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();

        }

    }





}


推荐答案

CreateUser 方法中,您引用的是 UserFile field 或当前 UserDataHandler 对象的实例变量,该变量是默认为null。您的 UserDataHandler 类的构造函数不会实例化此 UserFile 字段,而是创建一个本地 File 该构造函数中的变量,此变量从未使用过。该行...

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");

应该是...

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

以便实例化 File 字段该实例。

so that the File field is instantiated for that instance.

我建议您也使用LowerCamelCase命名约定来命名变量,字段和方法。

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

这篇关于Minecraft每个配置一名玩家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆