Solved Offline player Problem

Discussion in 'Plugin Development' started by leavin, Oct 13, 2018.

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

    leavin

    When I type /stats (offline player name) it says "An internal error occured while attempting to perform this command" instead of "That player must be online to check his stats!" and I don't know how to fix it, please help me!

    here's the code:
    Code:
    package me.leavin.core;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Statistic;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class main extends JavaPlugin implements Listener {
        public void onEnable() {
            Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "ZaliumStats has been enabled!");
            Bukkit.getPluginManager().registerEvents(this, this);
        }
        public void onDisable() {
            Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "ZaliumStats has been disabled!");
            saveConfig();
        }
      
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args){
            if (!(sender instanceof Player)) {
                sender.sendMessage("You must be a player to perform this command.");
                return true;
            }
            if (sender instanceof Player) {
            Player p = (Player) sender;
                if (command.getName().equalsIgnoreCase("stats")) {
                    if (args.length == 0) {
                        p.sendMessage("");
                        p.sendMessage(ChatColor.GOLD + "Your Stats:");
                        p.sendMessage(ChatColor.GOLD + " Kills: " + ChatColor.YELLOW + p.getStatistic(Statistic.PLAYER_KILLS));
                        p.sendMessage(ChatColor.GOLD + " Deaths: " + ChatColor.YELLOW + p.getStatistic(Statistic.DEATHS));
                        p.sendMessage("");
                        return true;
                    }
                  
                    if (args.length > 1) {
                        p.sendMessage(ChatColor.RED + "Correct usage: /stats (<player>)");
                    }
                  
                    if(args.length == 1) {
                      
                    @SuppressWarnings("deprecation")
                        Player target = Bukkit.getServer().getPlayer(args[0]);
                        if (target.isOnline()) {
                            p.sendMessage("");
                            p.sendMessage(ChatColor.GOLD + target.getName() + "'s Stats:");
                            p.sendMessage(ChatColor.GOLD + " Kills: " + ChatColor.YELLOW + target.getStatistic(Statistic.PLAYER_KILLS));
                            p.sendMessage(ChatColor.GOLD + " Deaths: " + ChatColor.YELLOW + target.getStatistic(Statistic.DEATHS));
                            p.sendMessage("");
                            return true;
                        } else {
                            p.sendMessage(ChatColor.RED + "That player must be online to check his stats!");
                            return true;
                        }
                    }
                }
            }
        return true;
        }
    }
    here's the error:
    Code:
    [20:41:34 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'stats' in plugin ZaliumStats v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_161]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_161]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
    Caused by: java.lang.NullPointerException
            at me.leavin.core.core.onCommand(core.java:48) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            ... 15 more
     
  2. @leavin

    My best guess would be to replace if(target.isOnline()) by if(target != null && target.isOnline())

    If that doesn't help, you need to tell us what line of your code is line 48 so that we know where the error exactly occurs.
     
  3. Use this instead: if(target == null){ //Player is not online }else{ //player is online and do stf}
     
  4. Offline

    leavin

    I tried what you suggested me but it gives me the same error.
    btw line 48 was "if(target.isOnline()) {" and now is "if(target != null && target.isOnline()) {"

    Ok a friend of mine helped me fix it, i just needed to put "if(target != null) {"

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 13, 2018
  5. what your friend said was correct i forgot to add the !
     
Thread Status:
Not open for further replies.

Share This Page