-
-
Notifications
You must be signed in to change notification settings - Fork 164
Add Equipment support #663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
8bfe3ed
add `valence_equipment`
maxomatic458 17c469d
redo event
maxomatic458 65396e2
remove logging
maxomatic458 d44b907
add entity (un)load event
maxomatic458 8b27d17
refactor + depgraph
maxomatic458 4ca80bc
add use equipment to lib.rs
maxomatic458 afc0f2a
remove unused valence_inventory dep
maxomatic458 c735887
remove inventory dep frfr
maxomatic458 c96634b
update depgraph
maxomatic458 849e116
add equipment & inventory syncing for players
maxomatic458 76b9664
only care about relevant inventory slot
maxomatic458 a7adbb1
update depgraph
maxomatic458 4570737
improvde docs + remove inventory feature from equipment
maxomatic458 5d97106
update depgraph
maxomatic458 442eecd
fix doctest + rename set_helmet -> set_head
maxomatic458 3924bd0
more tests + fix client changes
maxomatic458 59bea02
fix case where selected slot would not change, but held item would
maxomatic458 f371f44
remove link to private function
maxomatic458 86c22d1
remove comment
maxomatic458 717481d
Merge branch 'main' into equipment
maxomatic458 da87c74
add doc comment
maxomatic458 7a9ce5d
add doc comments
maxomatic458 8de9c05
fix link to inventory
maxomatic458 5336925
format
maxomatic458 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| [package] | ||
| name = "valence_equipment" | ||
| description = "Equipment support for Valence" | ||
| readme = "README.md" | ||
| version.workspace = true | ||
| edition.workspace = true | ||
| repository.workspace = true | ||
| documentation.workspace = true | ||
| license.workspace = true | ||
|
|
||
| [lints] | ||
| workspace = true | ||
|
|
||
| [dependencies] | ||
| bevy_app.workspace = true | ||
| bevy_ecs.workspace = true | ||
| derive_more.workspace = true | ||
| tracing.workspace = true | ||
| valence_server.workspace = true | ||
| valence_inventory.workspace = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # `valence_equipment` | ||
| Manages Minecraft's entity equipment (armor, held items) via the `Equipment` component. | ||
| By default this is separated from an entities `Inventory` (which means that changes are only visible to other players), but it can be synced by attaching the `EquipmentInventorySync` | ||
| component to a entity (currently only Players). | ||
|
|
||
| ## Example | ||
|
|
||
| ```rust | ||
| use bevy_ecs::prelude::*; | ||
| use valence_equipment::*; | ||
| 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, | ||
| ( | ||
| Added<Equipment>, | ||
| With<PlayerEntity>, | ||
| ), | ||
| >, | ||
| ) { | ||
| for mut equipment in &mut clients | ||
| { | ||
| 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` | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| use valence_inventory::player_inventory::PlayerInventory; | ||
| use valence_inventory::{HeldItem, Inventory, UpdateSelectedSlotEvent}; | ||
| use valence_server::entity::player::PlayerEntity; | ||
|
|
||
| use super::*; | ||
|
|
||
| /// This component will sync a player's [`Equipment`], which is visible to other | ||
| /// players, with the player [`Inventory`]. | ||
| #[derive(Debug, Default, Clone, Component)] | ||
| pub struct EquipmentInventorySync; | ||
|
|
||
| /// Syncs the player [`Equipment`] with the [`Inventory`]. | ||
| /// If a change in the player's inventory and in the equipment occurs in the | ||
| /// same tick, the equipment change has priority. | ||
| /// Note: This system only handles direct changes to the held item (not actual | ||
| /// changes from the client) see [`equipment_held_item_sync_from_client`] | ||
| pub(crate) fn equipment_inventory_sync( | ||
| mut clients: Query< | ||
| (&mut Equipment, &mut Inventory, &mut HeldItem), | ||
| ( | ||
| Or<(Changed<Equipment>, Changed<Inventory>, Changed<HeldItem>)>, | ||
| With<EquipmentInventorySync>, | ||
| With<PlayerEntity>, | ||
| ), | ||
| >, | ||
| ) { | ||
| for (mut equipment, mut inventory, held_item) in &mut clients { | ||
| // Equipment change has priority over held item changes | ||
| if equipment.changed & (1 << Equipment::MAIN_HAND_IDX) != 0 { | ||
| let item = equipment.main_hand().clone(); | ||
| inventory.set_slot(held_item.slot(), item); | ||
| } else { | ||
| // If we change the inventory (e.g by pickung up an item) | ||
| // then the HeldItem slot wont be changed | ||
|
|
||
| // This will only be called if we change the held item from valence, | ||
| // the client change is handled in `equipment_held_item_sync_from_client` | ||
| let item = inventory.slot(held_item.slot()).clone(); | ||
| equipment.set_main_hand(item); | ||
| } | ||
|
|
||
| let slots = [ | ||
| (Equipment::OFF_HAND_IDX, PlayerInventory::SLOT_OFFHAND), | ||
| (Equipment::HEAD_IDX, PlayerInventory::SLOT_HEAD), | ||
| (Equipment::CHEST_IDX, PlayerInventory::SLOT_CHEST), | ||
| (Equipment::LEGS_IDX, PlayerInventory::SLOT_LEGS), | ||
| (Equipment::FEET_IDX, PlayerInventory::SLOT_FEET), | ||
| ]; | ||
|
|
||
| // We cant rely on the changed bitfield of inventory here | ||
| // because that gets reset when the client changes the inventory | ||
|
|
||
| for (equipment_slot, inventory_slot) in slots { | ||
| // Equipment has priority over inventory changes | ||
| if equipment.changed & (1 << equipment_slot) != 0 { | ||
| let item = equipment.slot(equipment_slot).clone(); | ||
| inventory.set_slot(inventory_slot, item); | ||
| } else if inventory.is_changed() { | ||
| let item = inventory.slot(inventory_slot).clone(); | ||
| equipment.set_slot(equipment_slot, item); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Handles the case where the client changes the slot (the bevy change is | ||
| /// suppressed for this) | ||
| pub(crate) fn equipment_held_item_sync_from_client( | ||
| mut clients: Query<(&HeldItem, &Inventory, &mut Equipment), With<EquipmentInventorySync>>, | ||
| mut events: EventReader<UpdateSelectedSlotEvent>, | ||
| ) { | ||
| for event in events.read() { | ||
| let Ok((held_item, inventory, mut equipment)) = clients.get_mut(event.client) else { | ||
| continue; | ||
| }; | ||
|
|
||
| let item = inventory.slot(held_item.slot()).clone(); | ||
| equipment.set_main_hand(item); | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn on_attach_inventory_sync( | ||
| entities: Query<Option<&PlayerEntity>, (Added<EquipmentInventorySync>, With<Inventory>)>, | ||
| ) { | ||
| for entity in &entities { | ||
| if entity.is_none() { | ||
| tracing::warn!( | ||
| "EquipmentInventorySync attached to non-player entity, this will have no effect" | ||
| ); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.