1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00

change DataComponentPatch::get to take in a generic (and add get_kind, has, and has_kind)

This commit is contained in:
mat 2024-11-28 03:17:58 +00:00
parent 08958c2278
commit c36201cc89
6 changed files with 262 additions and 87 deletions

View file

@ -10,14 +10,13 @@ use azalea_core::{
use azalea_entity::{ use azalea_entity::{
clamp_look_direction, view_vector, Attributes, EyeHeight, LocalEntity, LookDirection, Position, clamp_look_direction, view_vector, Attributes, EyeHeight, LocalEntity, LookDirection, Position,
}; };
use azalea_inventory::{ItemStack, ItemStackData}; use azalea_inventory::{components, ItemStack, ItemStackData};
use azalea_physics::clip::{BlockShapeType, ClipContext, FluidPickType}; use azalea_physics::clip::{BlockShapeType, ClipContext, FluidPickType};
use azalea_protocol::packets::game::{ use azalea_protocol::packets::game::{
s_interact::InteractionHand, s_interact::InteractionHand,
s_swing::ServerboundSwing, s_swing::ServerboundSwing,
s_use_item_on::{BlockHit, ServerboundUseItemOn}, s_use_item_on::{BlockHit, ServerboundUseItemOn},
}; };
use azalea_registry::DataComponentKind;
use azalea_world::{Instance, InstanceContainer, InstanceName}; use azalea_world::{Instance, InstanceContainer, InstanceName};
use bevy_app::{App, Plugin, Update}; use bevy_app::{App, Plugin, Update};
use bevy_ecs::{ use bevy_ecs::{
@ -268,7 +267,7 @@ pub fn check_block_can_be_broken_by_item_in_adventure_mode(
// minecraft caches the last checked block but that's kind of an unnecessary // minecraft caches the last checked block but that's kind of an unnecessary
// optimization and makes the code too complicated // optimization and makes the code too complicated
let Some(_can_destroy) = item.components.get(DataComponentKind::CanBreak) else { if !item.components.has::<components::CanBreak>() {
// no CanDestroy tag // no CanDestroy tag
return false; return false;
}; };

View file

@ -11,7 +11,6 @@ use std::{
}; };
use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite, BufReadError}; use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite, BufReadError};
use serde::{Deserialize, Serialize};
use crate::resource_location::ResourceLocation; use crate::resource_location::ResourceLocation;
@ -213,7 +212,7 @@ macro_rules! vec3_impl {
/// Used to represent an exact position in the world where an entity could be. /// Used to represent an exact position in the world where an entity could be.
/// For blocks, [`BlockPos`] is used instead. /// For blocks, [`BlockPos`] is used instead.
#[derive(Clone, Copy, Debug, Default, PartialEq, AzBuf)] #[derive(Clone, Copy, Debug, Default, PartialEq, AzBuf)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Vec3 { pub struct Vec3 {
pub x: f64, pub x: f64,
pub y: f64, pub y: f64,
@ -238,7 +237,7 @@ impl Vec3 {
/// The coordinates of a block in the world. For entities (if the coordinate /// The coordinates of a block in the world. For entities (if the coordinate
/// with decimals), use [`Vec3`] instead. /// with decimals), use [`Vec3`] instead.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct BlockPos { pub struct BlockPos {
pub x: i32, pub x: i32,
pub y: i32, pub y: i32,

View file

@ -13,7 +13,9 @@ use uuid::Uuid;
use crate::ItemStack; use crate::ItemStack;
pub trait DataComponent: Send + Sync + Any {} pub trait DataComponent: Send + Sync + Any {
const KIND: DataComponentKind;
}
pub trait EncodableDataComponent: Send + Sync + Any { pub trait EncodableDataComponent: Send + Sync + Any {
fn encode(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error>; fn encode(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error>;
@ -138,21 +140,27 @@ pub fn from_kind(
pub struct CustomData { pub struct CustomData {
pub nbt: Nbt, pub nbt: Nbt,
} }
impl DataComponent for CustomData {} impl DataComponent for CustomData {
const KIND: DataComponentKind = DataComponentKind::CustomData;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct MaxStackSize { pub struct MaxStackSize {
#[var] #[var]
pub count: i32, pub count: i32,
} }
impl DataComponent for MaxStackSize {} impl DataComponent for MaxStackSize {
const KIND: DataComponentKind = DataComponentKind::MaxStackSize;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct MaxDamage { pub struct MaxDamage {
#[var] #[var]
pub amount: i32, pub amount: i32,
} }
impl DataComponent for MaxDamage {} impl DataComponent for MaxDamage {
const KIND: DataComponentKind = DataComponentKind::MaxDamage;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct Damage { pub struct Damage {
@ -160,13 +168,17 @@ pub struct Damage {
pub amount: i32, pub amount: i32,
} }
impl DataComponent for Damage {} impl DataComponent for Damage {
const KIND: DataComponentKind = DataComponentKind::Damage;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct Unbreakable { pub struct Unbreakable {
pub show_in_tooltip: bool, pub show_in_tooltip: bool,
} }
impl DataComponent for Unbreakable {} impl DataComponent for Unbreakable {
const KIND: DataComponentKind = DataComponentKind::Unbreakable;
}
impl Default for Unbreakable { impl Default for Unbreakable {
fn default() -> Self { fn default() -> Self {
Self { Self {
@ -179,20 +191,26 @@ impl Default for Unbreakable {
pub struct CustomName { pub struct CustomName {
pub name: FormattedText, pub name: FormattedText,
} }
impl DataComponent for CustomName {} impl DataComponent for CustomName {
const KIND: DataComponentKind = DataComponentKind::CustomName;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct ItemName { pub struct ItemName {
pub name: FormattedText, pub name: FormattedText,
} }
impl DataComponent for ItemName {} impl DataComponent for ItemName {
const KIND: DataComponentKind = DataComponentKind::ItemName;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct Lore { pub struct Lore {
pub lines: Vec<FormattedText>, pub lines: Vec<FormattedText>,
// vanilla also has styled_lines here but it doesn't appear to be used for the protocol // vanilla also has styled_lines here but it doesn't appear to be used for the protocol
} }
impl DataComponent for Lore {} impl DataComponent for Lore {
const KIND: DataComponentKind = DataComponentKind::Lore;
}
#[derive(Clone, PartialEq, Copy, AzBuf)] #[derive(Clone, PartialEq, Copy, AzBuf)]
pub enum Rarity { pub enum Rarity {
@ -201,7 +219,9 @@ pub enum Rarity {
Rare, Rare,
Epic, Epic,
} }
impl DataComponent for Rarity {} impl DataComponent for Rarity {
const KIND: DataComponentKind = DataComponentKind::Rarity;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct Enchantments { pub struct Enchantments {
@ -209,7 +229,9 @@ pub struct Enchantments {
pub levels: HashMap<Enchantment, u32>, pub levels: HashMap<Enchantment, u32>,
pub show_in_tooltip: bool, pub show_in_tooltip: bool,
} }
impl DataComponent for Enchantments {} impl DataComponent for Enchantments {
const KIND: DataComponentKind = DataComponentKind::Enchantments;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub enum BlockStateValueMatcher { pub enum BlockStateValueMatcher {
@ -245,13 +267,17 @@ pub struct AdventureModePredicate {
pub struct CanPlaceOn { pub struct CanPlaceOn {
pub predicate: AdventureModePredicate, pub predicate: AdventureModePredicate,
} }
impl DataComponent for CanPlaceOn {} impl DataComponent for CanPlaceOn {
const KIND: DataComponentKind = DataComponentKind::CanPlaceOn;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct CanBreak { pub struct CanBreak {
pub predicate: AdventureModePredicate, pub predicate: AdventureModePredicate,
} }
impl DataComponent for CanBreak {} impl DataComponent for CanBreak {
const KIND: DataComponentKind = DataComponentKind::CanBreak;
}
#[derive(Clone, Copy, PartialEq, AzBuf)] #[derive(Clone, Copy, PartialEq, AzBuf)]
pub enum EquipmentSlotGroup { pub enum EquipmentSlotGroup {
@ -297,43 +323,59 @@ pub struct AttributeModifiers {
pub modifiers: Vec<AttributeModifiersEntry>, pub modifiers: Vec<AttributeModifiersEntry>,
pub show_in_tooltip: bool, pub show_in_tooltip: bool,
} }
impl DataComponent for AttributeModifiers {} impl DataComponent for AttributeModifiers {
const KIND: DataComponentKind = DataComponentKind::AttributeModifiers;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct CustomModelData { pub struct CustomModelData {
#[var] #[var]
pub value: i32, pub value: i32,
} }
impl DataComponent for CustomModelData {} impl DataComponent for CustomModelData {
const KIND: DataComponentKind = DataComponentKind::CustomModelData;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct HideAdditionalTooltip; pub struct HideAdditionalTooltip;
impl DataComponent for HideAdditionalTooltip {} impl DataComponent for HideAdditionalTooltip {
const KIND: DataComponentKind = DataComponentKind::HideAdditionalTooltip;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct HideTooltip; pub struct HideTooltip;
impl DataComponent for HideTooltip {} impl DataComponent for HideTooltip {
const KIND: DataComponentKind = DataComponentKind::HideTooltip;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct RepairCost { pub struct RepairCost {
#[var] #[var]
pub cost: u32, pub cost: u32,
} }
impl DataComponent for RepairCost {} impl DataComponent for RepairCost {
const KIND: DataComponentKind = DataComponentKind::RepairCost;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct CreativeSlotLock; pub struct CreativeSlotLock;
impl DataComponent for CreativeSlotLock {} impl DataComponent for CreativeSlotLock {
const KIND: DataComponentKind = DataComponentKind::CreativeSlotLock;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct EnchantmentGlintOverride { pub struct EnchantmentGlintOverride {
pub show_glint: bool, pub show_glint: bool,
} }
impl DataComponent for EnchantmentGlintOverride {} impl DataComponent for EnchantmentGlintOverride {
const KIND: DataComponentKind = DataComponentKind::EnchantmentGlintOverride;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct IntangibleProjectile; pub struct IntangibleProjectile;
impl DataComponent for IntangibleProjectile {} impl DataComponent for IntangibleProjectile {
const KIND: DataComponentKind = DataComponentKind::IntangibleProjectile;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct MobEffectDetails { pub struct MobEffectDetails {
@ -368,7 +410,9 @@ pub struct Food {
pub eat_seconds: f32, pub eat_seconds: f32,
pub effects: Vec<PossibleEffect>, pub effects: Vec<PossibleEffect>,
} }
impl DataComponent for Food {} impl DataComponent for Food {
const KIND: DataComponentKind = DataComponentKind::Food;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct ToolRule { pub struct ToolRule {
@ -384,7 +428,9 @@ pub struct Tool {
#[var] #[var]
pub damage_per_block: i32, pub damage_per_block: i32,
} }
impl DataComponent for Tool {} impl DataComponent for Tool {
const KIND: DataComponentKind = DataComponentKind::Tool;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct StoredEnchantments { pub struct StoredEnchantments {
@ -392,52 +438,68 @@ pub struct StoredEnchantments {
pub enchantments: HashMap<Enchantment, i32>, pub enchantments: HashMap<Enchantment, i32>,
pub show_in_tooltip: bool, pub show_in_tooltip: bool,
} }
impl DataComponent for StoredEnchantments {} impl DataComponent for StoredEnchantments {
const KIND: DataComponentKind = DataComponentKind::StoredEnchantments;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct DyedColor { pub struct DyedColor {
pub rgb: i32, pub rgb: i32,
pub show_in_tooltip: bool, pub show_in_tooltip: bool,
} }
impl DataComponent for DyedColor {} impl DataComponent for DyedColor {
const KIND: DataComponentKind = DataComponentKind::DyedColor;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct MapColor { pub struct MapColor {
pub color: i32, pub color: i32,
} }
impl DataComponent for MapColor {} impl DataComponent for MapColor {
const KIND: DataComponentKind = DataComponentKind::MapColor;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct MapId { pub struct MapId {
#[var] #[var]
pub id: i32, pub id: i32,
} }
impl DataComponent for MapId {} impl DataComponent for MapId {
const KIND: DataComponentKind = DataComponentKind::MapId;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct MapDecorations { pub struct MapDecorations {
pub decorations: NbtCompound, pub decorations: NbtCompound,
} }
impl DataComponent for MapDecorations {} impl DataComponent for MapDecorations {
const KIND: DataComponentKind = DataComponentKind::MapDecorations;
}
#[derive(Clone, Copy, PartialEq, AzBuf)] #[derive(Clone, Copy, PartialEq, AzBuf)]
pub enum MapPostProcessing { pub enum MapPostProcessing {
Lock, Lock,
Scale, Scale,
} }
impl DataComponent for MapPostProcessing {} impl DataComponent for MapPostProcessing {
const KIND: DataComponentKind = DataComponentKind::MapPostProcessing;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct ChargedProjectiles { pub struct ChargedProjectiles {
pub items: Vec<ItemStack>, pub items: Vec<ItemStack>,
} }
impl DataComponent for ChargedProjectiles {} impl DataComponent for ChargedProjectiles {
const KIND: DataComponentKind = DataComponentKind::ChargedProjectiles;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct BundleContents { pub struct BundleContents {
pub items: Vec<ItemStack>, pub items: Vec<ItemStack>,
} }
impl DataComponent for BundleContents {} impl DataComponent for BundleContents {
const KIND: DataComponentKind = DataComponentKind::BundleContents;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct PotionContents { pub struct PotionContents {
@ -445,7 +507,9 @@ pub struct PotionContents {
pub custom_color: Option<i32>, pub custom_color: Option<i32>,
pub custom_effects: Vec<MobEffectInstance>, pub custom_effects: Vec<MobEffectInstance>,
} }
impl DataComponent for PotionContents {} impl DataComponent for PotionContents {
const KIND: DataComponentKind = DataComponentKind::PotionContents;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct SuspiciousStewEffect { pub struct SuspiciousStewEffect {
@ -458,13 +522,17 @@ pub struct SuspiciousStewEffect {
pub struct SuspiciousStewEffects { pub struct SuspiciousStewEffects {
pub effects: Vec<SuspiciousStewEffect>, pub effects: Vec<SuspiciousStewEffect>,
} }
impl DataComponent for SuspiciousStewEffects {} impl DataComponent for SuspiciousStewEffects {
const KIND: DataComponentKind = DataComponentKind::SuspiciousStewEffects;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct WritableBookContent { pub struct WritableBookContent {
pub pages: Vec<String>, pub pages: Vec<String>,
} }
impl DataComponent for WritableBookContent {} impl DataComponent for WritableBookContent {
const KIND: DataComponentKind = DataComponentKind::WritableBookContent;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct WrittenBookContent { pub struct WrittenBookContent {
@ -475,7 +543,9 @@ pub struct WrittenBookContent {
pub pages: Vec<FormattedText>, pub pages: Vec<FormattedText>,
pub resolved: bool, pub resolved: bool,
} }
impl DataComponent for WrittenBookContent {} impl DataComponent for WrittenBookContent {
const KIND: DataComponentKind = DataComponentKind::WrittenBookContent;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct Trim { pub struct Trim {
@ -483,57 +553,75 @@ pub struct Trim {
pub pattern: TrimPattern, pub pattern: TrimPattern,
pub show_in_tooltip: bool, pub show_in_tooltip: bool,
} }
impl DataComponent for Trim {} impl DataComponent for Trim {
const KIND: DataComponentKind = DataComponentKind::Trim;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct DebugStickState { pub struct DebugStickState {
pub properties: NbtCompound, pub properties: NbtCompound,
} }
impl DataComponent for DebugStickState {} impl DataComponent for DebugStickState {
const KIND: DataComponentKind = DataComponentKind::DebugStickState;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct EntityData { pub struct EntityData {
pub entity: NbtCompound, pub entity: NbtCompound,
} }
impl DataComponent for EntityData {} impl DataComponent for EntityData {
const KIND: DataComponentKind = DataComponentKind::EntityData;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct BucketEntityData { pub struct BucketEntityData {
pub entity: NbtCompound, pub entity: NbtCompound,
} }
impl DataComponent for BucketEntityData {} impl DataComponent for BucketEntityData {
const KIND: DataComponentKind = DataComponentKind::BucketEntityData;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct BlockEntityData { pub struct BlockEntityData {
pub entity: NbtCompound, pub entity: NbtCompound,
} }
impl DataComponent for BlockEntityData {} impl DataComponent for BlockEntityData {
const KIND: DataComponentKind = DataComponentKind::BlockEntityData;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct Instrument { pub struct Instrument {
pub instrument: azalea_registry::Instrument, pub instrument: azalea_registry::Instrument,
} }
impl DataComponent for Instrument {} impl DataComponent for Instrument {
const KIND: DataComponentKind = DataComponentKind::Instrument;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct OminousBottleAmplifier { pub struct OminousBottleAmplifier {
#[var] #[var]
pub amplifier: i32, pub amplifier: i32,
} }
impl DataComponent for OminousBottleAmplifier {} impl DataComponent for OminousBottleAmplifier {
const KIND: DataComponentKind = DataComponentKind::OminousBottleAmplifier;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct Recipes { pub struct Recipes {
pub recipes: Vec<ResourceLocation>, pub recipes: Vec<ResourceLocation>,
} }
impl DataComponent for Recipes {} impl DataComponent for Recipes {
const KIND: DataComponentKind = DataComponentKind::Recipes;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct LodestoneTracker { pub struct LodestoneTracker {
pub target: Option<GlobalPos>, pub target: Option<GlobalPos>,
pub tracked: bool, pub tracked: bool,
} }
impl DataComponent for LodestoneTracker {} impl DataComponent for LodestoneTracker {
const KIND: DataComponentKind = DataComponentKind::LodestoneTracker;
}
#[derive(Clone, Copy, PartialEq, AzBuf)] #[derive(Clone, Copy, PartialEq, AzBuf)]
pub enum FireworkExplosionShape { pub enum FireworkExplosionShape {
@ -552,7 +640,9 @@ pub struct FireworkExplosion {
pub has_trail: bool, pub has_trail: bool,
pub has_twinkle: bool, pub has_twinkle: bool,
} }
impl DataComponent for FireworkExplosion {} impl DataComponent for FireworkExplosion {
const KIND: DataComponentKind = DataComponentKind::FireworkExplosion;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct Fireworks { pub struct Fireworks {
@ -560,7 +650,9 @@ pub struct Fireworks {
pub flight_duration: i32, pub flight_duration: i32,
pub explosions: Vec<FireworkExplosion>, pub explosions: Vec<FireworkExplosion>,
} }
impl DataComponent for Fireworks {} impl DataComponent for Fireworks {
const KIND: DataComponentKind = DataComponentKind::Fireworks;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct GameProfileProperty { pub struct GameProfileProperty {
@ -575,13 +667,17 @@ pub struct Profile {
pub id: Option<Uuid>, pub id: Option<Uuid>,
pub properties: Vec<GameProfileProperty>, pub properties: Vec<GameProfileProperty>,
} }
impl DataComponent for Profile {} impl DataComponent for Profile {
const KIND: DataComponentKind = DataComponentKind::Profile;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct NoteBlockSound { pub struct NoteBlockSound {
pub sound: ResourceLocation, pub sound: ResourceLocation,
} }
impl DataComponent for NoteBlockSound {} impl DataComponent for NoteBlockSound {
const KIND: DataComponentKind = DataComponentKind::NoteBlockSound;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct BannerPattern { pub struct BannerPattern {
@ -595,7 +691,9 @@ pub struct BannerPattern {
pub struct BannerPatterns { pub struct BannerPatterns {
pub patterns: Vec<BannerPattern>, pub patterns: Vec<BannerPattern>,
} }
impl DataComponent for BannerPatterns {} impl DataComponent for BannerPatterns {
const KIND: DataComponentKind = DataComponentKind::BannerPatterns;
}
#[derive(Clone, Copy, PartialEq, AzBuf)] #[derive(Clone, Copy, PartialEq, AzBuf)]
pub enum DyeColor { pub enum DyeColor {
@ -621,25 +719,33 @@ pub enum DyeColor {
pub struct BaseColor { pub struct BaseColor {
pub color: DyeColor, pub color: DyeColor,
} }
impl DataComponent for BaseColor {} impl DataComponent for BaseColor {
const KIND: DataComponentKind = DataComponentKind::BaseColor;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct PotDecorations { pub struct PotDecorations {
pub items: Vec<Item>, pub items: Vec<Item>,
} }
impl DataComponent for PotDecorations {} impl DataComponent for PotDecorations {
const KIND: DataComponentKind = DataComponentKind::PotDecorations;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct Container { pub struct Container {
pub items: Vec<ItemStack>, pub items: Vec<ItemStack>,
} }
impl DataComponent for Container {} impl DataComponent for Container {
const KIND: DataComponentKind = DataComponentKind::Container;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct BlockState { pub struct BlockState {
pub properties: HashMap<String, String>, pub properties: HashMap<String, String>,
} }
impl DataComponent for BlockState {} impl DataComponent for BlockState {
const KIND: DataComponentKind = DataComponentKind::BlockState;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct BeehiveOccupant { pub struct BeehiveOccupant {
@ -654,26 +760,34 @@ pub struct BeehiveOccupant {
pub struct Bees { pub struct Bees {
pub occupants: Vec<BeehiveOccupant>, pub occupants: Vec<BeehiveOccupant>,
} }
impl DataComponent for Bees {} impl DataComponent for Bees {
const KIND: DataComponentKind = DataComponentKind::Bees;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct Lock { pub struct Lock {
pub key: String, pub key: String,
} }
impl DataComponent for Lock {} impl DataComponent for Lock {
const KIND: DataComponentKind = DataComponentKind::Lock;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct ContainerLoot { pub struct ContainerLoot {
pub loot: NbtCompound, pub loot: NbtCompound,
} }
impl DataComponent for ContainerLoot {} impl DataComponent for ContainerLoot {
const KIND: DataComponentKind = DataComponentKind::ContainerLoot;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct JukeboxPlayable { pub struct JukeboxPlayable {
pub song: azalea_registry::JukeboxSong, pub song: azalea_registry::JukeboxSong,
pub show_in_tooltip: bool, pub show_in_tooltip: bool,
} }
impl DataComponent for JukeboxPlayable {} impl DataComponent for JukeboxPlayable {
const KIND: DataComponentKind = DataComponentKind::JukeboxPlayable;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct Consumable { pub struct Consumable {
@ -683,7 +797,9 @@ pub struct Consumable {
pub has_consume_particles: bool, pub has_consume_particles: bool,
pub on_consuime_effects: Vec<ConsumeEffectKind>, pub on_consuime_effects: Vec<ConsumeEffectKind>,
} }
impl DataComponent for Consumable {} impl DataComponent for Consumable {
const KIND: DataComponentKind = DataComponentKind::Consumable;
}
#[derive(Clone, Copy, PartialEq, AzBuf)] #[derive(Clone, Copy, PartialEq, AzBuf)]
pub enum ItemUseAnimation { pub enum ItemUseAnimation {
@ -703,33 +819,43 @@ pub enum ItemUseAnimation {
pub struct UseRemainder { pub struct UseRemainder {
pub convert_into: ItemStack, pub convert_into: ItemStack,
} }
impl DataComponent for UseRemainder {} impl DataComponent for UseRemainder {
const KIND: DataComponentKind = DataComponentKind::UseRemainder;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct UseCooldown { pub struct UseCooldown {
pub seconds: f32, pub seconds: f32,
pub cooldown_group: Option<ResourceLocation>, pub cooldown_group: Option<ResourceLocation>,
} }
impl DataComponent for UseCooldown {} impl DataComponent for UseCooldown {
const KIND: DataComponentKind = DataComponentKind::UseCooldown;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct Enchantable { pub struct Enchantable {
#[var] #[var]
pub value: u32, pub value: u32,
} }
impl DataComponent for Enchantable {} impl DataComponent for Enchantable {
const KIND: DataComponentKind = DataComponentKind::Enchantable;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct Repairable { pub struct Repairable {
pub items: HolderSet<Item, ResourceLocation>, pub items: HolderSet<Item, ResourceLocation>,
} }
impl DataComponent for Repairable {} impl DataComponent for Repairable {
const KIND: DataComponentKind = DataComponentKind::Repairable;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct ItemModel { pub struct ItemModel {
pub resource_location: ResourceLocation, pub resource_location: ResourceLocation,
} }
impl DataComponent for ItemModel {} impl DataComponent for ItemModel {
const KIND: DataComponentKind = DataComponentKind::ItemModel;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct DamageResistant { pub struct DamageResistant {
@ -743,7 +869,9 @@ pub struct DamageResistant {
// resourcelocation for now // resourcelocation for now
pub types: ResourceLocation, pub types: ResourceLocation,
} }
impl DataComponent for DamageResistant {} impl DataComponent for DamageResistant {
const KIND: DataComponentKind = DataComponentKind::DamageResistant;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct Equippable { pub struct Equippable {
@ -752,7 +880,9 @@ pub struct Equippable {
pub model: Option<ResourceLocation>, pub model: Option<ResourceLocation>,
pub allowed_entities: HolderSet<EntityKind, ResourceLocation>, pub allowed_entities: HolderSet<EntityKind, ResourceLocation>,
} }
impl DataComponent for Equippable {} impl DataComponent for Equippable {
const KIND: DataComponentKind = DataComponentKind::Equippable;
}
#[derive(Clone, Copy, Debug, PartialEq, AzBuf)] #[derive(Clone, Copy, Debug, PartialEq, AzBuf)]
pub enum EquipmentSlot { pub enum EquipmentSlot {
@ -769,16 +899,22 @@ pub enum EquipmentSlot {
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct Glider; pub struct Glider;
impl DataComponent for Glider {} impl DataComponent for Glider {
const KIND: DataComponentKind = DataComponentKind::Glider;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct TooltipStyle { pub struct TooltipStyle {
pub resource_location: ResourceLocation, pub resource_location: ResourceLocation,
} }
impl DataComponent for TooltipStyle {} impl DataComponent for TooltipStyle {
const KIND: DataComponentKind = DataComponentKind::TooltipStyle;
}
#[derive(Clone, PartialEq, AzBuf)] #[derive(Clone, PartialEq, AzBuf)]
pub struct DeathProtection { pub struct DeathProtection {
pub death_effects: Vec<ConsumeEffectKind>, pub death_effects: Vec<ConsumeEffectKind>,
} }
impl DataComponent for DeathProtection {} impl DataComponent for DeathProtection {
const KIND: DataComponentKind = DataComponentKind::DeathProtection;
}

View file

@ -1,4 +1,5 @@
use std::{ use std::{
any::Any,
collections::HashMap, collections::HashMap,
fmt, fmt,
io::{Cursor, Write}, io::{Cursor, Write},
@ -177,9 +178,51 @@ pub struct DataComponentPatch {
} }
impl DataComponentPatch { impl DataComponentPatch {
pub fn get(&self, kind: DataComponentKind) -> Option<&dyn components::EncodableDataComponent> { /// Returns the value of the component in the generic argument for this
/// item.
///
/// ```
/// # use azalea_inventory::{ItemStackData, DataComponentPatch, components};
/// # use azalea_registry::Item;
/// # fn example(item: &ItemStackData) -> Option<()> {
/// let item_nutrition = item.components.get::<components::Food>()?.nutrition;
/// # Some(())
/// # }
/// ```
pub fn get<T: components::DataComponent>(&self) -> Option<&T> {
let component = self.components.get(&T::KIND).and_then(|c| c.as_deref())?;
let component_any = component as &dyn Any;
component_any.downcast_ref::<T>()
}
pub fn get_kind(
&self,
kind: DataComponentKind,
) -> Option<&dyn components::EncodableDataComponent> {
self.components.get(&kind).and_then(|c| c.as_deref()) self.components.get(&kind).and_then(|c| c.as_deref())
} }
/// Returns whether the component in the generic argument is present for
/// this item.
///
/// ```
/// # use azalea_inventory::{ItemStackData, DataComponentPatch, components};
/// # use azalea_registry::Item;
/// # let item = ItemStackData {
/// # kind: Item::Stone,
/// # count: 1,
/// # components: Default::default(),
/// # };
/// let is_edible = item.components.has::<components::Food>();
/// # assert!(!is_edible);
/// ```
pub fn has<T: components::DataComponent>(&self) -> bool {
self.components.contains_key(&T::KIND)
}
pub fn has_kind(&self, kind: DataComponentKind) -> bool {
self.components.contains_key(&kind)
}
} }
impl AzaleaRead for DataComponentPatch { impl AzaleaRead for DataComponentPatch {

View file

@ -1,7 +1,7 @@
use azalea_block::{Block, BlockState}; use azalea_block::{Block, BlockState};
use azalea_client::{inventory::Inventory, Client}; use azalea_client::{inventory::Inventory, Client};
use azalea_entity::{FluidOnEyes, Physics}; use azalea_entity::{FluidOnEyes, Physics};
use azalea_inventory::{ItemStack, Menu}; use azalea_inventory::{components, ItemStack, Menu};
use azalea_registry::{DataComponentKind, Fluid}; use azalea_registry::{DataComponentKind, Fluid};
#[derive(Debug)] #[derive(Debug)]
@ -89,17 +89,13 @@ pub fn accurate_best_tool_in_hotbar_for_block(
physics, physics,
)); ));
} }
ItemStack::Present(item_slot) => { ItemStack::Present(item_stack) => {
// lazy way to avoid checking durability since azalea doesn't have durability // lazy way to avoid checking durability since azalea doesn't have durability
// data yet // data yet
if item_slot if !item_stack.components.has::<components::Damage>() {
.components
.get(DataComponentKind::Damage)
.is_none()
{
this_item_speed = Some(azalea_entity::mining::get_mine_progress( this_item_speed = Some(azalea_entity::mining::get_mine_progress(
block.as_ref(), block.as_ref(),
item_slot.kind, item_stack.kind,
menu, menu,
fluid_on_eyes, fluid_on_eyes,
physics, physics,

View file

@ -109,12 +109,12 @@ def remove_variant(variant: str):
# now remove the struct # now remove the struct
line_before_struct = None # this is the #[derive] line line_before_struct = None # this is the #[derive] line
line_after_struct = None # impl DataComponent for ... {} line_after_struct = None # impl DataComponent for ... {\n...\n}
for i, line in enumerate(list(code)): for i, line in enumerate(list(code)):
if line == f'pub struct {variant} {{' or line == f'pub struct {variant};': if line == f'pub struct {variant} {{' or line == f'pub struct {variant};':
line_before_struct = i - 1 line_before_struct = i - 1
elif line == f'impl DataComponent for {variant} {{}}': elif line == f'impl DataComponent for {variant} {{':
line_after_struct = i + 1 line_after_struct = i + 3
break break
if line_before_struct is None: if line_before_struct is None:
raise ValueError(f'Couldn\'t find struct {variant}') raise ValueError(f'Couldn\'t find struct {variant}')
@ -153,7 +153,9 @@ def add_variant(variant: str):
code.append(f'pub struct {variant} {{') code.append(f'pub struct {variant} {{')
code.append(' pub todo: todo!(), // see DataComponents.java') code.append(' pub todo: todo!(), // see DataComponents.java')
code.append('}') code.append('}')
code.append(f'impl DataComponent for {variant} {{}}') code.append(f'impl DataComponent for {variant} {{')
code.append(f' const KIND: DataComponentKind = DataComponentKind::{variant};')
code.append('}')
with open(ITEM_COMPONENTS_DIR, 'w') as f: with open(ITEM_COMPONENTS_DIR, 'w') as f:
f.write('\n'.join(code)) f.write('\n'.join(code))