Skip to content

Add HeldItem::set_slot#525

Merged
dyc3 merged 4 commits into
valence-rs:mainfrom
AviiNL:set_slot_on_held_item
Sep 8, 2023
Merged

Add HeldItem::set_slot#525
dyc3 merged 4 commits into
valence-rs:mainfrom
AviiNL:set_slot_on_held_item

Conversation

@AviiNL

@AviiNL AviiNL commented Sep 8, 2023

Copy link
Copy Markdown
Contributor

Objective

This PR adds functionality to allow the server to change the users selected hotbar slot using HeldItem::set_slot(36..44)

@Bafbi Bafbi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pattern you'r using made use of a second change detection variable, for the fact that you don't want to send the packet when it's the client that update it I suppose.
But I think it would be better to use the events system to solve this problem (like what was use for ablilitites )
I think it's a better pattern because

  • events (when #487 will be usable) will permit the dev to cancel the client behaviour.
  • keep the consistencies to not really use getter and setter for our component.

pub fn set_slot(&mut self, slot: u16) {
// temp
assert!(
(36..=44).contains(&slot),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The packet seems to be using 0..8 https://wiki.vg/Protocol#Set_Held_Item

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am aware that the packet uses 0..8, the reason why i'm not using it here is to keep it consistent with the slot() return value, which is 36..44, and changing that is out of scope of this PR imo and backwards-compat breaking. (see line 684 where it gets converted)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea right, weird, but not the scope of the pr

@AviiNL

AviiNL commented Sep 8, 2023

Copy link
Copy Markdown
Contributor Author

There are no events involved here, this isn't code that responds to some client request, how would I fire off an event from the set_slot fn without any knowledge of anything other than a number of the selected slot? This is a server-side-only thing where the server can force a client to have a specific hotbar slot selected.

Comment thread crates/valence_inventory/src/lib.rs Outdated
pub struct HeldItem {
held_item_slot: u16,
#[deref(ignore)]
changed: bool,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we use bevy's built-in change detection instead?
https://docs.rs/bevy/0.9.1/bevy/ecs/query/struct.Changed.html

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no because when the client change slot, we trigger one.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And that's where I wanted to go with my review, what I proposing is that the system, who handle the packet bypass the change detection, so the only way the user of the api know that something occurs would be the event send.
now you would'nt need a seconde variable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont really understand what needs to be bypassed, as i mentioned, this isnt triggered from something automatically, this function gives a developer the option to change a clients selected slot index. Imo, this is simple enough to use as-is, and when #487 gets finalized, this can be look at again if required?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm talking about the system that handle the c2s held_item packet, the things I don't like here is the fact that you need a double change detection

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i havent touched anything related to c2s in this pr, out of scope?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no because, how the c2s was implemented was making sense when you didn't care that the change detection was trigger, but now you care for your purpose.

Comment thread crates/valence_inventory/src/lib.rs Outdated

/// Handles the `HeldItem` component being changed on a client, which
/// indicates that the server has changed the selected hotbar slot.
fn update_player_selected_slot(mut clients: Query<(&mut Client, &mut HeldItem)>) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather use bevy change detection to detect when HeldItem is changed, and bypass the change detection when the c2s packet is received.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how am I so bad I explaining my thought when it was this easy.

@dyc3 dyc3 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just a nit

Comment thread crates/valence_inventory/src/lib.rs Outdated
Co-authored-by: Carson McManus <dyc3@users.noreply.github.com>
@dyc3 dyc3 enabled auto-merge (squash) September 8, 2023 18:38
@dyc3 dyc3 merged commit c3d112d into valence-rs:main Sep 8, 2023
@AviiNL AviiNL mentioned this pull request Oct 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants