Skip to content

Commit

Permalink
feat: implement item click handling system (#574)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgazelka authored Nov 16, 2024
1 parent bf8a9e4 commit 866e8fe
Show file tree
Hide file tree
Showing 36 changed files with 461 additions and 444 deletions.
26 changes: 15 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[profile]
[profile.release-debug]
debug = true
inherits = 'release'
Expand Down Expand Up @@ -30,6 +31,7 @@ members = [
'crates/hyperion-clap',
'crates/hyperion-command',
'crates/hyperion-rank-tree',
'crates/hyperion-item',
]
resolver = '2'

Expand All @@ -49,14 +51,14 @@ convert_case = '0.6.0'
criterion = '0.5.1'
derive-build = '0.1.1'
directories = '5.0.1'
dotenvy = '0.15.7'
enumset = '1.1.5'
fastrand = '2.1.0'
futures-util = '0.3.31'
glam = '0.29.0'
heapless = '0.8.0'
heed = '0.20.5'
hex = '0.4.3'
indexmap = { version = '2.6.0', features = ['rayon'] }
itertools = '0.13.0'
kanal = '0.1.0-pre8'
libc = '0.2.155'
Expand Down Expand Up @@ -141,6 +143,9 @@ path = 'crates/hyperion-event-macros'
[workspace.dependencies.hyperion-inventory]
path = 'crates/hyperion-inventory'

[workspace.dependencies.hyperion-item]
path = 'crates/hyperion-item'

[workspace.dependencies.hyperion-nerd-font]
path = 'crates/hyperion-nerd-font'

Expand All @@ -165,6 +170,10 @@ path = 'crates/hyperion-text'
[workspace.dependencies.hyperion-utils]
path = 'crates/hyperion-utils'

[workspace.dependencies.indexmap]
features = ['rayon']
version = '2.6.0'

[workspace.dependencies.ndarray]
features = ['blas']
version = '0.16.1'
Expand Down
24 changes: 12 additions & 12 deletions crates/bvh-region/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ plotters = {workspace = true, features = [
"image"
], optional = true}
plotters-bitmap = {workspace = true, optional = true}
serde = {workspace = true, features = ["derive"]}
arrayvec.workspace = true
fastrand.workspace = true
itertools.workspace = true
ordered-float.workspace = true
rayon.workspace = true
tracing.workspace = true
serde = {workspace = true}
arrayvec = {workspace = true}
fastrand = {workspace = true}
itertools = {workspace = true}
ordered-float = {workspace = true}
rayon = {workspace = true}
tracing = {workspace = true}

[dev-dependencies]
criterion.workspace = true
divan.workspace = true
rand.workspace = true
tango-bench.workspace = true
tracing-subscriber.workspace = true
criterion = {workspace = true}
divan = {workspace = true}
rand = {workspace = true}
tango-bench = {workspace = true}
tracing-subscriber = {workspace = true}

[features]
default = []
Expand Down
12 changes: 6 additions & 6 deletions crates/hyperion-clap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ readme = "README.md"
publish = false

[dependencies]
clap = { version = "4.5.20", features = ["derive"] }
flecs_ecs.workspace = true
hyperion-command.workspace = true
tracing.workspace = true
hyperion.workspace = true
valence_protocol.workspace = true
clap ={ workspace = true }
flecs_ecs = { workspace = true }
hyperion-command = { workspace = true }
tracing = { workspace = true }
hyperion = { workspace = true }
valence_protocol = { workspace = true }

[lints]
workspace = true
10 changes: 5 additions & 5 deletions crates/hyperion-command/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ readme = "README.md"
publish = false

[dependencies]
flecs_ecs.workspace = true
hyperion.workspace = true
gxhash.workspace = true
tracing.workspace = true
indexmap.workspace = true
flecs_ecs = {workspace = true}
hyperion = {workspace = true}
gxhash = {workspace = true}
tracing = {workspace = true}
indexmap = {workspace = true}

[lints]
workspace = true
10 changes: 5 additions & 5 deletions crates/hyperion-crafting/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
cargo-features = ["edition2024"]

[dependencies]
anyhow.workspace = true
derive-build.workspace = true
flecs_ecs.workspace = true
slotmap.workspace = true
valence_protocol.workspace = true
anyhow = { workspace = true }
derive-build = { workspace = true }
flecs_ecs = { workspace = true }
slotmap = { workspace = true }
valence_protocol = { workspace = true }

[lints]
workspace = true
Expand Down
15 changes: 15 additions & 0 deletions crates/hyperion-event-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ impl EventsInput {
fn generate(&self) -> proc_macro2::TokenStream {
// Generate all fields and initializers
let fields = self.events.iter().map(EventType::generate_field);

let field_idents = self.events.iter().map(|event| {
let field_name = event.ident.to_string().to_case(Case::Snake);
format_ident!("{field_name}")
});

let initializers = self.events.iter().map(EventType::generate_initializer);

// Generate all trait implementations
Expand All @@ -197,6 +203,15 @@ impl EventsInput {
#(#initializers)*
}
}

pub fn clear(&mut self) {
#(
let ptr = self.#field_idents.0;
let ptr = ptr.cast_mut();
let ptr = unsafe { &mut *ptr };
ptr.clear();
)*
}
}
};

