Detect when a certain player is online

Discussion in 'Plugin Development' started by thedirptastic, Apr 17, 2022.

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


    I was wondering if there was a way to detect when a certain player is online. (This is meant to run in a command)

    Without the command logic, here is the code.

        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("reviveall")) {
                if(p.hasPermission("reviveall.use")) {
                    for(Player pa : plugin.getServer().getOnlinePlayers()) {
                        if(pa.getAttribute(Attribute.GENERIC_MAX_HEALTH).getBaseValue() < 20.0) {
                            if(p.getDisplayName() == "thedirptastic") {
                                Bukkit.broadcastMessage("Reviving all players!");
                            return true;
                }else {
                    p.sendMessage("You cannot use this command!");
            return false;
  2. Offline


    The way you tried to do it is very inconsistent. For java, use .equals() when trying to compare strings instead of the == operator. Also, it would be better to try and look for the player's UUID instead of their displayname since the displayname can change. You'd be better off doing something like (psuedocode):
    if(pa.getUUID().equals(UUID.fromString("YOUR UUID HERE")))
    Edit: you can get UUIDs from usernames here
  3. Offline

    timtower Administrator Administrator Moderator

    String checks need to use equals or equalsIgnoreCase, not ==
Thread Status:
Not open for further replies.

Share This Page