Solved getPlayer Help

Discussion in 'Plugin Development' started by VenamousV, Jun 15, 2015.

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

    VenamousV

    If I am trying to create a command that is something like "/togglesneak (playername)" how do I tell the sever to set sneaking on said player to true without using the getPlayer method?
    this is what I tried to do:
    Code:
    Player p1 = Bukkit.getPlayer(args[1]);
    This line creates an ArrayIndexOutOfBoundsException: 1 error.
    This is what I'm trying to do with it:
    Code:
    if(args.length == 1){
                    Player p1 = Bukkit.getPlayer(args[1]);
                    if(p1 instanceof Player){
                        if(p1.isOnline()){
                            if(p.hasPermission("autosneak.cansneak.others")){
                                if(args[1] == p.getName()){
                                    if(p.hasPermission("autosneak.cansneak")){
                                        p1.setSneaking(true);
                                    }
                                }else{
                                    p1.setSneaking(true);
                                }
                            }
                        }
                    }
                }
     
  2. Offline

    caderape

    @VenamousV
    Yep, cuz args[1] doesn't exist. So you get an error.
    args.lengh start with 1, args[] with 0.

    Check if the sender is a player before casting.
    If player is not null, that means he's online. Cuz the method check for online players.
    Use equals and not == for compare string
     
  3. Offline

    CoolGamerXD

    Always remember java is zero based language.
     
Thread Status:
Not open for further replies.

Share This Page