Skip to content

Commit 23db72a

Browse files
committed
added encryption
1 parent f9e5116 commit 23db72a

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.nxyi.addon.Utils;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.io.OutputStreamWriter;
7+
import java.net.HttpRetryException;
8+
import java.net.HttpURLConnection;
9+
import java.net.URL;
10+
import java.nio.charset.StandardCharsets;
11+
import java.util.UUID;
12+
import com.google.gson.JsonObject;
13+
import org.apache.http.client.HttpResponseException;
14+
15+
public class NetworkUtils {
16+
public static String postJson(final String urlString, JsonObject requestBody) throws IOException {
17+
URL url = new URL(urlString);
18+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
19+
connection.setDoInput(true);
20+
connection.setDoOutput(true);
21+
connection.setRequestMethod("POST");
22+
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
23+
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8);
24+
writer.write(requestBody.toString());
25+
writer.close();
26+
if (connection.getResponseCode() == 204)
27+
return "";
28+
if (connection.getResponseCode() != 200) {
29+
throw new HttpRetryException("", 403);
30+
}
31+
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
32+
StringBuilder builder = new StringBuilder();
33+
String line;
34+
while ((line = reader.readLine()) != null) {
35+
builder.append(line);
36+
}
37+
reader.close();
38+
connection.disconnect();
39+
return builder.toString();
40+
}
41+
}

src/main/java/com/nxyi/addon/modules/Bookchat.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import net.minecraft.util.Hand;
2121

2222
import java.lang.reflect.Array;
23+
import org.apache.commons.codec.binary.Base64;
2324
import java.util.*;
2425

2526
public class Bookchat extends Module {
@@ -32,13 +33,21 @@ public Bookchat() {
3233
private final SettingGroup sgGeneral = this.settings.getDefaultGroup();
3334
private final Setting<String> person = sgGeneral.add(new StringSetting.Builder().name("Person").description("Person your chatting with").defaultValue("").build());
3435

36+
private final Setting<Boolean> encryption = sgGeneral.add(new BoolSetting.Builder()
37+
.name("encryption")
38+
.description("Encrypt the messages in base64.")
39+
.defaultValue(false)
40+
.build()
41+
);
42+
3543
@EventHandler
3644
private void onchat(SendMessageEvent event){
37-
String msg = mc.player.getEntityName() + " : " + event.message;
45+
String msg = event.message;
3846
if (mc.player.getMainHandStack().getItem().equals(Items.WRITABLE_BOOK)){
3947
event.cancel();
40-
info(msg);
41-
mc.player.networkHandler.sendPacket(new BookUpdateC2SPacket(mc.player.getInventory().selectedSlot, Collections.singletonList(msg), Optional.empty()));
48+
info(mc.player.getEntityName() + " : " + msg);
49+
String encrypted = Base64.encodeBase64String(msg.getBytes());
50+
mc.player.networkHandler.sendPacket(new BookUpdateC2SPacket(mc.player.getInventory().selectedSlot, Collections.singletonList((encryption.get() ? encrypted : msg)), Optional.empty()));
4251
}
4352
}
4453

@@ -56,6 +65,7 @@ private void ontick(TickEvent.Pre event){
5665
PlayerEntity playerEntity = null;
5766

5867
if (person.get().isEmpty()) return;
68+
if(mc.world.getPlayers() == null) return;
5969
for (PlayerEntity p : mc.world.getPlayers()){
6070
if (p.getEntityName().equalsIgnoreCase(person.get())){
6171
playerEntity = p;
@@ -65,16 +75,22 @@ private void ontick(TickEvent.Pre event){
6575
if (playerEntity == null) return;
6676

6777
if (playerEntity.getMainHandStack().getItem().equals(Items.WRITABLE_BOOK)){
78+
if (playerEntity.getMainHandStack().getNbt().get("pages").toString() == null) return;
6879
String text = playerEntity.getMainHandStack().getNbt().get("pages").toString()
6980
.replaceAll("\\]", "")
7081
.replaceAll("\\[", "")
7182
.replaceAll("\"", "")
7283
.replaceAll("," , " ");
7384

85+
byte[] decodedBytes = Base64.decodeBase64(text);
86+
String decodedString = new String(decodedBytes);
87+
7488
if (Objects.equals(lasttext, text)) return;
7589

90+
boolean isBase64 = Base64.isBase64(text.getBytes());
91+
7692
lasttext = text;
77-
info(text);
93+
info(playerEntity.getEntityName() + " : " + (isBase64 ? decodedString : text));
7894
}
7995
}
8096
}

0 commit comments

Comments
 (0)