Skip to content

Commit ebd2f98

Browse files
committed
add chunk batch acknowledgements to receive chunk updates after teleport
add mojmap packet name to packet debug remove snapshot code
1 parent a283791 commit ebd2f98

File tree

10 files changed

+125
-29
lines changed

10 files changed

+125
-29
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package systems.kinau.fishingbot.event.play;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import systems.kinau.fishingbot.event.Event;
6+
7+
@Getter
8+
@AllArgsConstructor
9+
public class ChunkBatchFinishedEvent extends Event {
10+
private final int chunkCount;
11+
}

src/main/java/systems/kinau/fishingbot/modules/ClientDefaultsModule.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import systems.kinau.fishingbot.network.protocol.common.PacketOutPing;
3030
import systems.kinau.fishingbot.network.protocol.common.PacketOutResourcePackResponse;
3131
import systems.kinau.fishingbot.network.protocol.play.PacketOutChatSessionUpdate;
32+
import systems.kinau.fishingbot.network.protocol.play.PacketOutChunkBatchReceived;
3233
import systems.kinau.fishingbot.network.protocol.play.PacketOutConfirmTransaction;
3334
import systems.kinau.fishingbot.network.protocol.play.PacketOutPosLook;
3435

@@ -188,4 +189,9 @@ public void onRegistryData(RegistryDataEvent event) {
188189
if (metaRegistry == null) return;
189190
metaRegistry.load(event.getRegistryData(), RegistryLoader.mapped(), FishingBot.getInstance().getCurrentBot().getServerProtocol());
190191
}
192+
193+
@EventHandler
194+
public void onChunkBatchFinished(ChunkBatchFinishedEvent event) {
195+
FishingBot.getInstance().getCurrentBot().getNet().sendPacket(new PacketOutChunkBatchReceived(20));
196+
}
191197
}

src/main/java/systems/kinau/fishingbot/network/protocol/NetworkHandler.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -189,31 +189,25 @@ private void readCompressed(int plen, int dlen) throws IOException {
189189
}
190190
}
191191

