ASyncPlayerChatEvent

Discussion in 'Plugin Development' started by Polishgaming, Nov 7, 2015.

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

    Polishgaming

    So I am having trouble setting up ASyncPlayerChatEvent. Since I am new to plugin development the API page isn't much help. My goal is to make a simpler way of doing italics (&o) in chat. This works by typing in:

    Code:
    I *want* to make this in italics.
    Where the first * is changed to &o and the second * is changed to &r (or if there is a way to use colors from before the italics i.e. &6 colors the whole message including the italics).

    So far I have this in my separate class file:

    Code:
    package me.XplosionInc.ArtexCore.Chat;
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    
    public class Italics implements Listener {
    
        @EventHandler
        public void onChat(AsyncPlayerChatEvent italics) {
            Player player = italics.getPlayer();
            String name = player.getName();
        }
    }
    I have been told to get the message then split("*") but I have no idea on how to do this.
     
  2. Offline

    567legodude

    @Polishgaming There are a couple of ways, here is one (probably the simplest):
    You have to figure out how to implement it yourself.
    You could split at each asterisk (keep in mind it's a regular expression character), then use a for-loop on each one.
    Code:
    for (int i = 0; i < splitString.length; i++)
    Then use the modulus operator like
    Code:
    if ((i % 2) == 1)
    If that is true then add §o, if not then add §r (or whatever color was used before)
     
  3. Offline

    FreeMotion45

    If I am getting you right, you want chat colors using this : "&", symbol right ?

    So you can use :

    Code:
    ChatColor.AlternateColorCodes('&', "&6This will be in gold !")
    
    // I wrote this right here so the AlternateColorCodes may be different. Hope I helped :)
     
  4. Offline

    Kudze

    I dont know if this gonna work what you are doing but what i use is String text = ChatColor.GOLD + "This text is going to be gold";


    My code will work like this
    Code:
    @EventHandler
    public void onChat(ASyncPlayerChatEvent event) {
    Player player = event.getPlayer();
    String message = event.getMessage();//I dont know if this method is called like this check it when u do it :D Kappa
    if(message.contains("*")) {
    message.replaceFirst("*", ChatColor.ITALIC + "");//U do + "" because replace first needs return string so u getting around that :>
    message.replace("*", ChatColor.RED + "");//same here
    }
    event.getRecipients().clear(); //doesnt send raw message to the players
    for(Player i : Bukkit.getOnlinePlayers()) {
    i.sendMessage("<"+player.getName()+">: " + message); //sends message to all players :>
    }
    }
    
    
    I hope this helped :> P.S. I didn't tested it so if it doesn't work just say me because there are couple places to fix Kappa ;D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 8, 2015
  5. Offline

    567legodude

    @Kudze You assume that they only use bold one time.
    This would work on all occurrences:
    Code:
    @EventHandler
    public void onAsyncPlayerChat(AsyncPlayerChatEvent event) {
        // Basic "*" replace
        String[] s = event.getMessage().split("\\*");
        for (int i = 0; i < s.length; i++) {
            if ((i % 2) == 1) {
                s[i] = "§o" + s[i] + "§r";
            }
        }
        String msg = StringUtils.join(s);
        // You could set the color/format of the message here
        event.setMessage(msg);
    }
     
Thread Status:
Not open for further replies.

Share This Page