Skip to content

Commit 0f41061

Browse files
committed
feat: players get blocks back after destroyed
1 parent 93a55c8 commit 0f41061

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

crates/hyperion-rank-tree/src/inventory.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@ impl Team {
1818
}
1919
}
2020

21+
pub const MAIN_SLOT: u16 = 0;
22+
pub const PICKAXE_SLOT: u16 = 1;
23+
pub const BLOCK_SLOT: u16 = 2;
24+
pub const UPGRADE_START_SLOT: u16 = 3;
25+
pub const GUI_SLOT: u16 = 8;
26+
2127
impl Rank {
2228
pub fn apply_inventory(self, team: Team, inventory: &mut PlayerInventory, world: &World) {
23-
const MAIN_SLOT: u16 = 0;
24-
const PICKAXE_SLOT: u16 = 1;
25-
const BUILD_SLOT: u16 = 2;
26-
27-
const UPGRADE_START_SLOT: u16 = 3;
28-
29-
const GUI_SLOT: u16 = 8;
30-
3129
let upgrade_not_available = ItemBuilder::new(ItemKind::GrayDye);
3230

3331
inventory.clear();
@@ -63,7 +61,7 @@ impl Rank {
6361
inventory.set_hotbar(PICKAXE_SLOT, default_pickaxe);
6462

6563
let default_build_item = team.build_item().count(16).build();
66-
inventory.set_hotbar(BUILD_SLOT, default_build_item);
64+
inventory.set_hotbar(BLOCK_SLOT, default_build_item);
6765

6866
match self {
6967
Self::Stick => {

events/proof-of-concept/src/module/block.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
};
55

66
use flecs_ecs::{
7-
core::{EntityViewGet, QueryBuilderImpl, SystemAPI, TableIter, TermBuilderImpl, World},
7+
core::{Entity, EntityViewGet, QueryBuilderImpl, SystemAPI, TableIter, TermBuilderImpl, World},
88
macros::{Component, system},
99
prelude::Module,
1010
};
@@ -27,6 +27,8 @@ use hyperion::{
2727
text::IntoText,
2828
},
2929
};
30+
use hyperion_inventory::PlayerInventory;
31+
use hyperion_rank_tree::inventory;
3032
use hyperion_scheduled::Scheduled;
3133
use tracing::{error, info_span};
3234

@@ -41,9 +43,14 @@ pub struct SetLevel {
4143
pub stage: u8,
4244
}
4345

46+
pub struct DestroyValue {
47+
pub position: IVec3,
48+
pub from: Entity,
49+
}
50+
4451
#[derive(Default, Component)]
4552
pub struct PendingDestruction {
46-
pub destroy_at: Scheduled<Instant, IVec3>,
53+
pub destroy_at: Scheduled<Instant, DestroyValue>,
4754
pub set_level_at: Scheduled<Instant, SetLevel>,
4855
}
4956

@@ -56,6 +63,7 @@ impl Module for BlockModule {
5663
world.set(PendingDestruction::default());
5764

5865
system!("handle_pending_air", world, &mut PendingDestruction($), &mut Blocks($), &Compose($))
66+
.write::<PlayerInventory>()
5967
.multi_threaded()
6068
.each_iter(
6169
move |it: TableIter<'_, false>,
@@ -87,9 +95,9 @@ impl Module for BlockModule {
8795
.send(&world)
8896
.unwrap();
8997
}
90-
for position in pending_air.destroy_at.pop_until(&now) {
98+
for destroy in pending_air.destroy_at.pop_until(&now) {
9199
// Play particle effect for block destruction
92-
let center_block = position.as_dvec3() + DVec3::splat(0.5);
100+
let center_block = destroy.position.as_dvec3() + DVec3::splat(0.5);
93101

94102
let particle_packet = play::ParticleS2c {
95103
particle: Cow::Owned(Particle::Explosion),
@@ -116,7 +124,18 @@ impl Module for BlockModule {
116124
.send(&world)
117125
.unwrap();
118126

119-
blocks.set_block(position, BlockState::AIR).unwrap();
127+
destroy.from
128+
.entity_view(world)
129+
.get::<&mut PlayerInventory>(|inventory| {
130+
let stack = inventory
131+
.get_hand_slot_mut(inventory::BLOCK_SLOT)
132+
.unwrap();
133+
134+
stack.count = stack.count.saturating_add(1);
135+
});
136+
137+
138+
blocks.set_block(destroy.position, BlockState::AIR).unwrap();
120139
}
121140
},
122141
);
@@ -186,7 +205,6 @@ impl Module for BlockModule {
186205
};
187206

188207

189-
190208
let from = event.from;
191209
let from_entity = world.entity_from_id(from);
192210
from_entity.get::<(&NetworkStreamRef, &mut Xp)>(|(&net, xp)| {
@@ -220,7 +238,6 @@ impl Module for BlockModule {
220238
});
221239

222240
system!("handle_placed_blocks", world, &mut Blocks($), &mut EventQueue<event::PlaceBlock>($), &mut PendingDestruction($))
223-
.multi_threaded()
224241
.each(move |(mc, event_queue, pending_air): (&mut Blocks, &mut EventQueue<event::PlaceBlock>, &mut PendingDestruction)| {
225242
let span = info_span!("handle_placed_blocks");
226243
let _enter = span.enter();
@@ -229,7 +246,12 @@ impl Module for BlockModule {
229246

230247
mc.set_block(position, event.block).unwrap();
231248

232-
pending_air.destroy_at.schedule(Instant::now() + TOTAL_DESTRUCTION_TIME, position);
249+
let destroy = DestroyValue {
250+
position,
251+
from: event.from,
252+
};
253+
254+
pending_air.destroy_at.schedule(Instant::now() + TOTAL_DESTRUCTION_TIME, destroy);
233255

234256
{
235257
let sequence = fastrand::i32(..);

0 commit comments

Comments
 (0)