improvements?
This commit is contained in:
parent
48f493695e
commit
b5b2afaf8e
@ -23,7 +23,10 @@ pub struct ClientContext {
|
|||||||
state: RwLock<ConnectionState>,
|
state: RwLock<ConnectionState>,
|
||||||
packet_buffer: Mutex<VecDeque<Packet>>,
|
packet_buffer: Mutex<VecDeque<Packet>>,
|
||||||
read_loop: AtomicBool,
|
read_loop: AtomicBool,
|
||||||
is_alive: AtomicBool
|
is_alive: AtomicBool,
|
||||||
|
position: RwLock<(f64, f64, f64)>,
|
||||||
|
velocity: RwLock<(f64, f64, f64)>,
|
||||||
|
rotation: RwLock<(f32, f32)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Реализуем сравнение через адрес
|
// Реализуем сравнение через адрес
|
||||||
@ -54,7 +57,10 @@ impl ClientContext {
|
|||||||
state: RwLock::new(ConnectionState::Handshake),
|
state: RwLock::new(ConnectionState::Handshake),
|
||||||
packet_buffer: Mutex::new(VecDeque::new()),
|
packet_buffer: Mutex::new(VecDeque::new()),
|
||||||
read_loop: AtomicBool::new(false),
|
read_loop: AtomicBool::new(false),
|
||||||
is_alive: AtomicBool::new(true)
|
is_alive: AtomicBool::new(true),
|
||||||
|
position: RwLock::new((0.0, 0.0, 0.0)),
|
||||||
|
velocity: RwLock::new((0.0, 0.0, 0.0)),
|
||||||
|
rotation: RwLock::new((0.0, 0.0))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,6 +106,30 @@ impl ClientContext {
|
|||||||
self.state.read().unwrap().clone()
|
self.state.read().unwrap().clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_position(self: &Arc<Self>, position: (f64, f64, f64)) {
|
||||||
|
*self.position.write().unwrap() = position;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_velocity(self: &Arc<Self>, velocity: (f64, f64, f64)) {
|
||||||
|
*self.velocity.write().unwrap() = velocity;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_rotation(self: &Arc<Self>, rotation: (f32, f32)) {
|
||||||
|
*self.rotation.write().unwrap() = rotation;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn position(self: &Arc<Self>) -> (f64, f64, f64) {
|
||||||
|
self.position.read().unwrap().clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn velocity(self: &Arc<Self>) -> (f64, f64, f64) {
|
||||||
|
self.velocity.read().unwrap().clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rotation(self: &Arc<Self>) -> (f32, f32) {
|
||||||
|
self.rotation.read().unwrap().clone()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn write_packet(self: &Arc<Self>, packet: &Packet) -> Result<(), ServerError> {
|
pub fn write_packet(self: &Arc<Self>, packet: &Packet) -> Result<(), ServerError> {
|
||||||
let state = self.state();
|
let state = self.state();
|
||||||
let mut packet = packet.clone();
|
let mut packet = packet.clone();
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use std::{io::Cursor, sync::Arc, thread, time::{Duration, SystemTime, UNIX_EPOCH}};
|
use std::{io::Cursor, sync::Arc, thread, time::{Duration, SystemTime, UNIX_EPOCH}};
|
||||||
|
|
||||||
use rust_mc_proto::{DataWriter, Packet, read_packet};
|
use rust_mc_proto::{read_packet, DataReader, DataWriter, Packet};
|
||||||
|
|
||||||
use crate::server::{data::{text_component::TextComponent, ReadWriteNBT}, player::context::ClientContext, ServerError};
|
use crate::server::{data::{text_component::TextComponent, ReadWriteNBT}, player::context::ClientContext, ServerError};
|
||||||
|
|
||||||
@ -241,6 +241,48 @@ pub fn handle_play_state(
|
|||||||
set_center_chunk(client.clone(), 0, 0)?;
|
set_center_chunk(client.clone(), 0, 0)?;
|
||||||
send_example_chunk(client.clone(), 0, 0)?;
|
send_example_chunk(client.clone(), 0, 0)?;
|
||||||
|
|
||||||
|
thread::spawn({
|
||||||
|
let client = client.clone();
|
||||||
|
|
||||||
|
move || -> Result<(), ServerError> {
|
||||||
|
while client.is_alive() {
|
||||||
|
let mut packet = client.read_any_packet()?;
|
||||||
|
|
||||||
|
match packet.id() {
|
||||||
|
serverbound::play::SET_PLAYER_POSITION => {
|
||||||
|
let x = packet.read_double()?;
|
||||||
|
let y = packet.read_double()?;
|
||||||
|
let z = packet.read_double()?;
|
||||||
|
let _ = packet.read_byte()?; // flags
|
||||||
|
|
||||||
|
client.set_position((x, y, z));
|
||||||
|
},
|
||||||
|
serverbound::play::SET_PLAYER_POSITION_AND_ROTATION => {
|
||||||
|
let x = packet.read_double()?;
|
||||||
|
let y = packet.read_double()?;
|
||||||
|
let z = packet.read_double()?;
|
||||||
|
let yaw = packet.read_float()?;
|
||||||
|
let pitch = packet.read_float()?;
|
||||||
|
let _ = packet.read_byte()?; // flags
|
||||||
|
|
||||||
|
client.set_position((x, y, z));
|
||||||
|
client.set_rotation((yaw, pitch));
|
||||||
|
},
|
||||||
|
serverbound::play::SET_PLAYER_ROTATION => {
|
||||||
|
let yaw = packet.read_float()?;
|
||||||
|
let pitch = packet.read_float()?;
|
||||||
|
let _ = packet.read_byte()?; // flags
|
||||||
|
|
||||||
|
client.set_rotation((yaw, pitch));
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
let mut ticks_alive = 0u64;
|
let mut ticks_alive = 0u64;
|
||||||
|
|
||||||
while client.is_alive() {
|
while client.is_alive() {
|
||||||
@ -249,7 +291,12 @@ pub fn handle_play_state(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ticks_alive % 20 == 0 { // 1 sec timer
|
if ticks_alive % 20 == 0 { // 1 sec timer
|
||||||
// do something
|
let (x, y, z) = client.position();
|
||||||
|
|
||||||
|
send_system_message(client.clone(),
|
||||||
|
TextComponent::rainbow(format!(
|
||||||
|
"Pos: {} {} {}", x as u64, y as u64, z as u64
|
||||||
|
)), false)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
send_system_message(client.clone(), TextComponent::rainbow(format!("Ticks alive: {}", ticks_alive)), true)?;
|
send_system_message(client.clone(), TextComponent::rainbow(format!("Ticks alive: {}", ticks_alive)), true)?;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user