Skip to content

Commit 9d067ad

Browse files
committedMay 13, 2022
22w19a
1 parent 2f1f2ac commit 9d067ad

34 files changed

+364
-88
lines changed
 

‎java/src/me/dustin/chatbot/entity/player/ClientPlayer.java

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package me.dustin.chatbot.entity.player;
22

33
import me.dustin.chatbot.ChatBot;
4+
import me.dustin.chatbot.block.BlockPos;
5+
import me.dustin.chatbot.block.BlockState;
46
import me.dustin.chatbot.helper.KeyHelper;
57
import me.dustin.chatbot.helper.StopWatch;
68
import me.dustin.chatbot.network.ClientConnection;

‎java/src/me/dustin/chatbot/helper/KeyHelper.java

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public static SaltAndSig generateSaltAndSig(Instant instant, String message) {
6565
}
6666

6767
public static KeyContainer getKeyContainer(KeyPairResponse keyPairResponse) {
68+
if (keyPairResponse == null)
69+
return null;
6870
return new KeyContainer(getPrivateKey(keyPairResponse.getPrivateKey()), new PublicKeyContainer(Instant.parse(keyPairResponse.getExpiresAt()), keyPairResponse.getPublicKey(), keyPairResponse.getPublicKeySignature()), Instant.parse(keyPairResponse.getRefreshedAfter()));
6971
}
7072

‎java/src/me/dustin/chatbot/nbt/NbtByte.java

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package me.dustin.chatbot.nbt;
22

3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonObject;
5+
import com.google.gson.JsonPrimitive;
6+
37
import java.io.DataInput;
48
import java.io.DataOutput;
59
import java.io.IOException;
@@ -24,6 +28,11 @@ public Object getValue() {
2428
return b;
2529
}
2630

