Skip to content

Commit

Permalink
feat: plugin channel API
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgazelka committed Dec 19, 2024
1 parent ace0cbf commit a7b22eb
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 10 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ members = [
'crates/hyperion-nerd-font',
'crates/hyperion-palette',
'crates/hyperion-permission',
'crates/hyperion-plugin-channel',
'crates/hyperion-proto',
'crates/hyperion-proxy',
'crates/hyperion-rank-tree',
Expand Down Expand Up @@ -183,6 +184,9 @@ path = 'crates/hyperion-palette'
[workspace.dependencies.hyperion-permission]
path = 'crates/hyperion-permission'

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

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

Expand Down
4 changes: 2 additions & 2 deletions crates/hyperion-clap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use flecs_ecs::{
use hyperion::{
net::{Compose, ConnectionId, DataBundle, agnostic},
simulation::{IgnMap, command::get_root_command_entity, handlers::PacketSwitchQuery},
storage::{CommandCompletionRequest, EventFn},
storage::{CommandCompletionRequest, BoxedEventFn},
};
pub use hyperion_clap_macros::CommandPermission;
pub use hyperion_command;
Expand Down Expand Up @@ -111,7 +111,7 @@ pub trait MinecraftCommand: Parser + CommandPermission {
};
};

let on_tab_complete: EventFn<CommandCompletionRequest<'static>> = Box::new(
let on_tab_complete: BoxedEventFn<CommandCompletionRequest<'static>> = Box::new(
|packet_switch_query: &mut PacketSwitchQuery<'_>,
completion: &CommandCompletionRequest<'_>| {
let full_query = completion.query;
Expand Down
4 changes: 2 additions & 2 deletions crates/hyperion-command/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use flecs_ecs::{
macros::Component,
prelude::Module,
};
use hyperion::storage::{CommandCompletionRequest, EventFn};
use hyperion::storage::{CommandCompletionRequest, BoxedEventFn};
use indexmap::IndexMap;

pub struct CommandHandler {
pub on_execute: fn(input: &str, system: EntityView<'_>, caller: Entity),
pub on_tab_complete: EventFn<CommandCompletionRequest<'static>>,
pub on_tab_complete: BoxedEventFn<CommandCompletionRequest<'static>>,
pub has_permissions: fn(world: &World, caller: Entity) -> bool,
}

Expand Down
4 changes: 2 additions & 2 deletions crates/hyperion-item/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use flecs_ecs::{
macros::Component,
prelude::Module,
};
use hyperion::storage::{EventFn, GlobalEventHandlers, InteractEvent};
use hyperion::storage::{BoxedEventFn, GlobalEventHandlers, InteractEvent};
use valence_protocol::nbt;

pub mod builder;
Expand All @@ -14,7 +14,7 @@ pub struct ItemModule;

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

impl Module for ItemModule {
Expand Down
1 change: 1 addition & 0 deletions crates/hyperion-plugin-channel/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
14 changes: 14 additions & 0 deletions crates/hyperion-plugin-channel/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "hyperion-plugin-channel"
version.workspace = true
edition.workspace = true
authors = ["Andrew Gazelka <[email protected]>"]
readme = "README.md"
publish = false

[dependencies]
flecs_ecs.workspace = true
hyperion.workspace = true

[lints]
workspace = true
1 change: 1 addition & 0 deletions crates/hyperion-plugin-channel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# hyperion-plugin-channel
90 changes: 90 additions & 0 deletions crates/hyperion-plugin-channel/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use std::collections::HashMap;

use flecs_ecs::{
core::{QueryBuilderImpl, SystemAPI, TermBuilderImpl, World},
macros::{Component, system},
prelude::Module,
};
use hyperion::{
HyperionCore,
simulation::event::PluginMessage,
storage::{BoxedEventFn, EventFn, EventQueue},
valence_ident::Ident,
};

#[derive(Component)]
struct PluginChannelModule;

#[derive(Component, Default)]
struct PluginChannelRegistry {
registry: HashMap<Ident<String>, BoxedEventFn<[u8]>>,
}

impl PluginChannelRegistry {
pub fn register(&mut self, name: impl Into<Ident<String>>, handler: impl EventFn<[u8]>) {
let name = name.into();
self.registry.insert(name, EventFn::boxed(handler));
}
}

impl Module for PluginChannelModule {
fn module(world: &World) {
world.import::<HyperionCore>();

world.component::<PluginChannelRegistry>();
world.add::<PluginChannelRegistry>();

system!(
"process-plugin-messages",
world,
&mut EventQueue<PluginMessage<'static>>($),
&PluginChannelRegistry($)
)
.each_iter(|it, _, (queue, registry)| {
for PluginMessage { channel, data } in queue.drain() {
let Some(handler) = registry.registry.get(channel) else {
continue;
};

handler(todo!(), data);
}
});
// world.get::<&mut EventQueue<PluginMessage<'static>>>(move |queue|
// for PluginMessage { channel, data } in queue.drain() {

// }
// });
}
}

#[cfg(test)]
mod tests {
use flecs_ecs::core::WorldGet;
use hyperion::{
valence_ident::ident,
valence_protocol::{RawBytes, packets::play},
};

use super::*;

#[test]
fn test_echo() {
let world = World::new();
world.import::<PluginChannelModule>();

world.get::<&mut PluginChannelRegistry>(|registry| {
registry.register(ident!("hyperion:echo"), |query, data| {
let data = RawBytes::from(data);
let response_packet = play::CustomPayloadS2c {
channel: ident!("hyperion:echo").into(),
data: data.into(),
};

query
.compose
.unicast(&response_packet, query.io_ref, query.system)
.unwrap();
});
});
}
}
4 changes: 2 additions & 2 deletions crates/hyperion-rank-tree/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use flecs_ecs::{
};
use hyperion::{
simulation::{Player, handlers::PacketSwitchQuery},
storage::{EventFn, InteractEvent},
storage::{BoxedEventFn, InteractEvent},
};

pub mod inventory;
Expand Down Expand Up @@ -66,7 +66,7 @@ impl Module for RankTree {
.component::<Player>()
.add_trait::<(flecs::With, Class)>();

let handler: EventFn<InteractEvent> = Box::new(|query: &mut PacketSwitchQuery<'_>, _| {
let handler: BoxedEventFn<InteractEvent> = Box::new(|query: &mut PacketSwitchQuery<'_>, _| {
let cursor = query.inventory.get_cursor();
tracing::debug!("clicked {cursor:?}");
});
Expand Down
16 changes: 14 additions & 2 deletions crates/hyperion/src/storage/event/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@ use valence_protocol::{

use crate::simulation::handlers::PacketSwitchQuery;

pub type EventFn<T> = Box<dyn Fn(&mut PacketSwitchQuery<'_>, &T) + 'static + Send + Sync>;
pub type BoxedEventFn<T> = Box<dyn Fn(&mut PacketSwitchQuery<'_>, &T) + 'static + Send + Sync>;

pub trait EventFn<T: ?Sized>:
Fn(&mut PacketSwitchQuery<'_>, &T) + 'static + Send + Sync + Sized
{
fn boxed(input: Self) -> BoxedEventFn<T> {
Box::new(input)
}
}
impl<T: ?Sized, F> EventFn<T> for F where
F: Fn(&mut PacketSwitchQuery<'_>, &T) + 'static + Send + Sync
{
}

pub struct CommandCompletionRequest<'a> {
pub query: &'a str,
Expand Down Expand Up @@ -73,7 +85,7 @@ pub struct GlobalEventHandlers {
}

pub struct EventHandlers<T> {
handlers: Vec<EventFn<T>>,
handlers: Vec<BoxedEventFn<T>>,
}

impl<T> Default for EventHandlers<T> {
Expand Down

0 comments on commit a7b22eb

Please sign in to comment.