Solved Chat Prefixes

Discussion in 'Plugin Development' started by Coopah, Jan 13, 2015.

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

    Coopah

    How would I go about doing a dynamic sort of chat thing.

    So if a players clan was enemy to another players clan there name in chat would be red? BUT, for all his clan members it would be aqua and green for allies.

    So I can't just use a asyncplayerchat and set the colour, right...? How would I make it so it would be different for 3 different types of people...

    So his name would be green for allies, blue for clan members and red for enemies.

    I've got a config and what not to get the players. I don't need help getting the allies etc and I need help with changing the colour.
     
  2. Offline

    unrealdesign

    Look into how to make a chat plugin and I think you'll find it to be very simple.

    1. Learned how to make a chat plugin and not break other plugins that want to hook into chat.
    2. In your new chat plugin, create separate colored messages for the different scenerios.
    3. Then do checks for who sent the message and send the corresponding message.

    Also, not sure if it's just a tag that goes into the prefix, and if it is, then don't edit the message and just edit the prefix.
     
  3. Offline

    Coopah

    @unrealdesign
    I'll look into it now, however, I don't want to have to make a whole plugin for it. I want the plugin I'm currently making only 1 file.
    Also do you have any good links to a forum post or a video?
     
  4. @Coopah The chat event doesn't support this sort of thing - you'll need to intercept it, and manually send it to the Players, formatted however you want. Beware thread safety!
     
  5. Offline

    Coopah

    @AdamQpzm
    Oh okay! So that just gave me a great idea.
    I could just use the Bukkit Broadcast method and then make my check and send messages according. I could also just cancel the whole chatevent.

    Thank you very much! :)
     
  6. @Coopah If you're talking about broadcastMessage() that wouldn't work, since it'd still end up sending the same to all players. If you mean broadcast() then that wouldn't work either, since your messages depend on something other than permissions (ally/enemy, for example). I think what you want is the sendMessage() method :p
     
  7. Offline

    Coopah

    @AdamQpzm
    Ohh my bad, I meant to say Bukkit.getOnlinePlayers. I will loop through it and then do my check then send it differently to each of the groups.

    I will post the finished code once it's finished.

    Code:
        Clans plugin;
    
        public ChatTags(Clans plugin) {
    
            this.plugin = plugin;
    
        }
    
        @EventHandler
        public void onChat(AsyncPlayerChatEvent e) {
    
            Player p = e.getPlayer();
    
            String args = e.getMessage();
    
            e.setCancelled(true);
    
            this.executeChat(p, args);
    
        }
    
        public void executeChat(Player p, String args) {
    
            String words = args;
            String cName = plugin.getConfig().getString(p.getName() + ".Clan");
    
            if (cName != null) {
               
                p.sendMessage(ChatColor.DARK_AQUA + cName + " " + ChatColor.AQUA + p.getName() + ChatColor.WHITE + " " + words);
           
            }
           
            for (Player players : Bukkit.getOnlinePlayers()) {
    
                String aPlayers = plugin.getConfig().getString(players.getName() + ".Clan");
    
    
                if (cName == null) {
    
                    players.sendMessage(ChatColor.YELLOW + p.getName() + " " + ChatColor.WHITE + words);
                }
    
                if (plugin.getConfig().getStringList(cName + ".Allies").contains(aPlayers)) {
    
                    if (plugin.getConfig().getStringList(aPlayers + ".Members").contains(players.getName()) ||
                            plugin.getConfig().getStringList(aPlayers + ".Moderators").contains(players.getName()) ||
                            plugin.getConfig().getStringList(aPlayers + ".Admins").contains(players.getName()) ||
                            plugin.getConfig().getString(aPlayers + ".Leader").contains(players.getName())) {
    
                        players.sendMessage(ChatColor.DARK_GREEN + cName + " " + ChatColor.GREEN + p.getName() + ChatColor.WHITE + " " + words);
                    }
                } else {
    
                    if (cName != null && players.getName() != p.getName()) {
    
                        players.sendMessage(ChatColor.GOLD + cName + " " + ChatColor.YELLOW + p.getName() + ChatColor.WHITE + " " + words);
    
    
                    }
                }
            }
        }
    That's the code I made up if anyone wants to use it for free. Uhmm, still need to add enemies but haven't made that yet.

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Jan 13, 2015
    AdamQpzm likes this.
  8. @Coopah Bare in mind that accessing the config isn't thread safe from what I know :)
     
  9. Offline

    Coopah

  10. Offline

    Coopah

    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page