Checking players for a permission

Discussion in 'Plugin Development' started by YoloSanta, Jan 10, 2018.

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

    YoloSanta

    So im trying to make a report plugin, and for some reason when I check for staff member for a specific permission it only send the report message to some people, not all the staff
    Code:
    package me.yolosanta.report.commands;
    
    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;
    
    import me.yolosanta.report.Main;
    
    public class ReportCommand implements CommandExecutor {
    
        private Main main;
    
        public ReportCommand(Main main) {
            this.main = main;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "You must be a player to use this command.");
                return true;
            } else {
                if (!sender.hasPermission("command.report")) {
                    sender.sendMessage(ChatColor.RED + "You do not have permission to use this command!");
                    return true;
                }
                if (args.length == 0) {
                    sender.sendMessage(ChatColor.RED + "Usage: /report <player> <reason>");
                    return true;
    
                }
                if (args.length == 1) {
                    @SuppressWarnings("deprecation")
                    Player target = Bukkit.getServer().getPlayer(args[0]);
                    if (target == null) {
                        sender.sendMessage(ChatColor.RED + "That player was not found!");
                        return true;
                    } else {
                        sender.sendMessage(ChatColor.RED + "Usage: /report <player> <reason>");
                        return true;
                    }
                }
                String message = "";
                for (int i = 1; i != args.length; i++) {
                    message += args[i] + " ";
                }
                for (Player staff : Bukkit.getOnlinePlayers()) {
                    @SuppressWarnings("deprecation")
                    Player target = Bukkit.getServer().getPlayer(args[0]);
                    sender.sendMessage(ChatColor.GREEN + "Your report has been sent, we will review it shortly.");
                    if (staff.hasPermission("report.view") || staff.isOp()) {
                        staff.sendMessage(ChatColor.translateAlternateColorCodes('&',
                                main.getConfig().getString("Report-Message").replaceAll("%sender%", sender.getName())
                                        .replaceAll("%cheater%", target.getName()).replaceAll("%reason%", message)));
                        return true;
                    } else {
                        return true;
                    }
                }
                return false;
            }
        }
    }
     
  2. Offline

    Zombie_Striker

    Opped players have all permissions. No need to check if they are op.

    The reason it does not work is because you are returning. Remove all return true lines in the for loop.
     
  3. Offline

    YoloSanta

    Those dang return statements get me every time.

    If I can't return, how would i be able to stop these?
    Current Code.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 11, 2018
    Zombie_Striker likes this.
Thread Status:
Not open for further replies.

Share This Page