Solved Mainclass is null

Discussion in 'Plugin Development' started by Gosintary, Feb 24, 2022.

Thread Status:
Not open for further replies.
  1. Offline

    Gosintary

    I have two classes.
    I have my main class "Main" which extends JavaPlugin.
    I have a Users class which I'm using to manage a bunch of variables.

    I need to access my getCustomConfig method from my main class in my user class to read and write from the config.

    I originally tried "Main plugin = new Main();" but got a plugin already initialized error. Then I tried
    Code:
    private Main mainClass;
    
        public User(Main mainClass) {
             this.mainClass = mainClass;
         }
    and now when I try to access a variable it says getCustomConfig cannot be accessed because mainClass is null.
     
  2. Offline

    Strahan

    When you create an instance of User, are you passing it a valid instance of Main?
     
  3. Offline

    Gosintary

    I'm creating an instance of User in my onCommand in Main. After I check to ensure the command sender is a player I'm running this:


    Code:
    User u = new User(p);
    u.displayVitals();
    EDIT: For a better look at the code, here are my two classes.

    Main
    Code:
    package me.ethanski.humanvitals;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.InvalidConfigurationException;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements CommandExecutor, Listener{
    
        private File customConfigFile;
        public FileConfiguration customConfig;
     
        public void onEnable() {
            createCustomConfig();
            this.getServer().getPluginManager().registerEvents(this, this);
            this.getCommand("vitals").setExecutor(this);
            for (Player target : Bukkit.getServer().getOnlinePlayers()) {
                    target.kickPlayer("Human Vitals has been enabled. Please relog to ensure proper funtionality.");
            }
        }
     
        public FileConfiguration getCustomConfig() {
            return this.customConfig;
        }
    
        private void createCustomConfig() {
            customConfigFile = new File(getDataFolder(), "player-data.yml");
            if (!customConfigFile.exists()) {
                customConfigFile.getParentFile().mkdirs();
                saveResource("player-data.yml", false);
             }
    
            customConfig = new YamlConfiguration();
            try {
                customConfig.load(customConfigFile);
            } catch (IOException | InvalidConfigurationException e) {
                e.printStackTrace();
            }
        }
     
        public void onDisable() {
         
        }
     
        public void createNewUser(Player p) {
            UUID newUUID = p.getUniqueId();
            getCustomConfig().set("Players."+newUUID+".thirst", 120);
            getCustomConfig().set("Players."+newUUID+".blood", 100);
            getCustomConfig().set("Players."+newUUID+".bleeding", false);
            getCustomConfig().set("Players."+newUUID+".brokenleg", false);
            getCustomConfig().set("Players."+newUUID+".illness", "none");
        }
     
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            if(getCustomConfig().getStringList("Players").contains(e.getPlayer().getUniqueId())){
                createNewUser(e.getPlayer());
            }
        }
     
        public boolean onCommand(CommandSender s, Command cmd, String l, String[] args) {
         
            if(cmd.getName().equalsIgnoreCase("vitals")) {
                if(s instanceof Player) {
                    Player p = (Player) s;
                    User u = new User(p);
                    u.displayVitals();
                }
            }
         
            return false;
         
        }
     
    }
    
    User
    Code:
    package me.ethanski.humanvitals;
    
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class User {
    
        private Main mainClass;
    
        public User(Main mainClass) {
             this.mainClass = mainClass;
         }
     
        private UUID uuid;
     
        public User(Player p) {
            this.uuid = p.getUniqueId();
        }
     
        int thirst = mainClass.getCustomConfig().getInt("Players."+uuid+".thirst");
        int bloodLevel = mainClass.getCustomConfig().getInt("Players."+uuid+".blood");
        boolean bleeding = mainClass.getCustomConfig().getBoolean("Players."+uuid+".bleeding");
        boolean brokenLeg = mainClass.getCustomConfig().getBoolean("Players."+uuid+".brokenleg");
        String illness = mainClass.getCustomConfig().getString("Players."+uuid+".illness");
     
        public boolean isBleeding() {
            return bleeding;
        }
     
        public boolean hasBrokenLeg() {
            return brokenLeg;
        }
     
        public int getThirst() {
            return thirst;
        }
     
        public int getBloodLevel() {
            return bloodLevel;
        }
     
        public void displayVitals() {
            Player p = Bukkit.getPlayer(uuid);
            p.sendMessage(cc("&a================"));
            p.sendMessage(cc("&cPlayer: &a"+p.getDisplayName()));
            p.sendMessage(cc("&bThirst: &a"+thirst));
            p.sendMessage(cc("&bBlood: &c"+bloodLevel));
            p.sendMessage(cc("&bBleeding: &6"+bleeding));
            p.sendMessage(cc("&bBroken Legs: &e"+brokenLeg));
            p.sendMessage(cc("&bHeath: &d"+illness));
            p.sendMessage(cc("&a================"));
        }
     
     
     
     
     
     
        //MISC Methods
    
     
        public String cc(String m) {
            return ChatColor.translateAlternateColorCodes('&', m);
         
        }
     
    
     
    }
    
    EDIT #2
    I think I figured out the issue:
    In my User class I have
    Code:
        private Main mainClass;
    
        public User(Main mainClass) {
             this.mainClass = mainClass;
         }
    
        private UUID uuid;
    
        public User(Player p) {
            this.uuid = p.getUniqueId();
        }
    which needs to be
    Code:
        private Main mainClass;
        private UUID uuid;
    
        public User(Main mainClass, Player p) {
             this.mainClass = mainClass;
             this.uuid = p.getUniqueId();
         }
    
    
       
    and I need to create my user instance like this:
    Code:
    User u = new User(this, p);
    Edit #3 :(

    Didn't work, here is the error log.
    Code:
    [16:11:27] [Server thread/INFO]: Gosintary_ issued server command: /vitals
    [16:11:27] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'vitals' in plugin HumanVitals v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[bukkit-1.18.1-R0.1-SNAPSHOT.jar:?]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:148) ~[bukkit-1.18.1-R0.1-SNAPSHOT.jar:?]
            at org.bukkit.craftbukkit.v1_18_R1.CraftServer.dispatchCommand(CraftServer.java:803) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.server.network.PlayerConnection.a(PlayerConnection.java:1895) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.server.network.PlayerConnection.a(PlayerConnection.java:1748) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.server.network.PlayerConnection.a(PlayerConnection.java:1729) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.network.protocol.game.PacketPlayInChat.a(PacketPlayInChat.java:29) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.network.protocol.game.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.network.protocol.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:30) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.server.TickTask.run(SourceFile:18) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.util.thread.IAsyncTaskHandler.c(SourceFile:151) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.util.thread.IAsyncTaskHandlerReentrant.c(SourceFile:23) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:1115) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.server.MinecraftServer.c(MinecraftServer.java:1) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.util.thread.IAsyncTaskHandler.y(SourceFile:125) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.server.MinecraftServer.bf(MinecraftServer.java:1094) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.server.MinecraftServer.y(MinecraftServer.java:1087) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.util.thread.IAsyncTaskHandler.bp(SourceFile:110) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.server.MinecraftServer.x(MinecraftServer.java:1070) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:1002) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.server.MinecraftServer.lambda$0(MinecraftServer.java:294) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at java.lang.Thread.run(Thread.java:833) [?:?]
    Caused by: java.lang.NullPointerException: Cannot invoke "me.ethanski.humanvitals.Main.getCustomConfig()" because "this.mainClass" is null
            at me.ethanski.humanvitals.User.<init>(User.java:20) ~[?:?]
            at me.ethanski.humanvitals.Main.onCommand(Main.java:78) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[bukkit-1.18.1-R0.1-SNAPSHOT.jar:?]
            ... 21 more
     
    Last edited: Feb 24, 2022
  4. Offline

    KarimAKL

    @Gosintary The instance variables in your User class are initialized before the constructor is called, so mainClass has not been initialized yet. You need to move the initialization of your instance variables into the constructor.
     
  5. Offline

    Gosintary

    Like this?
    Code:
    public User(Main mainClass, Player p){
    
        Main mainClass = mainClass;
        UUID uuid = p.getUniqueID();
    
    }
    That would put mainClass and uuid outside of the scope of where I need to access them.
     
  6. Offline

    Strahan

    When you changed from two separate single var constructors to the one, that was the right move. If you called that new User(this, p) from the main class, that should have been fine. Post your current main and User classes.

    Nah, he said move the initialization into the constructor, not the declaration. That said, the latest code snippet you provided is initializing there so it should have worked.

    Also in the code you posted:
    Code:
    private Main mainClass;
    private UUID uuid;
    
    public User(Main mainClass, Player p) {
      this.mainClass = mainClass;
      this.uuid = p.getUniqueId();
    }
    You know just for info's sake you don't need to do this.uuid = p.getUniqueId(). You don't have a scope conflict, so this is superfluous. Not relevant to any issue, just letting you know. This is only necessary when you need to let the compiler know you explicitly mean the instance scope var.
     
  7. Offline

    Gosintary

    For the first bit you said, correct me if I'm wrong, but my understand of the terminology goes like this:


    Code:
    int myInteger;
    is an declaration,
    Code:
    myInteger = 5;
    would initialize myInteger

    and
    Code:
    int myInteger = 5;
    is both a declaration and an initalization.

    That would mean:
    Code:
    //That these are my declarations,
    private Main mainClass;
    private UUID uuid;
    
        public User(Main mainClass, Player p) {
    //And this is me initializing the variables inside the constructor.
             this.mainClass = mainClass;
             uuid = p.getUniqueId();
         }
    Which means that this code:
    Code:
    package me.ethanski.humanvitals;
    
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class User {
    
        private Main mainClass;
        private UUID uuid;
    
        public User(Main mainClass, Player p) {
             this.mainClass = mainClass;
             uuid = p.getUniqueID();
         }
    
        int thirst = mainClass.getCustomConfig().getInt("Players."+uuid+".thirst");
        int bloodLevel = mainClass.getCustomConfig().getInt("Players."+uuid+".blood");
        boolean bleeding = mainClass.getCustomConfig().getBoolean("Players."+uuid+".bleeding");
        boolean brokenLeg = mainClass.getCustomConfig().getBoolean("Players."+uuid+".brokenleg");
        String illness = mainClass.getCustomConfig().getString("Players."+uuid+".illness");
        public boolean isBleeding() {
            return bleeding;
        }
        public boolean hasBrokenLeg() {
            return brokenLeg;
        }
        public int getThirst() {
            return thirst;
        }
        public int getBloodLevel() {
            return bloodLevel;
        }
        public void displayVitals() {
            Player p = Bukkit.getPlayer(uuid);
            p.sendMessage(cc("&a================"));
            p.sendMessage(cc("&cPlayer: &a"+p.getDisplayName()));
            p.sendMessage(cc("&bThirst: &a"+thirst));
            p.sendMessage(cc("&bBlood: &c"+bloodLevel));
            p.sendMessage(cc("&bBleeding: &6"+bleeding));
            p.sendMessage(cc("&bBroken Legs: &e"+brokenLeg));
            p.sendMessage(cc("&bHeath: &d"+illness));
            p.sendMessage(cc("&a================"));
        }
        //MISC Methods
    
        public String cc(String m) {
            return ChatColor.translateAlternateColorCodes('&', m);
    
        }
    
    }
    Should have a valid instance of Main to use after
    Code:
    User u = new User(this, player);
    is ran in main. However that does not seem to be the case.

    =================================================================
    Just tested in a fresh server with no other plugins, still not working correctly :/
    I feel like this is a really dumb mistake but I'm not sure.

    Logs (open)

    Code:
    C:\Users\ethan\Desktop\Dev Server>java -Xmx12G -Xms12G -Dfile.encoding=UTF-8 -jar server.jar nogui
    Unbundling libraries to C:\Users\ethan\Desktop\Dev Server\bundler
    Starting server
    Loading libraries, please wait...
    [23:57:34] [ServerMain/INFO]: Environment: authHost='https://authserver.mojang.com', accountsHost='https://api.mojang.com', sessionHost='https://sessionserver.mojang.com', servicesHost='https://api.minecraftservices.com', name='PROD'
    [23:57:35] [ServerMain/INFO]: Reloading ResourceManager: Default, bukkit
    [23:57:36] [Worker-Main-6/INFO]: Loaded 7 recipes
    [23:57:34] [ServerMain/INFO]: Environment: authHost='https://authserver.mojang.com', accountsHost='https://api.mojang.com', sessionHost='https://sessionserver.mojang.com', servicesHost='https://api.minecraftservices.com', name='PROD'
    >[23:57:35] [ServerMain/INFO]: Reloading ResourceManager: Default, bukkit
    [23:57:36] [Worker-Main-6/INFO]: Loaded 7 recipes
    [23:57:38] [Server thread/INFO]: Starting minecraft server version 1.18.1
    [23:57:38] [Server thread/INFO]: Loading properties
    [23:57:38] [Server thread/INFO]: Default game type: SURVIVAL
    [23:57:38] [Server thread/INFO]: Generating keypair
    [23:57:38] [Server thread/INFO]: Starting Minecraft server on *:25565
    [23:57:38] [Server thread/INFO]: Using default channel type
    [23:57:39] [Server thread/INFO]: This server is running CraftBukkit version 3424-Bukkit-bdac46b (MC: 1.18.1) (Implementing API version 1.18.1-R0.1-SNAPSHOT)
    [23:57:39] [Server thread/INFO]: [HumanVitals] Loading HumanVitals v1.0
    [23:57:39] [Server thread/INFO]: Preparing level "world"
    [23:57:41] [Server thread/INFO]: Preparing start region for dimension minecraft:overworld
    [23:57:42] [Worker-Main-6/INFO]: Preparing spawn area: 0%
    [23:57:42] [Worker-Main-6/INFO]: Preparing spawn area: 0%
    [23:57:43] [Worker-Main-5/INFO]: Preparing spawn area: 0%
    [23:57:43] [Worker-Main-5/INFO]: Preparing spawn area: 0%
    [23:57:43] [Worker-Main-6/INFO]: Preparing spawn area: 0%
    [23:57:43] [Worker-Main-6/INFO]: Preparing spawn area: 0%
    [23:57:44] [Worker-Main-6/INFO]: Preparing spawn area: 0%
    [23:57:44] [Worker-Main-5/INFO]: Preparing spawn area: 0%
    [23:57:45] [Worker-Main-5/INFO]: Preparing spawn area: 1%
    [23:57:46] [Worker-Main-4/INFO]: Preparing spawn area: 6%
    [23:57:46] [Worker-Main-4/INFO]: Preparing spawn area: 6%
    [23:57:50] [Worker-Main-4/INFO]: Preparing spawn area: 11%
    [23:57:50] [Worker-Main-5/INFO]: Preparing spawn area: 11%
    [23:57:50] [Worker-Main-5/INFO]: Preparing spawn area: 11%
    [23:57:50] [Worker-Main-6/INFO]: Preparing spawn area: 11%
    [23:57:50] [Worker-Main-6/INFO]: Preparing spawn area: 11%
    [23:57:50] [Worker-Main-6/INFO]: Preparing spawn area: 11%
    [23:57:50] [Worker-Main-4/INFO]: Preparing spawn area: 11%
    [23:57:50] [Worker-Main-4/INFO]: Preparing spawn area: 17%
    [23:57:50] [Worker-Main-4/INFO]: Preparing spawn area: 32%
    [23:57:51] [Worker-Main-4/INFO]: Preparing spawn area: 45%
    [23:57:51] [Worker-Main-4/INFO]: Preparing spawn area: 65%
    [23:57:52] [Worker-Main-6/INFO]: Preparing spawn area: 83%
    [23:57:52] [Worker-Main-4/INFO]: Preparing spawn area: 99%
    [23:57:52] [Server thread/INFO]: Time elapsed: 11570 ms
    [23:57:53] [Server thread/INFO]: Preparing start region for dimension minecraft:the_nether
    [23:57:53] [Worker-Main-6/INFO]: Preparing spawn area: 0%
    [23:57:53] [Worker-Main-4/INFO]: Preparing spawn area: 0%
    [23:57:54] [Worker-Main-6/INFO]: Preparing spawn area: 0%
    [23:57:54] [Worker-Main-4/INFO]: Preparing spawn area: 0%
    [23:57:55] [Worker-Main-4/INFO]: Preparing spawn area: 0%
    [23:57:55] [Worker-Main-5/INFO]: Preparing spawn area: 3%
    [23:57:56] [Worker-Main-5/INFO]: Preparing spawn area: 3%
    [23:57:56] [Worker-Main-5/INFO]: Preparing spawn area: 15%
    [23:57:57] [Worker-Main-4/INFO]: Preparing spawn area: 35%
    [23:57:57] [Worker-Main-5/INFO]: Preparing spawn area: 43%
    [23:57:58] [Worker-Main-5/INFO]: Preparing spawn area: 64%
    [23:57:58] [Worker-Main-5/INFO]: Preparing spawn area: 68%
    [23:57:59] [Worker-Main-5/INFO]: Preparing spawn area: 69%
    [23:57:59] [Worker-Main-5/INFO]: Preparing spawn area: 72%
    [23:58:00] [Worker-Main-6/INFO]: Preparing spawn area: 74%
    [23:58:00] [Worker-Main-5/INFO]: Preparing spawn area: 78%
    [23:58:01] [Worker-Main-6/INFO]: Preparing spawn area: 81%
    [23:58:01] [Worker-Main-6/INFO]: Preparing spawn area: 83%
    [23:58:02] [Worker-Main-4/INFO]: Preparing spawn area: 85%
    [23:58:02] [Worker-Main-4/INFO]: Preparing spawn area: 86%
    [23:58:03] [Worker-Main-6/INFO]: Preparing spawn area: 88%
    [23:58:03] [Worker-Main-6/INFO]: Preparing spawn area: 90%
    [23:58:04] [Worker-Main-5/INFO]: Preparing spawn area: 93%
    [23:58:04] [Worker-Main-4/INFO]: Preparing spawn area: 96%
    [23:58:05] [Worker-Main-4/INFO]: Preparing spawn area: 99%
    [23:58:05] [Server thread/INFO]: Time elapsed: 12063 ms
    [23:58:05] [Server thread/INFO]: Preparing start region for dimension minecraft:the_end
    [23:58:05] [Worker-Main-5/INFO]: Preparing spawn area: 0%
    [23:58:05] [Worker-Main-6/INFO]: Preparing spawn area: 0%
    [23:58:06] [Worker-Main-6/INFO]: Preparing spawn area: 0%
    [23:58:06] [Worker-Main-5/INFO]: Preparing spawn area: 0%
    [23:58:07] [Worker-Main-5/INFO]: Preparing spawn area: 1%
    [23:58:07] [Worker-Main-5/INFO]: Preparing spawn area: 7%
    [23:58:08] [Worker-Main-4/INFO]: Preparing spawn area: 17%
    [23:58:08] [Worker-Main-4/INFO]: Preparing spawn area: 42%
    [23:58:09] [Worker-Main-4/INFO]: Preparing spawn area: 54%
    [23:58:09] [Worker-Main-6/INFO]: Preparing spawn area: 73%
    [23:58:10] [Worker-Main-6/INFO]: Preparing spawn area: 88%
    [23:58:10] [Server thread/INFO]: Time elapsed: 5310 ms
    [23:58:10] [Server thread/INFO]: [HumanVitals] Enabling HumanVitals v1.0
    [23:58:10] [Server thread/INFO]: Server permissions file permissions.yml is empty, ignoring it
    [23:58:10] [Server thread/INFO]: Done (30.821s)! For help, type "help"
    [23:58:19] [User Authenticator #1/INFO]: UUID of player Gosintary_ is e7389076-1774-4ca0-b985-bc1522931fdb
    [23:58:20] [Server thread/INFO]: Gosintary_[/192.168.1.18:55987] logged in with entity id 96 at ([world]107.9539303275257, 73.0, -612.3355901722499)
    [23:59:26] [Server thread/INFO]: Gosintary_ issued server command: /vitals
    [23:59:26] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'vitals' in plugin HumanVitals v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[bukkit-1.18.1-R0.1-SNAPSHOT.jar:?]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:148) ~[bukkit-1.18.1-R0.1-SNAPSHOT.jar:?]
            at org.bukkit.craftbukkit.v1_18_R1.CraftServer.dispatchCommand(CraftServer.java:803) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.server.network.PlayerConnection.a(PlayerConnection.java:1895) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.server.network.PlayerConnection.a(PlayerConnection.java:1748) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.server.network.PlayerConnection.a(PlayerConnection.java:1729) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.network.protocol.game.PacketPlayInChat.a(PacketPlayInChat.java:29) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.network.protocol.game.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.network.protocol.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:30) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.server.TickTask.run(SourceFile:18) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.util.thread.IAsyncTaskHandler.c(SourceFile:151) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.util.thread.IAsyncTaskHandlerReentrant.c(SourceFile:23) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:1115) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.server.MinecraftServer.c(MinecraftServer.java:1) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.util.thread.IAsyncTaskHandler.y(SourceFile:125) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.server.MinecraftServer.bf(MinecraftServer.java:1094) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.server.MinecraftServer.y(MinecraftServer.java:1087) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.util.thread.IAsyncTaskHandler.c(SourceFile:134) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.server.MinecraftServer.x(MinecraftServer.java:1071) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:1002) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at net.minecraft.server.MinecraftServer.lambda$0(MinecraftServer.java:294) ~[craftbukkit-1.18.1-R0.1-SNAPSHOT.jar:3424-Bukkit-bdac46b]
            at java.lang.Thread.run(Thread.java:833) [?:?]
    Caused by: java.lang.NullPointerException: Cannot invoke "me.ethanski.humanvitals.Main.getCustomConfig()" because "this.mainClass" is null
            at me.ethanski.humanvitals.User.<init>(User.java:20) ~[?:?]
            at me.ethanski.humanvitals.Main.onCommand(Main.java:78) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[bukkit-1.18.1-R0.1-SNAPSHOT.jar:?]
            ... 21 more
    >


    Main Class (open)

    Code:
    package me.ethanski.humanvitals;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.InvalidConfigurationException;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements CommandExecutor, Listener{
    
        private File customConfigFile;
        public FileConfiguration customConfig;
    
        public void onEnable() {
            createCustomConfig();
            this.getServer().getPluginManager().registerEvents(this, this);
            this.getCommand("vitals").setExecutor(this);
            for (Player target : Bukkit.getServer().getOnlinePlayers()) {
                    target.kickPlayer("Human Vitals has been enabled. Please relog to ensure proper funtionality.");
            }
        }
    
        public FileConfiguration getCustomConfig() {
            return this.customConfig;
        }
    
        private void createCustomConfig() {
            customConfigFile = new File(getDataFolder(), "player-data.yml");
            if (!customConfigFile.exists()) {
                customConfigFile.getParentFile().mkdirs();
                saveResource("player-data.yml", false);
             }
    
            customConfig = new YamlConfiguration();
            try {
                customConfig.load(customConfigFile);
            } catch (IOException | InvalidConfigurationException e) {
                e.printStackTrace();
            }
        }
    
        public void onDisable() {
      
        }
    
        public void createNewUser(Player p) {
            UUID newUUID = p.getUniqueId();
            getCustomConfig().set("Players."+newUUID+".thirst", 120);
            getCustomConfig().set("Players."+newUUID+".blood", 100);
            getCustomConfig().set("Players."+newUUID+".bleeding", false);
            getCustomConfig().set("Players."+newUUID+".brokenleg", false);
            getCustomConfig().set("Players."+newUUID+".illness", "none");
        }
    
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            if(getCustomConfig().getStringList("Players").contains(e.getPlayer().getUniqueId())){
                createNewUser(e.getPlayer());
            }
        }
    
        public boolean onCommand(CommandSender s, Command cmd, String l, String[] args) {
      
            if(cmd.getName().equalsIgnoreCase("vitals")) {
                if(s instanceof Player) {
                    Player p = (Player) s;
                    User u = new User(this, p);
                    u.displayVitals();
                }
            }
      
            return false;
      
        }
    
    }
    


    User Class (open)

    Code:
    package me.ethanski.humanvitals;
    
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class User {
    
        private UUID uuid;
        private Main mainClass;
    
        public User(Main main, Player p) {
            uuid = p.getUniqueId();
            this.mainClass = main;
         }
    
        int thirst = mainClass.getCustomConfig().getInt("Players."+uuid+".thirst");
        int bloodLevel = mainClass.getCustomConfig().getInt("Players."+uuid+".blood");
        boolean bleeding = mainClass.getCustomConfig().getBoolean("Players."+uuid+".bleeding");
        boolean brokenLeg = mainClass.getCustomConfig().getBoolean("Players."+uuid+".brokenleg");
        String illness = mainClass.getCustomConfig().getString("Players."+uuid+".illness");
    
        public boolean isBleeding() {
            return bleeding;
        }
    
        public boolean hasBrokenLeg() {
            return brokenLeg;
        }
    
        public int getThirst() {
            return thirst;
        }
    
        public int getBloodLevel() {
            return bloodLevel;
        }
    
        public void displayVitals() {
            Player p = Bukkit.getPlayer(uuid);
            p.sendMessage(cc("&a================"));
            p.sendMessage(cc("&cPlayer: &a"+p.getDisplayName()));
            p.sendMessage(cc("&bThirst: &a"+thirst));
            p.sendMessage(cc("&bBlood: &c"+bloodLevel));
            p.sendMessage(cc("&bBleeding: &6"+bleeding));
            p.sendMessage(cc("&bBroken Legs: &e"+brokenLeg));
            p.sendMessage(cc("&bHeath: &d"+illness));
            p.sendMessage(cc("&a================"));
        }
    
    
    
    
    
    
        //MISC Methods
    
    
        public String cc(String m) {
            return ChatColor.translateAlternateColorCodes('&', m);
      
        }
    
    
    
    }
    


    ====================================
    I have figured out what the issue is.
    I ended up using
    Code:
    private Main mainClass = (Main)JavaPlugin.getPlugin(Main.class);
    outside of my User consturctor and got it working, but now I'm noticing that my uuid is null!
    It seems to me like in my original code (the code posted earlier in this comment)
    I'm declaring my main and uuid variables in the instance's scope, but because I'm initalizing them in User's scope they are only being initialized within the scope of User(Main m, Player p)?

    Edit, I figured it out.

    What I did was I created a new method called initalizeVariables(); and I declared all my variables (thirst, blood, etc) in the instance scope, and then initalized them in my initalizeVariables() method which I call in my User(Player p) method.

    New User Class (open)

    Code:
    package me.ethanski.humanvitals;
    
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginLogger;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class User implements Listener{
    
        public Player player;
        private Main mainClass = (Main)JavaPlugin.getPlugin(Main.class);
        PluginLogger l = new PluginLogger(mainClass);
      
      
      
        public User(Player p) {
            this.player = p;
            l.info(player.getDisplayName());
            initalizeVariables();
        }
      
        public String getPlayerName() {
            return player.getDisplayName();
        }
      
        int thirst; //= mainClass.getCustomConfig().getInt("Players."+player.getDisplayName()+".thirst");
        int bloodLevel; //= mainClass.getCustomConfig().getInt("Players."+player.getDisplayName()+".blood");
        boolean bleeding; //= mainClass.getCustomConfig().getBoolean("Players."+player.getDisplayName()+".bleeding");
        boolean brokenLeg; //= mainClass.getCustomConfig().getBoolean("Players."+player.getDisplayName()+".brokenleg");
        String illness; //= mainClass.getCustomConfig().getString("Players."+player.getDisplayName()+".illness");
      
        private void initalizeVariables() {
            thirst = mainClass.getCustomConfig().getInt("Players."+player.getDisplayName()+".thirst");
            bloodLevel = mainClass.getCustomConfig().getInt("Players."+player.getDisplayName()+".blood");
            bleeding = mainClass.getCustomConfig().getBoolean("Players."+player.getDisplayName()+".bleeding");
            brokenLeg = mainClass.getCustomConfig().getBoolean("Players."+player.getDisplayName()+".brokenleg");
            illness = mainClass.getCustomConfig().getString("Players."+player.getDisplayName()+".illness");
        }
      
        public boolean isBleeding() {
            return bleeding;
        }
      
        public boolean hasBrokenLeg() {
            return brokenLeg;
        }
      
        public int getThirst() {
            return thirst;
        }
      
        public int getBloodLevel() {
            return bloodLevel;
        }
      
        public void displayVitals() {
            Player p = Bukkit.getPlayer(this.player.getDisplayName());
            p.sendMessage(cc("&a================"));
            p.sendMessage(cc("&cPlayer: &a"+p.getDisplayName()));
            p.sendMessage(cc("&bThirst: &a"+thirst));
            p.sendMessage(cc("&bBlood: &c"+bloodLevel));
            p.sendMessage(cc("&bBleeding: &6"+bleeding));
            p.sendMessage(cc("&bBroken Legs: &e"+brokenLeg));
            p.sendMessage(cc("&bHeath: &d"+illness));
            p.sendMessage(cc("&a================"));
        }
      
      
      
      
      
      
        //MISC Methods
    
      
        public String cc(String m) {
            return ChatColor.translateAlternateColorCodes('&', m);
          
        }
      
    
      
    }
    

    @KarimAKL had the answer:
    I just misunderstood you to mean that I needed to inialized Main and UUID inside my User method where as in reality you meant that my thirst, blood, etc variables needed to be inialized after the User Instance was created which is not possible if they are being initalized using variables which are not in their scope.
     
    Last edited: Feb 24, 2022
    Strahan and KarimAKL like this.
  8. Offline

    Strahan

    Yea, I'm blind... I didn't see you had other instance vars being setup after the constructor. I never put methods/constructors before variables so my brain just shut off and stopped looking once I hit the constructor, lol
     
Thread Status:
Not open for further replies.

Share This Page