RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
ChatCodec.java
1package com.osroyale.util;
2
3import com.osroyale.game.world.entity.mob.player.relations.ChatMessage;
4
5import java.nio.ByteBuffer;
6
38
39* A utility class for encoding/decoding messages.
40 *
41 * @author nshusa
42 */
43public final class ChatCodec {
44
48 private static final int MAX_COMPRESS_LEN = 80;
49
55 private static final char[] CHARACTER_TABLE = {
56 ' ', 'e', 't', 'a', 'o', 'i', 'h', 'n', 's', 'r',
57 'd', 'l', 'u', 'm', 'w', 'c', 'y', 'f', 'g', 'p',
58 'b', 'v', 'k', 'x', 'j', 'q', 'z', '0', '1', '2',
59 '3', '4', '5', '6', '7', '8', '9', ' ', '!', '?',
60 '.', ',', ':', ';', '(', ')', '-', '&', '*', '\\',
61 '\'', '@', '#', '+', '=', '\243', '$', '%', '"',
62 '[', ']', '_', '/', '<', '>', '^', '|'
63 };
64
68 private ChatCodec() {
69
70 }
71
80 public static byte[] encode(String input) {
81 if (input.length() > ChatMessage.CHARACTER_LIMIT) {
82 input = input.substring(0, ChatMessage.CHARACTER_LIMIT).toLowerCase();
83 } else {
84 input = input.toLowerCase();
85 }
86
87 ByteBuffer buffer = ByteBuffer.allocate(ChatMessage.CHARACTER_LIMIT);
88
89 int count = 0;
90 for (int i = 0; i < input.length(); i++) {
91 char key = input.charAt(i);
92 int index = 0;
93
94 for (int n = 0; n < CHARACTER_TABLE.length; n++) {
95 if (key != CHARACTER_TABLE[n]) {
96 continue;
97 }
98
99 index = n;
100 count++;
101 break;
102 }
103
104 buffer.put((byte) index);
105 }
106
107 final byte[] temp = new byte[count];
108 System.arraycopy(buffer.array(), 0, temp, 0, temp.length);
109
110 return temp;
111 }
112
121 public static String decode(byte[] input) {
122 if (input.length > MAX_COMPRESS_LEN) {
123 byte[] temp = new byte[MAX_COMPRESS_LEN];
124 System.arraycopy(input, 0, temp, 0, temp.length);
125 input = temp;
126 }
127
128 final ByteBuffer buffer = ByteBuffer.wrap(input);
129 final StringBuilder builder = new StringBuilder();
130 for (int i = 0; i < buffer.capacity(); i++) {
131 final int index = buffer.get() & 0xFF;
132 if (index < CHARACTER_TABLE.length) {
133 builder.append(CHARACTER_TABLE[index]);
134 }
135 }
136 return filter(builder.toString());
137 }
138
147 public static String filter(String input) {
148 final StringBuilder builder = new StringBuilder();
149 for (char c : input.toLowerCase().toCharArray()) {
150 for (char validChar : CHARACTER_TABLE) {
151 if (c == validChar) {
152 builder.append(c);
153 break;
154 }
155 }
156 }
157
158 boolean capitalize = true;
159
160 int length = builder.length();
161
162 for (int index = 0; index < length; index++) {
163 char character = builder.charAt(index);
164
165 if (character == '.' || character == '!' || character == '?') {
166 capitalize = true;
167 } else if (capitalize && !Character.isWhitespace(character)) {
168 builder.setCharAt(index, Character.toUpperCase(character));
169 capitalize = false;
170 } else {
171 builder.setCharAt(index, Character.toLowerCase(character));
172 }
173 }
174
175 return builder.toString();
176 }
177
178}