Check if player is/isnt Online

Discussion in 'Plugin Development' started by flaminsnowman99, Apr 29, 2012.

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

    flaminsnowman99

    I need a way to tell the sender of the command that a player isnt online if he isnt. Heres the code. What should I add?
    Code:text
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    2. Player player = null;
    3. if (sender instanceof Player) {
    4. player = (Player) sender;
    5. }
    6. if (cmd.getName().equalsIgnoreCase("explode")){
    7. if (args.length == 1){
    8. String target = args[0];
    9. Player player1 = Bukkit.getPlayer( target );
    10. if( player1 != null && player1.isOnline() ){
    11. Location loc = player1.getLocation();
    12. player1.getWorld().createExplosion(loc, 0);
    13. player1.setHealth(0);
    14. player1.getName();
    15. Bukkit.broadcastMessage(ChatColor.YELLOW + sender.getName() + " has blown up " + player1.getName());
    16. }
    17. return true;
    18. }
    19. else if (cmd.getName().equalsIgnoreCase("explode")) {
    20. if (player == null) {
    21. sender.sendMessage(ChatColor.RED + "/explode [Player]");
    22. } else {
    23. sender.sendMessage(ChatColor.RED + "/explode [Player]");
    24. }
    25. return true;
    26. }
    27. return false;
    28. }
    29. return false;
    30. }
    31.  
     
  2. Offline

    davejavu

    I use this:
    Code:java
    1. if (!getServer().getOfflinePlayer(<player name>).isOnline()){
    2. //Code
    3. }
     
  3. Offline

    Njol

    You already check whether the player is online, so simply add an else clause to the if statement:
    Code:java
    1. if( player1 != null && player1.isOnline() ){
    2. Location loc = player1.getLocation();
    3. player1.getWorld().createExplosion(loc, 0);
    4. player1.setHealth(0);
    5. player1.getName();
    6. Bukkit.broadcastMessage(ChatColor.YELLOW + sender.getName() + " has blown up " + player1.getName());
    7. } else {
    8. sender.sendMessage(args[0]+" isn't online!");
    9. }
     
Thread Status:
Not open for further replies.

Share This Page