2023-05-25 21:24:50 +10:00
Valence has a public Discord server [here ](https://discord.gg/8Fqqy9XrYb ) and GitHub discussions [here ](https://github.com/valence-rs/valence/discussions ). Check those out if you have additional questions
2022-10-09 12:06:05 +11:00
or comments.
# What version of Rust should I use?
2022-10-09 12:42:02 +11:00
To _use_ Valence, only the most recent stable version of Rust is required. However, contributors should know that
unstable `rustfmt` settings are enabled in the project. To run `rustfmt` with the nightly toolchain, use
the `cargo +nightly fmt` command.
2022-10-09 12:06:05 +11:00
2023-05-25 22:09:51 +10:00
# What issues should I work on?
2022-10-09 12:06:05 +11:00
Issues
labelled [good first issue ](https://github.com/valence-rs/valence/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22 )
2023-05-25 22:09:51 +10:00
are a good place to start. This label is reserved for issues that are relatively uncontroversial and shouldn't require too much specialized domain
2022-10-09 12:06:05 +11:00
knowledge to complete. New contributors are not required to start with these issues.
If you plan to work on something that's not an open issue, consider making one first so that it can be discussed. This
way, your contribution will not be rejected when it is submitted for review.
2023-03-24 00:54:07 +11:00
## Playgrounds
2023-02-12 04:51:53 +11:00
Playgrounds are meant to provide a quick and minimal environment to test out new code or reproduce bugs. Playgrounds are also a great way test out quick ideas. This is the preferred method for providing code samples in issues and pull requests.
To get started with a new playground, copy the template to `playground.rs` .
```bash
Reorganize Project (#321)
## Description
- `valence` and `valence_protocol` have been divided into smaller crates
in order to parallelize the build and improve IDE responsiveness. In the
process, code architecture has been made clearer by removing circular
dependencies between modules. `valence` is now just a shell around the
other crates.
- `workspace.packages` and `workspace.dependencies` are now used. This
makes dependency managements and crate configuration much easier.
- `valence_protocol` is no more. Most things from `valence_protocol`
ended up in `valence_core`. We won't advertise `valence_core` as a
general-purpose protocol library since it contains too much
valence-specific stuff. Closes #308.
- Networking code (login, initial TCP connection handling, etc.) has
been extracted into the `valence_network` crate. The API has been
expanded and improved with better defaults. Player counts and initial
connections to the server are now tracked separately. Player counts
function by default without any user configuration.
- Some crates like `valence_anvil`, `valence_network`,
`valence_player_list`, `valence_inventory`, etc. are now optional. They
can be enabled/disabled with feature flags and `DefaultPlugins` just
like bevy.
- Whole-server unit tests have been moved to `valence/src/tests` in
order to avoid [cyclic
dev-dependencies](https://github.com/rust-lang/cargo/issues/4242).
- Tools like `valence_stresser` and `packet_inspector` have been moved
to a new `tools` directory. Renamed `valence_stresser` to `stresser`.
Closes #241.
- Moved all benches to `valence/benches/` to make them easier to run and
organize.
Ignoring transitive dependencies and `valence_core`, here's what the
dependency graph looks like now:
```mermaid
graph TD
network --> client
client --> instance
biome --> registry
dimension --> registry
instance --> biome
instance --> dimension
instance --> entity
player_list --> client
inventory --> client
anvil --> instance
entity --> block
```
### Issues
- Inventory tests inspect many private implementation details of the
inventory module, forcing us to mark things as `pub` and
`#[doc(hidden)]`. It would be ideal if the tests only looked at
observable behavior.
- Consider moving packets in `valence_core` elsewhere. `Particle` wants
to use `BlockState`, but that's defined in `valence_block`, so we can't
use it without causing cycles.
- Unsure what exactly should go in `valence::prelude`.
- This could use some more tests of course, but I'm holding off on that
until I'm confident this is the direction we want to take things.
## TODOs
- [x] Update examples.
- [x] Update benches.
- [x] Update main README.
- [x] Add short READMEs to crates.
- [x] Test new schedule to ensure behavior is the same.
- [x] Update tools.
- [x] Copy lints to all crates.
- [x] Fix docs, clippy, etc.
2023-04-22 07:43:59 +10:00
cp tools/playground/src/playground.template.rs tools/playground/src/playground.rs
2023-02-12 04:51:53 +11:00
```
Make your changes to `crates/playground/src/playground.rs` . To run it:
```bash
cargo run -p playground # simply run the playground, or
cargo watch -c -x "run -p playground" # run the playground and watch for changes
```
2022-10-09 12:06:05 +11:00
# Automatic Checks
When you submit a pull request, your code will automatically run through clippy, rustfmt, etc. to check for any errors.
If an error does occur, it must be fixed before the pull request can be merged.
# Code Conventions
Here are some rules you should follow for your code. Generally the goal here is to be consistent with existing code, the
2022-10-09 13:51:01 +11:00
standard library, and the Rust ecosystem as a whole. Nonconforming code is not necessarily a blocker for accepting your
2023-05-23 22:43:06 +10:00
contribution, but conformance is advised.
2022-10-09 12:06:05 +11:00
These guidelines are intended to complement
the [Rust API Guidelines ](https://rust-lang.github.io/api-guidelines/naming.html ).
## Top-down Modules
2022-10-09 13:51:01 +11:00
Readers of the module should be able to understand your code by reading it from top to bottom.
Whenever [items ](https://doc.rust-lang.org/reference/items.html ) in your module form a parent-child relationship, the
parent should be written above the children. Typically this means that important `pub` items are placed before private
implementation details.
2022-10-09 12:06:05 +11:00
2022-10-09 13:51:01 +11:00
For instance, here are three functions. Notice how the definition of `foo` is placed above its dependencies. The parent
is `foo` while its children are `bar` and `baz` .
2022-10-09 12:06:05 +11:00
```rust
pub fn foo() {
bar();
baz();
}
fn bar() {}
fn baz() {}
```
2022-10-09 12:42:02 +11:00
This guideline applies to types as well.
2022-10-09 12:06:05 +11:00
```rust
pub struct Foo {
2022-10-09 13:51:01 +11:00
bars: Vec< Bar > ,
2022-10-09 12:06:05 +11:00
}
2022-10-09 13:51:01 +11:00
struct Bar {
2022-10-09 12:06:05 +11:00
// ...
}
```
## Getters and Setters
2023-05-25 22:09:51 +10:00
Getters should not start with a `get_` prefix.
< table >
< tr >
< th > Good< / th >
< th > Bad< / th >
< / tr >
< tr >
< td >
2022-10-09 12:06:05 +11:00
```rust
impl Foo {
fn bar(& self) -> & Bar { ... }
fn set_bar(& mut self, bar: Bar) { ... }
}
```
2023-05-25 22:09:51 +10:00
< / td >
< td >
2022-10-09 12:06:05 +11:00
```rust
impl Foo {
fn get_bar(& self) -> & Bar { ... }
fn set_bar(& mut self, bar: Bar) { ... }
}
```
2023-05-25 22:09:51 +10:00
< / td >
< / tr >
< / table >
2022-10-09 12:06:05 +11:00
See [`SocketAddr` ](https://doc.rust-lang.org/stable/std/net/enum.SocketAddr.html ) for an example of a standard library
type that uses this convention.
Under appropriate circumstances a different naming scheme can be
used. [`Command` ](https://doc.rust-lang.org/stable/std/process/struct.Command.html ) is a standard type that demonstrates
this.
2022-10-09 12:42:02 +11:00
If a `bar` field exists and no invariants need to be maintained by the getters and setters, it is usually better to make
the `bar` field public.
2023-05-25 22:09:51 +10:00
## Bevy `Event`s
2022-10-09 12:06:05 +11:00
2023-05-23 22:43:06 +10:00
Types intended to be used as events in [`EventReader`] and [`EventWriter`] should end in the `Event` suffix.
This is helpful for readers trying to distinguish events from other types in the program.
2023-05-25 22:09:51 +10:00
< table >
< tr >
< th > Good< / th >
< th > Bad< / th >
< / tr >
< tr >
< td >
2023-05-23 22:43:06 +10:00
```rust
struct CollisionEvent { ... }
fn handle_collisions(mut events: EventReader< CollisionEvent > ) { ... }
```
2023-05-25 22:09:51 +10:00
< / td >
< td >
2023-05-23 22:43:06 +10:00
```rust
struct Collision { ... }
fn handle_collisions(mut events: EventReader< Collision > ) { ... }
```
2023-05-25 22:09:51 +10:00
< / td >
< / tr >
< / table >
2023-05-23 22:43:06 +10:00
[`EventReader`]: https://docs.rs/bevy_ecs/latest/bevy_ecs/event/struct.EventReader.html
[`EventWriter`]: https://docs.rs/bevy_ecs/latest/bevy_ecs/event/struct.EventWriter.html
## Specifying Dependencies
When adding a new dependency to a crate, make sure you specify the full semver version.
2023-05-25 22:09:51 +10:00
< table >
< tr >
< th > Good< / th >
< th > Bad< / th >
< / tr >
< tr >
< td >
2023-05-23 22:43:06 +10:00
```toml
[dependencies]
serde_json = "1.0.96"
```
2023-05-25 22:09:51 +10:00
< / td >
< td >
2023-05-23 22:43:06 +10:00
```toml
[dependencies]
serde_json = "1"
```
2023-05-25 22:09:51 +10:00
< / td >
< / tr >
< / table >
## Writing Unit Tests
When writing unit tests, unwrap errors instead of returning them.
Panicking displays the line and column of the error, which is useful for debugging.
This information is lost when the error is returned.
< table >
< tr >
< th > Good< / th >
< th > Bad< / th >
< / tr >
< tr >
< td >
```rust
#[test]
fn my_test() {
some_fallible_func().unwrap();
}
```
< / td >
< td >
```rust
#[test]
fn my_test() -> anyhow::Result< ()> {
some_fallible_func()?;
// ...
Ok(())
}
```
< / td >
< / tr >
< / table >
2022-10-09 12:06:05 +11:00
## Documentation
All public items should be documented. Documentation must be written with complete sentences and correct grammar.
Consider using [intra-doc links ](https://doc.rust-lang.org/rustdoc/write-documentation/linking-to-items-by-name.html )
where appropriate.
2023-02-12 04:51:53 +11:00
## Unit Tests
Unit tests help your contributions last! They ensure that your code works as expected and that it continues to work in
the future.
2023-05-23 22:43:06 +10:00
whole-server unit tests can be found in [`crates/valence/src/tests/` ](crates/valence/src/tests ).
## Naming Quantities
2023-05-25 22:09:51 +10:00
Variables intended to hold quantities should be written with the `_count` suffix instead of the `num_` prefix.
< table >
< tr >
< th > Good< / th >
< th > Bad< / th >
< / tr >
< tr >
< td >
```rust
let block_count = ...;
```
< / td >
< td >
```rust
let num_blocks = ...;
```
< / td >
< / tr >
< / table >