Skip to content

Commit

Permalink
fix doctest + rename set_helmet -> set_head
Browse files Browse the repository at this point in the history
  • Loading branch information
maxomatic458 committed Oct 17, 2024
1 parent 5d97106 commit 442eecd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 45 deletions.
63 changes: 22 additions & 41 deletions crates/valence_equipment/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,36 @@ component to a entity (currently only Players).
## Example

```rust
use valence::prelude::*;
use bevy_ecs::prelude::*;
use valence_equipment::*;

// Spawn a player with full armor, a sword and a shield.
fn init_clients(
mut commands: Commands,
use valence_server::{
ItemStack, ItemKind,
entity::player::PlayerEntity,
};
// Add equipment to players when they are added to the world.
fn init_equipment(
mut clients: Query<
&mut Equipment,
(
Entity,
&mut Position,
&mut EntityLayerId,
&mut VisibleChunkLayer,
&mut VisibleEntityLayers,
&mut GameMode,
Added<Equipment>,
With<PlayerEntity>,
),
Added<Client>,
>,
layers: Query<Entity, (With<ChunkLayer>, With<EntityLayer>)>,
) {
for (
player,
mut pos,
mut layer_id,
mut visible_chunk_layer,
mut visible_entity_layers,
mut game_mode,
) in &mut clients
for mut equipment in &mut clients
{
let layer = layers.single();

pos.0 = [0.0, f64::from(SPAWN_Y) + 1.0, 0.0].into();
layer_id.0 = layer;
visible_chunk_layer.0 = layer;
visible_entity_layers.0.insert(layer);
*game_mode = GameMode::Survival;

let equipment = Equipment::new(
ItemStack::new(ItemKind::DiamondSword, 1, None), // Main hand
ItemStack::new(ItemKind::Shield, 1, None), // Off hand
ItemStack::new(ItemKind::DiamondBoots, 1, None), // Feet
ItemStack::new(ItemKind::DiamondLeggings, 1, None), // Legs
ItemStack::new(ItemKind::DiamondChestplate, 1, None), // Chest
ItemStack::new(ItemKind::DiamondHelmet, 1, None), // Head
);

commands.entity(player).insert(equipment); // Add the equipment to the player

commands.entity(player).insert(EquipmentInventorySync); // Sync the equipment with the player's inventory. This is not required if you want to only show equipment for other players.
equipment.set_main_hand(ItemStack::new(ItemKind::DiamondSword, 1, None));
equipment.set_off_hand(ItemStack::new(ItemKind::Shield, 1, None));
equipment.set_feet(ItemStack::new(ItemKind::DiamondBoots, 1, None));
equipment.set_legs(ItemStack::new(ItemKind::DiamondLeggings, 1, None));
equipment.set_chest(ItemStack::new(ItemKind::DiamondChestplate, 1, None));
equipment.set_head(ItemStack::new(ItemKind::DiamondHelmet, 1, None));
}
}
```

### See also

Examples related to inventories in the `valence/examples/` directory:
- `equipment`

2 changes: 1 addition & 1 deletion crates/valence_equipment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl Equipment {
self.set_slot(Self::CHEST_IDX, item);
}

pub fn set_helmet(&mut self, item: ItemStack) {
pub fn set_head(&mut self, item: ItemStack) {
self.set_slot(Self::HEAD_IDX, item);
}

Expand Down
6 changes: 3 additions & 3 deletions src/tests/equipment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn test_multiple_entities() {
.expect("could not get entity equipment");

equipment.set_chest(ItemStack::new(ItemKind::DiamondChestplate, 1, None));
equipment.set_helmet(ItemStack::new(ItemKind::DiamondHelmet, 1, None));
equipment.set_head(ItemStack::new(ItemKind::DiamondHelmet, 1, None));

app.update();

Expand All @@ -85,7 +85,7 @@ fn test_multiple_entities() {

// Set the zombie's equipment to the same items
equipment.set_chest(ItemStack::new(ItemKind::DiamondChestplate, 1, None));
equipment.set_helmet(ItemStack::new(ItemKind::DiamondHelmet, 1, None));
equipment.set_head(ItemStack::new(ItemKind::DiamondHelmet, 1, None));

app.update();

Expand Down Expand Up @@ -127,7 +127,7 @@ fn test_update_on_load_entity() {
.expect("could not get entity equipment");

equipment.set_chest(ItemStack::new(ItemKind::DiamondChestplate, 1, None));
equipment.set_helmet(ItemStack::new(ItemKind::DiamondHelmet, 1, None));
equipment.set_head(ItemStack::new(ItemKind::DiamondHelmet, 1, None));

app.update();

Expand Down

0 comments on commit 442eecd

Please sign in to comment.