for (Player p : Bukkit.getOnlinePlayers()) { failing

Discussion in 'Spigot Plugin Development' started by DecisionsYT, May 15, 2016.

  1. Hey!

    In my command spy, when doing /cmdspy on. I see the commands perfectly, however, I DON'T see commands of other players, just mine.

    CODE:

    Code (Text):
    package ChatFilter;

    import java.util.ArrayList;
    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 org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;

    import me.DecisionsYT.Main;

    public class CmdSpy implements Listener, CommandExecutor {
       
    Main plugin;
       
        public CmdSpy(Main passedPlugin) {
            this.plugin = passedPlugin;
        }

        public static ArrayList<String> spy = new ArrayList<String>();
       
        @EventHandler
        public void PlayerCommand(PlayerCommandPreprocessEvent e) {
            Player player = e.getPlayer();
            for (Player p : Bukkit.getOnlinePlayers())
                if (CmdSpy.spy.contains(p.getName()))
                  player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("CmdSpy-Format").replace("{player}", player.getName()).replace("{display_name}", player.getDisplayName()).replace("{command}", e.getMessage())));
                 
        }

      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
       
          if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Non-Player")));
                return true;
            }
           
            Player player = (Player) sender;
         
            if (args.length == 0) {
                if (sender.hasPermission("ChatX.CmdSpy")) {
                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("CmdSpy-Usage").replace("{player}", player.getName()).replace("{display_name}", player.getDisplayName())));
                return true;
                }
           }
           
           
             else if (args.length == 1) {
                 if (sender.hasPermission("ChatX.CmdSpy")) {
                     if (args[0].equalsIgnoreCase("On")) {
                         sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("CmdSpy-Enabled").replace("{player}", player.getName()).replace("{display_name}", player.getDisplayName())));
                         CmdSpy.spy.add(sender.getName());
                         return true;
                     }
                     else if (args[0].equalsIgnoreCase("Off")) {
                         sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("CmdSpy-Disabled").replace("{player}", player.getName()).replace("{display_name}", player.getDisplayName())));
                         CmdSpy.spy.remove(sender.getName());
                         return true;
                     }
                 }
             }
           
           
            if (!sender.hasPermission("ChatX.CmdSpy")) {
                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("No-Perm-Message").replace("{player}", player.getName()).replace("{display_name}", player.getDisplayName())));
                return true;
            }
       
       
       
        return true;
            }
          }
     
  2. Its because you are checking if it contains the player in the CmdSpy collection or whatever it is. Do another for loop circling through the players inside CmdSpy, and send the message to every player with it toggled on. You are checking if the player EXECUTING the command has it toggled on, meaning you arent even sending the commandspy message to others with it toggled on as you are only sending it to e.getPlayer() instead of all players in the collection.
     
  3. No


    Sent from my iPod touch using Tapatalk
     
  4. have you register the listener ?
     
  5. Yes


    Sent from my iPod touch using Tapatalk
     
  6. edit this


    Code (Text):
    @EventHandler
        public void PlayerCommand(PlayerCommandPreprocessEvent e) {
            Player player = e.getPlayer();
            for (Player p : Bukkit.getOnlinePlayers())
                if (CmdSpy.spy.contains(p.getName()))
                  player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("CmdSpy-Format").replace("{player}", player.getName()).replace("{display_name}", player.getDisplayName()).replace("{command}", e.getMessage())));
                 
        }

    to this

    Code (Text):
    @EventHandler
        public void PlayerCommand(PlayerCommandPreprocessEvent e) {
            Player player = e.getPlayer();
            for (Player p : Bukkit.getOnlinePlayers())
                if (CmdSpy.spy.contains(p.getName()))
                  p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("CmdSpy-Format").replace("{player}", player.getName()).replace("{display_name}", player.getDisplayName()).replace("{command}", e.getMessage())));
                 
        }
     
  7. Whats the difference between them?
     
  8. The one you had checked if the player EXECUTING the command had it toggled on, if so send them the commandspy msg
    The one he made goes through all players and sends EVERYONE with it toggled on the command, you had it sending the command to only the player doing the command if it was toggled on