Solved Permission for code

Discussion in 'Spigot Plugin Development' started by Tur3nt, Jul 29, 2018.

  1. I dont get it ... how can I add an Permission to this command.Please help me (I tryed everything [other commands work, this one is only giving errors in exlipse.])

    Player p = (Player) sender;

    if (cmd.getName().equalsIgnoreCase("fly")) {
    if(args.length == 0){
    p.sendMessage("§c[§aEasyS§c] §b/fly §aon§b:§coff");
    }else if(args.length == 1){
    if(args[0].equalsIgnoreCase("on")){
    p.setAllowFlight(true);
    p.sendMessage("§c[§aEasyS§c] §bYou can fly now!");

    return false;

    }else if(args[0].equalsIgnoreCase("off")){
    p.setAllowFlight(false);
    p.sendMessage("§c[§aEasyS§c] §bYou can't fly anymore!");

    return false;
    }else{
    } p.sendMessage("§c[§aEasyS§c] §4No permission.");


    Thanks :p
     
    • Learn Java
    • Use reversed if statements
    • Use code blocks
    Here's the fixed one:
    Code (Text):

    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class MyCommand implements CommandExecutor {
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String s, String[] args) {
            if (!cmd.getName().equalsIgnoreCase("fly")) {
                return false;
            }

            if(!(sender instanceof Player)){
                sender.sendMessage(ChatColor.RED + "Only players may use this command!");
                return false;
            }

            Player player = (Player) sender;
            if(args.length == 0){
                player.sendMessage("§c[§aEasyS§c] §b/fly §aon§b:§coff");
                return false;
            }

            if(!player.hasPermission("core.fly")){
                player.sendMessage(ChatColor.RED + "You don't have the permission to execute this command!");
                return false;
            }

            if(args[0].equalsIgnoreCase("on")) {
                player.setAllowFlight(true);
                player.sendMessage("§c[§aEasyS§c] §bYou can fly now!");
                return true;
            }

            if(args[0].equalsIgnoreCase("off")) {
                player.setAllowFlight(false);
                player.sendMessage("§c[§aEasyS§c] §bYou can't fly anymore!");
                return true;
            }
            return true;
        }
    }
     
     
    • Winner Winner x 1
  2. There are a few things incomplete in this code that you currently have:
    • You never make use of the hasPermission(String permission) method.
    • You never check if the commandSender actually is a player. (Running this from the console will throw errors)
    • Returning 'false' in the command executor method will cause spigot to print the "Syntax Error" message after it is run. You always want to return true

    Code (Java):
    if(!(sender instanceof Player)){
        sender.sendMessage("This command cannot be executed from the console.");
        return false;
    }
    Player p = (Player) sender;

    if (cmd.getName().equalsIgnoreCase("fly")) {
     
        if(!p.hasPermission("permission.to.fly")){
            p.sendMessage("§c[§aEasyS§c] §4No permission.");
            return true;
        }
     
        if(args.length == 0){
            p.sendMessage("§c[§aEasyS§c] §b/fly §aon§b:§coff");
        }else if(args.length == 1){
            if(args[0].equalsIgnoreCase("on")){
                p.setAllowFlight(true);
                p.sendMessage("§c[§aEasyS§c] §bYou can fly now!");
                return true;
            }else if(args[0].equalsIgnoreCase("off")){
                p.setAllowFlight(false);
                p.sendMessage("§c[§aEasyS§c] §bYou can't fly anymore!");
                return true;
            }
        }
        return false;
    }
    Nooooo @Alex0589 was quicker ;)
     
  3. You can become faster by downloading IntelliJ. Try it now for free and, if you are a student, get the pro version for free!

    EDIT:
    Totally not a sponsor
     
    • Funny Funny x 1
  4. Who needs IntelliJ when you have Notepad++ to quickly fix someone's code ;)
    upload_2018-7-29_11-50-24.png
    #VoteForBestIDE
     
  5. Guys, why are you doing this in code?
    You can set permission and permission-message for command at plugin.yml file.
    https://bukkit.gamepedia.com/Plugin_YAML
    Code (Text):

    commands:
      someCommand:
        permission: some.permission
        permission-message: "blabla"
     
    and why are you spoon feeding him =/
     
    • Informative Informative x 1
  6. yes, and your code-style looks very awful
     
    • Agree Agree x 1
    • Funny Funny x 1
  7. Code is better anytime
     


  8. 1) Im learning Java right now.
    2) I will take a look :D
    3. Thanks for the fix ill try it out :)!
     
  9. I tryed using hasPermission but what I get is only errors.
    I didn't post the whole code :)
     

  10. Wo this looks easy..
     
  11. Whats the error?

    Some people like different permissions for different sub-commands etc. Most people code that way, doesn't mean they're doing it wrong lmao :D
     
    • Like Like x 1
  12. Alrdy fixed :) thanks to everyone.
     
  13. Mark the thread as solved then :)
     
    • Like Like x 1
  14. Yeah ill do it :)
     
    • Winner Winner x 1