@@ -2,20 +2,21 @@ use std::borrow::Cow;
2
2
3
3
use flecs_ecs:: prelude:: * ;
4
4
use hyperion:: {
5
+ chat,
5
6
egress:: player_join:: { PlayerListActions , PlayerListEntry , PlayerListS2c } ,
6
7
net:: { Compose , NetworkStreamRef } ,
7
8
simulation:: {
8
9
blocks:: Blocks ,
9
- command:: { add_command, get_root_command, Command , Parser } ,
10
- event, Health , InGameName , Position , Uuid ,
10
+ command:: { add_command, cmd_with , get_root_command, Command , Parser } ,
11
+ event, Health , IgnMap , InGameName , Position , Uuid ,
11
12
} ,
12
13
storage:: EventQueue ,
13
14
system_registry:: SystemId ,
14
15
uuid,
16
+ valence_ident:: ident,
15
17
valence_protocol:: {
16
18
self ,
17
19
game_mode:: OptGameMode ,
18
- ident,
19
20
math:: IVec3 ,
20
21
nbt,
21
22
packets:: play:: {
@@ -43,9 +44,26 @@ pub fn add_to_tree(world: &World) {
43
44
// add to tree
44
45
add_command ( world, Command :: literal ( "team" ) , root_command) ;
45
46
add_command ( world, Command :: literal ( "zombie" ) , root_command) ;
46
- add_command ( world, Command :: literal ( "give" ) , root_command) ;
47
47
add_command ( world, Command :: literal ( "upgrade" ) , root_command) ;
48
48
49
+ cmd_with ( world, "give" , |scope| {
50
+ scope. argument_with (
51
+ "player" ,
52
+ Parser :: Entity {
53
+ single : true ,
54
+ only_players : true ,
55
+ } ,
56
+ |scope| {
57
+ scope. argument_with ( "" , Parser :: ItemStack , |scope| {
58
+ scope. argument ( "count" , Parser :: Integer {
59
+ min : Some ( 1 ) ,
60
+ max : None ,
61
+ } ) ;
62
+ } ) ;
63
+ } ,
64
+ ) ;
65
+ } ) ;
66
+
49
67
let speed = add_command ( world, Command :: literal ( "speed" ) , root_command) ;
50
68
add_command (
51
69
world,
@@ -101,6 +119,7 @@ struct CommandContext<'a> {
101
119
inventory : & ' a mut PlayerInventory ,
102
120
level : & ' a mut Level ,
103
121
health : & ' a mut Health ,
122
+ ign_map : & ' a IgnMap ,
104
123
}
105
124
106
125
fn process_command ( command : & ParsedCommand , context : & mut CommandContext < ' _ > ) {
@@ -109,7 +128,11 @@ fn process_command(command: &ParsedCommand, context: &mut CommandContext<'_>) {
109
128
ParsedCommand :: Team => handle_team_command ( context) ,
110
129
ParsedCommand :: Zombie => handle_zombie_command ( context) ,
111
130
ParsedCommand :: Dirt { x, y, z } => handle_dirt_command ( * x, * y, * z, context) ,
112
- ParsedCommand :: Give => handle_give_command ( context) ,
131
+ ParsedCommand :: Give {
132
+ username,
133
+ item,
134
+ count,
135
+ } => handle_give_command ( username, item, * count, context) ,
113
136
ParsedCommand :: Upgrade => handle_upgrade_command ( context) ,
114
137
ParsedCommand :: Stats ( stat, amount) => handle_stats ( * stat, * amount, context) ,
115
138
ParsedCommand :: Health ( amount) => handle_health_command ( * amount, context) ,
@@ -351,24 +374,39 @@ fn handle_stats(stat: Stat, amount: f32, context: &CommandContext<'_>) {
351
374
} ) ;
352
375
}
353
376
354
- fn handle_give_command ( context : & mut CommandContext < ' _ > ) {
355
- let mut blue_wool_nbt = nbt:: Compound :: new ( ) ;
377
+ fn handle_give_command ( username : & str , item_name : & str , count : i8 , context : & CommandContext < ' _ > ) {
378
+ let Some ( item) = ItemKind :: from_str ( item_name) else {
379
+ let packet = chat ! ( "Unknown item '{item_name:?}'" ) ;
380
+ context
381
+ . compose
382
+ . unicast ( & packet, context. stream , context. system_id , context. world )
383
+ . unwrap ( ) ;
384
+ return ;
385
+ } ;
386
+
387
+ let Some ( player) = context. ign_map . get ( username) else {
388
+ let chat = chat ! ( "Player {username} does not exist" ) ;
389
+ context
390
+ . compose
391
+ . unicast ( & chat, context. stream , context. system_id , context. world )
392
+ . unwrap ( ) ;
393
+ return ;
394
+ } ;
356
395
357
- let can_place_on = [
358
- "minecraft:stone" ,
359
- "minecraft:dirt" ,
360
- "minecraft:grass_block" ,
361
- "minecraft:blue_wool" ,
362
- ]
363
- . into_iter ( )
364
- . map ( std:: convert:: Into :: into)
365
- . collect ( ) ;
396
+ context
397
+ . world
398
+ . entity_from_id ( * player)
399
+ . get :: < & mut PlayerInventory > ( |inventory| {
400
+ inventory. try_add_item ( ItemStack :: new ( item, count, None ) ) ;
401
+ } ) ;
366
402
367
- blue_wool_nbt . insert ( "CanPlaceOn" , nbt :: List :: String ( can_place_on ) ) ;
403
+ let name = item . to_str ( ) ;
368
404
405
+ let packet = chat ! ( "Gave {count} [{name}] to {username}" ) ;
369
406
context
370
- . inventory
371
- . try_add_item ( ItemStack :: new ( ItemKind :: BlueWool , 4 , Some ( blue_wool_nbt) ) ) ;
407
+ . compose
408
+ . unicast ( & packet, context. stream , context. system_id , context. world )
409
+ . unwrap ( ) ;
372
410
}
373
411
374
412
fn handle_dirt_command ( x : i32 , y : i32 , z : i32 , context : & mut CommandContext < ' _ > ) {
@@ -603,9 +641,9 @@ impl Module for CommandModule {
603
641
604
642
let system_id = SystemId ( 8 ) ;
605
643
606
- system ! ( "handle_poc_events_player" , world, & Compose ( $) , & mut EventQueue <event:: Command <' _>>( $) , & mut Blocks ( $) )
644
+ system ! ( "handle_poc_events_player" , world, & Compose ( $) , & mut EventQueue <event:: Command <' _>>( $) , & mut Blocks ( $) , & IgnMap ( $ ) )
607
645
. multi_threaded ( )
608
- . each_iter ( move |it : TableIter < ' _ , false > , _, ( compose, event_queue, mc) | {
646
+ . each_iter ( move |it : TableIter < ' _ , false > , _, ( compose, event_queue, mc, ign_map ) | {
609
647
let span = trace_span ! ( "handle_poc_events_player" ) ;
610
648
let _enter = span. enter ( ) ;
611
649
@@ -644,6 +682,7 @@ impl Module for CommandModule {
644
682
level,
645
683
health,
646
684
position,
685
+ ign_map,
647
686
} ;
648
687
process_command ( & command, & mut context) ;
649
688
} ,
0 commit comments