Skip to content

Commit

Permalink
1.21 (#145)
Browse files Browse the repository at this point in the history
* 24w18a (data driven enchantments not implemented yet)

* 1.21
  • Loading branch information
mat-1 authored Jun 14, 2024
1 parent 38eab50 commit f66d2d4
Show file tree
Hide file tree
Showing 14 changed files with 653 additions and 132 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ A collection of Rust crates for making Minecraft bots, clients, and tools.

<!-- The line below is automatically read and updated by the migrate script, so don't change it manually. -->

_Currently supported Minecraft version: `1.20.6`._
_Currently supported Minecraft version: `1.21`._

> [!WARNING]
> Azalea is still very unfinished, though most crates are in a somewhat useable state
Expand Down
5 changes: 1 addition & 4 deletions azalea-client/src/interact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,7 @@ fn update_modifiers_for_held_item(
};
attributes
.attack_speed
.remove(&azalea_entity::attributes::BASE_ATTACK_SPEED_UUID);
attributes
.attack_speed
.insert(azalea_entity::attributes::tool_attack_speed_modifier(
.insert(azalea_entity::attributes::base_attack_speed_modifier(
added_attack_speed,
))
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion azalea-client/src/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ fn set_sprinting(
} else {
attributes
.speed
.remove(&azalea_entity::attributes::sprinting_modifier().uuid)
.remove(&azalea_entity::attributes::sprinting_modifier().id)
.is_none()
}
}
Expand Down
70 changes: 16 additions & 54 deletions azalea-entity/src/attributes.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
//! See <https://minecraft.fandom.com/wiki/Attribute>.

use std::{
collections::HashMap,
io::{Cursor, Write},
};
use std::collections::HashMap;

use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable};
use azalea_buf::McBuf;
use azalea_core::resource_location::ResourceLocation;
use bevy_ecs::component::Component;
use thiserror::Error;
use uuid::{uuid, Uuid};

#[derive(Clone, Debug, Component)]
pub struct Attributes {
Expand All @@ -19,7 +16,7 @@ pub struct Attributes {
#[derive(Clone, Debug)]
pub struct AttributeInstance {
pub base: f64,
modifiers_by_uuid: HashMap<Uuid, AttributeModifier>,
modifiers_by_id: HashMap<ResourceLocation, AttributeModifier>,
}

#[derive(Clone, Debug, Error)]
Expand All @@ -30,13 +27,13 @@ impl AttributeInstance {
pub fn new(base: f64) -> Self {
Self {
base,
modifiers_by_uuid: HashMap::new(),
modifiers_by_id: HashMap::new(),
}
}

pub fn calculate(&self) -> f64 {
let mut total = self.base;
for modifier in self.modifiers_by_uuid.values() {
for modifier in self.modifiers_by_id.values() {
match modifier.operation {
AttributeModifierOperation::Addition => total += modifier.amount,
AttributeModifierOperation::MultiplyBase => total += self.base * modifier.amount,
Expand All @@ -52,8 +49,8 @@ impl AttributeInstance {
/// Add a new modifier to this attribute.
pub fn insert(&mut self, modifier: AttributeModifier) -> Result<(), AlreadyPresentError> {
if self
.modifiers_by_uuid
.insert(modifier.uuid, modifier)
.modifiers_by_id
.insert(modifier.id.clone(), modifier)
.is_some()
{
Err(AlreadyPresentError)
Expand All @@ -62,17 +59,16 @@ impl AttributeInstance {
}
}

/// Remove the modifier with the given UUID from this attribute, returning
/// Remove the modifier with the given ID from this attribute, returning
/// the previous modifier is present.
pub fn remove(&mut self, uuid: &Uuid) -> Option<AttributeModifier> {
self.modifiers_by_uuid.remove(uuid)
pub fn remove(&mut self, id: &ResourceLocation) -> Option<AttributeModifier> {
self.modifiers_by_id.remove(id)
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, McBuf)]
pub struct AttributeModifier {
pub uuid: Uuid,
pub name: String,
pub id: ResourceLocation,
pub amount: f64,
pub operation: AttributeModifierOperation,
}
Expand All @@ -86,50 +82,16 @@ pub enum AttributeModifierOperation {

pub fn sprinting_modifier() -> AttributeModifier {
AttributeModifier {
uuid: uuid!("662A6B8D-DA3E-4C1C-8813-96EA6097278D"),
name: "Sprinting speed boost".to_string(),
id: ResourceLocation::new("sprinting"),
amount: 0.30000001192092896,
operation: AttributeModifierOperation::MultiplyTotal,
}
}

pub static BASE_ATTACK_SPEED_UUID: Uuid = uuid!("FA233E1C-4180-4865-B01B-BCCE9785ACA3");
pub fn weapon_attack_speed_modifier(amount: f64) -> AttributeModifier {
AttributeModifier {
uuid: BASE_ATTACK_SPEED_UUID,
name: "Weapon modifier".to_string(),
amount,
operation: AttributeModifierOperation::Addition,
}
}
pub fn tool_attack_speed_modifier(amount: f64) -> AttributeModifier {
pub fn base_attack_speed_modifier(amount: f64) -> AttributeModifier {
AttributeModifier {
uuid: BASE_ATTACK_SPEED_UUID,
name: "Tool modifier".to_string(),
id: ResourceLocation::new("base_attack_speed"),
amount,
operation: AttributeModifierOperation::Addition,
}
}

impl McBufReadable for AttributeModifier {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let uuid = Uuid::read_from(buf)?;
let amount = f64::read_from(buf)?;
let operation = AttributeModifierOperation::read_from(buf)?;
Ok(Self {
uuid,
name: "Unknown synced attribute modifier".to_string(),
amount,
operation,
})
}
}

impl McBufWritable for AttributeModifier {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
self.uuid.write_into(buf)?;
self.amount.write_into(buf)?;
self.operation.write_into(buf)?;
Ok(())
}
}
10 changes: 10 additions & 0 deletions azalea-inventory/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ pub fn from_kind(
kind: azalea_registry::DataComponentKind,
buf: &mut Cursor<&[u8]>,
) -> Result<Box<dyn EncodableDataComponent>, BufReadError> {
// if this is causing a compile-time error, look at DataComponents.java in the
// decompiled vanilla code to see how to implement new components
Ok(match kind {
DataComponentKind::CustomData => Box::new(CustomData::read_from(buf)?),
DataComponentKind::MaxStackSize => Box::new(MaxStackSize::read_from(buf)?),
Expand Down Expand Up @@ -114,6 +116,7 @@ pub fn from_kind(
DataComponentKind::Bees => Box::new(Bees::read_from(buf)?),
DataComponentKind::Lock => Box::new(Lock::read_from(buf)?),
DataComponentKind::ContainerLoot => Box::new(ContainerLoot::read_from(buf)?),
DataComponentKind::JukeboxPlayable => todo!(),
})
}

Expand Down Expand Up @@ -654,3 +657,10 @@ pub struct ContainerLoot {
pub loot: NbtCompound,
}
impl DataComponent for ContainerLoot {}

#[derive(Clone, PartialEq, McBuf)]
pub struct JukeboxPlayable {
pub song: azalea_registry::JukeboxSong,
pub show_in_tooltip: bool,
}
impl DataComponent for JukeboxPlayable {}
Loading

0 comments on commit f66d2d4

Please sign in to comment.