1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00
This commit is contained in:
mat 2022-06-25 17:37:29 -05:00
parent 978880b756
commit e8deda5d2e
12 changed files with 48 additions and 56 deletions

View file

@ -36,7 +36,7 @@ struct BlockDefinition {
properties_and_defaults: Vec<PropertyAndDefault>,
}
impl PropertyAndDefault {
fn into_property_with_name_and_default(&self, name: String) -> PropertyWithNameAndDefault {
fn as_property_with_name_and_default(&self, name: String) -> PropertyWithNameAndDefault {
PropertyWithNameAndDefault {
name,
struct_name: self.struct_name.clone(),
@ -110,11 +110,7 @@ impl Parse for BlockDefinition {
let mut properties_and_defaults = Vec::new();
loop {
let property = match content.parse() {
Ok(property) => property,
Err(_) => break,
};
while let Ok(property) = content.parse() {
content.parse::<Token![=]>()?;
let property_default = content.parse()?;
properties_and_defaults.push(PropertyAndDefault {
@ -248,7 +244,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
for property_name in block_property_names {
let property_variants = properties_map
.get(property_name)
.expect(format!("Property '{}' not found", property_name).as_str())
.unwrap_or_else(|| panic!("Property '{}' not found", property_name))
.clone();
block_properties_vec.push(property_variants);
}
@ -274,13 +270,13 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
};
let mut property_name = property_struct_names_to_names
.get(&property.struct_name.to_string())
.expect(format!("Property '{}' is bad", property.struct_name).as_str())
.unwrap_or_else(|| panic!("Property '{}' is bad", property.struct_name))
.clone();
if let Some(index) = index {
property_name.push_str(&format!("_{}", &index.to_string()));
}
properties_with_name
.push(property.into_property_with_name_and_default(property_name.clone()));
.push(property.as_property_with_name_and_default(property_name.clone()));
}
// pub face: properties::Face,
@ -297,7 +293,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
{
// let property_name_snake =
// Ident::new(&property.to_string(), proc_macro2::Span::call_site());
let name_ident = Ident::new(&name, proc_macro2::Span::call_site());
let name_ident = Ident::new(name, proc_macro2::Span::call_site());
block_struct_fields.extend(quote! {
pub #name_ident: #struct_name,
})
@ -317,7 +313,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
let first_state_id = state_id;
// if there's no properties, then the block is just a single state
if block_properties_vec.len() == 0 {
if block_properties_vec.is_empty() {
block_state_enum_variants.extend(quote! {
#block_name_pascal_case,
});
@ -418,7 +414,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
let block_behavior = &block.behavior;
let block_id = block.name.to_string();
let from_block_to_state_match = if block.properties_and_defaults.len() > 0 {
let from_block_to_state_match = if !block.properties_and_defaults.is_empty() {
quote! {
match b {
#from_block_to_state_match_inner

View file

@ -1,6 +1,6 @@
pub fn combinations_of<T: Clone>(items: &[Vec<T>]) -> Vec<Vec<T>> {
let mut combinations = Vec::new();
if items.len() == 0 {
if items.is_empty() {
return combinations;
};
if items.len() == 1 {
@ -13,8 +13,7 @@ pub fn combinations_of<T: Clone>(items: &[Vec<T>]) -> Vec<Vec<T>> {
for i in 0..items[0].len() {
let item = &items[0][i];
for other_combinations in combinations_of(&items[1..]) {
let mut combination = Vec::new();
combination.push(item.clone());
let mut combination = vec![item.clone()];
combination.extend(other_combinations);
combinations.push(combination);
}
@ -29,13 +28,11 @@ pub fn to_pascal_case(s: &str) -> String {
for c in s.chars() {
if c == '_' {
prev_was_underscore = true;
} else if prev_was_underscore {
result.push(c.to_ascii_uppercase());
prev_was_underscore = false;
} else {
if prev_was_underscore {
result.push(c.to_ascii_uppercase());
prev_was_underscore = false;
} else {
result.push(c);
}
result.push(c);
}
}
result

View file

@ -7,8 +7,10 @@ pub use blocks::*;
use std::mem;
impl BlockState {
/// Transmutes a u32 to a block state. UB if the value is not a valid block
/// state.
/// Transmutes a u32 to a block state.
///
/// # Safety
/// The `state_id` should be a valid block state.
#[inline]
pub unsafe fn from_u32_unsafe(state_id: u32) -> Self {
mem::transmute::<u32, BlockState>(state_id)

View file

@ -231,7 +231,7 @@ impl McBufVarReadable for i64 {
for i in 0..8 {
buf.read_exact(&mut buffer)
.map_err(|_| "Invalid VarLong".to_string())?;
ans |= ((buffer[0] & 0b0111_1111) as i64) << 7 * i;
ans |= ((buffer[0] & 0b0111_1111) as i64) << (7 * i);
if buffer[0] & 0b1000_0000 == 0 {
break;
}

View file

@ -191,7 +191,7 @@ impl McBufVarWritable for i64 {
}
// this only writes a single byte, so write_all isn't necessary
// the let _ = is so clippy doesn't complain
let _ = buf.write(&mut buffer)?;
let _ = buf.write(&buffer)?;
}
Ok(())
}

View file

@ -25,7 +25,7 @@ impl TranslatableComponent {
}
pub fn read(&self) -> Result<String, fmt::Error> {
let template = azalea_language::get(&self.key).unwrap_or_else(|| &self.key);
let template = azalea_language::get(&self.key).unwrap_or(&self.key);
// decode the % things
let mut result = String::new();

View file

@ -155,8 +155,8 @@ impl Client {
// we got the GameConnection, so the server is now connected :)
let client = Client {
game_profile: game_profile.clone(),
conn: conn.clone(),
game_profile,
conn,
player: Arc::new(Mutex::new(Player::default())),
dimension: Arc::new(Mutex::new(None)),
};
@ -167,7 +167,7 @@ impl Client {
// read the error to see where the issue is
// you might be able to just drop the lock or put it in its own scope to fix
tokio::spawn(Self::protocol_loop(client.clone(), tx.clone()));
tokio::spawn(Self::game_tick_loop(client.clone(), tx.clone()));
tokio::spawn(Self::game_tick_loop(client.clone(), tx));
Ok((client, rx))
}

View file

@ -5,20 +5,20 @@ use azalea_protocol::packets::game::serverbound_move_player_packet_pos_rot::Serv
impl Client {
/// Set the client's position to the given coordinates.
pub async fn move_to(&mut self, new_pos: EntityPos) -> Result<(), String> {
let mut dimension_lock = self.dimension.lock().unwrap();
let dimension = dimension_lock.as_mut().unwrap();
{
let mut dimension_lock = self.dimension.lock().unwrap();
let dimension = dimension_lock.as_mut().unwrap();
let player_lock = self.player.lock().unwrap();
let player_lock = self.player.lock().unwrap();
let player_id = if let Some(player_lock) = player_lock.entity(dimension) {
player_lock.id
} else {
return Err("Player entity not found".to_string());
};
let player_id = if let Some(player_lock) = player_lock.entity(dimension) {
player_lock.id
} else {
return Err("Player entity not found".to_string());
};
dimension.move_entity(player_id, new_pos)?;
drop(dimension_lock);
drop(player_lock);
dimension.move_entity(player_id, new_pos)?;
}
self.conn
.lock()

View file

@ -182,9 +182,9 @@ impl From<ChunkSectionPos> for ChunkPos {
impl From<&BlockPos> for ChunkBlockPos {
fn from(pos: &BlockPos) -> Self {
ChunkBlockPos {
x: pos.x.rem_euclid(16).abs() as u8,
x: pos.x.rem_euclid(16).unsigned_abs() as u8,
y: pos.y,
z: pos.z.rem_euclid(16).abs() as u8,
z: pos.z.rem_euclid(16).unsigned_abs() as u8,
}
}
}
@ -192,9 +192,9 @@ impl From<&BlockPos> for ChunkBlockPos {
impl From<&BlockPos> for ChunkSectionBlockPos {
fn from(pos: &BlockPos) -> Self {
ChunkSectionBlockPos {
x: pos.x.rem(16).abs() as u8,
y: pos.y.rem(16).abs() as u8,
z: pos.z.rem(16).abs() as u8,
x: pos.x.rem(16).unsigned_abs() as u8,
y: pos.y.rem(16).unsigned_abs() as u8,
z: pos.z.rem(16).unsigned_abs() as u8,
}
}
}
@ -203,7 +203,7 @@ impl From<&ChunkBlockPos> for ChunkSectionBlockPos {
fn from(pos: &ChunkBlockPos) -> Self {
ChunkSectionBlockPos {
x: pos.x,
y: pos.y.rem(16).abs() as u8,
y: pos.y.rem(16).unsigned_abs() as u8,
z: pos.z,
}
}

View file

@ -22,15 +22,12 @@ impl McBufReadable for BlockStateWithPosition {
let data = u64::var_read_from(buf)?;
let position_part = data & 4095;
let state = (data >> 12) as u32;
let position = ChunkSectionBlockPos {
let pos = ChunkSectionBlockPos {
x: (position_part >> 8 & 15) as u8,
y: (position_part >> 0 & 15) as u8,
y: (position_part & 15) as u8,
z: (position_part >> 4 & 15) as u8,
};
Ok(BlockStateWithPosition {
pos: position,
state: state,
})
Ok(BlockStateWithPosition { pos, state })
}
}

View file

@ -31,9 +31,9 @@ impl ConnectionProtocol {
#[derive(Clone, Debug)]
pub enum Packet {
Game(game::GamePacket),
Handshake(handshake::HandshakePacket),
Login(login::LoginPacket),
Game(Box<game::GamePacket>),
Handshake(Box<handshake::HandshakePacket>),
Login(Box<login::LoginPacket>),
Status(Box<status::StatusPacket>),
}

View file

@ -44,7 +44,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let dimension = dimension_lock.as_ref().unwrap();
let player = client.player.lock().unwrap();
let entity = player
.entity(&dimension)
.entity(dimension)
.expect("Player entity is not in world");
entity.pos().add_y(0.5)
};