Skip to content

Commit a5620ae

Browse files
committed
fix(tag): deny placing torches
1 parent e22d7a8 commit a5620ae

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

crates/hyperion/src/simulation/blocks/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{future::Future, ops::Try, path::Path, pin::Pin, sync::Arc};
55
use anyhow::Context;
66
use bytes::Bytes;
77
use chunk::Column;
8+
use derive_more::Constructor;
89
use flecs_ecs::{
910
core::{Entity, World, WorldGet},
1011
macros::Component,
@@ -41,6 +42,7 @@ pub enum GetChunk<'a> {
4142
Loading,
4243
}
4344

45+
#[derive(Constructor, Debug)]
4446
pub struct EntityAndSequence {
4547
pub entity: Entity,
4648
pub sequence: i32,

crates/hyperion/src/simulation/handlers.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,6 @@ pub fn player_interact_block(
442442
return Ok(());
443443
}
444444

445-
query.inventory.take_one_held();
446-
447445
query.events.push(
448446
event::PlaceBlock {
449447
position,

events/tag/src/module/block.rs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use flecs_ecs::{
99
prelude::Module,
1010
};
1111
use 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+
4656
pub 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

Comments
 (0)