@@ -9,7 +9,7 @@ use flecs_ecs::{
99 prelude:: Module ,
1010} ;
1111use hyperion:: {
12- BlockKind ,
12+ BlockKind , chat ,
1313 net:: { Compose , NetworkStreamRef , agnostic} ,
1414 simulation:: {
1515 Xp ,
@@ -43,6 +43,16 @@ pub struct SetLevel {
4343 pub stage : u8 ,
4444}
4545
46+ impl SetLevel {
47+ pub fn new ( position : IVec3 , stage : u8 ) -> Self {
48+ Self {
49+ position,
50+ sequence : fastrand:: i32 ( ..) ,
51+ stage,
52+ }
53+ }
54+ }
55+
4656pub struct DestroyValue {
4757 pub position : IVec3 ,
4858 pub from : Entity ,
@@ -239,45 +249,58 @@ impl Module for BlockModule {
239249 }
240250 } ) ;
241251
242- system ! ( "handle_placed_blocks" , world, & mut Blocks ( $) , & mut EventQueue <event:: PlaceBlock >( $) , & mut PendingDestruction ( $) )
243- . each_iter ( move |it, _, ( mc, event_queue, pending_air) : ( & mut Blocks , & mut EventQueue < event:: PlaceBlock > , & mut PendingDestruction ) | {
252+ system ! ( "handle_placed_blocks" , world, & mut Blocks ( $) , & mut EventQueue <event:: PlaceBlock >( $) , & mut PendingDestruction ( $) , & Compose ( $ ) )
253+ . each_iter ( move |it, _, ( mc, event_queue, pending_air, compose ) : ( & mut Blocks , & mut EventQueue < event:: PlaceBlock > , & mut PendingDestruction , & Compose ) | {
244254 let world = it. world ( ) ;
245255 let span = info_span ! ( "handle_placed_blocks" ) ;
246256 let _enter = span. enter ( ) ;
247- for event in event_queue. drain ( ) {
248- let position = event. position ;
257+ for event:: PlaceBlock { position, block, from, sequence } in event_queue. drain ( ) {
258+ if block. collision_shapes ( ) . is_empty ( ) {
259+ mc. to_confirm . push ( EntityAndSequence :: new ( from, sequence) ) ;
249260
250- mc. set_block ( position, event. block ) . unwrap ( ) ;
261+ from. entity_view ( world) . get :: < ( & mut PlayerInventory , & NetworkStreamRef ) > ( |( inventory, stream) | {
262+ // so we send update to player
263+ let _ = inventory. get_cursor_mut ( ) ;
251264
252- event. from . entity_view ( world) . get :: < & mut MainBlockCount > ( |main_block_count| {
265+ let msg = chat ! ( "§cYou can't place this block" ) ;
266+
267+ compose. unicast ( & msg, * stream, SystemId ( 8 ) , & world) . unwrap ( ) ;
268+ } ) ;
269+
270+ continue ;
271+ }
272+
273+ mc. set_block ( position, block) . unwrap ( ) ;
274+
275+ from. entity_view ( world) . get :: < & mut PlayerInventory > ( |inventory| {
276+ inventory. take_one_held ( ) ;
277+ } ) ;
278+
279+ from. entity_view ( world) . get :: < & mut MainBlockCount > ( |main_block_count| {
253280 * * main_block_count = ( * * main_block_count - 1 ) . max ( 0 ) ;
254281 } ) ;
255282
256283 let destroy = DestroyValue {
257284 position,
258- from : event . from ,
285+ from,
259286 } ;
260287
288+
261289 pending_air. destroy_at . schedule ( Instant :: now ( ) + TOTAL_DESTRUCTION_TIME , destroy) ;
262290
263291 {
264- let sequence = fastrand:: i32 ( ..) ;
265292 // Schedule destruction stages 0 through 9
266293 for stage in 0_u8 ..=10 { // 10 represents no animation
267294 let delay = TOTAL_DESTRUCTION_TIME / 10 * u32:: from ( stage) ;
268295 pending_air. set_level_at . schedule (
269296 Instant :: now ( ) + delay,
270- SetLevel {
271- position,
272- sequence,
273- stage,
274- } ,
297+ SetLevel :: new ( position, stage) ,
275298 ) ;
276299 }
277300 }
278301 mc. to_confirm . push ( EntityAndSequence {
279- entity : event . from ,
280- sequence : event . sequence ,
302+ entity : from,
303+ sequence,
281304 } ) ;
282305 }
283306 } ) ;
0 commit comments