Chat style
This commit is contained in:
58
core/src/main/java/mc/core/ChatStyle.java
Normal file
58
core/src/main/java/mc/core/ChatStyle.java
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <dimon550@gmail.com>
|
||||||
|
* 2018-04-30
|
||||||
|
*/
|
||||||
|
package mc.core;
|
||||||
|
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public enum ChatStyle {
|
||||||
|
BLACK ('0'),
|
||||||
|
DARK_BLUE ('1'),
|
||||||
|
DARK_GREEN('2'),
|
||||||
|
DARK_CYAN ('3'),
|
||||||
|
DARK_RED ('4'),
|
||||||
|
PURPLE ('5'),
|
||||||
|
GOLD ('6'),
|
||||||
|
GRAY ('7'),
|
||||||
|
DARK_GRAY ('8'),
|
||||||
|
BLUE ('9'),
|
||||||
|
GREEN ('a'),
|
||||||
|
CYAN ('b'),
|
||||||
|
RED ('c'),
|
||||||
|
PINK ('d'),
|
||||||
|
YELLOW('e'),
|
||||||
|
WHITE ('f');
|
||||||
|
|
||||||
|
private static final char COLOR_CHAR = '\u00a7'; // §
|
||||||
|
private static final String codes = "0123456789aAbBcCdDeEfF";
|
||||||
|
private static final Pattern EXCAPE_PATTERN = Pattern.compile(COLOR_CHAR + "[0-9a-f]", Pattern.CASE_INSENSITIVE);
|
||||||
|
|
||||||
|
public static String format(char colorChar, String message) {
|
||||||
|
char[] chars = message.toCharArray();
|
||||||
|
for (int i = 0; i < chars.length; i++) {
|
||||||
|
if (chars[i] == colorChar && codes.indexOf(chars[i+1]) > -1) {
|
||||||
|
chars[i] = COLOR_CHAR;
|
||||||
|
chars[i+1] = Character.toLowerCase(chars[i+1]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return String.valueOf(chars);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String escapeStyle(String message) {
|
||||||
|
return EXCAPE_PATTERN.matcher(message).replaceAll("");
|
||||||
|
}
|
||||||
|
|
||||||
|
private char[] toString;
|
||||||
|
|
||||||
|
ChatStyle(char ch) {
|
||||||
|
toString = new char[]{ COLOR_CHAR, ch };
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return String.valueOf(toString);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,7 +16,6 @@ import mc.core.network.CSPacket;
|
|||||||
import mc.core.network.proto_125.netty.wrappers.WrapperNetChannel;
|
import mc.core.network.proto_125.netty.wrappers.WrapperNetChannel;
|
||||||
import mc.core.network.proto_125.packets.*;
|
import mc.core.network.proto_125.packets.*;
|
||||||
import org.slf4j.Marker;
|
import org.slf4j.Marker;
|
||||||
import org.slf4j.helpers.BasicMarker;
|
|
||||||
import org.slf4j.helpers.BasicMarkerFactory;
|
import org.slf4j.helpers.BasicMarkerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
@@ -173,7 +172,7 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void onChatMessagePacket(Channel channel, ChatMessagePacket packet) {
|
public void onChatMessagePacket(Channel channel, ChatMessagePacket packet) {
|
||||||
log.info(CHAT_MARKER, "<{}>: {}", channel.attr(ATTR_PLAYER).get().getName(), packet.getMessage());
|
log.info(CHAT_MARKER, "<{}>: {}", channel.attr(ATTR_PLAYER).get().getName(), ChatStyle.escapeStyle(packet.getMessage()));
|
||||||
playerManager.getBroadcastChannel().writeAndFlush(packet);
|
playerManager.getBroadcastChannel().writeAndFlush(packet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user