192-
private void readPacket(int len, int packetId, ByteArrayDataInputWrapper buf) throws IOException {
193-
Class<? extends Packet> clazz = null;
194-
192+
private PacketRegistry getCurrentPacketRegistry() {
195193
switch (state) {
196-
case HANDSHAKE:
197-
clazz = getHandshakeRegistry().getPacket(packetId);
198-
break;
199-
case LOGIN:
200-
clazz = getLoginRegistryIn().getPacket(packetId);
201-
break;
202-
case PLAY:
203-
clazz = getPlayRegistryIn().getPacket(packetId);
204-
break;
205-
case CONFIGURATION:
206-
clazz = getConfigurationRegistryIn().getPacket(packetId);
207-
break;
208-
default:
209-
break;
194+
case HANDSHAKE: return getHandshakeRegistry();
195+
case LOGIN: return getLoginRegistryIn();
196+
case PLAY: return getPlayRegistryIn();
197+
case CONFIGURATION: return getConfigurationRegistryIn();
210198
}
199+
return null;
200+
}
201+
202+
private void readPacket(int len, int packetId, ByteArrayDataInputWrapper buf) throws IOException {
203+
PacketRegistry packetRegistry = getCurrentPacketRegistry();
204+
Class<? extends Packet> clazz = packetRegistry == null ? null : packetRegistry.getPacket(packetId);
211205

212206
if (clazz == null) {
213207
if (FishingBot.getInstance().getCurrentBot().getConfig().isLogPackets()) {
214208
byte[] bytes = new byte[buf.getAvailable()];
215209
buf.readFully(bytes);
216-
FishingBot.getLog().info("[" + getState().name().toUpperCase() + "] |C| <<< S : 0x" + Integer.toHexString(packetId));
210+
FishingBot.getLog().info("[" + getState().name().toUpperCase() + "] |C| <<< S : 0x" + Integer.toHexString(packetId) + " (" + (packetRegistry != null ? packetRegistry.getMojMapPacketName(packetId) : "") + ")");
217211
}
218212
return;
219213
} else if (FishingBot.getInstance().getCurrentBot().getConfig().isLogPackets())

src/main/java/systems/kinau/fishingbot/network/protocol/PacketRegistry.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import systems.kinau.fishingbot.network.utils.InvalidPacketException;
2020

2121
import java.io.InputStreamReader;
22+
import java.util.HashMap;
23+
import java.util.Map;
2224

2325
@Getter
2426
public class PacketRegistry {
@@ -29,6 +31,7 @@ public class PacketRegistry {
2931
private final JsonParser parser = new JsonParser();
3032

3133
private final BiMap<Integer, Class<? extends Packet>> registeredPackets = HashBiMap.create();
34+
private final Map<Integer, String> idToMojMapName = new HashMap<>();
3235

3336
public PacketRegistry(int protocolId, ProtocolState state, ProtocolFlow flow) {
3437
this.protocolId = protocolId;
@@ -45,16 +48,18 @@ public PacketRegistry(int protocolId, ProtocolState state, ProtocolFlow flow) {
4548
JsonObject flowObj = stateObj.getAsJsonObject(flow.getId());
4649
if (flowObj == null) throw new IllegalArgumentException("Could not load bundled packets for " + state.getId() + "/" + flow.getId() + "/" + ProtocolConstants.getVersionString(protocolId));
4750
for (String packetId : flowObj.keySet()) {
51+
JsonObject packetData = flowObj.getAsJsonObject(packetId);
52+
int packetProtocolId = packetData.getAsJsonPrimitive("protocol_id").getAsInt();
53+
54+
idToMojMapName.put(packetProtocolId, packetId);
55+
4856
Class<? extends Packet> packetClazz = mapMojangPacketId(packetId);
4957
if (packetClazz == null) {
5058
if (FishingBot.getInstance().getCurrentBot().getConfig().isLogPackets())
5159
FishingBot.getLog().warning("Could not map packet id " + packetId + " in " + state.name() + " (" + flow.name() + ")");
5260
continue;
5361
}
5462

55-
JsonObject packetData = flowObj.getAsJsonObject(packetId);
56-
int packetProtocolId = packetData.getAsJsonPrimitive("protocol_id").getAsInt();
57-
5863
registerPacket(packetProtocolId, packetClazz);
5964
}
6065
}
@@ -147,6 +152,7 @@ private Class<? extends Packet> mapMojangPacketId(String mojangPacketId) {
147152
case "minecraft:ping": return PacketInPing.class;
148153
case "minecraft:set_player_inventory": return PacketInPlayerInventory.class;
149154
case "minecraft:entity_position_sync": return PacketInEntityPositionSync.class;
155+
case "minecraft:chunk_batch_finished": return PacketInChunkBatchFinished.class;
150156
}
151157
} else if (flow == ProtocolFlow.OUTGOING_PACKET) {
152158
switch (mojangPacketId) {
@@ -172,6 +178,7 @@ private Class<? extends Packet> mapMojangPacketId(String mojangPacketId) {
172178
case "minecraft:swing": return PacketOutArmAnimation.class;
173179
case "minecraft:confirm_transaction": return PacketOutConfirmTransaction.class;
174180
case "minecraft:pong": return PacketOutPing.class;
181+
case "minecraft:chunk_batch_received": return PacketOutChunkBatchReceived.class;
175182
}
176183
}
177184
}
@@ -181,9 +188,6 @@ private Class<? extends Packet> mapMojangPacketId(String mojangPacketId) {
181188
public String getRegistryFileName(int protocolId) {
182189
if (protocolId == ProtocolConstants.AUTOMATIC)
183190
protocolId = ProtocolConstants.getLatest();
184-
//TODO: REMOVE SNAPSHOT DATA
185-
if (protocolId == ProtocolConstants.MC_1_21_2)
186-
return "mc_data/1_21_2/packets.json";
187191
String version = ProtocolConstants.getVersionString(protocolId);
188192
if (version.contains("/"))
189193
version = version.split("/")[0];
@@ -209,6 +213,10 @@ private void registerPacket(int id, Class<? extends Packet> clazz) {
209213
registeredPackets.put(id, clazz);
210214
}
211215

216+
public String getMojMapPacketName(int id) {
217+
return idToMojMapName.get(id);
218+
}
219+
212220
public Class<? extends Packet> getPacket(int id) {
213221
return registeredPackets.get(id);
214222
}

src/main/java/systems/kinau/fishingbot/network/protocol/common/PacketOutClientSettings.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public void write(ByteArrayDataOutput out, int protocolId) {
1818
switch (protocolId) {
1919
case ProtocolConstants.MC_1_8: {
2020
writeString("en_7s", out); //use speach "Pirate Speak", arrr
21-
out.writeByte(1); //render-distance
21+
out.writeByte(2); //render-distance
2222
out.writeByte(0); //chat enabled
2323
out.writeBoolean(true); //support colors
2424
out.writeByte(128); //skin bitmask
@@ -51,7 +51,7 @@ public void write(ByteArrayDataOutput out, int protocolId) {
5151
case ProtocolConstants.MC_1_16_3:
5252
case ProtocolConstants.MC_1_16_4:
5353
writeString("lol_aa", out); //use speach "LOLCAT", lol
54-
out.writeByte(1); //render-distance
54+
out.writeByte(2); //render-distance
5555
writeVarInt(0, out); //chat enabled
5656
out.writeBoolean(true); //support colors
5757
out.writeByte(128); //skin bitmask
@@ -60,7 +60,7 @@ public void write(ByteArrayDataOutput out, int protocolId) {
6060
case ProtocolConstants.MC_1_17:
6161
case ProtocolConstants.MC_1_17_1: {
6262
writeString("lol_aa", out); //use speach "LOLCAT", lol
63-
out.writeByte(1); //render-distance
63+
out.writeByte(2); //render-distance
6464
writeVarInt(0, out); //chat enabled
6565
out.writeBoolean(true); //support colors
6666
out.writeByte(128); //skin bitmask
@@ -80,7 +80,7 @@ public void write(ByteArrayDataOutput out, int protocolId) {
8080
case ProtocolConstants.MC_1_20_5:
8181
case ProtocolConstants.MC_1_21: {
8282
writeString("lol_aa", out); //use speach "LOLCAT", lol
83-
out.writeByte(1); //render-distance
83+
out.writeByte(2); //render-distance
8484
writeVarInt(0, out); //chat enabled
8585
out.writeBoolean(true); //support colors
8686
out.writeByte(128); //skin bitmask
@@ -92,7 +92,7 @@ public void write(ByteArrayDataOutput out, int protocolId) {
9292
case ProtocolConstants.MC_1_21_2:
9393
default: {
9494
writeString("lol_aa", out); //use speach "LOLCAT", lol
95-
out.writeByte(1); //render-distance
95+
out.writeByte(2); //render-distance
9696
writeVarInt(0, out); //chat enabled
9797
out.writeBoolean(true); //support colors
9898
out.writeByte(128); //skin bitmask
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package systems.kinau.fishingbot.network.protocol.play;
2+
3+
import com.google.common.io.ByteArrayDataOutput;
4+
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
6+
import systems.kinau.fishingbot.FishingBot;
7+
import systems.kinau.fishingbot.event.play.ChunkBatchFinishedEvent;
8+
import systems.kinau.fishingbot.network.protocol.NetworkHandler;
9+
import systems.kinau.fishingbot.network.protocol.Packet;
10+
import systems.kinau.fishingbot.network.utils.ByteArrayDataInputWrapper;
11+
12+
import java.io.IOException;
13+
14+
@Getter
15+
@NoArgsConstructor
16+
public class PacketInChunkBatchFinished extends Packet {
17+
18+
int chunkCount;
19+
20+
@Override
21+
public void write(ByteArrayDataOutput out, int protocolId) throws IOException {
22+
// Only incoming packet
23+
}
24+
25+
@Override
26+
public void read(ByteArrayDataInputWrapper in, NetworkHandler networkHandler, int length, int protocolId) throws IOException {
27+
this.chunkCount = readVarInt(in);
28+
FishingBot.getInstance().getCurrentBot().getEventManager().callEvent(new ChunkBatchFinishedEvent(chunkCount));
29+
}
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package systems.kinau.fishingbot.network.protocol.play;
2+
3+
import com.google.common.io.ByteArrayDataOutput;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
import systems.kinau.fishingbot.network.protocol.NetworkHandler;
8+
import systems.kinau.fishingbot.network.protocol.Packet;
9+
import systems.kinau.fishingbot.network.utils.ByteArrayDataInputWrapper;
10+
11+
import java.io.IOException;
12+
13+
@AllArgsConstructor
14+
@NoArgsConstructor
15+
@Getter
16+
public class PacketOutChunkBatchReceived extends Packet {
17+
18+
private float chunksPerTick;
19+
20+
@Override
21+
public void write(ByteArrayDataOutput out, int protocolId) throws IOException {
22+
out.writeFloat(chunksPerTick);
23+
}
24+
25+
@Override
26+
public void read(ByteArrayDataInputWrapper in, NetworkHandler networkHandler, int length, int protocolId) throws IOException {
27+
// Only outgoing packet
28+
}
29+
}

src/main/resources/mc_data/1_20_2/packets.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@
151151
},
152152
"minecraft:ping": {
153153
"protocol_id": 51
154+
},
155+
"minecraft:chunk_batch_finished": {
156+
"protocol_id": 12
154157
}
155158
},
156159
"serverbound": {
@@ -166,6 +169,9 @@
166169
"minecraft:chat_session_update": {
167170
"protocol_id": 6
168171
},
172+
"minecraft:chunk_batch_received": {
173+
"protocol_id": 7
174+
},
169175
"minecraft:client_command": {
170176
"protocol_id": 8
171177
},

src/main/resources/mc_data/1_20_3/packets.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@
151151
},
152152
"minecraft:ping": {
153153
"protocol_id": 51
154+
},
155+
"minecraft:chunk_batch_finished": {
156+
"protocol_id": 12
154157
}
155158
},
156159
"serverbound": {
@@ -166,6 +169,9 @@
166169
"minecraft:chat_session_update": {
167170
"protocol_id": 6
168171
},
172+
"minecraft:chunk_batch_received": {
173+
"protocol_id": 7
174+
},
169175
"minecraft:client_command": {
170176
"protocol_id": 8
171177
},

src/main/resources/mc_data/1_20_5/packets.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@
157157
},
158158
"minecraft:ping": {
159159
"protocol_id": 53
160+
},
161+
"minecraft:chunk_batch_finished": {
162+
"protocol_id": 12
160163
}
161164
},
162165
"serverbound": {
@@ -175,6 +178,9 @@
175178
"minecraft:chat_session_update": {
176179
"protocol_id": 7
177180
},
181+
"minecraft:chunk_batch_received": {
182+
"protocol_id": 8
183+
},
178184
"minecraft:client_command": {
179185
"protocol_id": 9
180186
},

0 commit comments

Comments
 (0)