Expand Down
10 changes: 5 additions & 5 deletions crates/hyperion-inventory/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
cargo-features = ["edition2024"]

[dependencies]
flecs_ecs.workspace = true
hyperion-crafting.workspace = true
roaring.workspace = true
snafu.workspace = true
valence_protocol.workspace = true
hyperion-crafting = {workspace = true}
roaring = {workspace = true}
snafu = {workspace = true}
valence_protocol = {workspace = true}
flecs_ecs = {workspace = true}

[lints]
workspace = true
Expand Down
11 changes: 10 additions & 1 deletion crates/hyperion-inventory/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::cmp::min;

use flecs_ecs::macros::Component;
use flecs_ecs::{core::World, macros::Component, prelude::Module};
use roaring::RoaringBitmap;
use valence_protocol::{ItemKind, ItemStack};

Expand Down Expand Up @@ -483,3 +483,12 @@ pub const OFFHAND_SLOT: u16 = 45;
// assert_eq!(inventory.get(37).unwrap().count, 63);
// }
// }

#[derive(Component)]
pub struct InventoryModule;

impl Module for InventoryModule {
fn module(world: &World) {
world.component::<PlayerInventory>();
}
}
1 change: 1 addition & 0 deletions crates/hyperion-item/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
18 changes: 18 additions & 0 deletions crates/hyperion-item/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "hyperion-item"
version = "0.1.0"
edition = "2021"
authors = ["Andrew Gazelka <andrew.gazelka@gmail.com>"]
readme = "README.md"
publish = false

[dependencies]
flecs_ecs = { workspace = true }
bytemuck = "1.19.0"
valence_protocol = { workspace = true }
hyperion = { workspace = true }
hyperion-inventory = { workspace = true }
derive_more = { workspace = true }

[lints]
workspace = true
1 change: 1 addition & 0 deletions crates/hyperion-item/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# hyperion-item
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use flecs_ecs::core::Entity;
use valence_protocol::{nbt, nbt::Value, ItemKind, ItemStack};

/// A builder for creating Minecraft items with NBT data
Expand Down Expand Up @@ -97,6 +98,17 @@ impl ItemBuilder {
self
}

pub fn handler(mut self, handler: Entity) -> Self {
let nbt = self.nbt.get_or_insert_with(nbt::Compound::new);
let id = handler.0;

// we are explicitly casting to i64 because although sign might be lost, when we read it back,
// we will revert it back to a u64.
let id: i64 = bytemuck::cast(id);
nbt.insert("Handler", Value::Long(id));
self
}

pub fn glowing(mut self) -> Self {
let nbt = self.nbt.get_or_insert_with(nbt::Compound::new);
nbt.insert(
Expand All @@ -122,6 +134,7 @@ impl ItemBuilder {
self
}

#[must_use]
pub fn build(self) -> ItemStack {
ItemStack::new(self.kind, self.count, self.nbt)
}
Expand Down
59 changes: 59 additions & 0 deletions crates/hyperion-item/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use derive_more::{Constructor, Deref, DerefMut};
use flecs_ecs::{
core::{EntityViewGet, QueryBuilderImpl, SystemAPI, TermBuilderImpl, World, WorldGet},
macros::Component,
prelude::Module,
};
use hyperion::storage::{EventHandler, EventHandlers, GlobalEventHandlers};
use valence_protocol::{nbt, Hand};

pub mod builder;

#[derive(Component)]
pub struct ItemModule;

#[derive(Component, Constructor, Deref, DerefMut)]
pub struct Handler {
on_click: EventHandler<Hand>,
}

impl Module for ItemModule {
fn module(world: &World) {
world.import::<hyperion_inventory::InventoryModule>();
world.component::<Handler>();

world.get::<&mut GlobalEventHandlers>(|handlers| {
handlers.click.register(|query, hand| {
let world = query.world;
let inventory = &mut *query.inventory;

let stack = inventory.get_cursor();

if stack.is_empty() {
return;
}

let Some(nbt) = stack.nbt.as_ref() else {
return;
};

let Some(handler) = nbt.get("Handler") else {
return;
};

let nbt::Value::Long(id) = handler else {
return;
};

let id: u64 = bytemuck::cast(*id);

let handler = world.entity_from_id(id);

handler.try_get::<&Handler>(|handler| {
let on_interact = &handler.on_click;
on_interact.trigger(query, hand);
});
});
});
}
}
18 changes: 9 additions & 9 deletions crates/hyperion-permission/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
cargo-features = ["edition2024"]

[dependencies]
anyhow.workspace = true
clap.workspace = true
flecs_ecs.workspace = true
heed.workspace = true
hyperion.workspace = true
num-derive.workspace = true
num-traits.workspace = true
tracing.workspace = true
uuid.workspace = true
anyhow = {workspace = true}
clap = {workspace = true}
flecs_ecs = {workspace = true}
heed = {workspace = true}
hyperion = {workspace = true}
num-derive = {workspace = true}
num-traits = {workspace = true}
tracing = {workspace = true}
uuid = {workspace = true}

[lints]
workspace = true
Expand Down
Loading

0 comments on commit 866e8fe

Please sign in to comment.