Fly coding help

Discussion in 'Plugin Development' started by Sizableshrimp0, Dec 29, 2014.

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

    Sizableshrimp0

    I may have asked this before, but i am right now updating one of my Bukkit plugins. One of the commands this plugin has is /fly. This command is a toggle command so if the player types /fly it will turn fly on and if they do /fly while in fly mode then it turns fly off. It works perfectly fine except i am having 1 problem. I am trying to get the command to work on other players. Whenever i add another arg i get errors when i try and use the command. This is the current coding (without the second arg)
    Code:
    if(commandLabel.equalsIgnoreCase("fly") ||
                    if(player.hasPermission("MyEssentials.fly")){
                                    if (args.length == 0){
                                    if(player.getAllowFlight()){
                                    player.sendMessage(ChatColor.BLUE + "Flying Has Been Disabled");              
                                    player.setAllowFlight(false);
                                    }else{                             
                                    player.sendMessage(ChatColor.BLUE + "Flying Has Been Enabled!");
                                    player.setAllowFlight(true);
    }}
    }else{ 
          player.sendMessage(ChatColor.DARK_RED + "You do not have permission!");
    Could someone please help me with this :)
     
  2. Offline

    Skionz

    @Sizableshrimp0 Check if the arguments length is 1, then get that player, but check if the player is null first. Then let the fly.
     
  3. Show the code for the /fly <playername> since this code seems fine
     
  4. Oh, he wrote "This is the current coding (without the second arg)" and right here doesn't show any support for adding a player name since it checks for args.length == 0
     
  5. @Sizableshrimp0 Ok let's list some issues in your code.
    1. Use cmd.getName() instead of commandLabel
    2. Sort out that syntax
    3. Since this looks like a tutorial copy, use if(sender instanceof Player){ before casting player
    4. You are checking if the command is /fly or they have permission MyEssentials.fly instead of and

    To add a player, check if args are 1, then get the players from that argument and do a null check.
     
  6. Offline

    Sizableshrimp0

    Code:
    if(commandLabel.equalsIgnoreCase("fly") |
                            if(player.hasPermission("MyEssentials.fly")){
                                    if (args.length == 0){
                                    //Player only typed /fly so lets fly them back!
                                    if(player.getAllowFlight()){
                                    player.sendMessage(ChatColor.BLUE + "Flying Has Been Disabled");              
                                    player.setAllowFlight(false);
                                    }else{                             
                                    player.sendMessage(ChatColor.BLUE + "Flying Has Been Enabled!");
                                    player.setAllowFlight(true);
                                    if(args.length == 1){
                                    Player target = Bukkit.getPlayerExact(args[0]);
                                    if (target == null){
                                    //Targets not online
                                    player.sendMessage(ChatColor.DARK_RED + "Player " + args[0] + " is not online!");
                                    //Targets online
                                    if(player.getAllowFlight()){
                                    player.sendMessage(ChatColor.BLUE + "Flying Has Been Disabled");              
                                    player.setAllowFlight(false);
                                    }else{                             
                                    player.sendMessage(ChatColor.BLUE + "Flying Has Been Enabled!");
                                    player.setAllowFlight(true);                              
                    }}
                    }else{          
                            player.sendMessage(ChatColor.DARK_RED + "You do not have permission!");
    That messes up the whole plugin though :/
     
    Last edited: Dec 29, 2014
  7. You never used a return statement, so the code keeps going past
    //Targets not online
    player.sendMessage(ChatColor.DARK_RED + "Player " + args[0] + " is not online!");

    EDIT: Maybe you didn't need to, I can't tell where the if statements end and such because the code isnt formatted
    please do ctrl + shift + f and format it for us.
     
  8. Offline

    Sizableshrimp0

    added the else if and now it fixed the rest of the plugin but /fly <player> doesnt do anything not even a error message :/
     
  9. @Sizableshrimp0 Please post your full code with the fixes I said before.
     
  10. Offline

    Sizableshrimp0

    ok hold on 1 sec

    Actually i just realized that this messes up the plugin :/
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel,   String[] args){{
    Player player = (Player) sender;
    if(sender instanceof Player){
    
    if(cmd.getName().equalsIgnoreCase("fly")
                    if(player.hasPermission("MyEssentials.fly")){
                                    }else if (args.length == 0){
                                    //Player only typed /fly so lets fly them back!
                                    if(player.getAllowFlight()){
                                    player.sendMessage(ChatColor.BLUE + "Flying Has Been Disabled");      
                                    player.setAllowFlight(false);
                                    }else{                    
                                    player.sendMessage(ChatColor.BLUE + "Flying Has Been Enabled!");
                                    player.setAllowFlight(true);
                                    if(args.length == 1){
                                    Player target = Bukkit.getPlayerExact(args[0]);
                                    if (target == null){
                                    //Targets not online
                                    player.sendMessage(ChatColor.DARK_RED + "Player " + args[0] + " is not online!");
                                    //Targets online
                                    if(player.getAllowFlight()){
                                    player.sendMessage(ChatColor.BLUE + "Flying Has Been Disabled");      
                                    player.setAllowFlight(false);
                                    }else{                    
                                    player.sendMessage(ChatColor.BLUE + "Flying Has Been Enabled!");
                                    player.setAllowFlight(true);                      
                    }}
                    }else{  
                            player.sendMessage(ChatColor.DARK_RED + "You do not have permission!");
    Its a little messy because when i paste it the spaces come out different :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2016
  11. Offline

    stoneminer02

    if(cmd.getName().equalsIgnoreCase("fly")
    You never finished that.
    if(player.hasPermission("MyEssentials.fly")){
    }else if (args.length == 0){
    You just checked if player has permission and then stops using that code if its true and goes on and checks if args.length == 0. This means anyone can use the command.
    if(args.length == 1){
    You check this inside the args.length == 0 output.
    }else{
    The first one, you never close it.
    if (target == null){
    Never closing this one either.
     
Thread Status:
Not open for further replies.

Share This Page