Skip to content

Commit 82895ec

Browse files
committed
feat: add /perms command
1 parent 9eb8a79 commit 82895ec

File tree

4 files changed

+92
-20
lines changed

4 files changed

+92
-20
lines changed

crates/hyperion-clap/src/lib.rs

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use flecs_ecs::{
77
};
88
use hyperion::{
99
net::{Compose, DataBundle, NetworkStreamRef, agnostic},
10-
simulation::{command::get_root_command_entity, handlers::PacketSwitchQuery},
10+
simulation::{IgnMap, command::get_root_command_entity, handlers::PacketSwitchQuery},
1111
storage::{CommandCompletionRequest, EventFn},
1212
system_registry::SystemId,
1313
};
@@ -268,8 +268,97 @@ pub enum GameMode {
268268
#[derive(Component)]
269269
pub struct ClapCommandModule;
270270

271+
#[derive(clap::Parser, Debug)]
272+
pub struct SetCommand {
273+
player: String,
274+
group: Group,
275+
}
276+
277+
#[derive(clap::Parser, Debug)]
278+
pub struct GetCommand {
279+
player: String,
280+
}
281+
282+
#[derive(Parser, CommandPermission, Debug)]
283+
#[command(name = "perms")]
284+
#[command_permission(group = "Normal")]
285+
pub enum PermissionCommand {
286+
Set(SetCommand),
287+
Get(GetCommand),
288+
}
289+
290+
impl MinecraftCommand for PermissionCommand {
291+
fn execute(self, world: &World, caller: Entity) {
292+
world.get::<&IgnMap>(|ign_map| {
293+
match self {
294+
Self::Set(cmd) => {
295+
// Handle setting permissions
296+
let Some(entity) = ign_map.get(cmd.player.as_str()) else {
297+
caller
298+
.entity_view(world)
299+
.get::<&NetworkStreamRef>(|stream| {
300+
let msg = format!("§c{} not found", cmd.player);
301+
let chat = hyperion::net::agnostic::chat(msg);
302+
world.get::<&Compose>(|compose| {
303+
compose.unicast(&chat, *stream, SystemId(8), world).unwrap();
304+
});
305+
});
306+
return;
307+
};
308+
309+
entity.entity_view(world).get::<&mut Group>(|group| {
310+
*group = cmd.group;
311+
caller
312+
.entity_view(world)
313+
.get::<&NetworkStreamRef>(|stream| {
314+
let msg = format!(
315+
"§b{}§r's group has been set to §e{:?}",
316+
cmd.player, cmd.group
317+
);
318+
let chat = hyperion::net::agnostic::chat(msg);
319+
world.get::<&Compose>(|compose| {
320+
compose.unicast(&chat, *stream, SystemId(8), world).unwrap();
321+
});
322+
});
323+
});
324+
}
325+
Self::Get(cmd) => {
326+
let Some(entity) = ign_map.get(cmd.player.as_str()) else {
327+
caller
328+
.entity_view(world)
329+
.get::<&NetworkStreamRef>(|stream| {
330+
let msg = format!("§c{} not found", cmd.player);
331+
let chat = hyperion::net::agnostic::chat(msg);
332+
world.get::<&Compose>(|compose| {
333+
compose.unicast(&chat, *stream, SystemId(8), world).unwrap();
334+
});
335+
});
336+
return;
337+
};
338+
339+
entity.entity_view(world).get::<&Group>(|group| {
340+
caller
341+
.entity_view(world)
342+
.get::<&NetworkStreamRef>(|stream| {
343+
let msg = format!("§b{}§r's group is §e{:?}", cmd.player, group);
344+
let chat = hyperion::net::agnostic::chat(msg);
345+
world.get::<&Compose>(|compose| {
346+
compose.unicast(&chat, *stream, SystemId(8), world).unwrap();
347+
});
348+
});
349+
});
350+
}
351+
}
352+
});
353+
}
354+
}
355+
271356
impl Module for ClapCommandModule {
272357
fn module(world: &World) {
273358
world.import::<hyperion_command::CommandModule>();
359+
360+
world.get::<&mut CommandRegistry>(|registry| {
361+
PermissionCommand::register(registry, world);
362+
});
274363
}
275364
}

crates/hyperion-permission/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ num-traits = {workspace = true}
1111
tracing = {workspace = true}
1212
uuid = {workspace = true}
1313

14+
1415
[lints]
1516
workspace = true
1617

crates/hyperion-permission/src/lib.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,6 @@ pub enum Group {
3333
Admin,
3434
}
3535

36-
#[derive(clap::Parser, Debug)]
37-
pub struct SetCommand {
38-
player: String,
39-
group: Group,
40-
}
41-
42-
#[derive(clap::Parser, Debug)]
43-
pub struct GetCommand {
44-
player: String,
45-
}
46-
47-
#[derive(clap::Parser, Debug)]
48-
#[command(name = "perms")]
49-
pub enum PermissionCommand {
50-
Set(SetCommand),
51-
Get(GetCommand),
52-
}
53-
5436
// todo:
5537

5638
impl Module for PermissionModule {

events/tag/src/command/fly.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub struct Flight {
2121

2222
#[derive(Parser, CommandPermission, Debug)]
2323
#[command(name = "fly")]
24-
#[command_permission(group = "Normal")]
24+
#[command_permission(group = "Moderator")]
2525
pub struct FlyCommand;
2626

2727
impl MinecraftCommand for FlyCommand {

0 commit comments

Comments
 (0)