Help on my /fly plugin

Discussion in 'Plugin Development' started by EMSFaruq, Jun 3, 2021.

  1. Offline

    EMSFaruq

    I coded a fly plugin but i can only do /fly and not /fly <player> any help here the code


    Code:
    package de.emsfaruq.essentialspro.commands;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class FlyCommand implements CommandExecutor {
    
    private ArrayList<Player> fly = new ArrayList<>(); {
    
    }
    
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    
    if(sender instanceof Player) {
    Player p = (Player) sender;
    if(args.length == 0) {
    if (p.hasPermission("fly") || p.hasPermission("god.*") || p.hasPermission("EssentialsPro.*")) {
    }else
    p.sendMessage("§8§kHIII§cDazu hast du keine rechte!§8§kHIII");
    
    Player on = (Player) sender;
    if(fly.contains(on)) {
    fly.remove(on);
    p.sendMessage("§8§kHIII§c4Du bist nicht mehr im Fly-Modus!§8§kHIII");
    p.setAllowFlight(false);
    
    }else if(!fly.contains(on)) {
    fly.add(on);
    p.sendMessage("§8§kHIII§aDu bist jetzt im Fly-Modus!§8§kHIII");
    p.setAllowFlight(true);
    
    }if(args.length == 1) {
    Player target = Bukkit.getPlayer(args[0]);
    if(target != null) {
    if(p.hasPermission("feed.others") || p.hasPermission("feed.*") || p.hasPermission("EssentialsPro.*")) {
    target.setFoodLevel(20);
    target.sendMessage("§aDu wurdest von §6" + p.getName() + " §agefüttert!");
    p.sendMessage("§8§kHIII§aDu hast den Spieler §6" + target.getName() + " §agefüttert!§8§kHIII");
    } else
    p.sendMessage("§8§kHIII§cDazu hast du keine Rechte§8§kHIII");
    
    } else
    p.sendMessage("§8§kHIII§cSpieler nicht online§8§kHIII");
    } else
    p.sendMessage("§8§kHIII§cBenutze /feed <Spieler>§8§kHIII");
    }
    }
    return true;
    }
    }
     
    Last edited by a moderator: Jun 3, 2021
  2. Online

    timtower Administrator Administrator Moderator

    @EMSFaruq Your args.length==1 check is inside the block for the args.length==0 check
     
  3. Offline

    Shqep

    Probably lost between c&ps but here is the reformatted code:
    Reformatted Code (open)

    Code:Java
    1. package de.emsfaruq.essentialspro.commands;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10.  
    11. public class FlyCommand implements CommandExecutor {
    12.  
    13. private ArrayList<Player> fly = new ArrayList<>();
    14.  
    15. {
    16.  
    17. }
    18.  
    19. @Override
    20. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    21.  
    22. if(sender instanceof Player) {
    23. Player p = (Player) sender;
    24. if(args.length == 0) {
    25. if(p.hasPermission("fly") || p.hasPermission("god.*") || p.hasPermission("EssentialsPro.*")) {
    26. } else p.sendMessage("§8§kHIII§cDazu hast du keine rechte!§8§kHIII");
    27. Player on = (Player) sender;
    28. if(fly.contains(on)) {
    29. fly.remove(on);
    30. p.sendMessage("§8§kHIII§c4Du bist nicht mehr im Fly-Modus!§8§kHIII");
    31. p.setAllowFlight(false);
    32. } else if(!fly.contains(on)) {
    33. fly.add(on);
    34. p.sendMessage("§8§kHIII§aDu bist jetzt im Fly-Modus!§8§kHIII");
    35. p.setAllowFlight(true);
    36. }
    37. if(args.length == 1) {
    38. Player target = Bukkit.getPlayer(args[0]);
    39. if(target != null) {
    40. if(p.hasPermission("feed.others") || p.hasPermission("feed.*") || p.hasPermission("EssentialsPro.*")) {
    41. target.setFoodLevel(20);
    42. target.sendMessage("§aDu wurdest von §6" + p.getName() + " §agefüttert!");
    43. p.sendMessage("§8§kHIII§aDu hast den Spieler §6" + target.getName() + " §agefüttert!§8§kHIII");
    44. } else
    45. p.sendMessage("§8§kHIII§cDazu hast du keine Rechte§8§kHIII");
    46.  
    47. } else
    48. p.sendMessage("§8§kHIII§cSpieler nicht online§8§kHIII");
    49. } else
    50. p.sendMessage("§8§kHIII§cBenutze /feed <Spieler>§8§kHIII");
    51. }
    52. }
    53. return true;
    54. }
    55.  
    56. }



    A lot of improvements can be made and it will still be up to you whether you want to implement:
    Click if you care (open)

    I use line numbers as in the reformatted code version, not in the original paste.

    Line 13: Memory leaks!
    [​IMG]

    Line 15-17: What are the curly braces doing there? I think you got what I'm trying to say.
    [​IMG]

    Line 22: People usually advise using negative returns over positive checks. As such:
    (The 2 following statements do the same thing.)
    [​IMG]
    [​IMG]

    Line 25-26: Empty if-statement body.
    [​IMG]

    Line 27: Did you already forget you casted the sender before?
    [​IMG]

    Line 28-36: Just simplify!
    [​IMG]

    Line 37: What Tim already mentioned. Useless if check.
    [​IMG]

    What.
    [​IMG]
    when class name:
    [​IMG]
    Did you put in the wrong class lol?

    Other than those, the last part is pretty solid (Line 37-50).
     
    Xp10d3 and davidclue like this.

Share This Page