Commit graph

280 commits

Author SHA1 Message Date
Ryan Johnson f4714cf255
Redesign packet processing and improve Client update procedure. (#146)
Closes #82
Closes #43
Closes #64

# Changes and Improvements
- Packet encoding/decoding happens within `Client` instead of being sent
over a channel first. This is better for performance and lays the
groundwork for #83.
- Reduce the amount of copying necessary by leveraging the `bytes` crate
and recent changes to `EncodePacket`. Performance is noticeably improved
with maximum players in the `rust-mc-bot` test going from 750 to 1050.
- Packet encoding/decoding code is decoupled from IO. This is easier to
understand and more suitable for a future protocol lib.
- Precise control over the number of bytes that are buffered for
sending/receiving. This is important for limiting maximum memory usage
correctly.
- "packet controllers" are introduced, which are convenient structures
for managing packet IO before and during the play state.
- `byte_channel` module is created to help implement the
`PlayPacketController`. This is essentially a channel of bytes
implemented with an `Arc<Mutex<BytesMut>>`.
- Error handling in the update procedure for clients was improved using
`anyhow::Result<()>` to exit as early as possible. The `client` module
is a bit cleaner as a result.
- The `LoginPlay` packet is always sent before all other play packets.
We no longer have to worry about the behavior of packets sent before
that packet. Most packet deferring performed currently can be
eliminated.
- The packet_inspector was rewritten in response to the above changes.
- Timeouts on IO operations behave better.

# Known Issues
- The packet_inspector now re-encodes packets rather than just decoding
them. This will cause problems when trying to use it with the vanilla
server because there are missing clientbound packets and other issues.
This will be fixed when the protocol module is moved to a separate
crate.
2022-11-01 03:11:51 -07:00
Ryan a20ed2ac21 Bump valence_nbt version 2022-10-30 15:26:47 -07:00
Gijs de Jong 7e24cf014c
Set length range for verify_token to (4, 16) (#143)
Notchian servers [ALWAYS](https://wiki.vg/Protocol#Encryption_Request)
have a `verify_token` length of 4.
This PR updates the range of the `BoundedArray` to support this.
2022-10-29 14:23:22 -07:00
Terminator e80bbc22b3
Avoid calling to_kind() in is_air() (#142)
This PR changes is_air() to not call to_kind() to increase performance.

Example flamegraphs of real usecase of parsing java chunks to
UnloadedChunk:

Before this change:

![flamegraph](https://user-images.githubusercontent.com/13693773/198851700-f455d055-a18d-495c-9989-110e3f569ad9.svg)



After this change:

![flamegraph](https://user-images.githubusercontent.com/13693773/198851673-01f2a6d8-1cb0-4ae7-b57b-6e47a38d9cee.svg)
2022-10-29 13:56:48 -07:00
Ryan Johnson 56ebcaf50d
Add Username<S> type (#132)
There are a number of places where usernames are passed around. Using this type ensures that the contained string is actually a valid username and not some other kind of string. For instance you can use it as a function argument to indicate that only valid usernames are accepted, or return it from a function to indicate that only valid usernames are produced.

This is analogous to the existing `Ident<S>` type.
2022-10-22 20:17:06 -07:00
Carson McManus bbbeb7ae28
don't bother marking the inventory as dirty when handling SetCreativeModeSlot (#134)
fixes #133

### Test plan

1. run building example
2. open inventory
3. place any item in hotbar
4. try to pick it up
5. see that it does not disappear
2022-10-22 18:36:17 -07:00
Ryan c683a0d94c Add warning about missing "plains" biome 2022-10-22 14:01:25 -07:00
Ryan Johnson b08a0a6845
Uncheck proxy support in README.md
Unchecking this for now since there is a bug preventing it from working fully.
2022-10-21 20:00:19 -07:00
Tert0 c707ed1d04
Proxy Support (#48)
This PR adds Proxy Support.

Closes #39

Co-authored-by: Ryan Johnson <ryanj00a@gmail.com>
2022-10-21 19:50:13 -07:00
Ryan Johnson 4253928b4d
Redesign SignedPlayerTextures (#130)
Combines `PlayerTextures` and `SignedPlayerTextures` into one type. The
skin URL is now non-optional because it appears to always be present.
2022-10-21 19:10:37 -07:00
Ryan Johnson f4d36554d7
Make replaceable blocks place correctly in the building example. (#129)
Closes #114

Note that this is a band-aid solution to a more general problem. In the
future, there should be a function in the `block` module to handle all
these placement rules.
2022-10-21 04:55:15 -07:00
Ryan Johnson ce457a3d20
Add prelude module to valence. (#128)
This removes boilerplate code in the examples and reduces friction when getting started.
2022-10-21 04:54:32 -07:00
EmperialDev 4a3ee77858
Added sequence where it was missing (#127) 2022-10-20 12:21:05 -07:00
Ryan Johnson 101aa3721b
Add code of conduct (#126) 2022-10-19 18:07:43 -07:00
Ryan Johnson 71d82c5330
Add encoded_len method to Encode trait. (#125)
This allows packets to calculate their exact length up front.
This isn't currently tested or being used for anything, but that will come in later changes.
2022-10-19 01:52:02 -07:00
Ryan 9b8fbc5d82 Better "encryption enabled" message in packet_inspector 2022-10-19 01:42:32 -07:00
Terminator 03e89adeb8
(valence_nbt) Add reverse From<Value> for Option<T> (#122)
This way we can use generic bounds to specify the return type and get `None` if the value does not match the type.
2022-10-18 14:40:46 -07:00
Ryan dc46f27d5d Fix math in chunk module 2022-10-18 01:45:26 -07:00
Ryan Johnson d22f1edae1
Update valence_nbt to version 0.3.0 (#120)
Improves write performance and adds `binary_encoded_len` to compounds.
2022-10-17 21:11:20 -07:00
Carson McManus 9faac7a0fb
add open_inventory to clients (#119) 2022-10-16 14:12:36 -07:00
Ryan 0e58bfbb77 Remove dbg! macro 2022-10-16 04:13:10 -07:00
Ryan Johnson ad153159dd
Add replaceable boolean to block states (#116) 2022-10-16 03:50:57 -07:00
Carson McManus 59ca1ab573
Set up Inventory module (#75)
This is a very rough version of the inventory module and associated features. This will be a good base to start building something more robust.

- adds a player inventory to all clients
- makes clients keep track of what inventory they have open
- adds very naive state sync for inventories

related: #53 

# Test plans

### Building

1. run `building` example
2. pick any block from creative
3. place it
4. pick a different block from creative
5. place 2
6. break the last one
7. see that both remaining blocks don't become dirt

### Chest inventories

1. run `chest` example
2. sneak to give yourself a stone block
3. open the chest (it's invisible on top of the stone block)
4. place stone in chest
5. see the stone moving around in the chest

## Known Issues

To be fixed in later PRs

- It's very easy to duplicate items if you take the stone out of the chest on the same tick as the server trying to move the item.
- The chest is invisible when you spawn in. It appears if you break it.
- `get_slot` and `set_slot` can panic.
2022-10-15 19:47:02 -07:00
Ryan Johnson f58e6662dd
Add performance tests (#113)
Adds the performance_tests/ directory.

In the future we could use our own fake client software instead of
rust-mc-bot. This would make it easier to run the tests.
2022-10-14 21:18:03 -07:00
Ryan a53738355f Fix bug in ident and use AsRef instead of Borrow 2022-10-13 22:28:12 -07:00
Ryan Johnson 056b4ebd32
Correctly encode biomes (#112) 2022-10-13 18:19:35 -07:00
Sekky61 a29542b467
Dying and respawning (#51)
* `RespawnRequest` client event

* `CombatDeath` packet

* `kill` client method

* `death.rs` example

* clippy lint fix

* Clean up examples

* Fix formatting

* `win_game`, `set_respawn_screen` client methods

* respawn screen no longer deferred
This means that death example does not have respawn screen now.

Co-authored-by: Ryan <ryanj00a@gmail.com>
2022-10-12 11:11:33 -07:00
Ryan Johnson 7d0c254874
Rewrite ident module (again) (#111)
Ident is now a wrapper around any string type `S`.
2022-10-12 03:53:59 -07:00
Ryan afe390836c Remove dbg! calls and make some tweaks to Ident 2022-10-11 14:05:00 -07:00
Zh_Jk e30ed62240
Fix typo in c2s.rs (#109) 2022-10-11 11:12:06 -07:00
Carson McManus a02b657f3c
clamp ItemStack item count to range 1-127 (#110) 2022-10-11 11:10:04 -07:00
Ryan Johnson 23fdc41610
Clean up ident module and add lifetime (#108)
This lifetime will be useful for zero-copy decoding later.
2022-10-11 01:10:49 -07:00
Tert0 c758f70c33
Implement Weather (#106)
* implemented raining, rain level and thunder level

* fix linter and cargo fmt

* removed field from the client, removed weather getters, added docs, ensuring valid rain/thunder level values and sending weather changing directly

* Remove results from `set_rain_level` and `set_thunder_level`

* Remove unused import

Co-authored-by: Ryan <ryanj00a@gmail.com>
2022-10-09 15:18:15 -07:00
Carson McManus 0652fa13a4
change Slot enum into alias of Option<ItemStack> (#107) 2022-10-09 12:38:00 -07:00
Ryan 82323eaa12 Clarify top-down modules guideline and remove "separate data and functions" guideline. 2022-10-08 19:51:01 -07:00
Ryan c214ffc262 Tweak CONTRIBUTING.md 2022-10-08 18:42:02 -07:00
Ryan Johnson f26f4be1fe
Add link to CONTRIBUTING.md in README.md 2022-10-08 18:08:48 -07:00
Ryan Johnson b0646a5398
Create CONTRIBUTING.md 2022-10-08 18:06:05 -07:00
Daniel Huth e13d0ffe7a
Skip spawn of non visible player characters (#100)
* Skip spawn of non visible player characters
* Don't spawn player if player list is `None`

Co-authored-by: Ryan <ryanj00a@gmail.com>
2022-10-08 15:12:30 -07:00
Ryan Johnson 153cde1a04
Chunk Rewrite With Paletted Containers (#91)
The current approach to managing chunk data is misconceived. This new approach uses genuine paletted containers and does not suffer from complexities caused by caching. As a result, memory usage (according to htop) in the terrain example with render distance = 32 has gone from 785 megs to 137 megs. That's 17.4% of the memory it used to use. Terrain generation speed was not affected.
2022-10-07 15:52:55 -07:00
Ryan 5a686a0e8b Derive terrain seed from current day
It's easier to test things when the terrain isn't constantly changing.
2022-10-07 00:17:14 -07:00
Ryan 886d036b97 Use valence.rs domain in extractor package 2022-10-06 13:49:30 -07:00
Ryan 1fcc5bd527 Set player head yaw when spawning 2022-10-05 15:02:40 -07:00
EmperialDev 1f996f7549
Add Items (#92)
Adding all the items, just like the blocks are. This will adress 1. and 2. bullet from issue #53
This will also make it easier to convert between block <-> item.
2022-10-05 09:28:08 -07:00
Ryan e985bae469 Use StatusCode in auth server response check 2022-10-04 23:35:34 -07:00
Ryan b330f97642 Remove top level object from enchantments 2022-10-04 23:16:48 -07:00
Ryan Johnson 85cc3a28ea
Add --workspace flag to commands in CI (#101) 2022-10-04 02:12:32 -07:00
Terminator b83b9f76ed
Expose enchantments to Rust (#99)
* Expose extracted enchantments to Rust.

* Give enchantments a `level: i16` field.

* Update enchantment derive impls

* Restructure how enchants are exposed to Rust through C-like enums

* Update build/enchant.rs

Co-authored-by: Ryan Johnson <ryanj00a@gmail.com>

* Update build/enchant.rs

Co-authored-by: Ryan Johnson <ryanj00a@gmail.com>

* Move enchantment sources to their own struct.

* make id() function return it's discriminant instead of matching

Co-authored-by: Ryan Johnson <ryanj00a@gmail.com>
2022-10-03 13:19:42 -07:00
Ryan cf5b9136df Bump valence_nbt version in valence 2022-10-01 15:47:59 -07:00
Ryan f94c50bfd3 Change valence_nbt repository link 2022-10-01 15:38:14 -07:00