1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 14:26:04 +00:00
This commit is contained in:
mat 2021-12-10 22:01:16 +00:00
parent 01a059c20c
commit 5039f9668f
12 changed files with 35 additions and 38 deletions

View file

@ -1,4 +1,4 @@
use std::borrow::BorrowMut;
use serde_json; use serde_json;
@ -42,7 +42,7 @@ impl Component {
// otherwise add the component to the array // otherwise add the component to the array
let c = Component::new(&with[i])?; let c = Component::new(&with[i])?;
if let Component::TextComponent(text_component) = c { if let Component::TextComponent(text_component) = c {
if text_component.base.siblings.len() == 0 if text_component.base.siblings.is_empty()
&& text_component.base.style.is_empty() && text_component.base.style.is_empty()
{ {
with_array.push(StringOrComponent::String(text_component.text)); with_array.push(StringOrComponent::String(text_component.text));
@ -91,11 +91,11 @@ impl Component {
return Err(format!("Don't know how to turn {} into a Component", json)); return Err(format!("Don't know how to turn {} into a Component", json));
} }
// object = GsonHelper.getAsString(jsonObject, "nbt"); // object = GsonHelper.getAsString(jsonObject, "nbt");
let nbt = json.get("nbt").unwrap().to_string(); let _nbt = json.get("nbt").unwrap().to_string();
// Optional<Component> optional = this.parseSeparator(type, jsonDeserializationContext, jsonObject); // Optional<Component> optional = this.parseSeparator(type, jsonDeserializationContext, jsonObject);
let separator = Component::parse_separator(json)?; let _separator = Component::parse_separator(json)?;
let interpret = match json.get("interpret") { let _interpret = match json.get("interpret") {
Some(v) => v.as_bool().ok_or(Some(false)).unwrap(), Some(v) => v.as_bool().ok_or(Some(false)).unwrap(),
None => false, None => false,
}; };
@ -127,7 +127,7 @@ impl Component {
Some(r) => r, Some(r) => r,
None => return Err("Extra isn't an array".to_string()), None => return Err("Extra isn't an array".to_string()),
}; };
if extra.len() == 0 { if extra.is_empty() {
return Err("Unexpected empty array of components".to_string()); return Err("Unexpected empty array of components".to_string());
} }
for extra_component in extra { for extra_component in extra {
@ -183,10 +183,10 @@ impl Component {
} }
/// Recursively call the function for every component in this component /// Recursively call the function for every component in this component
pub fn visit<F>(&self, f: &mut F) -> () pub fn visit<F>(&self, f: &mut F)
where where
// The closure takes an `i32` and returns an `i32`. // The closure takes an `i32` and returns an `i32`.
F: FnMut(&Component) -> (), F: FnMut(&Component),
{ {
f(self); f(self);
self.get_base() self.get_base()
@ -211,9 +211,9 @@ impl Component {
let ansi_text = running_style.compare_ansi(component_style); let ansi_text = running_style.compare_ansi(component_style);
built_string.push_str(&ansi_text); built_string.push_str(&ansi_text);
built_string.push_str(&component_text); built_string.push_str(component_text);
running_style.apply(&component_style); running_style.apply(component_style);
}); });
if !running_style.is_empty() { if !running_style.is_empty() {

View file

@ -10,7 +10,7 @@ pub struct TextColor {
impl TextColor { impl TextColor {
pub fn parse(value: String) -> Result<TextColor, String> { pub fn parse(value: String) -> Result<TextColor, String> {
if value.starts_with("#") { if value.starts_with('#') {
let n = value.chars().skip(1).collect::<String>(); let n = value.chars().skip(1).collect::<String>();
let n = u32::from_str_radix(&n, 16).unwrap(); let n = u32::from_str_radix(&n, 16).unwrap();
return Ok(TextColor::from_rgb(n)); return Ok(TextColor::from_rgb(n));
@ -152,7 +152,7 @@ impl<'a> ChatFormatting<'a> {
color: Option<u32>, color: Option<u32>,
) -> ChatFormatting { ) -> ChatFormatting {
ChatFormatting { ChatFormatting {
name: name, name,
code, code,
is_format, is_format,
id, id,
@ -275,11 +275,7 @@ impl Style {
} else if self.strikethrough.unwrap_or(false) && !after.strikethrough.unwrap_or(true) { } else if self.strikethrough.unwrap_or(false) && !after.strikethrough.unwrap_or(true) {
true true
// if it used to be obfuscated and now it's not, reset // if it used to be obfuscated and now it's not, reset
} else if self.obfuscated.unwrap_or(false) && !after.obfuscated.unwrap_or(true) { } else { self.obfuscated.unwrap_or(false) && !after.obfuscated.unwrap_or(true) }
true
} else {
false
}
}; };
let mut ansi_codes = String::new(); let mut ansi_codes = String::new();
@ -290,7 +286,7 @@ impl Style {
// we should apply after into before and use that as after // we should apply after into before and use that as after
ansi_codes.push_str(Ansi::RESET); ansi_codes.push_str(Ansi::RESET);
let mut updated_after = self.clone(); let mut updated_after = self.clone();
updated_after.apply(&after); updated_after.apply(after);
(Style::new(), updated_after) (Style::new(), updated_after)
} else { } else {
(self.clone(), after.clone()) (self.clone(), after.clone())

View file

@ -2,7 +2,7 @@ use minecraft_chat::{
component::Component, component::Component,
style::{Ansi, ChatFormatting, TextColor}, style::{Ansi, ChatFormatting, TextColor},
}; };
use serde_json::{Result, Value}; use serde_json::{Value};
#[test] #[test]
fn basic_ansi_test() { fn basic_ansi_test() {

View file

@ -1,4 +1,4 @@
use minecraft_protocol;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View file

@ -54,10 +54,10 @@ impl Connection {
// the first thing minecraft sends us is the length as a varint, which can be up to 5 bytes long // the first thing minecraft sends us is the length as a varint, which can be up to 5 bytes long
let mut buf = BufReader::with_capacity(4 * 1024 * 1024, &mut self.stream); let mut buf = BufReader::with_capacity(4 * 1024 * 1024, &mut self.stream);
println!("reading length varint"); println!("reading length varint");
let (packet_size, packet_size_varint_size) = mc_buf::read_varint(&mut buf).await?; let (_packet_size, _packet_size_varint_size) = mc_buf::read_varint(&mut buf).await?;
// then, minecraft tells us the packet id as a varint // then, minecraft tells us the packet id as a varint
println!("reading id varint"); println!("reading id varint");
let (packet_id, packet_id_size) = mc_buf::read_varint(&mut buf).await?; let (packet_id, _packet_id_size) = mc_buf::read_varint(&mut buf).await?;
// if we recognize the packet id, parse it // if we recognize the packet id, parse it

View file

@ -1,8 +1,8 @@
//! Utilities for reading and writing for the Minecraft protocol //! Utilities for reading and writing for the Minecraft protocol
use std::io::{Cursor, Write}; use std::io::Write;
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{BigEndian, WriteBytesExt};
use tokio::io::{AsyncRead, AsyncReadExt, BufReader}; use tokio::io::{AsyncRead, AsyncReadExt, BufReader};
// const DEFAULT_NBT_QUOTA: u32 = 2097152; // const DEFAULT_NBT_QUOTA: u32 = 2097152;
@ -37,8 +37,8 @@ pub async fn read_varint<T: AsyncRead + std::marker::Unpin>(
for i in 0..4 { for i in 0..4 {
buf.read_exact(&mut buffer) buf.read_exact(&mut buffer)
.await .await
.or_else(|_| Err("Invalid VarInt".to_string()))?; .map_err(|_| "Invalid VarInt".to_string())?;
ans |= ((buffer[0] & 0b0111_1111) as i32) << 7 * i; ans |= ((buffer[0] & 0b0111_1111) as i32) << (7 * i);
if buffer[0] & 0b1000_0000 == 0 { if buffer[0] & 0b1000_0000 == 0 {
return Ok((ans, i + 1)); return Ok((ans, i + 1));
} }
@ -64,6 +64,8 @@ pub fn write_varint(buf: &mut Vec<u8>, mut value: i32) {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use std::io::Cursor;
#[test] #[test]
fn test_write_varint() { fn test_write_varint() {
let mut buf = Vec::new(); let mut buf = Vec::new();
@ -98,7 +100,7 @@ pub async fn read_utf_with_len<T: AsyncRead + std::marker::Unpin>(
buf: &mut BufReader<T>, buf: &mut BufReader<T>,
max_length: u32, max_length: u32,
) -> Result<String, String> { ) -> Result<String, String> {
let (length, length_varint_length) = read_varint(buf).await?; let (length, _length_varint_length) = read_varint(buf).await?;
// i don't know why it's multiplied by 4 but it's like that in mojang's code so // i don't know why it's multiplied by 4 but it's like that in mojang's code so
if length < 0 { if length < 0 {
return Err( return Err(
@ -119,7 +121,7 @@ pub async fn read_utf_with_len<T: AsyncRead + std::marker::Unpin>(
let mut buffer = vec![0; length as usize]; let mut buffer = vec![0; length as usize];
buf.read_exact(&mut buffer) buf.read_exact(&mut buffer)
.await .await
.or_else(|_| Err("Invalid UTF-8".to_string()))?; .map_err(|_| "Invalid UTF-8".to_string())?;
string.push_str(std::str::from_utf8(&buffer).unwrap()); string.push_str(std::str::from_utf8(&buffer).unwrap());
if string.len() > length as usize { if string.len() > length as usize {

View file

@ -31,7 +31,7 @@ impl PacketTrait for ClientIntentionPacket {
} }
async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>(
buf: &mut BufReader<T>, _buf: &mut BufReader<T>,
) -> Result<Packet, String> { ) -> Result<Packet, String> {
Err("ClientIntentionPacket::parse not implemented".to_string()) Err("ClientIntentionPacket::parse not implemented".to_string())
// Ok(ClientIntentionPacket {}.get()) // Ok(ClientIntentionPacket {}.get())

View file

@ -46,9 +46,9 @@ impl Packet {
pub fn id(&self) -> u32 { pub fn id(&self) -> u32 {
match self { match self {
Packet::ClientIntentionPacket(packet) => 0x00, Packet::ClientIntentionPacket(_packet) => 0x00,
Packet::ServerboundStatusRequestPacket(packet) => 0x00, Packet::ServerboundStatusRequestPacket(_packet) => 0x00,
Packet::ClientboundStatusResponsePacket(packet) => 0x00, Packet::ClientboundStatusResponsePacket(_packet) => 0x00,
} }
} }
@ -98,7 +98,7 @@ impl Packet {
pub trait PacketTrait { pub trait PacketTrait {
/// Return a version of the packet that you can actually use for stuff /// Return a version of the packet that you can actually use for stuff
fn get(self) -> Packet; fn get(self) -> Packet;
fn write(&self, buf: &mut Vec<u8>) -> (); fn write(&self, buf: &mut Vec<u8>);
async fn read<T: AsyncRead + std::marker::Unpin + std::marker::Send>( async fn read<T: AsyncRead + std::marker::Unpin + std::marker::Send>(
buf: &mut BufReader<T>, buf: &mut BufReader<T>,
) -> Result<Packet, String> ) -> Result<Packet, String>

View file

@ -52,7 +52,7 @@ impl PacketTrait for ClientboundStatusResponsePacket {
// this.status = GsonHelper.fromJson(GSON, friendlyByteBuf.readUtf(32767), ServerStatus.class); // this.status = GsonHelper.fromJson(GSON, friendlyByteBuf.readUtf(32767), ServerStatus.class);
Ok(ClientboundStatusResponsePacket { Ok(ClientboundStatusResponsePacket {
// version: status_json.get("version"), // version: status_json.get("version"),
description: Component::new(&description_string)?, description: Component::new(description_string)?,
} }
.get()) .get())
} }

View file

@ -3,7 +3,6 @@ use std::hash::Hash;
use tokio::io::BufReader; use tokio::io::BufReader;
use crate::{ use crate::{
mc_buf,
packets::{Packet, PacketTrait}, packets::{Packet, PacketTrait},
}; };
@ -18,7 +17,7 @@ impl PacketTrait for ServerboundStatusRequestPacket {
fn write(&self, _buf: &mut Vec<u8>) {} fn write(&self, _buf: &mut Vec<u8>) {}
async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>(
buf: &mut BufReader<T>, _buf: &mut BufReader<T>,
) -> Result<Packet, String> { ) -> Result<Packet, String> {
Err("ServerboundStatusRequestPacket::read not implemented".to_string()) Err("ServerboundStatusRequestPacket::read not implemented".to_string())
} }

View file

@ -14,7 +14,7 @@ pub async fn resolve_address(address: &ServerAddress) -> Result<ServerIpAddress,
// If the address.host is already in the format of an ip address, return it. // If the address.host is already in the format of an ip address, return it.
if let Ok(ip) = address.host.parse::<IpAddr>() { if let Ok(ip) = address.host.parse::<IpAddr>() {
return Ok(ServerIpAddress { return Ok(ServerIpAddress {
ip: ip, ip,
port: address.port, port: address.port,
}); });
} }

View file

@ -9,7 +9,7 @@ use crate::{
}; };
pub async fn ping_server(address: &ServerAddress) -> Result<(), String> { pub async fn ping_server(address: &ServerAddress) -> Result<(), String> {
let resolved_address = resolver::resolve_address(&address).await?; let resolved_address = resolver::resolve_address(address).await?;
let mut conn = Connection::new(&resolved_address).await?; let mut conn = Connection::new(&resolved_address).await?;