Skip to content

Commit

Permalink
Merge branch 'main' into motion_blocking_heightmap
Browse files Browse the repository at this point in the history
  • Loading branch information
rj00a authored Sep 16, 2023
2 parents 8bc6f77 + c3d112d commit 3cae49a
Show file tree
Hide file tree
Showing 49 changed files with 868 additions and 651 deletions.
32 changes: 28 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ env:

jobs:
valence-fmt:
name: Formatting
runs-on: ubuntu-latest
steps:
- name: Checkout Actions Repository
Expand All @@ -30,6 +31,7 @@ jobs:
run: cargo fmt --all -- --check

valence-docs:
name: Documentation
runs-on: ubuntu-latest
steps:
- name: Checkout Actions Repository
Expand All @@ -45,9 +47,10 @@ jobs:
run: sudo apt-get update && sudo apt-get install -y libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev libxkbcommon-dev libssl-dev libclang-dev libgtk-3-dev

- name: Validate documentation
run: cargo doc --workspace --no-deps --all-features --document-private-items
run: RUSTDOCFLAGS="-D warnings" cargo doc --workspace --no-deps --all-features --document-private-items

typos:
name: Typos
runs-on: ubuntu-latest
steps:
- name: Checkout Actions Repository
Expand All @@ -57,6 +60,7 @@ jobs:
uses: crate-ci/[email protected]

valence-clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- name: Checkout Actions Repository
Expand Down Expand Up @@ -88,9 +92,8 @@ jobs:
include:
- style: default
flags: ""

name: Tests (${{ matrix.platform }})
runs-on: ${{ matrix.platform }}

steps:
- name: Checkout Actions Repository
uses: actions/checkout@v3
Expand All @@ -114,7 +117,8 @@ jobs:
- name: Run valence_nbt tests without preserve_order feature
run: cargo test -p valence_nbt --all-targets

extractor-tests:
extractor-build:
name: Build Extractor
runs-on: ubuntu-latest
steps:
- name: Checkout Actions Repository
Expand All @@ -131,6 +135,7 @@ jobs:
working-directory: extractor

check-depgraph:
name: Dependency Graph (assets/depgraph.svg)
runs-on: ubuntu-latest
steps:
- name: Checkout Actions Repository
Expand Down Expand Up @@ -183,3 +188,22 @@ jobs:
with:
name: bad-depgraph
path: bad-depgraph/
udeps:
name: Unused Dependencies
runs-on: ubuntu-latest
steps:
- name: Checkout Actions Repository
uses: actions/checkout@v3

- name: Setup Rust toolchain and cache
uses: actions-rust-lang/[email protected]
with:
toolchain: "nightly"

- name: Install cargo-udeps
uses: baptiste0928/[email protected]
with:
crate: cargo-udeps

- name: Check for unused dependencies
run: cargo udeps --all --all-features
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ directories = "5.0.0"
eframe = { version = "0.22.0", default-features = false }
egui = "0.22.0"
egui_dock = "0.6"
enum-map = "2.5.0"
flate2 = "1.0.24"
flume = "0.11.0"
fs_extra = "1.2.0"
Expand Down
10 changes: 6 additions & 4 deletions crates/valence_anvil/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ impl Region {
None => return Err(RegionError::InvalidCompressionScheme(compression)),
};

let (data, _) = Compound::from_binary(&mut nbt_slice)?;
let (data, _) = valence_nbt::from_binary(&mut nbt_slice)?;

