Commit graph

393 commits

Author SHA1 Message Date
Carson McManus 0319635a8b
Implement Drop Item Events (#252)
<!-- Please make sure that your PR is aligned with the guidelines in
CONTRIBUTING.md to the best of your ability. -->
<!-- Good PRs have tests! Make sure you have sufficient test coverage.
-->

## Description

<!-- Describe the changes you've made. You may include any justification
you want here. -->
Dropping items is heavily coupled to inventories. Clients also predict
state changes when they try to drop items, so we need to be able to
replicate that change in order to stay in sync.

This will also remove `DropItem` events in favor of just `DropItemStack`
events. Having 2 event streams that basically mean the same thing seems
verbose and error prone.

As of right now, these changes require the event loop to have a
reference to the client's inventory. This seems like something we are
going to need to do a lot more of to complete #199.

## Test Plan

<!-- Explain how you tested your changes, and include any code that you
used to test this. -->
<!-- If there is an example that is sufficient to use in place of a
playground, replace the playground section with a note that indicates
this. -->

<details>

<summary>Playground</summary>

```rust
use tracing::info;
use valence::client::despawn_disconnected_clients;
use valence::client::event::{default_event_handler, DropItemStack};
use valence::prelude::*;

#[allow(unused_imports)]
use crate::extras::*;

const SPAWN_Y: i32 = 64;

pub fn build_app(app: &mut App) {
    app.add_plugin(ServerPlugin::new(()).with_connection_mode(ConnectionMode::Offline))
        .add_system_to_stage(EventLoop, default_event_handler)
        .add_startup_system(setup)
        .add_system(init_clients)
        .add_system(despawn_disconnected_clients)
        .add_system(toggle_gamemode_on_sneak)
        .add_system(drop_items);
}

fn setup(world: &mut World) {
    let mut instance = world
        .resource::<Server>()
        .new_instance(DimensionId::default());

    for z in -5..5 {
        for x in -5..5 {
            instance.insert_chunk([x, z], Chunk::default());
        }
    }

    for z in -25..25 {
        for x in -25..25 {
            instance.set_block([x, SPAWN_Y, z], BlockState::GRASS_BLOCK);
        }
    }

    world.spawn(instance);
}

fn init_clients(
    mut clients: Query<&mut Client, Added<Client>>,
    instances: Query<Entity, With<Instance>>,
) {
    let instance = instances.get_single().unwrap();

    for mut client in &mut clients {
        client.set_position([0.5, SPAWN_Y as f64 + 1.0, 0.5]);
        client.set_instance(instance);
        client.set_game_mode(GameMode::Creative);
    }
}

fn drop_items(clients: Query<&Client>, mut drop_stack_events: EventReader<DropItemStack>) {
    if drop_stack_events.is_empty() {
        return;
    }

    for event in drop_stack_events.iter() {
        info!("drop stack: {:?}", event);
    }
}

```

</details>

<!-- You need to include steps regardless of whether or not you are
using a playground. -->
Steps:
1. `cargo test -p valence --tests`
2. Run playground `cargo run -p playground`
3. Open creative menu
4. Pick an item and click to drop it outside of the creative menu
5. Pick an entire stack of an item, place it in your hotbar
6. Hover over the item, press your drop key to drop an item from the
stack
7. Press shift to switch to survival
8. Select the item stack in your hotbar, press your drop key to drop an
item from the stack
9. Open your inventory, grab the stack, hover outside the window and
click to drop the entire stack
10. Grab another stack from creative, place it in your hotbar
11. switch back to survival, select the stack, and press your control +
drop key to drop the entire stack
12. For each item you dropped, you should see a log message with the
event

#### Related

<!-- Link to any issues that have context for this or that this PR
fixes. -->
2023-02-20 15:37:09 -08:00
Ryan Johnson 7d05cb8309
Block entity fixes (#251)
Clean up unresolved issues from #32
2023-02-19 12:26:32 -08:00
Mrln 1ceafe0ce0
Add block entities (#32)
This PR aims to add block entities.
Fixes #5

---------

Co-authored-by: Ryan Johnson <ryanj00a@gmail.com>
2023-02-18 10:16:01 -08:00
Ryan Johnson bcd686990d
Tweak README.md 2023-02-17 14:05:10 -08:00
Ryan Johnson 286e6aa0d0
Fix CI (#245)
## Description

Attempting to fix the CI failures in #32 by switching to the stable
toolchain.
2023-02-16 19:48:55 -08:00
Gingeh 50018a52bf
Add sounds (#244)
This is my first time contributing here so I was pretty unfamiliar with
the codebase and may have done some things incorrectly.

## Description
 - Added a sound extractor to extract sound event ids and identifiers
 - Added a `Sound` enum (with a build script) to represent sound effects
 - Added a `play_sound` method to `Instance` and `Client`
 - Re-implemented sound effects in the parkour example

## Test Plan
I tested this using the sounds I added to the parkour example.

#### Related
Hopefully fixes #206
2023-02-15 02:36:21 -08:00
Riley Sutton bee44d32b5
Add ping calculation system (#243)
## Description

Add the ping computation system and capability to send ping to
connecting clients.

Currently the player list will only update upon connection. Should
another ticket be made for updating this since it seems out of scope for
this issue?

## Test Plan

Printing ping per keepalive packet on a different computer on my
network. It seems to work fine but would need extra feedback.

#### Related

#207
2023-02-14 22:22:14 -08:00
Joel Ellis f0027aa097
Colorise the inspector output based on S2C or C2S (#242)
<!-- Please make sure that your PR is aligned with the guidelines in
CONTRIBUTING.md to the best of your ability. -->
<!-- Good PRs have tests! Make sure you have sufficient test coverage.
-->

## Description

<!-- Describe the changes you've made. You may include any justification
you want here. -->
I've used the popular `owo-colors` library to colourise the output of
the `packet_inspector`.
This makes it a lot easier to see who's sending what for debugging
purposes.

In future, I'd like to be able to filter out common, low-information
packets like `SetPlayerPosition` and `KeepAlive` as well, but that's not
in this PR.


![image](https://user-images.githubusercontent.com/28905212/218880818-69c0461c-bd23-45c4-9fd4-7c5868be4b67.png)

## Test Plan

No tests - just eyeball 😄.
2023-02-14 15:31:17 -08:00
Ryan Johnson 909f7d3909
Clean up dimension/biome code (#239) 2023-02-14 03:36:52 -08:00
Guac c494b83a56
VarInt/VarLong Encode and Decode Optimizations (#227)
Aims to improve upon VarInt/VarLong Encode and Decode procedures (#208).
# Baselines:
## VarInt:

![VarInt::decode](https://user-images.githubusercontent.com/60993440/218295740-c7507993-1c1f-4d71-a42b-4ba9f1437bd7.png)

![VarInt::encode](https://user-images.githubusercontent.com/60993440/218295804-3cda044a-e2e4-4109-83ce-6e0a135d467b.png)
## VarLong

![VarLong::encode](https://user-images.githubusercontent.com/60993440/218295958-e445c2ea-ec2e-422f-92a4-d53bf41c8ec4.png)

![VarLong::decode](https://user-images.githubusercontent.com/60993440/218296047-802cec4a-b69b-435b-a140-0d9bf5df49a8.png)
2023-02-14 03:04:32 -08:00
Carson McManus e1a3e2dc00
make dropping an item in creative not crash the server (#234)
<!-- Please make sure that your PR is aligned with the guidelines in
CONTRIBUTING.md to the best of your ability. -->
<!-- Good PRs have tests! Make sure you have sufficient test coverage.
-->

## Description

<!-- Describe the changes you've made. You may include any justification
you want here. -->
Added a quick and dirty bounds check because we haven't created drop
item/itemstack event(s) yet.

## Test Plan

<!-- Explain how you tested your changes, and include any code that you
used to test this. -->
<!-- If there is an example that is sufficient to use in place of a
playground, replace the playground section with a note that indicates
this. -->

use the `building` example

<!-- You need to include steps regardless of whether or not you are
using a playground. -->
Steps:
1. `cargo run --example building`
2. open inventory
3. pick an item
4. drop the item outside of the inventory window
5. see that the server does not panic

#### Related

<!-- Link to any issues that have context for this or that this PR
fixes. -->
fixes #233
2023-02-11 20:04:52 -08:00
Ryan Johnson cb9230ec34
ECS Rewrite (#184)
This PR redesigns Valence's architecture around the Bevy Entity
Component System framework (`bevy_ecs` and `bevy_app`). Along the way, a
large number of changes and improvements have been made.
- Valence is now a Bevy plugin. This allows Valence to integrate with
the wider Bevy ecosystem.
- The `Config` trait has been replaced with the plugin struct which is
much easier to configure. Async callbacks are grouped into their own
trait.
- `World` has been renamed to `Instance` to avoid confusion with
`bevy_ecs::world::World`.
- Entities, clients, player list, and inventories are all just ECS
components/resources. There is no need for us to have our own
generational arena/slotmap/etc for each one.
- Client events use Bevy's event system. Users can read events with the
`EventReader` system parameter. This also means that events are
dispatched at an earlier stage of the program where access to the full
server is available. There is a special "event loop" stage which is used
primarily to avoid the loss of ordering information between events.
- Chunks have been completely overhauled to be simpler and faster. The
distinction between loaded and unloaded chunks has been mostly
eliminated. The per-section bitset that tracked changes has been
removed, which should further reduce memory usage. More operations on
chunks are available such as removal and cloning.
- The full client's game profile is accessible rather than just the
textures.
- Replaced `vek` with `glam` for parity with Bevy.
- Basic inventory support has been added.
- Various small changes to `valence_protocol`.
- New Examples
- The terrain and anvil examples are now fully asynchronous and will not
block the main tick loop while chunks are loading.

# TODOs
- [x] Implement and dispatch client events.
- ~~[ ] Finish implementing the new entity/chunk update algorithm.~~ New
approach ended up being slower. And also broken.
- [x] [Update rust-mc-bot to
1.19.3](https://github.com/Eoghanmc22/rust-mc-bot/pull/3).
- [x] Use rust-mc-bot to test for and fix any performance regressions.
Revert to old entity/chunk update algorithm if the new one turns out to
be slower for some reason.
- [x] Make inventories an ECS component.
- [x] Make player lists an ECS ~~component~~ resource.
- [x] Expose all properties of the client's game profile.
- [x] Update the examples.
- [x] Update `valence_anvil`.
- ~~[ ] Update `valence_spatial_index` to use `glam` instead of `vek`.~~
Maybe later
- [x] Make entity events use a bitset.
- [x] Update docs.

Closes #69
Closes #179
Closes #53

---------

Co-authored-by: Carson McManus <dyc3@users.noreply.github.com>
Co-authored-by: AviiNL <me@avii.nl>
Co-authored-by: Danik Vitek <x3665107@gmail.com>
Co-authored-by: Snowiiii <71594357+Snowiiii@users.noreply.github.com>
2023-02-11 09:51:53 -08:00
Guac 44ea6915db
Implement remaining clientbound packets (#197)
Aims to close #186
2023-02-03 22:30:12 -08:00
Ryan Johnson 9f8250d4f9
Update README.md with current project status. 2023-02-03 19:04:54 -08:00
CaveNightingale ca269cd03e
Add a simple SNBT parser (#201)
To implement the hoverEvent show_item, we need an SNBT parser and a SNBT
serializer.
An SNBT parser is implemented in this commit.
2023-02-03 03:16:27 -08:00
Ryan Johnson b5a355c756
Non-Object Text Deserialization (#194)
- Extends `Text`'s `Deserialize` impl to allow for deserializing JSON
data types other than objects.
- Add additional `From` impls for `Text`.
- Better `Debug` impl for `Text`.
- Tweaked text unit tests.
2023-01-21 00:17:19 -08:00
Guac ad89e4457c
Implement clientbound packets (#190)
Co-authored-by: Ryan Johnson <ryanj00a@gmail.com>
2023-01-19 22:00:35 -08:00
Ryan Johnson 34c01d0f24
Add debug logging to valence_protocol (#191)
Adds `tracing` to `valence_protocol` so we can log debug info about
partially decoded packets. `packet_inspector` has been set to print log
messages at the debug level.
2023-01-16 16:32:22 -08:00
Ryan Johnson ad675547b2
Update packet inspector README 2023-01-11 22:58:00 -08:00
Ryan Johnson 7574fa33c5
Commands and recipe book packets (#183)
Implement the commands and recipe book packets. Also reorganizes some
modules in valence_protocol.
2022-12-31 22:59:22 -08:00
Ryan Johnson 8d9c0a7553
Add recipe and tags packets (#182)
Also improve generated `Encode`, `Decode` error messages and fix
unnecessary build script reruns.
2022-12-30 06:18:24 -08:00
Ryan 623e88908d Disable biome extractor
Need to figure out what became of the biome registry in 1.19.3
2022-12-29 23:31:06 -08:00
Ryan Johnson e386c9e2c2
Add Anvil file format support to README.md 2022-12-29 21:16:33 -08:00
Ryan Johnson c9aeda7dbd
Support NBT lists of type TAG_End (#181)
In NBT, lists are allowed to have the element type TAG_End iff their
length is zero. This adds an explicit `List::End` enum variant to
valence_nbt.
2022-12-29 18:10:29 -08:00
Ryan Johnson 2597e92b8f
Reorganize Packages (#180)
Moves all the packages to the `crates/` directory.
2022-12-29 16:51:05 -08:00
jivvy 295678e92d
Add Particles (#150)
Implements the clientbound particle packet.

Co-authored-by: Ryan Johnson <ryanj00a@gmail.com>
2022-12-29 00:26:19 -08:00
Ryan Johnson 5d3364d452
Update to 1.19.3 (#171)
Closes #165
2022-12-28 00:42:54 -08:00
Terminator 6de5de57a5
Anvil file support (blocks and biomes) (#145)
Adds the `valence_anvil` crate for loading anvil worlds. It can only read blocks and biomes currently. Support for saving data is to be added later.

Co-authored-by: Ryan <ryanj00a@gmail.com>
2022-12-26 06:55:52 -08:00
UStuej 8a7782e16f
Update doc (#178)
Refer to `EncodePacket` and `DecodePacket` instead of `Packet`.
2022-12-24 17:13:42 -08:00
Ryan Johnson c26bbe7ec2
Valence protocol redesign (#177)
- Fix performance issues related to writing individual bytes instead of
using `Write::write_all`.
- Make manual implementations of `Encode` and `Decode` easier:
  - Remove `encoded_len` method from `Encode`
  - Split `Packet` trait into `EncodePacket` and `DecodePacket`.
  - Simplify derive macros.
2022-12-23 06:30:57 -08:00
Ryan Johnson 1f5ec6f94f
Valence protocol benchmarks (#175)
Adds criterion benchmarks for some packets and `VarInt` in
`valence_protocol`
2022-12-21 01:24:02 -08:00
Ryan Johnson 2fc59d90a8
Improve valence_nbt write perf and remove zerocopy dependency (#174) 2022-12-20 20:17:07 -08:00
Carson McManus 4194acaa67
Use Option<InventoryId> for open_inventory on client (#167)
- automatically handle CloseContainer packet so the client updates open_inventory
- Use `Option<InventoryId>` for `open_inventory` on client

Co-authored-by: Ryan Johnson <ryanj00a@gmail.com>
2022-12-19 06:55:26 -08:00
Terminator c4ed95a7d3
Performance enhancements for #141 (#173)
This PR also includes the Criterion benchmark.

## Before this commit
> Benchmarking BlockState::to_kind
> Benchmarking BlockState::to_kind: Warming up for 3.0000 s Benchmarking
BlockState::to_kind: Collecting 100 samples in estimated 20.045 s (273k
iterations) Benchmarking BlockState::to_kind: Analyzing
> BlockState::to_kind     time:   [68.925 µs 69.202 µs 69.477 µs]
> 
> Found 5 outliers among 100 measurements (5.00%)
>   4 (4.00%) high mild
>   1 (1.00%) high severe

## After this commit
> Benchmarking BlockState::to_kind
> Benchmarking BlockState::to_kind: Warming up for 3.0000 s Benchmarking
BlockState::to_kind: Collecting 100 samples in estimated 20.010 s (3.8M
iterations) Benchmarking BlockState::to_kind: Analyzing
> BlockState::to_kind     time:   [1.5814 µs 1.5859 µs 1.5912 µs]
> change: [-97.692% -97.678% -97.666%] (p = 0.00 < 0.05)
>                         Performance has improved.
> Found 4 outliers among 100 measurements (4.00%)
>   4 (4.00%) high mild

Co-authored-by: Ryan Johnson <ryanj00a@gmail.com>
2022-12-19 06:25:26 -08:00
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 d85b7f5e89
Update packet inspector and fix packet bugs (#172)
- Implement `encoded_len` for packet enums.
- Fix bug in `has_next_packet` causing a later `unwrap` to panic.
- Add robust `encoded_len` check in `PacketEncoder` for debug builds.
- Split packet inspector regex into separate "inclusive" and "exclusive"
arguments because the `regex` crate does not support negative lookahead.
- Better `Debug` impls for `Ident` and `Compound`.
- Remove read/write timeouts from packet inspector.
2022-12-16 08:23:48 -08:00
Terminator c73abacc98
Re-visit From impl for native data types found in NBT data format. (#169) 2022-12-15 18:38:52 -08:00
Ryan Johnson 740778ec41
Update chunk data packet cache lazily (#170)
Previously, the chunk data cache was regenerated at the end of each tick
every time a block or biome was modified in a chunk. This causes
performance problems in situations where chunks are being modified every
tick, like the conway example or redstone machines.

This changes things so that the chunk data packet is regenerated only
when a client actually needs to load the chunk. This isn't perfect
because the regeneration cannot happen in parallel, but the benefits
outweigh the costs. (The chunk data packet _is_ generated in parallel
when the chunk is first created. There is also some parallelism when
clients are loading different chunks. The packet cache is guarded by a
`Mutex`.)

This has revealed that compression is surprisingly slow. This should be
investigated later.
2022-12-13 22:56:22 -08:00
Carson McManus 8b50e93b0e
add swap_slot to Inventory (#168)
This effectively exposes the slice `swap` method:
https://doc.rust-lang.org/std/primitive.slice.html#method.swap
2022-12-11 09:35:14 -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 b61bdbb71d Fix conflicting Debug impl on bitfield structs 2022-12-09 17:02:37 -08:00
Ryan Johnson 842ddb4127
Optimize sending tracked entity data (#163) 2022-11-30 15:55:46 -08:00
Ryan Johnson 0e28e5ad05
Improve slab_rc implementation (#162) 2022-11-29 17:14:04 -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