Commit graph

105 commits

Author SHA1 Message Date
Ryan aeea435a7e Fix examples, but for real this time 2022-12-19 06:24:06 -08:00
Ryan 4e302ca09f Remove remaining unnecessary casts in examples 2022-12-19 06:11:03 -08:00
Ryan 985f7891e4 Remove unnecessary casts in death example 2022-12-19 02:17:35 -08:00
Ryan 01ff465db9 Remove unnecessary casts in parkour example 2022-12-19 01:50:51 -08:00
Ryan Johnson ae7eaf15fc
Big Optimizations (#164)
Closes #84

# Changes

- Implemented new algorithm for tracking loaded entities and chunks.
- The client does not need to maintain a list of loaded chunks and
entities anymore, as this information can be inferred using carefully
maintained data from the previous tick.
- Chunks are used as a spatial partition for entities. Entity visibility
is now based on chunk visibility rather than the euclidean distance to
clients.
- The BVH is no longer strictly necessary, so it has been moved to the
new `valence_spatial_index` crate.
- The API has been generalized to support things other than entities.
The crate does not have a dependency on the main `valence` crate.
- Chunk, entity, and player list packets are now aggressively cached to
increase performance.
- Chunk packets now include some filler light data. This makes the
vanilla client lag a lot less.
- Entities and chunks must now be marked as deleted before they are
removed.
- Improved `ChunkPos` interface.
- Added function to get the duration per tick.
- Added `Index` and `IndexMut` impls to collection types.

As a result of the above changes, performance under heavy load has
increased significantly. With the rust-mc-bot test on my machine, I went
from a max of ~1000 players to ~4000 players.
2022-12-11 02:37:02 -08:00
Carson McManus 4188bd71bb
re-implement inventory_piano example (#166)
# Test plan

```
cargo r -r --example inventory_piano
```

Co-authored-by: Ryan Johnson <ryanj00a@gmail.com>
2022-12-10 18:08:32 -08:00
Ryan Johnson 58f8197913
Client Cleanup (#159)
# Noteworthy Changes
- Simplified the `client` module and update procedure. Sending packets
is not deferred unless necessary.
- Client events are no longer buffered in `Client` before reaching the
user.
- Expanded `ClientEvent` to account for most packets.
- Most types containing custom `state` now implement `Deref` and
`DerefMut`. This means you don't have to write `.state` all over the
place. `Server` was excluded from this because it does not play well
with the borrow checker.
- Fixed bugs related to entity visibility.
- Client now correctly holds the semaphore permit from the initial
connection.
- Other miscellaneous API changes throughout the project. 

# Known Issues
- Inventory stuff is still incomplete. The inventory examples have been
temporarily disabled.
2022-11-29 03:37:32 -08:00
Sandro Marques 6437381339
Translation key extractor and code generator (#160)
Generates a new `translation_key.rs` with all bundled translations.
Closes #158.
2022-11-27 05:12:08 -08:00
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 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
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
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 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
Carson McManus 9faac7a0fb
add open_inventory to clients (#119) 2022-10-16 14:12:36 -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 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 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
Carson McManus 47538ea05c
Refactor Slot (#87)
* create ItemStack type

* move slot back into protocol

* change DropItemStack to use ItemStack, because it's imposible to drop nothing
2022-09-26 20:44:06 -07:00
emortal 2044add969
Create parkour.rs example (#28) 2022-09-24 19:40:10 -07:00
Ryan 1dc5d84ba9 Add biomes example (which is currently broken due to bugs in chunk module) 2022-09-23 19:10:11 -07:00
Carson McManus 4574e18d49
inventory client events (#66)
* finish packet definition for ClickContainer

* add CloseScreen client event

* add DropItem client event

* add SetSlotCreative client event

* implement ClientEvent::CloseScreen

* ClientEvent::DropItem

* implement ClientEvent::SetSlotCreative

* cargo fmt

* add inventory_piano example to demo inventory slot click events

* lints

* implement ClickContainer event

* inventory_piano: deduplicate note playing logic

* add DropItemStack client event

* implement ClientEvent::DropItemStack

* adjust logging

* tweak inventory_piano example, send text to chat instead of stdout

* fix lint

* move Slot outside of protocol module

* avoid cloning slot in ClickContainer packet handler

* fix inventory_piano example
2022-09-19 11:29:41 -07:00
Ryan cab1337716 Add Giant to entity_raycast example 2022-09-17 23:30:54 -07:00
Ryan Johnson 9ec1df1978
Entity Hitbox Improvements (#70)
Solves #45. With help from @guac420
2022-09-17 23:14:48 -07:00
Ryan dcd4a2b5ed Update resource pack link and add SHA-1 hash 2022-09-17 19:40:26 -07:00
James P e6f01e5e4d
Custom resource packs (#68)
Implements custom resource pack support.

Adds a `Client` method to set resource pack, and a `ClientEvent` for listening to resource pack status updates from clients.
2022-09-17 19:32:40 -07:00
Carson McManus f165b55c7b
add building example (#49)
This adds a new example to demonstrate how to implement block breaking/placing.
2022-09-13 18:30:45 -07:00
Ryan 65dc561868 Document and rename default_client_event to handle_event_default. 2022-09-11 21:59:05 -07:00
Ryan bc470f3eb3 Consistent logo file names 2022-09-11 00:20:17 -07:00
Ryan Johnson ca4f47c768
Implement the player sample (#47)
Verify that it works by hovering your mouse over the player count in the
cow_sphere example.
2022-09-10 16:02:12 -07:00
Ryan 5c9a6c973c Add --all-targets to actions 2022-09-09 22:35:24 -07:00
Ryan Johnson 20546e2fb8
Split Chunk into loaded and unloaded types (#37)
These changes will pave the way for nonblocking terrain generation in `terrain.rs`.
2022-09-08 19:33:24 -07:00
Ryan 7d55dfb964 Simplify position conversion 2022-09-05 22:12:28 -07:00
SirJosh3917 08ee7acacf
Fix order of chunk creation in conway's example (#27)
This would typically go unnoticed, but upon expanding the and making meaningful use of both directions, this being an issue arises.
2022-09-05 20:16:21 -07:00
Tristan F 10a8de3bbd
Add action bar support (#15)
* Action bar + conway

* Docs for action bar

* Apply suggestions

* fmt
2022-09-05 17:23:32 -07:00
Mrln 42dfcef57d
Add ClientEvent::InteractWithBlock (#26)
* Add ClientEvent::InteractWithBlock

* Add an example of ClientEvent::InteractWithBlock to the conway example
2022-09-05 17:01:39 -07:00
Tristan F df3e5b12bb
Add custom sound effect packet and sound example to conway (#12)
* Add custom sound effect packet, sound example

* Small logic cleanup

* More sound packets

* fmt

* Apply suggestions
2022-09-05 15:37:59 -07:00
Ryan 30e05bb373 cargo fmt 2022-09-04 22:21:24 -07:00
Tristan F d7e229e0a9 Make default client event 2022-09-04 15:42:08 -04:00
Ryan f3c96403e0 Slow down conway example 2022-09-03 01:40:09 -07:00
Ryan 6e0a7e0a88 Allow pausing and clearing the conway example 2022-08-31 04:51:13 -07:00
Ryan 2a266fb091 Change rustfmt settings 2022-08-28 21:31:07 -07:00
Ryan 793a795732 Check protocol version during login 2022-08-14 15:18:22 -07:00
Ryan 55cb595740 Improve chunks API 2022-08-10 20:09:10 -07:00
Ryan 003c2e3649 Enable authentication in examples 2022-08-10 10:32:05 -07:00
Ryan b2310db7b7 Fix chunks loading multiple times
The default chunk view distance has also been lowered to 2 since that is the most conservative guess.
2022-08-10 10:27:25 -07:00