Skip to content

Commit

Permalink
Converted a bunch of other code to use the new network crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Bash-09 committed Nov 30, 2022
1 parent 7e1a0ce commit 7ad1155
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 150 deletions.
6 changes: 3 additions & 3 deletions src/chat.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use mcnetwork::{packets::ChatIncoming, types::UUID};
use mcproto_rs::{types, v1_16_3::PlayServerChatMessageSpec};
use serde_json::Value;

pub struct Chat {
history: Vec<ChatMessage>,
history: Vec<types::Chat>,

input: String,
pub send: bool,
Expand All @@ -21,7 +21,7 @@ impl Chat {
&self.history
}

pub fn add_message(&mut self, chat: &ChatIncoming) {
pub fn add_message(&mut self, chat: &PlayServerChatMessageSpec) {
let value: Value =
serde_json::from_str(&chat.json).expect("Failed to unwrap JSON from chat message");

Expand Down
13 changes: 6 additions & 7 deletions src/entities.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use crate::{
mcnetwork::types::UUID,
renderer::Vertex,
resources::{self, ENTITIES},
};

pub mod components;
use components::*;
use glam::Vec3;
use serde_json::Value;
use mcproto_rs::uuid::UUID4;

pub struct Entity {
pub id: i32,
pub uuid: UUID,
pub uuid: UUID4,

pub entity_type: &'static resources::Entity,

Expand All @@ -30,7 +29,7 @@ impl Entity {
pub fn new(entity_type: u32) -> Entity {
Entity {
id: 0,
uuid: UUID([0, 0]),
uuid: UUID4::random(),

entity_type: ENTITIES
.get(&entity_type)
Expand All @@ -49,7 +48,7 @@ impl Entity {

pub fn new_with_values(
id: i32,
uuid: UUID,
uuid: UUID4,
entity_type: u32,
data: i32,
px: f32,
Expand Down Expand Up @@ -82,8 +81,8 @@ impl Entity {
self.id
}

pub fn get_uuid(&self) -> UUID {
self.uuid.clone()
pub fn get_uuid(&self) -> UUID4 {
self.uuid
}

pub fn get_type(&self) -> &'static resources::Entity {
Expand Down
2 changes: 1 addition & 1 deletion src/gui/main_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use log::{debug, error};
use serde::{Deserialize, Serialize};

use crate::{
network::{NetworkCommand, NetworkManager, PROTOCOL_1_17_1},
network::{NetworkCommand, NetworkManager},
server::Server,
state::State,
Client,
Expand Down
24 changes: 14 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ extern crate lazy_static;
extern crate log;
extern crate quartz_nbt;

extern crate mcnetwork;

use std::{sync::mpsc::TryRecvError, time::Instant};

use crate::network::*;
Expand All @@ -28,7 +26,7 @@ use glium_app::{
context::Context,
utils::persistent_window::{PersistentWindow, PersistentWindowManager},
};
use mcnetwork::packets::{encode, PlayerPositionAndRotation};
use mcproto_rs::{v1_16_3::PlayClientPlayerPositionAndRotationSpec, types::{EntityLocation, self}};
use state::State;

pub mod chat;
Expand Down Expand Up @@ -121,13 +119,19 @@ impl Application for Client {
Some(serv) => {
// Send player position update packets
if serv.get_player().id != 0 {
serv.send_packet(encode(PlayerPositionAndRotation {
x: (serv.get_player().get_position().x as f64),
feet_y: (serv.get_player().get_position().y as f64),
z: (serv.get_player().get_position().z as f64),
yaw: (serv.get_player().get_orientation().get_yaw() as f32),
pitch: (serv.get_player().get_orientation().get_head_pitch() as f32),
on_ground: (true),
serv.send_packet(encode(PlayClientPlayerPositionAndRotationSpec {
feet_location: EntityLocation{
position: types::Vec3{
x: serv.get_player().get_position().x as f64,
y: serv.get_player().get_position().y as f64,
z: serv.get_player().get_position().z as f64
},
rotation: types::EntityRotation {
yaw: serv.get_player().get_orientation().get_yaw() as f32,
pitch: serv.get_player().get_orientation().get_head_pitch() as f32,
},
},
on_ground: true,
}));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ fn write_varint<W: Write>(w: &mut W, val: i32) -> io::Result<()> {
Ok(())
}

fn encode<S: Serialize>(packet: S) -> Vec<u8> {
pub fn encode<S: Serialize>(packet: S) -> Vec<u8> {
let mut serializer = BytesSerializer {
data: Vec::new(),
};
Expand Down
13 changes: 7 additions & 6 deletions src/player.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use glam::Vec3;
use mcproto_rs::v1_16_3::{ClientChatMode, ClientMainHand, ClientDisplayedSkinParts};

use super::entities::components::Orientation;

Expand All @@ -15,9 +16,9 @@ pub struct Player {
// Client Settings
pub locale: String,
pub view_distance: i8,
pub chat_mode: i32, // 0 - Enabled, 1 - Commands only, 2 - Hidden
pub displayed_skin_parts: u8, // Bitmask - https://wiki.vg/Protocol#Client_Settings
pub main_hand: i32, // 0 - Left, 1 - Right
pub chat_mode: ClientChatMode, // 0 - Enabled, 1 - Commands only, 2 - Hidden
pub displayed_skin_parts: ClientDisplayedSkinParts, // Bitmask - https://wiki.vg/Protocol#Client_Settings
pub main_hand: ClientMainHand, // 0 - Left, 1 - Right
pub disable_text_filtering: bool, // idek what this does
}

Expand All @@ -35,9 +36,9 @@ impl Player {

locale: String::from("en_GB"),
view_distance: 8,
chat_mode: 0,
displayed_skin_parts: 0xFF,
main_hand: 0,
chat_mode: ClientChatMode::Enabled,
displayed_skin_parts: ClientDisplayedSkinParts::default(),
main_hand: ClientMainHand::Left,
disable_text_filtering: true,
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use std::collections::HashMap;
use glam::{Mat4, Vec3};
use glium::index::{
NoIndices,
PrimitiveType::{LineStrip, TrianglesList},
PrimitiveType::TrianglesList,
};
use glium::*;
use glium::{Display, Surface};
use log::info;


use crate::{
entities::{self, Entity},
Expand Down
4 changes: 2 additions & 2 deletions src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use inflector::Inflector;
use lazy_static::lazy_static;
use serde_json::{self, map::Values, to_string, Value};
use serde_json::{self, Value};

pub struct Entity {
pub name: String,
Expand Down Expand Up @@ -61,7 +61,7 @@ lazy_static! {
id,
BlockState {
name: name.clone(),
id: id,
id,
model: {
match state.get("model") {
Some(model) => Some(model.as_str().unwrap().to_string()),
Expand Down
Loading

0 comments on commit 7ad1155

Please sign in to comment.