Using the indefinite parameter operator (...) doesn't work when trying to send messages.

Discussion in 'Plugin Development' started by Mr Burkes, May 20, 2014.

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

    Mr Burkes

    First of all, I know that these are the methods that aren't working; I traced it and got it down to this part in the code.

    I'll post my code, which is a lot, but it's all just repeated:

    Code:
    public class M
    {
        public static void gm(String p_msg, CommandSender ... p_pls)
        {
            for(CommandSender i_pl : p_pls)
            {
                i_pl.sendMessage(ChatColor.GRAY + "[GM] " + ChatColor.WHITE + p_msg);
            }
        }
       
        public static void gmError(String p_msg, CommandSender ... p_pls)
        {
            for(CommandSender i_pl : p_pls)
            {
                i_pl.sendMessage(ChatColor.RED + "[GM] " + ChatColor.WHITE + p_msg);
            }
        }
       
        public static void gmWarn(String p_msg, CommandSender ... p_pls)
        {
            for(CommandSender i_pl : p_pls)
            {
                i_pl.sendMessage(ChatColor.GOLD + "[GM] " + ChatColor.WHITE + p_msg);
            }
        }
       
        public static void spectator(String p_msg, CommandSender ... p_pls)
        {
            for(CommandSender i_pl : p_pls)
            {
                i_pl.sendMessage(ChatColor.GRAY + "[*] " + ChatColor.WHITE + p_msg);
            }
        }
       
        public static void alive(String p_msg, CommandSender ... p_pls)
        {
            for(CommandSender i_pl : p_pls)
            {
                i_pl.sendMessage(ChatColor.GRAY + "[!] " + ChatColor.WHITE + p_msg);
            }
        }
    }
    As you can see, I made the M class for the purpose of making chat easier on me. The reason I take CommandSender instead of Player is so that I can send to the console easily as well. Because Player is a subclass of CommandSender, there should be no problem. However, these methods are not working, and I'm wondering why? This is the latest CB beta build on 1.7.9.
     
  2. Offline

    Trevor1134

    What are you trying to do, broadcast a message? Because there can only be one CommandSender per command... If you want to message a list of players, add then to a list<String> and then iterate through the list, convert the string to player, and send them a message.
     
  3. Offline

    Mr Burkes

    I'm afraid you did not read my post wholly.

    I'm trying to broadcast a message, yes, but only to certain people. CommandSender is a superinterface of Player so I do not see why I am having this problem.
     
  4. Offline

    evilmidget38

    Mr Burkes Can you expand upon "Does not work"? What exactly is the issue? The message isn't being sent? Can you also paste the code where you call these methods? Have you done some debug to ensure that these methods are being called?
     
    lukegb likes this.
  5. Offline

    Trevor1134

    Mr Burkes what is the issue? I did, indeed read your post entirely, but you are not being clear. If you want help, you need to explain the issue, rather than just saying "its not working." Is a stack trace being printed? Have you tried debugging lines?
     
  6. Offline

    Mr Burkes

    Sorry, the message is not being sent. The code runs, but nothing happens. No stack trace, no errors, just no output.
     
  7. Offline

    xTigerRebornx

    Mr Burkes
     
  8. Offline

    RawCode

    add debug messages.
     
  9. Offline

    Mr Burkes

    It doesn't matter where I call them; they run. All code around it runs, so that means that my methods must run. But I get no output.

    I know it runs, and no exceptions are thrown; so what should debug methods should I add?
     
  10. Offline

    RawCode

    entry and exit for every method

    entry and exit for every loop

    specs of input params like array lenght
     
  11. Offline

    Etsijä

    I would suggest you try it out first with using Player instead of CommandSender. If that doesn't work, then your problem lies somewhere else. Also you could try calling one of your methods with one Player/CommandSender argument only. It always helps to simplify around the problematic code.
     
  12. Offline

    Garris0n

    They're called varargs.

    And I literally had to read that multiple times because the variable names kept throwing me off. Seriously, why...
     
    Konkz likes this.
  13. Offline

    RawCode

    varargs is syntax sugar of array param.
     
  14. Etsijä Not sure why Player would make any difference.
     
  15. Offline

    Etsijä

    Probably it doesn't. It's just that I'd try out something different in a mystical case like this.
     
  16. Offline

    Rocoty

    They might well run. But are you certain you are passing appropriate arguments to the methods? You might as well show us the code where you call the methods.
     
Thread Status:
Not open for further replies.

Share This Page