Skip to content
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

Add HeldItem::set_slot #525

Merged
merged 4 commits into from
Sep 8, 2023
Merged

Conversation

AviiNL
Copy link
Contributor

@AviiNL AviiNL commented Sep 8, 2023

Objective

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

Copy link
Contributor

@Bafbi Bafbi left a comment

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 Events rework #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
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
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
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
Copy link
Contributor Author

AviiNL commented Sep 8, 2023

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.

@@ -397,6 +399,8 @@ impl ClientInventoryState {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Component, Deref)]
pub struct HeldItem {
held_item_slot: u16,
#[deref(ignore)]
changed: bool,
Copy link
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
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
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
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
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
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
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.

@@ -659,6 +675,20 @@ fn update_player_inventories(
}
}

/// 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
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
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.

Copy link
Collaborator

@dyc3 dyc3 left a comment

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

crates/valence_inventory/src/lib.rs Outdated Show resolved Hide resolved
@dyc3 dyc3 enabled auto-merge (squash) September 8, 2023 18:38
@dyc3 dyc3 merged commit c3d112d into valence-rs:main Sep 8, 2023
10 checks passed
@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