31+
@Override
32+
public JsonElement toJson() {
33+
return new JsonPrimitive(b);
34+
}
35+
2736
@Override
2837
public String toString() {
2938
return "NbtByte{" +

‎java/src/me/dustin/chatbot/nbt/NbtByteArray.java

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package me.dustin.chatbot.nbt;
22

3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonElement;
5+
36
import java.io.DataInput;
47
import java.io.DataOutput;
58
import java.io.IOException;
@@ -18,6 +21,15 @@ public Object getValue() {
1821
return bs;
1922
}
2023

24+
@Override
25+
public JsonElement toJson() {
26+
JsonArray jsonArray = new JsonArray();
27+
for (byte b : bs) {
28+
jsonArray.add(new NbtByte(b).toJson());
29+
}
30+
return jsonArray;
31+
}
32+
2133
public static NbtByteArray read(DataInput input, int depth) throws IOException {
2234
int size = input.readInt();
2335
byte[] bs = new byte[size];

‎java/src/me/dustin/chatbot/nbt/NbtCompound.java

+25-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package me.dustin.chatbot.nbt;
22

3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
import com.google.gson.JsonElement;
6+
import com.google.gson.JsonObject;
7+
38
import java.io.DataInput;
49
import java.io.DataOutput;
510
import java.io.IOException;
@@ -52,17 +57,34 @@ public void put(String name, NbtElement element) {
5257

5358
@Override
5459
public String toString() {
55-
return "NbtCompound{" +
56-
"elements=" + elements +
57-
'}';
60+
Gson prettyGson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
61+
return "NbtCompound: " + prettyGson.toJson(toJson());
5862
}
5963

6064
public Map<String, NbtElement> getElements() {
6165
return elements;
6266
}
6367

68+
public boolean has(String element) {
69+
return getElements().containsKey(element);
70+
}
71+
6472
@Override
6573
public Object getValue() {
6674
return elements;
6775
}
76+
77+
public NbtElement get(String element) {
78+
return getElements().get(element);
79+
}
80+
81+
@Override
82+
public JsonElement toJson() {
83+
JsonObject o = new JsonObject();
84+
for (String s1 : elements.keySet()) {
85+
NbtElement nbtElement = elements.get(s1);
86+
o.add(s1, nbtElement.toJson());
87+
}
88+
return o;
89+
}
6890
}

‎java/src/me/dustin/chatbot/nbt/NbtDouble.java

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package me.dustin.chatbot.nbt;
22

3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonPrimitive;
5+
36
import java.io.DataInput;
47
import java.io.DataOutput;
58
import java.io.IOException;
@@ -16,6 +19,11 @@ public Object getValue() {
1619
return d;
1720
}
1821

22+
@Override
23+
public JsonElement toJson() {
24+
return new JsonPrimitive(d);
25+
}
26+
1927
public static NbtDouble read(DataInput input, int depth) throws IOException {
2028
return new NbtDouble(input.readDouble());
2129
}

‎java/src/me/dustin/chatbot/nbt/NbtElement.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package me.dustin.chatbot.nbt;
22

3+
import com.google.gson.JsonElement;
4+
35
import java.io.DataInput;
46
import java.io.DataOutput;
57
import java.io.IOException;
@@ -100,8 +102,5 @@ public default int getType() {
100102

101103
void write(DataOutput dataOutput) throws IOException;
102104
Object getValue();
103-
104-
public default String asString() {
105-
return toString();
106-
}
105+
JsonElement toJson();
107106
}

‎java/src/me/dustin/chatbot/nbt/NbtEnd.java

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package me.dustin.chatbot.nbt;
22

3+
import com.google.gson.JsonElement;
4+
35
import java.io.DataInput;
46
import java.io.DataOutput;
57
import java.io.IOException;
@@ -21,4 +23,9 @@ public void write(DataOutput dataOutput) throws IOException {
2123
public Object getValue() {
2224
return null;
2325
}
26+
27+
@Override
28+
public JsonElement toJson() {
29+
return null;
30+
}
2431
}

‎java/src/me/dustin/chatbot/nbt/NbtFloat.java

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package me.dustin.chatbot.nbt;
22

3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonPrimitive;
5+
36
import java.io.DataInput;
47
import java.io.DataOutput;
58
import java.io.IOException;
@@ -16,6 +19,11 @@ public Object getValue() {
1619
return f;
1720
}
1821

22+
@Override
23+
public JsonElement toJson() {
24+
return new JsonPrimitive(f);
25+
}
26+
1927
public static NbtFloat read(DataInput input, int depth) throws IOException {
2028
return new NbtFloat(input.readFloat());
2129
}

‎java/src/me/dustin/chatbot/nbt/NbtInt.java

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package me.dustin.chatbot.nbt;
22

3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonPrimitive;
5+
36
import java.io.DataInput;
47
import java.io.DataOutput;
58
import java.io.IOException;
@@ -16,6 +19,11 @@ public Object getValue() {
1619
return i;
1720
}
1821

22+
@Override
23+
public JsonElement toJson() {
24+
return new JsonPrimitive(i);
25+
}
26+
1927
public static NbtInt read(DataInput input, int depth) throws IOException {
2028
return new NbtInt(input.readInt());
2129
}

‎java/src/me/dustin/chatbot/nbt/NbtIntArray.java

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package me.dustin.chatbot.nbt;
22

3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonElement;
5+
36
import java.io.DataInput;
47
import java.io.DataOutput;
58
import java.io.IOException;
@@ -33,6 +36,15 @@ public Object getValue() {
3336
return is;
3437
}
3538

39+
@Override
40+
public JsonElement toJson() {
41+
JsonArray jsonArray = new JsonArray();
42+
for (int i : is) {
43+
jsonArray.add(new NbtInt(i).toJson());
44+
}
45+
return jsonArray;
46+
}
47+
3648
@Override
3749
public int size() {
3850
return 0;

‎java/src/me/dustin/chatbot/nbt/NbtList.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package me.dustin.chatbot.nbt;
22

33
import com.google.common.collect.Lists;
4+
import com.google.gson.JsonArray;
5+
import com.google.gson.JsonElement;
46

57
import java.io.DataInput;
68
import java.io.DataOutput;
@@ -23,10 +25,19 @@ public NbtList() {
2325
}
2426

2527
@Override
26-
public Object getValue() {
28+
public List<NbtElement> getValue() {
2729
return elements;
2830
}
2931

32+
@Override
33+
public JsonElement toJson() {
34+
JsonArray jsonArray = new JsonArray();
35+
for (NbtElement nbtElement : elements) {
36+
jsonArray.add(nbtElement.toJson());
37+
}
38+
return jsonArray;
39+
}
40+
3041
@Override
3142
public void write(DataOutput output) throws IOException {
3243
this.type = this.elements.isEmpty() ? (byte)0 : this.elements.get(0).getType();

‎java/src/me/dustin/chatbot/nbt/NbtLong.java

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package me.dustin.chatbot.nbt;
22

3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonPrimitive;
5+
36
import java.io.DataInput;
47
import java.io.DataOutput;
58
import java.io.IOException;
@@ -25,6 +28,11 @@ public void write(DataOutput output) throws IOException {
2528
output.writeLong(this.l);
2629
}
2730

31+
@Override
32+
public JsonElement toJson() {
33+
return new JsonPrimitive(l);
34+
}
35+
2836
@Override
2937
public long longValue() {
3038
return this.l;

‎java/src/me/dustin/chatbot/nbt/NbtLongArray.java

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package me.dustin.chatbot.nbt;
22

3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonElement;
5+
36
import java.io.DataInput;
47
import java.io.DataOutput;
58
import java.io.IOException;
@@ -33,6 +36,15 @@ public Object getValue() {
3336
return ls;
3437
}
3538

39+
@Override
40+
public JsonElement toJson() {
41+
JsonArray jsonArray = new JsonArray();
42+
for (long l : ls) {
43+
jsonArray.add(new NbtLong(l).toJson());
44+
}
45+
return jsonArray;
46+
}
47+
3648
@Override
3749
public int size() {
3850
return 0;

‎java/src/me/dustin/chatbot/nbt/NbtShort.java

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package me.dustin.chatbot.nbt;
22

3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonPrimitive;
5+
36
import java.io.DataInput;
47
import java.io.DataOutput;
58
import java.io.IOException;
@@ -25,6 +28,12 @@ public void write(DataOutput output) throws IOException {
2528
output.writeShort(this.s);
2629
}
2730

31+
@Override
32+
public JsonElement toJson() {
33+
return new JsonPrimitive(s);
34+
}
35+
36+
2837
@Override
2938
public long longValue() {
3039
return this.s;

‎java/src/me/dustin/chatbot/nbt/NbtString.java

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package me.dustin.chatbot.nbt;
22

3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonPrimitive;
5+
36
import java.io.DataInput;
47
import java.io.DataOutput;
58
import java.io.IOException;
@@ -16,6 +19,11 @@ public void write(DataOutput dataOutput) throws IOException {
1619
dataOutput.writeUTF(this.string);
1720
}
1821

22+
@Override
23+
public JsonElement toJson() {
24+
return new JsonPrimitive(string);
25+
}
26+
1927
@Override
2028
public Object getValue() {
2129
return string;

‎java/src/me/dustin/chatbot/network/ClientConnection.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import me.dustin.chatbot.helper.StopWatch;
1818
import me.dustin.chatbot.helper.TPSHelper;
1919
import me.dustin.chatbot.network.key.KeyContainer;
20+
import me.dustin.chatbot.network.key.PublicKeyContainer;
2021
import me.dustin.chatbot.network.key.SaltAndSig;
2122
import me.dustin.chatbot.network.packet.Packet;
2223
import me.dustin.chatbot.network.packet.impl.handshake.ServerBoundHandshakePacket;
@@ -44,6 +45,7 @@
4445
import java.time.Instant;
4546
import java.util.HashMap;
4647
import java.util.Map;
48+
import java.util.Optional;
4749
import java.util.Queue;
4850

4951
public class ClientConnection {
@@ -137,8 +139,9 @@ public void connect() {
137139
this.commandManager.init();
138140
GeneralHelper.print("Setting client version to " + ProtocolHandler.getCurrent().getName() + " (" + ProtocolHandler.getCurrent().getProtocolVer() + ")", ChatMessage.TextColor.AQUA);
139141
GeneralHelper.print("Sending Handshake and LoginStart packets...", ChatMessage.TextColor.GREEN);
142+
Optional<PublicKeyContainer> publicKeyContainer = keyContainer == null ? Optional.empty() : Optional.of(keyContainer.publicKey());
140143
sendPacket(new ServerBoundHandshakePacket(ProtocolHandler.getCurrent().getProtocolVer(), getMinecraftServerAddress().getIp(), getMinecraftServerAddress().getPort(), ServerBoundHandshakePacket.LOGIN_STATE));
141-
sendPacket(new ServerBoundLoginStartPacket(getSession().getUsername(), keyContainer == null ? null : keyContainer.publicKey()));
144+
sendPacket(new ServerBoundLoginStartPacket(getSession().getUsername(), publicKeyContainer));
142145
}
143146

144147
public boolean contactSessionServers(String serverHash) {

‎java/src/me/dustin/chatbot/network/ProtocolHandler.java

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import me.dustin.chatbot.ChatBot;
77
import me.dustin.chatbot.helper.GeneralHelper;
88

9+
import java.io.File;
910
import java.util.ArrayList;
1011
import java.util.Map;
1112

@@ -15,6 +16,13 @@ public class ProtocolHandler {
1516
private static final ArrayList<ProtocolVersion> versions = new ArrayList<>();
1617

1718
public static void downloadData() {
19+
/*File f = new File(System.getProperty("java.class.path"));
20+
File dir = f.getAbsoluteFile().getParentFile();
21+
String jarPath = dir.toString();
22+
if (jarPath.contains(":"))
23+
jarPath = new File("").getAbsolutePath();
24+
25+
String data = GeneralHelper.readFile(new File(jarPath, "versions" + File.separator + "protocol_versions.json"));*/
1826
String data = GeneralHelper.httpRequest("https://raw.githubusercontent.com/DustinRepo/ChatBot/master/versions/protocol_versions.json", null, null, "GET").data();
1927
JsonObject jsonObject = GeneralHelper.prettyGson.fromJson(data, JsonObject.class);
2028
for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {

‎java/src/me/dustin/chatbot/network/packet/handler/PlayClientBoundPacketHandler.java

+25-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import me.dustin.chatbot.event.EventReceiveChatMessage;
1111
import me.dustin.chatbot.event.EventRemovePlayer;
1212
import me.dustin.chatbot.helper.GeneralHelper;
13+
import me.dustin.chatbot.nbt.NbtCompound;
14+
import me.dustin.chatbot.nbt.NbtElement;
15+
import me.dustin.chatbot.nbt.NbtList;
1316
import me.dustin.chatbot.network.ProtocolHandler;
1417
import me.dustin.chatbot.network.packet.impl.play.c2s.*;
1518
import me.dustin.chatbot.network.packet.impl.play.s2c.*;
@@ -46,9 +49,29 @@ public void handleJoinGamePacket(ClientBoundJoinGamePacket clientBoundJoinGamePa
4649
ClientPlayer clientPlayer = getClientConnection().getClientPlayer();
4750
clientPlayer.setEntityId(clientBoundJoinGamePacket.getEntityId());
4851
clientPlayer.setGameMode(clientBoundJoinGamePacket.getGameMode());
49-
52+
//world
5053
World world = getClientConnection().getWorld();
5154
world.setDimension(clientBoundJoinGamePacket.getDimension());
55+
NbtCompound dimCompound = clientBoundJoinGamePacket.getDimensionNbt();
56+
if (dimCompound != null) {
57+
if (dimCompound.has("min_y"))
58+
world.setMinY(clientBoundJoinGamePacket.getDimension(), (int) dimCompound.get("min_y").getValue());
59+
if (dimCompound.has("height"))
60+
world.setWorldHeight(clientBoundJoinGamePacket.getDimension(), (int) dimCompound.get("height").getValue());
61+
} else {
62+
dimCompound = (NbtCompound)clientBoundJoinGamePacket.getDimensionCodec().get("minecraft:dimension_type");
63+
NbtList list = (NbtList) dimCompound.get("value");
64+
for (NbtElement element : list.getValue()) {
65+
NbtCompound compound = (NbtCompound)element;
66+
String name = (String)compound.get("name").getValue();
67+
World.Dimension dim = World.Dimension.get(name);
68+
NbtCompound data = (NbtCompound)compound.get("element");
69+
if (data.has("min_y"))
70+
world.setMinY(dim, (int) data.get("min_y").getValue());
71+
if (data.has("height"))
72+
world.setWorldHeight(dim, (int) data.get("height").getValue());
73+
}
74+
}
5275
if (clientBoundJoinGamePacket.getDifficulty() != null)
5376
world.setDifficulty(clientBoundJoinGamePacket.getDifficulty());
5477
//send settings
@@ -59,8 +82,7 @@ public void handleJoinGamePacket(ClientBoundJoinGamePacket clientBoundJoinGamePa
5982
channel = "MC|Brand";
6083
getClientConnection().sendPacket(new ServerBoundCustomDataPacket(channel, new PacketByteBuf(Unpooled.buffer()).writeString("vanilla")));
6184

62-
//fix for servers that pass you through proxy servers spamming the console
63-
if (!getClientConnection().isInGame()) {
85+
if (!getClientConnection().isInGame()) {//fix for servers that pass you through proxy servers spamming the console
6486
GeneralHelper.print("Received Join Game. Loading processes.", ChatMessage.TextColor.GOLD);
6587
}
6688
getClientConnection().getEventManager().run(clientBoundJoinGamePacket);

‎java/src/me/dustin/chatbot/network/packet/impl/login/c2s/ServerBoundEncryptionResponsePacket.java

+7-16
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,16 @@ public static SecretKey generateSecret() {
4444
public void createPacket(PacketByteBuf packetByteBuf) throws IOException {
4545
if (ProtocolHandler.getCurrent().getProtocolVer() > ProtocolHandler.getVersionFromName("1.18.2").getProtocolVer()) {
4646
packetByteBuf.writeByteArray(encryptedSecret);
47-
if (saltAndSig == null) {
48-
packetByteBuf.writeBoolean(true);
49-
packetByteBuf.writeByteArray(encryptedVerify);
50-
} else {
51-
packetByteBuf.writeBoolean(false);
52-
saltAndSig.write(packetByteBuf);
53-
}
47+
packetByteBuf.writeEitherOrWithBoolean(saltAndSig == null, packetByteBuf1 -> packetByteBuf1.writeByteArray(encryptedVerify), saltAndSig::write);
5448
return;
5549
}
56-
if (ProtocolHandler.getCurrent().getProtocolVer() <= ProtocolHandler.getVersionFromName("1.7.10").getProtocolVer())
57-
packetByteBuf.writeShort(encryptedSecret.length);
58-
else
59-
packetByteBuf.writeVarInt(encryptedSecret.length);
50+
assert encryptedVerify != null;
51+
boolean oneSeven = ProtocolHandler.getCurrent().getProtocolVer() <= ProtocolHandler.getVersionFromName("1.7.10").getProtocolVer();
52+
int sLength = encryptedSecret.length;
53+
int vLength = encryptedVerify.length;
54+
packetByteBuf.writeEitherOr(oneSeven, packetByteBuf1 -> packetByteBuf1.writeShort(sLength), packetByteBuf1 -> packetByteBuf1.writeVarInt(sLength));
6055
packetByteBuf.writeBytes(encryptedSecret);
61-
62-
if (ProtocolHandler.getCurrent().getProtocolVer() <= ProtocolHandler.getVersionFromName("1.7.10").getProtocolVer())
63-
packetByteBuf.writeShort(encryptedVerify.length);
64-
else
65-
packetByteBuf.writeVarInt(encryptedVerify.length);
56+
packetByteBuf.writeEitherOr(oneSeven, packetByteBuf1 -> packetByteBuf1.writeShort(vLength), packetByteBuf1 -> packetByteBuf1.writeVarInt(vLength));
6657
packetByteBuf.writeBytes(encryptedVerify);
6758
}
6859
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package me.dustin.chatbot.network.packet.impl.login.c2s;
22

3+
import me.dustin.chatbot.helper.KeyHelper;
34
import me.dustin.chatbot.nbt.NbtCompound;
45
import me.dustin.chatbot.nbt.NbtString;
56
import me.dustin.chatbot.network.ProtocolHandler;
@@ -8,27 +9,26 @@
89
import me.dustin.chatbot.network.packet.pipeline.PacketByteBuf;
910

1011
import java.io.IOException;
12+
import java.util.Base64;
13+
import java.util.Optional;
1114

1215
public class ServerBoundLoginStartPacket extends Packet {
1316
private final String name;
14-
private final NbtCompound publicKeyNbt;
15-
public ServerBoundLoginStartPacket(String name, PublicKeyContainer publicKeyContainer) {
17+
private final Optional<PublicKeyContainer> keyContainer;
18+
public ServerBoundLoginStartPacket(String name, Optional<PublicKeyContainer> publicKeyContainer) {
1619
super(0x00);
1720
this.name = name;
18-
if (publicKeyContainer != null) {
19-
publicKeyNbt = new NbtCompound();
20-
publicKeyNbt.put("expires_at", new NbtString(publicKeyContainer.expiresAt().toString()));
21-
publicKeyNbt.put("key", new NbtString(publicKeyContainer.keyString()));
22-
publicKeyNbt.put("signature", new NbtString(publicKeyContainer.signature()));
23-
} else {
24-
publicKeyNbt = null;
25-
}
21+
this.keyContainer = publicKeyContainer;
2622
}
2723

2824
@Override
2925
public void createPacket(PacketByteBuf packetByteBuf) throws IOException {
3026
packetByteBuf.writeString(name);
3127
if (ProtocolHandler.getCurrent().getProtocolVer() > ProtocolHandler.getVersionFromName("1.18.2").getProtocolVer())
32-
packetByteBuf.writeOptionalNBT(publicKeyNbt);
28+
packetByteBuf.writeOptional(keyContainer, (packetByteBuf1, publicKeyContainer) -> {
29+
packetByteBuf1.writeLong(publicKeyContainer.expiresAt().toEpochMilli());
30+
packetByteBuf1.writeByteArray(KeyHelper.getPublicKey(publicKeyContainer.keyString()).getEncoded());
31+
packetByteBuf1.writeByteArray(Base64.getDecoder().decode(publicKeyContainer.signature()));
32+
});
3333
}
3434
}

‎java/src/me/dustin/chatbot/network/packet/impl/play/c2s/ServerBoundChatPacket.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ public ServerBoundChatPacket(String message, Instant instant, SaltAndSig saltAnd
3232
public void createPacket(PacketByteBuf packetByteBuf) throws IOException {
3333
packetByteBuf.writeString(message);
3434
if (ProtocolHandler.getCurrent().getProtocolVer() > ProtocolHandler.getVersionFromName("1.18.2").getProtocolVer()) {
35-
packetByteBuf.writeLong(instant.getEpochSecond());
35+
packetByteBuf.writeLong(instant.toEpochMilli());
3636
saltAndSig.write(packetByteBuf);
37+
packetByteBuf.writeBoolean(false);//whether we want a preview or not
3738
}
3839
}
3940
}

‎java/src/me/dustin/chatbot/network/packet/impl/play/c2s/ServerBoundCommandPacket.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public ServerBoundCommandPacket(String command, Instant timestamp, SaltAndSig.Sa
2121
@Override
2222
public void createPacket(PacketByteBuf packetByteBuf) {
2323
packetByteBuf.writeString(command);
24-
packetByteBuf.writeLong(timestamp.getEpochSecond());
24+
packetByteBuf.writeLong(timestamp.toEpochMilli());
2525
saltAndSigs.write(packetByteBuf);
2626
}
2727
}

‎java/src/me/dustin/chatbot/network/packet/impl/play/c2s/ServerBoundInteractEntityPacket.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@ public ServerBoundInteractEntityPacket(LivingEntity livingEntity, int useType) {
1919

2020
@Override
2121
public void createPacket(PacketByteBuf packetByteBuf) throws IOException {
22-
if (ProtocolHandler.getCurrent().getProtocolVer() <= ProtocolHandler.getVersionFromName("1.7.10").getProtocolVer() && useType == INTERACT_AT)
22+
boolean oneSeven = ProtocolHandler.getCurrent().getProtocolVer() <= ProtocolHandler.getVersionFromName("1.7.10").getProtocolVer();
23+
if (oneSeven && useType == INTERACT_AT)
2324
return;
24-
if (ProtocolHandler.getCurrent().getProtocolVer() <= ProtocolHandler.getVersionFromName("1.7.10").getProtocolVer()) {
25-
packetByteBuf.writeInt(livingEntity.getEntityId());
26-
packetByteBuf.writeByte(useType);
27-
} else {
25+
packetByteBuf.writeEitherOr(oneSeven, packetByteBuf1 -> {
26+
packetByteBuf1.writeInt(livingEntity.getEntityId());
27+
packetByteBuf1.writeByte(useType);
28+
}, packetByteBuf1 -> {
2829
packetByteBuf.writeVarInt(livingEntity.getEntityId());
2930
packetByteBuf.writeVarInt(useType);
30-
}
31+
});
3132
if (useType == INTERACT_AT) {
3233
packetByteBuf.writeFloat((float)livingEntity.getX());
3334
packetByteBuf.writeFloat((float)livingEntity.getY());

‎java/src/me/dustin/chatbot/network/packet/impl/play/c2s/ServerBoundKeepAlivePacket.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
public class ServerBoundKeepAlivePacket extends Packet {
1010

11-
private long id;
11+
private final long id;
1212

1313
public ServerBoundKeepAlivePacket(long id) {
1414
super(ProtocolHandler.getCurrent().getPacketId(ProtocolHandler.NetworkSide.SERVERBOUND, "heartbeat"));
@@ -17,9 +17,10 @@ public ServerBoundKeepAlivePacket(long id) {
1717

1818
@Override
1919
public void createPacket(PacketByteBuf packetByteBuf) throws IOException {
20-
if (ProtocolHandler.getCurrent().getProtocolVer() <= ProtocolHandler.getVersionFromName("1.12.1").getProtocolVer())//in 1.12.1 and below keepalive id is an int, not a long
21-
packetByteBuf.writeVarInt((int) id);
22-
else
23-
packetByteBuf.writeLong(id);
20+
packetByteBuf.writeEitherOr(ProtocolHandler.getCurrent().getProtocolVer() <= ProtocolHandler.getVersionFromName("1.12.1").getProtocolVer(), packetByteBuf1 ->
21+
packetByteBuf1.writeVarInt((int) id)
22+
,packetByteBuf1 ->
23+
packetByteBuf1.writeLong(id)
24+
);
2425
}
2526
}

‎java/src/me/dustin/chatbot/network/packet/impl/play/s2c/ClientBoundChatMessagePacket.java

+19
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
public class ClientBoundChatMessagePacket extends Packet.ClientBoundPacket {
2828
public final static int MESSAGE_TYPE_CHAT = 0, MESSAGE_TYPE_SYSTEM = 1, MESSAGE_TYPE_GAME_INFO = 2;
2929
private final ChatMessage message;
30+
private final ChatMessage unsigned;
3031
private final byte type;
3132
private final ChatSender sender;
3233
private final Instant expiresAt;
@@ -39,6 +40,16 @@ public ClientBoundChatMessagePacket(PacketByteBuf packetByteBuf) {
3940
String name;
4041
this.message = ChatMessage.of(packetByteBuf.readString());
4142
name = this.message.getSenderName();
43+
44+
if (ProtocolHandler.getCurrent().getProtocolVer() > ProtocolHandler.getVersionFromName("1.18.2").getProtocolVer()) {
45+
boolean hasUnsigned = packetByteBuf.readBoolean();
46+
if (hasUnsigned)
47+
unsigned = ChatMessage.of(packetByteBuf.readString());
48+
else
49+
unsigned = null;
50+
} else
51+
unsigned = null;
52+
4253
if (ProtocolHandler.getCurrent().getProtocolVer() > ProtocolHandler.getVersionFromName("1.7.10").getProtocolVer())
4354
this.type = packetByteBuf.readByte();
4455
else
@@ -81,6 +92,10 @@ public ChatMessage getMessage() {
8192
return message;
8293
}
8394

95+
public ChatMessage getUnsigned() {
96+
return unsigned;
97+
}
98+
8499
public byte getType() {
85100
return type;
86101
}
@@ -89,6 +104,10 @@ public ChatSender getSender() {
89104
return sender;
90105
}
91106

107+
public ChatMessage getTeamName() {
108+
return teamName;
109+
}
110+
92111
public Instant getExpiresAt() {
93112
return expiresAt;
94113
}

‎java/src/me/dustin/chatbot/network/packet/impl/play/s2c/ClientBoundChunkDataPacket.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public ClientBoundChunkDataPacket(PacketByteBuf packetByteBuf) {
3030
PacketByteBuf chunkSectionsByteBuf = new PacketByteBuf(Unpooled.wrappedBuffer(sectionsData));
3131

3232
//find all blocks and add them in the chunk
33-
for (int i = 0; i < 19; i++) {//i < vertical chunk sections in the world, don't know how to get that yet
33+
int vSections = getClientConnection().getWorld().countVerticalSections();
34+
for (int i = 0; i < vSections; i++) {
3435
int nonEmptyBlockCount = chunkSectionsByteBuf.readShort();
3536
if (nonEmptyBlockCount < 0)
3637
continue;

‎java/src/me/dustin/chatbot/network/packet/impl/play/s2c/ClientBoundJoinGamePacket.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ public ClientBoundJoinGamePacket(PacketByteBuf packetByteBuf) {
7676
this.dimNames[i] = packetByteBuf.readString();
7777
}
7878
this.dimensionCodec = (NbtCompound) packetByteBuf.readNbt();
79-
this.dimensionNBT = (NbtCompound) packetByteBuf.readNbt();
79+
if (ProtocolHandler.getCurrent().getProtocolVer() > ProtocolHandler.getVersionFromName("1.18.2").getProtocolVer()) {
80+
this.dimensionNBT = null;
81+
String dim = packetByteBuf.readString();
82+
} else
83+
this.dimensionNBT = (NbtCompound) packetByteBuf.readNbt();
8084
this.dimension = World.Dimension.get(packetByteBuf.readString());
8185
this.hashedSeed = packetByteBuf.readLong();
8286
this.maxPlayers = packetByteBuf.readVarInt();

‎java/src/me/dustin/chatbot/network/packet/pipeline/PacketByteBuf.java

+22-11
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,38 @@ public void forEachInCollection(Consumer<PacketByteBuf> consumer) {
8383

8484
}
8585

86-
public <T> void writeOptional(Optional<T> value, BiConsumer<PacketByteBuf, T> serializer) {
87-
if (value.isPresent()) {
86+
public void writeEitherOrWithBoolean(boolean first, Consumer<PacketByteBuf> firstConsumer, Consumer<PacketByteBuf> secondConsumer) {
87+
this.writeBoolean(first);
88+
writeEitherOr(first, firstConsumer, secondConsumer);
89+
}
90+
91+
public void writeEitherOr(boolean first, Consumer<PacketByteBuf> firstConsumer, Consumer<PacketByteBuf> secondConsumer) {
92+
if (first) {
93+
firstConsumer.accept(this);
94+
} else {
95+
secondConsumer.accept(this);
96+
}
97+
}
98+
99+
public <T> void writeOptional(Optional<T> optional, BiConsumer<PacketByteBuf, T> consumer) {
100+
if (optional.isPresent()) {
88101
this.writeBoolean(true);
89-
serializer.accept(this, value.get());
102+
consumer.accept(this, optional.get());
90103
} else {
91104
this.writeBoolean(false);
92105
}
93106
}
94107

95-
public PacketByteBuf writeOptionalNBT(@Nullable NbtCompound compound) {
96-
if (compound == null) {
97-
this.writeBoolean(false);
98-
} else {
108+
public <T> void writeOptional(Optional<T> optional, Consumer<PacketByteBuf> consumer) {
109+
if (optional.isPresent()) {
99110
this.writeBoolean(true);
100-
this.writeNbt(compound);
111+
consumer.accept(this);
112+
} else {
113+
this.writeBoolean(false);
101114
}
102-
return this;
103115
}
104116

105-
public PacketByteBuf writeNbt(@Nullable NbtCompound compound) {
117+
public void writeNbt(@Nullable NbtCompound compound) {
106118
if (compound == null) {
107119
this.writeByte(0);
108120
} else {
@@ -113,7 +125,6 @@ public PacketByteBuf writeNbt(@Nullable NbtCompound compound) {
113125
throw new EncoderException(iOException);
114126
}
115127
}
116-
return this;
117128
}
118129

119130
public byte[] readByteArray() {

‎java/src/me/dustin/chatbot/world/World.java

+61-16
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package me.dustin.chatbot.world;
22

3+
import me.dustin.chatbot.block.BlockPos;
34
import me.dustin.chatbot.block.BlockState;
45
import me.dustin.chatbot.entity.Entity;
56
import me.dustin.chatbot.entity.player.PlayerEntity;
67
import me.dustin.chatbot.network.ClientConnection;
78
import me.dustin.chatbot.world.chunk.Chunk;
89

910
import java.util.ArrayList;
11+
import java.util.HashMap;
12+
import java.util.Map;
1013

1114
public class World {
1215
private final ClientConnection clientConnection;
@@ -33,23 +36,11 @@ public ClientConnection getClientConnection() {
3336
return clientConnection;
3437
}
3538

36-
public BlockState getBlockState(int x, int y, int z) {
37-
int sectionCoord = y >> 4;
38-
int bottomSectionCoord = -64 >> 4;//TODO: find a way to determine bottom of world without hard-coding it
39-
int sectionIndex = sectionCoord - bottomSectionCoord;
40-
int chunkX = (int)(x / 16);
41-
int chunkZ = (int)(z / 16);
39+
public BlockState getBlockState(BlockPos blockPos) {
40+
int chunkX = getSectionCoord(blockPos.getX());
41+
int chunkZ = getSectionCoord(blockPos.getZ());
4242
Chunk chunk = getChunk(chunkX, chunkZ);
43-
if (chunk == null)
44-
return null;
45-
if (sectionIndex < 0 || sectionIndex > chunk.getChunkSections().size()) {
46-
System.out.println("Invalid section index! " + sectionIndex);
47-
}
48-
try {
49-
return chunk.getChunkSections().get(sectionIndex).getBlockState(x & 0xF, y & 0xF, z & 0xF);
50-
} catch (Exception e) {
51-
return null;
52-
}
43+
return chunk.getBlockState(blockPos);
5344
}
5445

5546
public Entity getEntity(int id) {
@@ -88,6 +79,42 @@ public ArrayList<PlayerEntity> getPlayerEntities() {
8879
return playerEntities;
8980
}
9081

82+
public int getMinY() {
83+
if (getDimension() == null)
84+
return 0;
85+
return getDimension().getMinY();
86+
}
87+
88+
public void setMinY(Dimension dimension, int minY) {
89+
if (dimension == null)
90+
return;
91+
dimension.setMinY(minY);
92+
}
93+
94+
public int getWorldHeight() {
95+
if (getDimension() == null)
96+
return 256;
97+
return getDimension().getWorldHeight();
98+
}
99+
100+
public void setWorldHeight(Dimension dimension, int worldHeight) {
101+
if (dimension == null)
102+
return;
103+
dimension.setWorldHeight(worldHeight);
104+
}
105+
106+
public int countVerticalSections() {
107+
Dimension dimension = getDimension();
108+
int topY = dimension.getWorldHeight() - dimension.getMinY();
109+
int topSectionY = getSectionCoord(topY - 1) + 1;
110+
int bottomSectionY = getSectionCoord(dimension.getMinY());
111+
return topSectionY - bottomSectionY;
112+
}
113+
114+
public int getSectionCoord(int coord) {
115+
return coord >> 4;
116+
}
117+
91118
public enum Difficulty {
92119
PEACEFUL(0, "Peaceful"),
93120
EASY(1, "Easy"),
@@ -119,6 +146,9 @@ public enum Dimension {
119146
private String name;
120147
private final int id;
121148

149+
private int min_y = 0;
150+
private int world_height = 256;
151+
122152
Dimension(String name, int id) {
123153
this.name = name;
124154
this.id = id;
@@ -149,5 +179,20 @@ public static Dimension get(int id) {
149179
}
150180
return CUSTOM;
151181
}
182+
public int getMinY() {
183+
return min_y;
184+
}
185+
186+
public void setMinY(int minY) {
187+
this.min_y = minY;
188+
}
189+
190+
public int getWorldHeight() {
191+
return world_height;
192+
}
193+
194+
public void setWorldHeight(int worldHeight) {
195+
this.world_height = worldHeight;
196+
}
152197
}
153198
}

‎java/src/me/dustin/chatbot/world/chunk/Chunk.java

+18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package me.dustin.chatbot.world.chunk;
22

3+
import me.dustin.chatbot.ChatBot;
34
import me.dustin.chatbot.block.BlockEntity;
5+
import me.dustin.chatbot.block.BlockPos;
6+
import me.dustin.chatbot.block.BlockState;
47

58
import java.util.ArrayList;
69

@@ -37,4 +40,19 @@ public int getChunkX() {
3740
public int getChunkZ() {
3841
return chunkZ;
3942
}
43+
44+
public BlockState getBlockState(BlockPos blockPos) {
45+
int sectionIndex = getSectionIndex(blockPos.getY());
46+
ChunkSection chunkSection;
47+
if (sectionIndex >= 0 && sectionIndex < chunkSections.size() && !(chunkSection = this.getChunkSections().get(sectionIndex)).isEmpty()) {
48+
return chunkSection.getBlockState(blockPos.getX() & 0xF, blockPos.getY() & 0xF, blockPos.getZ() & 0xF);
49+
}
50+
return null;
51+
}
52+
53+
private int getSectionIndex(int y) {
54+
int bottomSectionY = ChatBot.getClientConnection().getWorld().getSectionCoord(ChatBot.getClientConnection().getWorld().getMinY());
55+
int coord = ChatBot.getClientConnection().getWorld().getSectionCoord(y);
56+
return coord - bottomSectionY;
57+
}
4058
}

‎java/src/me/dustin/chatbot/world/chunk/ChunkSection.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
import me.dustin.chatbot.block.BlockState;
44

55
public class ChunkSection {
6-
private final int blockCount;
6+
private final int nonEmptyBlockCount;
77
private final Palette palette;
88

9-
public ChunkSection(int blockCount, Palette palette) {
10-
this.blockCount = blockCount;
9+
public ChunkSection(int nonEmptyBlockCount, Palette palette) {
10+
this.nonEmptyBlockCount = nonEmptyBlockCount;
1111
this.palette = palette;
1212
}
1313

14-
public int getBlockCount() {
15-
return blockCount;
14+
public int getNonEmptyBlockCount() {
15+
return nonEmptyBlockCount;
1616
}
1717

1818
public Palette getPalette() {
@@ -22,4 +22,8 @@ public Palette getPalette() {
2222
public BlockState getBlockState(int x, int y, int z) {
2323
return (BlockState)getPalette().get(getPalette().computeIndex(x, y, z));
2424
}
25+
26+
public boolean isEmpty() {
27+
return nonEmptyBlockCount == 0;
28+
}
2529
}

‎java/src/me/dustin/chatbot/world/chunk/Palette.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ public void fromPacket(PacketByteBuf packetByteBuf) {
5454

5555
class ArrayPalette implements Palette {
5656
private int size;
57-
private BlockState[] blockStates;
57+
private final BlockState[] blockStates;
5858
private final byte bitsPerBlock;
5959

6060
public ArrayPalette(byte bitsPerBlock) {
61+
blockStates = new BlockState[1 << bitsPerBlock];
6162
this.bitsPerBlock = bitsPerBlock;
6263
}
6364

@@ -77,7 +78,6 @@ public Object get(int index) {
7778
@Override
7879
public void fromPacket(PacketByteBuf packetByteBuf) {
7980
size = packetByteBuf.readVarInt();
80-
blockStates = new BlockState[size];
8181
for (int i = 0; i < size; i++)
8282
blockStates[i] = BlockState.get(packetByteBuf.readVarInt());
8383
}

‎versions/protocol_versions.json

+10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
{
2+
"840": {
3+
"name": "22w19a",
4+
"protocol_id": 1073741908,
5+
"packets": {
6+
"c2s": ["confirm_teleport", "block_nbt", "difficulty", "command_message", "chat_message", "chat_preview", "client_action", "settings", "chat_suggestions", "container_button", "container_click", "close_container", "plugin", "book", "entity_nbt", "entity_interact", "generate_structure", "heartbeat", "lock_difficulty", "position", "position_rotation", "rotation", "ground_change", "move_vehicle", "steer_boat", "item_pick", "crafting_recipe", "toggle_fly", "player_action", "entity_action", "steer_vehicle", "pong", "displayed_recipe", "recipe_book", "anvil_item_name", "resourcepack", "advancement_tab", "trade", "beacon_effect", "hotbar_slot", "command_block", "minecart_command_block", "item_stack_create", "jigsaw_block", "structure_block", "sign_text", "swing_arm", "entity_spectate", "block_interact", "use_item"],
7+
"s2c": [
8+
"entity_object_spawn", "entity_experience_orb", "entity_player", "entity_animation", "statistics", "block_break", "block_break_animation", "block_data", "block_action", "block", "bossbar", "difficulty", "chat_preview", "clear_title", "chat_suggestions", "commands", "close_container", "container_items", "container_properties", "container_item", "item_cooldown", "plugin", "named_sound", "kick", "entity_status", "explosion", "unload_chunk", "game_event", "open_horse_container", "initialize_world_border", "heartbeat", "chunk", "world_event", "particle", "chunk_light", "initialize", "map", "villager_trades", "relative_move", "movement_rotation", "rotation", "move_vehicle", "book", "open_container", "sign_editor", "ping", "crafting_recipe", "player_abilities", "chat_message", "end_combat_event", "enter_combat_event", "kill_combat_event", "tab_list", "player_face", "position_rotation", "unlock_recipes", "entity_destroy", "entity_remove_effect", "resourcepack", "respawn", "head_rotation", "blocks", "advancement_tab", "server_data", "hotbar_text", "center_world_border", "interpolate_world_border", "size_world_border", "warn_time_world_border", "warn_blocks_world_border", "camera", "hotbar_slot", "chunk_center", "view_distance", "compass_position", "objective_position", "entity_data", "entity_attach", "velocity", "entity_equipment", "experience", "health", "objective", "entity_passenger", "teams", "scoreboard_score", "simulation_distance", "subtitle", "time", "title_text", "title_times", "entity_sound", "sound_event", "stop_sound", "game_message", "tab_list_text", "nbt_response", "entity_collect", "teleport", "advancements", "entity_attributes", "entity_effect", "recipes", "tags"
9+
]
10+
}
11+
},
212
"839": {
313
"name": "22w18a",
414
"protocol_id": 1073741907,

0 commit comments

Comments
 (0)
Please sign in to comment.