-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement item click handling system (#574)
- Loading branch information
1 parent
bf8a9e4
commit 866e8fe
Showing
36 changed files
with
461 additions
and
444 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# hyperion-item |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.