Commit graph

294 commits

Author SHA1 Message Date
Sandro Marques 86be031a31
Implement missing text component types (#144)
Implements the remaining text component types. Should hopefully close
#19.

Co-authored-by: Ryan <ryanj00a@gmail.com>
2022-11-19 16:48:01 -08:00
Ryan Johnson 4226201fed
Replace log crate with tracing (#157)
The `tracing` crate seems to be the go-to
logging/profiling/instrumentation solution nowadays. Perhaps in the
future we could use `tracing` for profiling instead of (or in addition
to) the `perf`-based `cargo flamegraph` command. This would sidestep the
issue of `rayon` polluting the output. I conducted an initial experiment
by adding some more spans but wasn't very happy with the result.

Log messages have also been improved. There is some additional context
and events are raised when clients are added/removed from the server.
2022-11-16 18:22:44 -08:00
Ryan Johnson ab2e1081ed
Re-export common items at the root of valence_protocol (#156) 2022-11-14 22:30:20 -08:00
Ryan 74e4f10ec5 Don't send empty MOTION_BLOCKING heightmap
The client seems to initialize it to zero when you don't send it, so
there's no point in sending an empty motion_blocking.
2022-11-14 17:18:49 -08:00
Ryan 21590a396c Fix cargo:rerun-if-changed=... in valence_protocol 2022-11-14 12:08:32 -08:00
Ryan Johnson 4b155f4c9b
Expand on the "encoded buf" idea in valence_protocol (#155)
Adds a `Cached<T>` type and renames some things.
2022-11-14 01:01:28 -08:00
Ryan Johnson b8e41cbb9a
Change 'Flexible' bullet in README.md
With the introduction of `valence_protocol` and some future plans, I've decided it's unrealistic to try and hide all the protocol code. There are situations where direct access is necessary.
2022-11-13 06:44:22 -08:00
Ryan Johnson 420f2d1b7c
Move protocol code to valence_protocol + redesigns (#153)
Closes #83 

This PR aims to move all of Valence's networking code to the new
`valence_protocol` crate. Anything not specific to valence is going in
the new crate. It also redesigns the way packets are defined and makes a
huge number of small additions and improvements. It should be much
easier to see where code is supposed to go from now on.

`valence_protocol` is a new library which enables interactions with
Minecraft's protocol. It is completely decoupled from valence and can be
used to build new clients, servers, tools, etc.

There are two additions that will help with #5 especially:
- It is now easy to define new packets or modifications of existing
packets. Not all packets need to be bidirectional.
- The `CachedEncode` type has been created. This is used to safely cache
redundant calls to `Encode::encode`.
2022-11-13 06:10:42 -08:00
Ryan Johnson 9f1c946839
Check off proxy support in README.md 2022-11-12 16:45:57 -08:00
emortaldev b1561eda73
Fix proxy joins (#154) 2022-11-12 06:32:00 -08:00
Ryan 923fabe890 Fix missing tokio context on client disconnect
The `PlayPacketController` might spawn a tokio task, but it was not in a
tokio context when that happened. This is because tokio contexts are
thread-local, but rayon tasks run in their own thread pool.
2022-11-09 18:30:44 -08:00
UStuej bb43856c9e
Implement radians conversions for ByteAngle (#149)
Adds `from_radians` function and `to_radians` method for `ByteAngle`.
2022-11-01 22:00:38 -07:00
Ryan Johnson ff136c2a4a
Clean up Config docs and add compression threshold (#148) 2022-11-01 20:27:08 -07:00
Carson McManus a885b949da
Better consume for inventories (#147)
- rework `consume_one` into `consume`
- update `consume_one_held_item` to `consume_held_item` to match
`consume`
- remove an unnecessary clone

Originally, I was planning on removing this function altogether. After
thinking about it for a bit though, I think it provides enough of a
convenience for end users to warrant keeping it.

This also implements the fix that was originally in #124
2022-11-01 19:31:28 -07:00
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