if !nbt_slice.is_empty() {
return Err(RegionError::TrailingNbtData);
Expand Down Expand Up @@ -553,15 +553,17 @@ impl Region {
compress_buf.clear();
let mut compress_cursor = Cursor::new(compress_buf);
match options.compression {
Compression::Gzip => chunk.to_binary(
Compression::Gzip => valence_nbt::to_binary(
chunk,
GzEncoder::new(&mut compress_cursor, flate2::Compression::default()),
"",
)?,
Compression::Zlib => chunk.to_binary(
Compression::Zlib => valence_nbt::to_binary(
chunk,
ZlibEncoder::new(&mut compress_cursor, flate2::Compression::default()),
"",
)?,
Compression::None => chunk.to_binary(&mut compress_cursor, "")?,
Compression::None => valence_nbt::to_binary(chunk, &mut compress_cursor, "")?,
}
let compress_buf = compress_cursor.into_inner();

Expand Down
31 changes: 12 additions & 19 deletions crates/valence_boss_bar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub use valence_server::protocol::packets::play::boss_bar_s2c::{
};
use valence_server::protocol::packets::play::BossBarS2c;
use valence_server::protocol::WritePacket;
use valence_server::{ChunkPos, ChunkView, Despawned, EntityLayer, Layer, UniqueId};
use valence_server::{ChunkView, Despawned, EntityLayer, Layer, UniqueId};

mod components;
pub use components::*;
Expand Down Expand Up @@ -68,9 +68,7 @@ fn update_boss_bar<T: Component + ToPacketAction>(
action: part.to_packet_action(),
};
if let Some(pos) = pos {
entity_layer
.view_writer(pos.to_chunk_pos())
.write_packet(&packet);
entity_layer.view_writer(pos.0).write_packet(&packet);
} else {
entity_layer.write_packet(&packet);
}
Expand Down Expand Up @@ -111,7 +109,7 @@ fn update_boss_bar_layer_view(
_old_view_distance,
) in clients_query.iter_mut()
{
let view = ChunkView::new(ChunkPos::from_pos(position.0), view_distance.get());
let view = ChunkView::new(position.0.into(), view_distance.get());

let old_layers = old_visible_entity_layers.get();
let current_layers = &visible_entity_layers.0;
Expand All @@ -122,7 +120,7 @@ fn update_boss_bar_layer_view(
.filter(|(_, _, _, _, _, layer_id, _)| layer_id.0 == added_layer)
{
if let Some(position) = boss_bar_position {
if view.contains(position.to_chunk_pos()) {
if view.contains(position.0.into()) {
client.write_packet(&BossBarS2c {
id: id.0,
action: BossBarAction::Add {
Expand Down Expand Up @@ -155,7 +153,7 @@ fn update_boss_bar_layer_view(
.filter(|(_, _, _, _, _, layer_id, _)| layer_id.0 == removed_layer)
{
if let Some(position) = boss_bar_position {
if view.contains(position.to_chunk_pos()) {
if view.contains(position.0.into()) {
client.write_packet(&BossBarS2c {
id: id.0,
action: BossBarAction::Remove,
Expand Down Expand Up @@ -205,19 +203,16 @@ fn update_boss_bar_chunk_view(
old_view_distance,
) in clients_query.iter_mut()
{
let view = ChunkView::new(ChunkPos::from_pos(position.0), view_distance.get());
let old_view = ChunkView::new(
ChunkPos::from_pos(old_position.get()),
old_view_distance.get(),
);
let view = ChunkView::new(position.0.into(), view_distance.get());
let old_view = ChunkView::new(old_position.get().into(), old_view_distance.get());

for layer in visible_entity_layers.0.iter() {
for (id, title, health, style, flags, _, boss_bar_position) in boss_bars_query
.iter()
.filter(|(_, _, _, _, _, layer_id, _)| layer_id.0 == *layer)
{
if view.contains(boss_bar_position.to_chunk_pos())
&& !old_view.contains(boss_bar_position.to_chunk_pos())
if view.contains(boss_bar_position.0.into())
&& !old_view.contains(boss_bar_position.0.into())
{
client.write_packet(&BossBarS2c {
id: id.0,
Expand All @@ -229,8 +224,8 @@ fn update_boss_bar_chunk_view(
flags: *flags,
},
});
} else if !view.contains(boss_bar_position.to_chunk_pos())
&& old_view.contains(boss_bar_position.to_chunk_pos())
} else if !view.contains(boss_bar_position.0.into())
&& old_view.contains(boss_bar_position.0.into())
{
client.write_packet(&BossBarS2c {
id: id.0,
Expand All @@ -253,9 +248,7 @@ fn boss_bar_despawn(
action: BossBarAction::Remove,
};
if let Some(pos) = position {
entity_layer
.view_writer(pos.to_chunk_pos())
.write_packet(&packet);
entity_layer.view_writer(pos.0).write_packet(&packet);
} else {
entity_layer.write_packet(&packet);
}
Expand Down
18 changes: 1 addition & 17 deletions crates/valence_entity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use paste::paste;
use tracing::warn;
use tracked_data::TrackedData;
use valence_math::{DVec3, Vec3};
use valence_protocol::{BlockPos, ChunkPos, Decode, Encode, VarInt};
use valence_protocol::{Decode, Encode, VarInt};
use valence_server_common::{Despawned, UniqueId};

include!(concat!(env!("OUT_DIR"), "/entity.rs"));
Expand Down Expand Up @@ -209,14 +209,6 @@ impl Position {
Self(pos.into())
}

pub fn to_chunk_pos(self) -> ChunkPos {
ChunkPos::from_pos(self.0)
}

pub fn to_block_pos(self) -> BlockPos {
BlockPos::from_pos(self.0)
}

pub fn get(self) -> DVec3 {
self.0
}
Expand Down Expand Up @@ -246,14 +238,6 @@ impl OldPosition {
pub fn get(&self) -> DVec3 {
self.0
}

pub fn chunk_pos(&self) -> ChunkPos {
ChunkPos::from_pos(self.0)
}

pub fn to_block_pos(&self) -> BlockPos {
BlockPos::from_pos(self.0)
}
}

impl PartialEq<Position> for OldPosition {
Expand Down
27 changes: 26 additions & 1 deletion crates/valence_inventory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub use valence_server::protocol::packets::play::player_action_c2s::PlayerAction
use valence_server::protocol::packets::play::{
ClickSlotC2s, CloseHandledScreenC2s, CloseScreenS2c, CreativeInventoryActionC2s, InventoryS2c,
OpenScreenS2c, PlayerActionC2s, ScreenHandlerSlotUpdateS2c, UpdateSelectedSlotC2s,
UpdateSelectedSlotS2c,
};
use valence_server::protocol::{VarInt, WritePacket};
use valence_server::text::IntoText;
Expand All @@ -54,6 +55,7 @@ impl Plugin for InventoryPlugin {
PostUpdate,
(
update_client_on_close_inventory.before(update_open_inventories),
update_player_selected_slot,
update_open_inventories,
update_player_inventories,
)
Expand Down Expand Up @@ -405,6 +407,16 @@ impl HeldItem {
pub fn slot(&self) -> u16 {
self.held_item_slot
}

pub fn set_slot(&mut self, slot: u16) {
// temp
assert!(
(36..=44).contains(&slot),
"slot index of {slot} out of bounds"
);

self.held_item_slot = slot;
}
}

/// The item stack that the client thinks it's holding under the mouse
Expand Down Expand Up @@ -1216,18 +1228,31 @@ pub struct UpdateSelectedSlotEvent {
pub slot: u8,
}

/// Handles the `HeldItem` component being changed on a client entity, which
/// indicates that the server has changed the selected hotbar slot.
fn update_player_selected_slot(mut clients: Query<(&mut Client, &HeldItem), Changed<HeldItem>>) {
for (mut client, held_item) in &mut clients {
client.write_packet(&UpdateSelectedSlotS2c {
slot: (held_item.held_item_slot - PLAYER_INVENTORY_MAIN_SLOTS_COUNT) as u8,
});
}
}

/// Client to Server HeldItem Slot
fn handle_update_selected_slot(
mut packets: EventReader<PacketEvent>,
mut clients: Query<&mut HeldItem>,
mut events: EventWriter<UpdateSelectedSlotEvent>,
) {
for packet in packets.iter() {
if let Some(pkt) = packet.decode::<UpdateSelectedSlotC2s>() {
if let Ok(mut held) = clients.get_mut(packet.client) {
if let Ok(mut mut_held) = clients.get_mut(packet.client) {
let held = mut_held.bypass_change_detection();
if pkt.slot > 8 {
// The client is trying to interact with a slot that does not exist, ignore.
continue;
}

held.held_item_slot = convert_hotbar_slot_id(pkt.slot);

events.send(UpdateSelectedSlotEvent {
Expand Down
33 changes: 29 additions & 4 deletions crates/valence_nbt/src/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! # Examples
//!
//! ```
//! use valence_nbt::{compound, Compound, List};
//! use valence_nbt::{compound, to_binary, Compound, List};
//!
//! let c = compound! {
//! "byte" => 5_i8,
Expand All @@ -18,21 +18,21 @@
//!
//! let mut buf = vec![];
//!
//! c.to_binary(&mut buf, "").unwrap();
//! to_binary(&c, &mut buf, "").unwrap();
//! ```
//!
//! Decode NBT data from its binary form.
//!
//! ```
//! use valence_nbt::{compound, Compound};
//! use valence_nbt::{compound, from_binary, Compound};
//!
//! let some_bytes = [10, 0, 0, 3, 0, 3, 105, 110, 116, 0, 0, 222, 173, 0];
//!
//! let expected_value = compound! {
//! "int" => 0xdead
//! };
//!
//! let (nbt, root_name) = Compound::from_binary(&mut some_bytes.as_slice()).unwrap();
//! let (nbt, root_name) = from_binary(&mut some_bytes.as_slice()).unwrap();
//!
//! assert_eq!(nbt, expected_value);
//! assert_eq!(root_name, "");
Expand All @@ -45,4 +45,29 @@ mod modified_utf8;
#[cfg(test)]
mod tests;

pub use decode::from_binary;
pub use encode::{to_binary, written_size};
pub use error::*;

use crate::Tag;

impl Tag {
/// Returns the name of this tag for error reporting purposes.
const fn name(self) -> &'static str {
match self {
Tag::End => "end",
Tag::Byte => "byte",
Tag::Short => "short",
Tag::Int => "int",
Tag::Long => "long",
Tag::Float => "float",
Tag::Double => "double",
Tag::ByteArray => "byte array",
Tag::String => "string",
Tag::List => "list",
Tag::Compound => "compound",
Tag::IntArray => "int array",
Tag::LongArray => "long array",
}
}
}
Loading

0 comments on commit 3cae49a

Please sign in to comment.