@@ -6,33 +6,22 @@ use std::{
6
6
} ;
7
7
8
8
use crate :: {
9
- entity:: player:: { ChatMode , GameMode , Hand , Player } ,
9
+ entity:: player:: { ChatMode , Hand } ,
10
10
server:: Server ,
11
11
} ;
12
12
13
13
use authentication:: GameProfile ;
14
14
use mio:: { event:: Event , net:: TcpStream , Token } ;
15
- use num_traits:: ToPrimitive ;
16
15
use pumpkin_core:: text:: TextComponent ;
17
16
use pumpkin_protocol:: {
18
17
bytebuf:: packet_id:: Packet ,
19
- client:: {
20
- config:: CConfigDisconnect ,
21
- login:: CLoginDisconnect ,
22
- play:: { CGameEvent , CPlayDisconnect , CSyncPlayerPosition , CSystemChatMessage } ,
23
- } ,
18
+ client:: { config:: CConfigDisconnect , login:: CLoginDisconnect , play:: CPlayDisconnect } ,
24
19
packet_decoder:: PacketDecoder ,
25
20
packet_encoder:: PacketEncoder ,
26
21
server:: {
27
22
config:: { SAcknowledgeFinishConfig , SClientInformationConfig , SKnownPacks , SPluginMessage } ,
28
23
handshake:: SHandShake ,
29
24
login:: { SEncryptionResponse , SLoginAcknowledged , SLoginPluginResponse , SLoginStart } ,
30
- play:: {
31
- SChatCommand , SChatMessage , SClientInformationPlay , SConfirmTeleport , SInteract ,
32
- SPlayPingRequest , SPlayerAction , SPlayerCommand , SPlayerPosition ,
33
- SPlayerPositionRotation , SPlayerRotation , SSetCreativeSlot , SSetHeldItem , SSwingArm ,
34
- SUseItemOn ,
35
- } ,
36
25
status:: { SStatusPingRequest , SStatusRequest } ,
37
26
} ,
38
27
ClientPacket , ConnectionState , PacketError , RawPacket , ServerPacket ,
@@ -46,6 +35,7 @@ mod client_packet;
46
35
mod container;
47
36
pub mod player_packet;
48
37
38
+ #[ derive( Clone ) ]
49
39
pub struct PlayerConfig {
50
40
pub locale : String , // 16
51
41
pub view_distance : i8 ,
@@ -57,9 +47,22 @@ pub struct PlayerConfig {
57
47
pub server_listing : bool ,
58
48
}
59
49
60
- pub struct Client {
61
- pub player : Option < Player > ,
50
+ impl Default for PlayerConfig {
51
+ fn default ( ) -> Self {
52
+ Self {
53
+ locale : "en_us" . to_string ( ) ,
54
+ view_distance : 2 ,
55
+ chat_mode : ChatMode :: Enabled ,
56
+ chat_colors : true ,
57
+ skin_parts : 0 ,
58
+ main_hand : Hand :: Main ,
59
+ text_filtering : false ,
60
+ server_listing : false ,
61
+ }
62
+ }
63
+ }
62
64
65
+ pub struct Client {
63
66
pub gameprofile : Option < GameProfile > ,
64
67
65
68
pub config : Option < PlayerConfig > ,
@@ -75,6 +78,8 @@ pub struct Client {
75
78
enc : PacketEncoder ,
76
79
dec : PacketDecoder ,
77
80
pub client_packets_queue : VecDeque < RawPacket > ,
81
+
82
+ pub make_player : bool ,
78
83
}
79
84
80
85
impl Client {
@@ -86,14 +91,14 @@ impl Client {
86
91
brand : None ,
87
92
token,
88
93
address,
89
- player : None ,
90
94
connection_state : ConnectionState :: HandShake ,
91
95
connection,
92
96
enc : PacketEncoder :: default ( ) ,
93
97
dec : PacketDecoder :: default ( ) ,
94
98
encryption : true ,
95
99
closed : false ,
96
100
client_packets_queue : VecDeque :: new ( ) ,
101
+ make_player : false ,
97
102
}
98
103
}
99
104
@@ -122,10 +127,6 @@ impl Client {
122
127
self . enc . set_compression ( compression) ;
123
128
}
124
129
125
- pub fn is_player ( & self ) -> bool {
126
- self . player . is_some ( )
127
- }
128
-
129
130
/// Send a Clientbound Packet to the Client
130
131
pub fn send_packet < P : ClientPacket > ( & mut self , packet : & P ) {
131
132
self . enc
@@ -145,37 +146,6 @@ impl Client {
145
146
Ok ( ( ) )
146
147
}
147
148
148
- pub fn teleport ( & mut self , x : f64 , y : f64 , z : f64 , yaw : f32 , pitch : f32 ) {
149
- assert ! ( self . is_player( ) ) ;
150
- // TODO
151
- let id = 0 ;
152
- let player = self . player . as_mut ( ) . unwrap ( ) ;
153
- let entity = & mut player. entity ;
154
- entity. x = x;
155
- entity. y = y;
156
- entity. z = z;
157
- entity. lastx = x;
158
- entity. lasty = y;
159
- entity. lastz = z;
160
- entity. yaw = yaw;
161
- entity. pitch = pitch;
162
- player. awaiting_teleport = Some ( id. into ( ) ) ;
163
- self . send_packet ( & CSyncPlayerPosition :: new ( x, y, z, yaw, pitch, 0 , id. into ( ) ) ) ;
164
- }
165
-
166
- pub fn update_health ( & mut self , health : f32 , food : i32 , food_saturation : f32 ) {
167
- let player = self . player . as_mut ( ) . unwrap ( ) ;
168
- player. health = health;
169
- player. food = food;
170
- player. food_saturation = food_saturation;
171
- }
172
-
173
- pub fn set_gamemode ( & mut self , gamemode : GameMode ) {
174
- let player = self . player . as_mut ( ) . unwrap ( ) ;
175
- player. gamemode = gamemode;
176
- self . send_packet ( & CGameEvent :: new ( 3 , gamemode. to_f32 ( ) . unwrap ( ) ) ) ;
177
- }
178
-
179
149
pub async fn process_packets ( & mut self , server : & mut Server ) {
180
150
let mut i = 0 ;
181
151
while i < self . client_packets_queue . len ( ) {
@@ -185,7 +155,7 @@ impl Client {
185
155
}
186
156
}
187
157
188
- /// Handles an incoming decoded Packet
158
+ /// Handles an incoming decoded not Play state Packet
189
159
pub async fn handle_packet ( & mut self , server : & mut Server , packet : & mut RawPacket ) {
190
160
// TODO: handle each packet's Error instead of calling .unwrap()
191
161
let bytebuf = & mut packet. bytebuf ;
@@ -254,71 +224,13 @@ impl Client {
254
224
packet. id. 0
255
225
) ,
256
226
} ,
257
- pumpkin_protocol:: ConnectionState :: Play => {
258
- if self . player . is_some ( ) {
259
- self . handle_play_packet ( server, packet) ;
260
- } else {
261
- // should be impossible
262
- self . kick ( "no player in play state?" )
263
- }
264
- }
265
227
_ => log:: error!( "Invalid Connection state {:?}" , self . connection_state) ,
266
228
}
267
229
}
268
230
269
- pub fn handle_play_packet ( & mut self , server : & mut Server , packet : & mut RawPacket ) {
270
- let bytebuf = & mut packet. bytebuf ;
271
- match packet. id . 0 {
272
- SConfirmTeleport :: PACKET_ID => {
273
- self . handle_confirm_teleport ( server, SConfirmTeleport :: read ( bytebuf) . unwrap ( ) )
274
- }
275
- SChatCommand :: PACKET_ID => {
276
- self . handle_chat_command ( server, SChatCommand :: read ( bytebuf) . unwrap ( ) )
277
- }
278
- SPlayerPosition :: PACKET_ID => {
279
- self . handle_position ( server, SPlayerPosition :: read ( bytebuf) . unwrap ( ) )
280
- }
281
- SPlayerPositionRotation :: PACKET_ID => self
282
- . handle_position_rotation ( server, SPlayerPositionRotation :: read ( bytebuf) . unwrap ( ) ) ,
283
- SPlayerRotation :: PACKET_ID => {
284
- self . handle_rotation ( server, SPlayerRotation :: read ( bytebuf) . unwrap ( ) )
285
- }
286
- SPlayerCommand :: PACKET_ID => {
287
- self . handle_player_command ( server, SPlayerCommand :: read ( bytebuf) . unwrap ( ) )
288
- }
289
- SSwingArm :: PACKET_ID => {
290
- self . handle_swing_arm ( server, SSwingArm :: read ( bytebuf) . unwrap ( ) )
291
- }
292
- SChatMessage :: PACKET_ID => {
293
- self . handle_chat_message ( server, SChatMessage :: read ( bytebuf) . unwrap ( ) )
294
- }
295
- SClientInformationPlay :: PACKET_ID => self . handle_client_information_play (
296
- server,
297
- SClientInformationPlay :: read ( bytebuf) . unwrap ( ) ,
298
- ) ,
299
- SInteract :: PACKET_ID => self . handle_interact ( server, SInteract :: read ( bytebuf) . unwrap ( ) ) ,
300
- SPlayerAction :: PACKET_ID => {
301
- self . handle_player_action ( server, SPlayerAction :: read ( bytebuf) . unwrap ( ) )
302
- }
303
- SUseItemOn :: PACKET_ID => {
304
- self . handle_use_item_on ( server, SUseItemOn :: read ( bytebuf) . unwrap ( ) )
305
- }
306
- SSetHeldItem :: PACKET_ID => {
307
- self . handle_set_held_item ( server, SSetHeldItem :: read ( bytebuf) . unwrap ( ) )
308
- }
309
- SSetCreativeSlot :: PACKET_ID => {
310
- self . handle_set_creative_slot ( server, SSetCreativeSlot :: read ( bytebuf) . unwrap ( ) )
311
- }
312
- SPlayPingRequest :: PACKET_ID => {
313
- self . handle_play_ping_request ( server, SPlayPingRequest :: read ( bytebuf) . unwrap ( ) )
314
- }
315
- _ => log:: error!( "Failed to handle player packet id {:#04x}" , packet. id. 0 ) ,
316
- }
317
- }
318
-
319
231
// Reads the connection until our buffer of len 4096 is full, then decode
320
232
/// Close connection when an error occurs
321
- pub async fn poll ( & mut self , server : & mut Server , event : & Event ) {
233
+ pub async fn poll ( & mut self , event : & Event ) {
322
234
if event. is_readable ( ) {
323
235
let mut received_data = vec ! [ 0 ; 4096 ] ;
324
236
let mut bytes_read = 0 ;
@@ -351,7 +263,6 @@ impl Client {
351
263
Ok ( packet) => {
352
264
if let Some ( packet) = packet {
353
265
self . add_packet ( packet) ;
354
- self . process_packets ( server) . await ;
355
266
}
356
267
}
357
268
Err ( err) => self . kick ( & err. to_string ( ) ) ,
@@ -361,10 +272,6 @@ impl Client {
361
272
}
362
273
}
363
274
364
- pub fn send_system_message ( & mut self , text : TextComponent ) {
365
- self . send_packet ( & CSystemChatMessage :: new ( text, false ) ) ;
366
- }
367
-
368
275
/// Kicks the Client with a reason depending on the connection state
369
276
pub fn kick ( & mut self , reason : & str ) {
370
277
dbg ! ( reason) ;
@@ -379,6 +286,7 @@ impl Client {
379
286
self . try_send_packet ( & CConfigDisconnect :: new ( reason) )
380
287
. unwrap_or_else ( |_| self . close ( ) ) ;
381
288
}
289
+ // So we can also kick on errors, but generally should use Player::kick
382
290
ConnectionState :: Play => {
383
291
self . try_send_packet ( & CPlayDisconnect :: new ( TextComponent :: text ( reason) ) )
384
292
. unwrap_or_else ( |_| self . close ( ) ) ;
0 commit comments