Skip to content

Commit e22d7a8

Browse files
committed
fix(tag): block count when changing classes
1 parent fc8e03b commit e22d7a8

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ pub const UPGRADE_START_SLOT: u16 = 3;
2525
pub const GUI_SLOT: u16 = 8;
2626

2727
impl Rank {
28-
pub fn apply_inventory(self, team: Team, inventory: &mut PlayerInventory, world: &World) {
28+
pub fn apply_inventory(
29+
self,
30+
team: Team,
31+
inventory: &mut PlayerInventory,
32+
world: &World,
33+
build_count: i8,
34+
) {
2935
let upgrade_not_available = ItemBuilder::new(ItemKind::GrayDye);
3036

3137
inventory.clear();
@@ -60,7 +66,7 @@ impl Rank {
6066
let default_pickaxe = ItemBuilder::new(ItemKind::WoodenPickaxe).build();
6167
inventory.set_hotbar(PICKAXE_SLOT, default_pickaxe);
6268

63-
let default_build_item = team.build_item().count(16).build();
69+
let default_build_item = team.build_item().count(build_count).build();
6470
inventory.set_hotbar(BLOCK_SLOT, default_build_item);
6571

6672
match self {

events/tag/src/command/rank.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ use hyperion_clap::{CommandPermission, MinecraftCommand};
2525
use hyperion_inventory::PlayerInventory;
2626
use hyperion_utils::EntityExt;
2727

28+
use crate::MainBlockCount;
29+
2830
#[derive(Parser, CommandPermission, Debug)]
2931
#[command(name = "class")]
3032
#[command_permission(group = "Normal")]
@@ -45,11 +47,12 @@ impl MinecraftCommand for ClassCommand {
4547
&Position,
4648
&Yaw,
4749
&Pitch,
50+
&MainBlockCount,
4851
)>(
49-
|(stream, uuid, inventory, position, yaw, pitch)| {
52+
|(stream, uuid, inventory, position, yaw, pitch, main_block_count)| {
5053
inventory.clear();
5154

52-
rank.apply_inventory(team, inventory, world);
55+
rank.apply_inventory(team, inventory, world, **main_block_count);
5356

5457
let minecraft_id = caller.minecraft_id();
5558
let mut bundle = DataBundle::new(compose);

events/tag/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,23 @@ struct OreVeins {
4040
ores: gxhash::HashSet<IVec3>,
4141
}
4242

43+
#[derive(Component, Deref, DerefMut)]
44+
struct MainBlockCount(i8);
45+
46+
impl Default for MainBlockCount {
47+
fn default() -> Self {
48+
Self(16)
49+
}
50+
}
51+
4352
impl Module for ProofOfConceptModule {
4453
fn module(world: &World) {
54+
world.component::<MainBlockCount>();
55+
56+
world
57+
.component::<Player>()
58+
.add_trait::<(flecs::With, MainBlockCount)>();
59+
4560
world.component::<component::team::Team>();
4661
world.import::<hyperion_rank_tree::RankTree>();
4762

events/tag/src/module/block.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use hyperion_rank_tree::inventory;
3232
use hyperion_scheduled::Scheduled;
3333
use tracing::{error, info_span};
3434

35-
use crate::OreVeins;
35+
use crate::{MainBlockCount, OreVeins};
3636

3737
#[derive(Component)]
3838
pub struct BlockModule;
@@ -95,6 +95,7 @@ impl Module for BlockModule {
9595
.send(&world)
9696
.unwrap();
9797
}
98+
9899
for destroy in pending_air.destroy_at.pop_until(&now) {
99100
// Play particle effect for block destruction
100101
let center_block = destroy.position.as_dvec3() + DVec3::splat(0.5);
@@ -126,12 +127,13 @@ impl Module for BlockModule {
126127

127128
destroy.from
128129
.entity_view(world)
129-
.get::<&mut PlayerInventory>(|inventory| {
130+
.get::<(&mut PlayerInventory, &mut MainBlockCount)>(|(inventory, main_block_count)| {
130131
let stack = inventory
131132
.get_hand_slot_mut(inventory::BLOCK_SLOT)
132133
.unwrap();
133134

134135
stack.count = stack.count.saturating_add(1);
136+
**main_block_count = main_block_count.saturating_add(1);
135137
});
136138

137139

@@ -238,14 +240,19 @@ impl Module for BlockModule {
238240
});
239241

240242
system!("handle_placed_blocks", world, &mut Blocks($), &mut EventQueue<event::PlaceBlock>($), &mut PendingDestruction($))
241-
.each(move |(mc, event_queue, pending_air): (&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)| {
244+
let world = it.world();
242245
let span = info_span!("handle_placed_blocks");
243246
let _enter = span.enter();
244247
for event in event_queue.drain() {
245248
let position = event.position;
246249

247250
mc.set_block(position, event.block).unwrap();
248251

252+
event.from.entity_view(world).get::<&mut MainBlockCount>(|main_block_count| {
253+
**main_block_count = (**main_block_count - 1).max(0);
254+
});
255+
249256
let destroy = DestroyValue {
250257
position,
251258
from: event.from,

0 commit comments

Comments
 (0)