-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Block editing #150
Draft
ReCore-sys
wants to merge
9
commits into
master
Choose a base branch
from
feature/block-edits
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
WIP: Block editing #150
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dc2206c
Block reading somewhat working
ReCore-sys 2c88410
now works more reliably
ReCore-sys 5efed7e
Block reading fixed
ReCore-sys af982e2
Block count tracking + dependency cleanup
ReCore-sys 26f30d5
Added packet generator script
ReCore-sys 6fed35f
Merge branch 'master' into feature/block-edits
ReCore-sys 8ad1965
Now correctly resizes sections
ReCore-sys 7acc227
Clippy + fmt
ReCore-sys aaf5d34
Fix tests
ReCore-sys File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use crate::packets::IncomingPacket; | ||
use crate::NetResult; | ||
use ferrumc_core::chunks::chunk_receiver::ChunkReceiver; | ||
use ferrumc_macros::{packet, NetDecode}; | ||
use ferrumc_state::ServerState; | ||
use std::sync::Arc; | ||
|
||
#[derive(NetDecode)] | ||
#[packet(packet_id = 0x08, state = "play")] | ||
pub struct ChunksPerTick { | ||
chunks_per_tick: f32, | ||
} | ||
|
||
impl IncomingPacket for ChunksPerTick { | ||
async fn handle(self, conn_id: usize, state: Arc<ServerState>) -> NetResult<()> { | ||
let mut chunk_recv = state.universe.get_mut::<ChunkReceiver>(conn_id)?; | ||
chunk_recv.chunks_per_tick = self.chunks_per_tick; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use crate::packets::IncomingPacket; | ||
use crate::NetResult; | ||
use ferrumc_macros::{packet, NetDecode}; | ||
use ferrumc_net_codec::net_types::network_position::NetworkPosition; | ||
use ferrumc_net_codec::net_types::var_int::VarInt; | ||
use ferrumc_state::ServerState; | ||
use std::sync::Arc; | ||
use tracing::debug; | ||
|
||
#[derive(NetDecode, Debug)] | ||
#[packet(packet_id = 0x38, state = "play")] | ||
pub struct PlaceBlock { | ||
pub hand: VarInt, | ||
pub position: NetworkPosition, | ||
pub face: VarInt, | ||
pub cursor_x: f32, | ||
pub cursor_y: f32, | ||
pub cursor_z: f32, | ||
pub inside_block: bool, | ||
pub sequence: VarInt, | ||
} | ||
|
||
impl IncomingPacket for PlaceBlock { | ||
async fn handle(self, _conn_id: usize, _state: Arc<ServerState>) -> NetResult<()> { | ||
debug!("{:?}", self); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum DataPackingError { | ||
#[error("Size ({0}) exceeds maximum size of data type: {1}")] | ||
SizeExceedsMaxSize(u8, u8), | ||
#[error("Not enough bits to read with size {0} at offset {1}")] | ||
NotEnoughBits(u8, u32), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
use crate::data_packing::errors::DataPackingError; | ||
|
||
/// Reads a specified number of bits from a given offset in a 64-bit signed integer. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `data` - A reference to the 64-bit signed integer to read from. | ||
/// * `size` - The number of bits to read (must be 16 or less). | ||
/// * `offset` - The bit offset from which to start reading. | ||
/// | ||
/// # Returns | ||
/// | ||
/// * `Ok(i16)` - The extracted bits as a 16-bit signed integer. | ||
/// * `Err(DataPackingError)` - If the size exceeds 16 bits or the offset plus size exceeds 64 bits. | ||
/// | ||
/// # Errors | ||
/// | ||
/// * `DataPackingError::SizeExceedsMaxSize` - If `size` is greater than 16. | ||
/// * `DataPackingError::NotEnoughBits` - If `offset + size` exceeds 64 bits. | ||
pub fn read_nbit_i16(data: &i64, size: u8, offset: u32) -> Result<i16, DataPackingError> { | ||
if size > 16 { | ||
return Err(DataPackingError::SizeExceedsMaxSize(size, 16)); | ||
} | ||
if offset + size as u32 > 64 { | ||
return Err(DataPackingError::NotEnoughBits(size, offset)); | ||
} | ||
let mask = (1 << size) - 1; | ||
let extracted_bits = ((data >> offset) & mask) as i16; | ||
// Sign extend if the extracted bits represent a negative number | ||
let sign_bit = 1 << (size - 1); | ||
if extracted_bits & sign_bit != 0 { | ||
Ok(extracted_bits | !mask as i16) | ||
} else { | ||
Ok(extracted_bits) | ||
} | ||
} | ||
|
||
/// Writes a specified number of bits to a given offset in a 64-bit signed integer. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `data` - A mutable reference to the 64-bit signed integer to write to. | ||
/// * `offset` - The bit offset from which to start writing. | ||
/// * `value` - The 16-bit signed integer value to write. | ||
/// * `size` - The number of bits to write (must be 16 or less). | ||
/// | ||
/// # Returns | ||
/// | ||
/// * `Ok(())` - If the bits were successfully written. | ||
/// * `Err(DataPackingError)` - If the size exceeds 16 bits or the offset plus size exceeds 64 bits. | ||
/// | ||
/// # Errors | ||
/// | ||
/// * `DataPackingError::SizeExceedsMaxSize` - If `size` is greater than 16. | ||
/// * `DataPackingError::NotEnoughBits` - If `offset + size` exceeds 64 bits. | ||
pub fn write_nbit_i16( | ||
data: &mut i64, | ||
offset: u32, | ||
value: i16, | ||
size: u8, | ||
) -> Result<(), DataPackingError> { | ||
if size > 16 { | ||
return Err(DataPackingError::SizeExceedsMaxSize(size, 16)); | ||
} | ||
if offset + size as u32 > 64 { | ||
return Err(DataPackingError::NotEnoughBits(size, offset)); | ||
} | ||
let mask = (1 << size) - 1; | ||
*data &= !(mask << offset); | ||
*data |= ((value as i64) & mask) << offset; | ||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
/// Tests the `read_nbit_i16` function with various inputs. | ||
#[test] | ||
fn test_read_nbit_i16() { | ||
let data: i64 = 0b110101011; | ||
assert_eq!(read_nbit_i16(&data, 3, 0).unwrap(), 0b011); | ||
assert_eq!(read_nbit_i16(&data, 3, 3).unwrap(), -3); // 0b101 as i16 is -3 | ||
assert_eq!(read_nbit_i16(&data, 3, 6).unwrap(), -2); // 0b110 as i16 is -2 | ||
assert_eq!(read_nbit_i16(&data, 3, 9).unwrap(), 0b000); | ||
} | ||
|
||
/// Tests the `write_nbit_i16` function with various inputs. | ||
#[test] | ||
fn test_write_nbit_i16() { | ||
let mut data: i64 = 0; | ||
write_nbit_i16(&mut data, 0, 0b011, 3).unwrap(); | ||
assert_eq!(data, 0b011); | ||
write_nbit_i16(&mut data, 3, -3, 3).unwrap(); // 0b101 as i16 is -3 | ||
assert_eq!(data, 0b101011); | ||
write_nbit_i16(&mut data, 6, -2, 3).unwrap(); // 0b110 as i16 is -2 | ||
assert_eq!(data, 0b110101011); | ||
write_nbit_i16(&mut data, 9, 0b000, 3).unwrap(); | ||
assert_eq!(data, 0b110101011); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep this as a ChunkReceiver, since you don't want to send play packets to someone in login state.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There has to be a better solution that that. Also why would a player during login have a player identity?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're more than welcome to find a better solution, but, player receives the PlayerIdentity component during login start packet.
Oh wait, you also add ChunkReceiver during login start? Add it when login process is completed, and keep this as is. So 'everyone' would be players that could receive chunks I suppose?