Skip to content

Commit 76f7bf6

Browse files
committed
Track gamemode changes in Respawn and JoinId properly
- `packet_log` clause was breaking this earlier - Still needs to be tested to see if this fixes respawning on 2b
1 parent 315caa8 commit 76f7bf6

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

src/networking/connection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from src.networking.encryption import *
66
from src.networking.packet_handler.serverbound import LoginHandler as ServerboundLoginHandler
77
from src.networking.packet_handler.clientbound import LoginHandler as ClientboundLoginHandler
8-
from src.networking.packets.clientbound import Respawn
8+
from src.networking.packets.clientbound import Respawn, JoinGame
99

1010
from src.networking.packet_handler import WorkerProcessor, ClientboundProcessor
1111

@@ -145,7 +145,7 @@ def __init__(self, username, ip, protocol, port=25565, server_port=1001, profile
145145
self.listen_thread = listen_thread
146146

147147
# JoinGame, ServerDifficulty, SpawnPosition, Respawn, Experience
148-
join_ids = [0x23, 0x0D, 0x46, Respawn.id, 0x40]
148+
join_ids = [JoinGame.id, 0x0D, 0x46, Respawn.id, 0x40]
149149
self.game_state = GameState(join_ids)
150150

151151
self.packet_processor = ClientboundProcessor(self.game_state)

src/networking/packet_handler/packet_processor.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from src.networking.packets.serverbound import KeepAlive as KeepAliveServerbound, TeleportConfirm, ClientStatus
22
from src.networking.packets.clientbound import ChunkData, UnloadChunk, SpawnEntity, \
33
DestroyEntities, KeepAlive, ChatMessage, PlayerPositionAndLook, TimeUpdate, \
4-
HeldItemChange, SetSlot, PlayerListItem, PlayerAbilities, Respawn, UpdateHealth
4+
HeldItemChange, SetSlot, PlayerListItem, PlayerAbilities, Respawn, UpdateHealth, JoinGame
55

66
from src.networking.packets.clientbound import GameState as GameStateP
77

@@ -67,6 +67,16 @@ def chunk_load(self, packet):
6767

6868
def process_packet(self, packet):
6969
with self.game_state.state_lock:
70+
if packet.id == Respawn.id:
71+
# In case the gamemode is changed through a respawn packet
72+
respawn = Respawn().read(packet.packet_buffer)
73+
self.game_state.gamemode = respawn.Gamemode
74+
print("Set gamemode to", respawn.Gamemode, flush=True)
75+
if packet.id == JoinGame.id:
76+
join_game = JoinGame().read(packet.packet_buffer)
77+
self.game_state.gamemode = join_game.Gamemode & 3 # Bit 4 (0x8) is the hardcore flaga
78+
print("Set gamemode to", self.game_state.gamemode, "JoinGame", flush=True)
79+
7080
if packet.id in self.game_state.join_ids:
7181
self.game_state.packet_log[packet.id] = packet
7282
elif packet.id == ChunkData.id: # ChunkData
@@ -111,11 +121,6 @@ def process_packet(self, packet):
111121
self.player_list(packet)
112122
elif packet.id == PlayerAbilities.id:
113123
self.game_state.abilities = PlayerAbilities().read(packet.packet_buffer)
114-
elif packet.id == Respawn.id:
115-
# In case the gamemode is changed through a respawn packet
116-
respawn = Respawn().read(packet.packet_buffer)
117-
self.game_state.gamemode = respawn.Gamemode
118-
print("Set gamemode to", respawn.Gamemode, flush=True)
119124
elif packet.id == UpdateHealth.id:
120125
update_health = UpdateHealth().read(packet.packet_buffer)
121126
self.game_state.update_health = update_health
@@ -125,4 +130,5 @@ def process_packet(self, packet):
125130
print("Client died, respawning", flush=True)
126131
return ClientStatus(ActionID=0)
127132

133+
128134
return None

src/networking/packets/clientbound/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ class LoginSuccess(Packet):
4949
}
5050

5151

52+
class JoinGame(Packet):
53+
id = 0x23
54+
definition = {
55+
"EntityID": Integer,
56+
"Gamemode": UnsignedByte,
57+
"Dimension": Integer,
58+
"Difficulty": UnsignedByte,
59+
"MaxPlayers": UnsignedByte,
60+
"LevelType": String,
61+
"Debug": Boolean,
62+
}
63+
64+
5265
class SetCompression(Packet):
5366
id = 0x03
5467
definition = {

0 commit comments

Comments
 (0)