1package com.runehive.util;
3import com.runehive.game.world.entity.mob.player.relations.ChatMessage;
5import java.nio.ByteBuffer;
12public final class ChatCodec {
17 private static final int MAX_COMPRESS_LEN = 80;
24 private static final char[] CHARACTER_TABLE = {
25 ' ',
'e',
't',
'a',
'o',
'i',
'h',
'n',
's',
'r',
26 'd',
'l',
'u',
'm',
'w',
'c',
'y',
'f',
'g',
'p',
27 'b',
'v',
'k',
'x',
'j',
'q',
'z',
'0',
'1',
'2',
28 '3',
'4',
'5',
'6',
'7',
'8',
'9',
' ',
'!',
'?',
29 '.',
',',
':',
';',
'(',
')',
'-',
'&',
'*',
'\\',
30 '\'',
'@',
'#',
'+',
'=',
'\243',
'$',
'%',
'"',
31 '[',
']',
'_',
'/',
'<',
'>',
'^',
'|'
49 public static byte[] encode(String input) {
50 if (input.length() > ChatMessage.CHARACTER_LIMIT) {
51 input = input.substring(0, ChatMessage.CHARACTER_LIMIT).toLowerCase();
53 input = input.toLowerCase();
56 ByteBuffer buffer = ByteBuffer.allocate(ChatMessage.CHARACTER_LIMIT);
59 for (
int i = 0; i < input.length(); i++) {
60 char key = input.charAt(i);
63 for (
int n = 0; n < CHARACTER_TABLE.length; n++) {
64 if (key != CHARACTER_TABLE[n]) {
73 buffer.put((
byte) index);
76 final byte[] temp =
new byte[count];
77 System.arraycopy(buffer.array(), 0, temp, 0, temp.length);
90 public static String decode(
byte[] input) {
91 if (input.length > MAX_COMPRESS_LEN) {
92 byte[] temp =
new byte[MAX_COMPRESS_LEN];
93 System.arraycopy(input, 0, temp, 0, temp.length);
97 final ByteBuffer buffer = ByteBuffer.wrap(input);
98 final StringBuilder builder =
new StringBuilder();
99 for (
int i = 0; i < buffer.capacity(); i++) {
100 final int index = buffer.get() & 0xFF;
101 if (index < CHARACTER_TABLE.length) {
102 builder.append(CHARACTER_TABLE[index]);
105 return filter(builder.toString());
116 public static String filter(String input) {
117 final StringBuilder builder =
new StringBuilder();
118 for (
char c : input.toLowerCase().toCharArray()) {
119 for (
char validChar : CHARACTER_TABLE) {
120 if (c == validChar) {
127 boolean capitalize =
true;
129 int length = builder.length();
131 for (
int index = 0;
index < length;
index++) {
132 char character = builder.charAt(index);
134 if (character ==
'.' || character ==
'!' || character ==
'?') {
136 }
else if (capitalize && !Character.isWhitespace(character)) {
137 builder.setCharAt(index, Character.toUpperCase(character));
140 builder.setCharAt(index, Character.toLowerCase(character));
144 return builder.toString();