Get Player Gamemode?

Discussion in 'Plugin Development' started by LandonTheGeek, May 17, 2013.

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

    LandonTheGeek

    Hey guys!

    I am trying to figure out how to go about printing the players gamemode off, and player.sendMessaging it to the player. What would the line of code be for the player.sendMessage and the getGamemode on this code right here?

    This format would be /gmcheck <player>

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(commandLabel.equalsIgnoreCase("gmcheck")){
                Player player = (Player) sender;
            if(sender.hasPermission("gm.check")){
               
            }
               
        }
     
    MrTrojan likes this.
  2. Offline

    Garris0n

    if(player.getGameMode() == GameMode.CREATIVE){
    player.sendMessage("gamemode is creative");
    }

    You should be able to figure it out from there.
     
  3. Offline

    LandonTheGeek

    Garris0n Works great! How would I make it so that there would be a target player though?

    Here is what I have I also want it to have permissions. I know how to use permissions, it's just, I don't know how to insert it into here!

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(commandLabel.equalsIgnoreCase("gmcheck")){
                Player player = (Player) sender;
               
            if(player.getGameMode() == GameMode.CREATIVE){
                player.sendMessage(player.getName() + " is in Creative!");
            }
           
            if(player.getGameMode() == GameMode.SURVIVAL){
                player.sendMessage(player.getName() + " is in Survival!");
               
            if(player.getGameMode() == GameMode.ADVENTURE){
                player.sendMessage(player.getName() + " is in Adventure!");
               
            }
               
        }
           
            }
     
  4. Offline

    JoTyler

    xXTh3B3astXxify first thing I notice is that you should have if if if ... It should be if then else if then more else if just add else before the if statements unless its the first in its own area like
    If{
    If{}
    }
    Is fine but

    If{}
    If{}
    If{}

    Leads to errors and you can do

    Player targrtplayer = args[0]

    I'm on my phone ATM ill be on my computer tomorrow and will post a better code :)
     
  5. Offline

    ZeusAllMighty11

    Code:
    public boolean onCommand(CommandSender...){ // EXAMPLE
        if(args.length >= 1){// EXAMPLE
            for(Player p: Bukkit.getServer().getOnlinePlayers().clone()){// EXAMPLE
                if(args[0].equalsIgnoreCase(p.getName())){// EXAMPLE
                    sender.sendMessage(p.getName() + "'s gamemode is " + p.getGameMode().toString().toLowerCase()); // this is what you want
                }// EXAMPLE
            }// EXAMPLE
        }// EXAMPLE
        return false;
    }
     
  6. Offline

    LandonTheGeek

    JoTyler

    Hey man! I would really appreciate that!

    TheGreenGamerHD

    What is that code aiming to do? How will it trigger? I wanted a command and permissions to check the player's gamemode.
     
  7. Offline

    GodzOfMadness

    xXTh3B3astXxify The code tells the user who executed the command the target's current gamemode. Read this if you are unsure. You can add the permissions to the command like so, Check if it's a player executing the command. If it is then create a player object.
    Player player = (Player) sender;
    then see if it has the permission like
    if(player.hasPermission("permission.node"){
    //do this
    }else{
    //do this
    }
     
  8. Offline

    JoTyler

  9. Offline

    LandonTheGeek

    JoTyler
    Alright, I fixed that error, but where does the targetplayer go? (Sorry guys, I am still learning Java) I just can't seem to figure out where to map it into each of the if statements.

    JoTyler
    Thanks a lot dude!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  10. Offline

    JoTyler

  11. Offline

    LandonTheGeek

    JoTyler
    Alright thanks. I'm not trying to bother you, just asking if you could include the permission in there too? xD
     
  12. Offline

    JoTyler

    xXTh3B3astXxify Oh no prob umm i already did :) but Sure >.<


    Code:
                if(player.hasPermission("gm.check")){
                    if(args.length <1 ){
                        if(player.getGameMode() == GameMode.CREATIVE){
                            player.sendMessage(player.getName() + " is in Creative!");
                        }
                     
                        else if(player.getGameMode() == GameMode.SURVIVAL){
                            player.sendMessage(player.getName() + " is in Survival!");
                        }
                        else if(player.getGameMode() == GameMode.ADVENTURE){
                            player.sendMessage(player.getName() + " is in Adventure!");
                        }}
                    else{
                        Player targetplayer = player.getServer().getPlayer(args[0]);
                        if(targetplayer.isOnline()){
                        if(targetplayer.getGameMode() == GameMode.CREATIVE){
                            player.sendMessage(targetplayer.getName() + " is in Creative!");
                        }
                     
                        else if(targetplayer.getGameMode() == GameMode.SURVIVAL){
                            player.sendMessage(targetplayer.getName() + " is in Survival!");
                        }
                        else if(targetplayer.getGameMode() == GameMode.ADVENTURE){
                            player.sendMessage(targetplayer.getName() + " is in Adventure!");
                        }}else{
                            player.sendMessage(ChatColor.RED + "Sorry that player isnt online");
                        }
                    }
                 
                }else{
                player.sendMessage(ChatColor.DARK_RED + "You dont have permission to use this command.");}
               
                 
            }
    I DID NOT TEST IT .

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  13. Offline

    ZeusAllMighty11

    My method is much easier than doing a bunch of if else statements
     
    GodzOfMadness likes this.
  14. Yeah, do what TheGreenGamerHD did. It's alot faster and cleaner than a bunch of if/else statements
     
  15. Offline

    LandonTheGeek

    JoTyler
    Is there a command to it? That's what I am wondering.
     
  16. Offline

    JoTyler

    Yes HAHA I FORGOT TO COPY THE FIRST LINE !! Ill post it again in a few
     
  17. Offline

    LandonTheGeek

Thread Status:
Not open for further replies.

Share This Page