Solved Code gives "no permission" twice

Discussion in 'Spigot Plugin Development' started by FazzyFabrak14, Mar 24, 2021.

  1. Hey, so I'm new to development so if I have any errors/anything I could improve on feel free to point it out!

    So I'm trying to create a "/feed" command so players could feed themselves or given the permission could feed other players.
    Currently, the plugin works alright, with permission I can use /feed and /feed (player) with no issues at all.
    But, when I try to execute /feed it gives me the no permission error twice, this is because as you can see I have two permissions "core.command.feed" and "core.command.feed.others" so it recognises that I lack both permissions and gives both errors at once, What I want it to do is give the error only once for "/feed" and once for "/feed (player)"
    How would I do that?

    Code (Text):
    package me.fazzyplayzmc.core.commands;

    import me.fazzyplayzmc.core.Core;
    import org.bukkit.Bukkit;
    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 FeedCmd implements CommandExecutor {

        private Core plugin;

        public FeedCmd(Core plugin){
            this.plugin = plugin;
        }

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args){
            if (!(sender instanceof Player)){
                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("must-be-player")));
                return true;
            }
            Player p = (Player) sender;
            if (p.hasPermission("core.command.feed.others")){
                if (args.length == 1){
                    if (Bukkit.getPlayerExact(args[0]) != null){
                        Player target = Bukkit.getPlayer(args[0]);
                        target.setFoodLevel(20);
                        target.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("target-fed") + sender.getName()));
                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("fed-others") + target.getName()));
                    } else {
                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("player-not-found")));
                    }
                }
            } else {
                p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("no-permission")));
            }

            if (p.hasPermission("core.command.feed")){
                if (args.length == 0){
                    p.setFoodLevel(20);
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("fed-self")));
                }
            } else {
                p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("no-permission")));
            }

            return true;
        }
    }
     
     
  2. You want to "return false" after almost every chunk of code in your 'if statements' unless you explicitly want your code to continue running. Also its better to check for argument length before checking for permission.
     
    • Agree Agree x 3
  3. add return true; after every no permission message, or atleast the first one
     
  4. AFAIK returning false in the earlier versions is wrong (not sure about 1.16), because it will display the command in chat.
     
  5. Hey, this worked.
    Adding a
    Code (Text):
    return true;
    after the first permission message fixes it.
     
  6. Please review my messages i need them
     
  7. I was going to rate this message with "funny" but now, no I don't think I will.
     
  8. You ask for help but dont help people...
     
  9. Man shut the fuck up and stop bumping this solved post
     
    • Agree Agree x 1