iceiix / stevenarella

Multi-protocol Minecraft-compatible client written in Rust
Apache License 2.0
1.45k stars 59 forks source link

Add the ability to break blocks #731

Closed nathanruiz closed 1 year ago

nathanruiz commented 2 years ago

I've added basic support for breaking blocks. At the moment, it can calculate the time required to mine each block for each tool with these exceptions:

I haven't added any animation yet since I wasn't too sure where to get started with that. I've also left out the ability to use the tool in the players hand, since I'm not sure how to detect that. I figured this would be a good starting position to build on.

iceiix commented 1 year ago

Thank you for this contribution @nathanruiz! This looks good, been wanting this feature for a while. Having some trouble testing it though. Trying on a 1.7.10 server, attempting to mine gravel, crashes for me:

Send start dig packet DiggingState { block: Gravel, position: <-877,68,1018>, face: Up, start: Instant { t: 174824505069784 }, finished: false } thread 'main' panicked at 'bad packet internal id 0x2f in Serverbound Play', protocol/src/protocol/versions/v1_7_10.rs:1:1

Looking for DiggingState I see it is defined as a struct in src/entity/mod.rs. Should it be defined as a packet in protocol too, or is another packet (PlayerDigging?) meant to be sent?

nathanruiz commented 1 year ago

I wasn't too sure how to give the ecs system the ability to send packets. I ended up using a VecDeque of PlayerDigging packets to pass packets I wanted to send back to the Server instance, which would then send them. Thinking about it a bit more, it would probably be better to somehow pass the connect around so that it can send the packet directly.

I think the crash was caused by me sending the wrong packet, since for older versions there are different variants of the PlayerDigging packets that should be sent instead. I'll try to find a way to pass the connection to the system so that it can send the correct packet directly.

nathanruiz commented 1 year ago

I've made a few changed to fix these issues:

iceiix commented 1 year ago

Testing latest changes on a 1.7.10 server, works great:

https://user-images.githubusercontent.com/43691553/198886670-11658075-5a7e-4fe3-b507-736a9e1395f6.mov

able to click and hold to dig, messages logged:

[entity/systems.rs:316][DEBUG] Send start dig packet DiggingState { block: Sand { red: false }, position: <-23,71,159>, face: Up, start: Instant { t: 107369536766216 }, finished: false }
[entity/systems.rs:317][DEBUG] Send cancel dig packet DiggingState { block: Sand { red: false }, position: <-23,71,159>, face: Up, start: Instant { t: 107369536766216 }, finished: false }

and the official client can observe the block break animation, and once it completes the block breaks, visible in our client. One limitation (as you noted) is the lack of displaying the block breaking animation in this client, but I agree this is a pretty good start.

iceiix commented 1 year ago

One more change: formatting in the steven_blocks crate. cargo fmt --all reports various small formatting changes (may have been missed since cargo fmt only defaults to the main crate):

% git diff|diffstat
 lib.rs |  202 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------
 1 file changed, 152 insertions(+), 50 deletions(-)
nathanruiz commented 1 year ago

I've fixed up that formatting issue

iceiix commented 1 year ago

Thanks @nathanruiz, merged! Also invited you as a collaborator if you're interested