How-to Chat Channels?

Discussion in 'Plugin Development' started by serfma, Mar 12, 2013.

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

    serfma

    I'll start off by saying that yes I looked at source code of multiple chat plugins and I still don't understand how I would create a couple or so chat channels that will be set in stone (I won't need to change it unless I absolutely need to). Could anyone help point me in the right direction and help me get started?

    I'll have two channels that will be sent to Players with a certain string attached to their name that I grab from a config file. I'll just call it "Guests" and "Users". I'm going to need to be able to set it so that when Guests talk, Users are unable to hear them and vice versa. Yes I know it sounds useless, but I'll be using it for other things.

    I kind of have a general idea on how to start, but I'm not quite sure. Would I need to add players that log onto the server that have "Guest" to be put on an arrayList and the ones with "User" to be on a separate list so when they chat, I use a "for" to check which players it sends the messages to? I may be completely wrong here though.
     
  2. Offline

    RealDope

    Keep a List<String> of player names for each channel.

    Listen to ASyncPlayerChatEvent, check what list that player is in, send it to everyone else on the list.
     
  3. Offline

    serfma

    Could you, or anyone else, explain it a bit further? I have the arraylists set up correctly, how would I use the for loop to go through the list?
     
  4. Offline

    ZeusAllMighty11

    Code:
    for(String s  : nameOfList){
        // do something with each value
    }
    
     
  5. Offline

    serfma

    Such as;

    Code:
            if (Redemption.playerSideConfig.getString("PlayerSide." + playerChat.getPlayer().getName() + ".side").equals("living"))
            {
                for (Player p : Redemption.livingPlayers)
                {
                    playerChat.setFormat(ChatColor.WHITE + "[" + ChatColor.DARK_AQUA + "Living" + ChatColor.WHITE + "] " + ChatColor.DARK_AQUA + playerChat.getPlayer().getName() + ChatColor.WHITE + ": " + ChatColor.AQUA + playerChat.getMessage());
                }
            }
    
    Which I know I'm wrong in some way. :p

    EDIT: NVM.
     
  6. Offline

    StevenMG

    You can manipulate who hears a chat message using playerChat.getRecipients().clear and then do playerChat.getRecipients().add(player) if they are in the channel.

    You could also do playerChat.getRecipients().remove(player) instead.
     
  7. Offline

    serfma

    Code:
                playerChat.getRecipients().clear();
                playerChat.getRecipients().addAll(Redemption.livingPlayers);
    
    Is what I used. It works how I need it. :p
     
  8. Offline

    StevenMG

    Ok, so you just need help with formatting? or is it solved now?
     
  9. Offline

    serfma

    It's solved. All I'm going to do next is set up a global and staff "channel" too. But I believe I've got it. Just use perms for the staff, and the global channel to be just like normal chatting.

    Actually, Now when a player puts "%" in chat, it tosses an error on setFormat.

    Code:
    playerChat.setFormat(ChatColor.WHITE + "[" + ChatColor.DARK_AQUA + "Living" + ChatColor.WHITE + "] " + ChatColor.DARK_AQUA + playerChat.getPlayer().getName() + ChatColor.WHITE + ": " + ChatColor.AQUA + playerChat.getMessage());
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  10. Offline

    RealDope

    Not sure if this would work, but before setting the format, get the message, look for any % and add // to the front of it. I don't know a ton about regex and special characters, but I've seen this done to make special characters "normal".
     
  11. Offline

    serfma

    It's only the case with %. It doesn't mess up anything in-game except show no format and doesn't show the message to anyone but the sender themselves.
     
  12. Offline

    StevenMG

    I doubt if that would work as a chat message will not be treated as regex in the first place. I'm not really sure what to in this case either though :( Is there any error in the server console? If so post it here so we can help you out :)
     
  13. You're using the format wrong.
    Format has nothing to do with regex by the way and they're not treated as such, you are treating them as such because you manually shoved the message into the format string which should only contain format characters which are replaced by the formatter.
    Use %s to define player's name and another %s to define the message, just as simple as that.

    But in case you don't understand, here:
    Code:
    playerChat.setFormat("[" + ChatColor.DARK_AQUA + "Living" + ChatColor.WHITE + "] " + ChatColor.DARK_AQUA + "%s" + ChatColor.WHITE + ": %s");
    Also, tutorial on format: http://www.homeandlearn.co.uk/java/java_formatted_strings.html
    This style of format is not only for Java, it's used in alot of other languages, it's useful to know :p
     
Thread Status:
Not open for further replies.

Share This Page