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

    thedirptastic

    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.


    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) {
                            pa.getAttribute(Attribute.GENERIC_MAX_HEALTH).setBaseValue(20.0);
                            pa.setHealth(20.0);
                            Bukkit.broadcastMessage(pa.getDisplayName());
                            Bukkit.broadcastMessage(p.getDisplayName());
                            if(p.getDisplayName() == "thedirptastic") {
                                Bukkit.broadcastMessage("Reviving all players!");
                                p.chat("hello");
                            }
                            return true;
                        }
                    }
                }else {
                    p.sendMessage("You cannot use this command!");
                }
            }
            return false;
        }
     
  2. Offline

    bowlerguy66

    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):
    Code:
    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