Skip to content

Commit

Permalink
bump targeting minecraft version to 1.20.2 (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
JieningYu authored Oct 6, 2023
1 parent ccc8f65 commit d7e698e
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 60 deletions.
4 changes: 1 addition & 3 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
[package]
name = "rimecraft_core"
name = "rimecraft-core"
version = "1.20.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rimecraft-primitives = { path = "../primitives", features = [
"serde",
Expand Down
24 changes: 12 additions & 12 deletions core/src/net/mod.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
pub mod listener;
pub mod packet;

#[repr(i32)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum State {
Handshaking = -1,
Play = 0,
Status = 1,
Login = 2,
Handshaking,
Play,
Status,
Login,
Configuration,
}

impl State {
pub fn from_id(id: i32) -> Option<Self> {
match id {
-1 => Some(Self::Handshaking),
0 => Some(Self::Play),
1 => Some(Self::Status),
2 => Some(Self::Login),
_ => None,
pub fn id(&self) -> &str {
match self {
State::Handshaking => "handshake",
State::Play => "play",
State::Status => "status",
State::Login => "login",
State::Configuration => "configuration",
}
}
}
12 changes: 12 additions & 0 deletions core/src/net/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ where
fn is_writing_err_skippable(&self) -> bool {
false
}

/// Returns a new network state to transition to, or `None`
/// to indicate no state change.
///
/// The state transition is done on both the sender and receiver
/// sides, but it is only in one direction (out of C2S and S2C).
/// Another packet must be processed in the reverse direction to
/// ensure the state in both directions are updated.
#[inline]
fn new_net_state(&self) -> Option<crate::net::State> {
None
}
}

/// Provides an abstraction to [`Packet::apply`], without complex
Expand Down
75 changes: 60 additions & 15 deletions core/src/net/packet/c2s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ pub struct Handshake {
proto_ver: i32,
addr: String,
port: u16,
intended_state: crate::net::State,
intended_state: ConnectionIntent,
}

impl Handshake {
#[inline]
pub fn new(addr: String, port: u16, intended_state: crate::net::State) -> Self {
pub fn new(addr: String, port: u16, intended_state: ConnectionIntent) -> Self {
///TODO: Need to implement net.minecraft.SharedConstants
const PROTO_VER: i32 = 114514;

Expand All @@ -26,6 +26,26 @@ impl Handshake {
intended_state,
}
}

#[inline]
pub fn proto_version(&self) -> i32 {
self.proto_ver
}

#[inline]
pub fn address(&self) -> &str {
&self.addr
}

#[inline]
pub fn port(&self) -> u16 {
self.port
}

#[inline]
pub fn intended_state(&self) -> ConnectionIntent {
self.intended_state
}
}

impl Encode for Handshake {
Expand All @@ -44,7 +64,6 @@ impl Encode for Handshake {
impl<'de> Decode<'de> for Handshake {
type Output = Self;

#[inline]
fn decode<B>(buf: &'de mut B) -> anyhow::Result<Self::Output>
where
B: bytes::Buf,
Expand All @@ -58,16 +77,42 @@ impl<'de> Decode<'de> for Handshake {
proto_ver,
addr,
port,
intended_state: crate::net::State::from_id(state).unwrap(),
intended_state: ConnectionIntent::n(state)
.ok_or_else(|| anyhow::anyhow!("unknown connection intent: {state}"))?,
})
}
}

impl<L> super::Packet<L> for Handshake where L: listener::Accept<Self> {}
impl<L> super::Packet<L> for Handshake
where
L: listener::Accept<Self>,
{
#[inline]
fn new_net_state(&self) -> Option<crate::net::State> {
Some(self.intended_state.state())
}
}

#[repr(i32)]
#[derive(Clone, Copy, PartialEq, Eq, enumn::N)]
pub enum ConnectionIntent {
Status,
Login,
}

impl ConnectionIntent {
#[inline]
pub fn state(self) -> crate::net::State {
match self {
ConnectionIntent::Status => crate::net::State::Status,
ConnectionIntent::Login => crate::net::State::Login,
}
}
}

pub struct LoginHello {
name: String,
uuid: Option<uuid::Uuid>,
profile_id: uuid::Uuid,
}

impl Encode for LoginHello {
Expand All @@ -77,7 +122,7 @@ impl Encode for LoginHello {
B: bytes::BufMut,
{
self.name.encode(buf)?;
self.uuid.encode(buf)
self.profile_id.encode(buf)
}
}

Expand All @@ -90,9 +135,12 @@ impl<'de> Decode<'de> for LoginHello {
B: bytes::Buf,
{
let name = String::decode(buf)?;
let uuid = Option::<uuid::Uuid>::decode(buf)?;
let uuid = uuid::Uuid::decode(buf)?;

Ok(Self { name, uuid })
Ok(Self {
name,
profile_id: uuid,
})
}
}

Expand All @@ -108,11 +156,6 @@ impl LoginQueryRes {
pub fn query_id(&self) -> i32 {
self.query_id
}

#[inline]
pub fn response(&self) -> Option<&[u8]> {
self.res.as_ref().map(|value| &value[..])
}
}

impl Encode for LoginQueryRes {
Expand Down Expand Up @@ -151,7 +194,9 @@ impl<'de> Decode<'de> for LoginQueryRes {
{
let remaining = buf.remaining();
if remaining <= super::QUERY_MAX_PAYLOAD_LEN {
Ok(buf.copy_to_bytes(remaining))
// this was changed in 1.20.2 so the bytes are empty
buf.advance(remaining);
Ok(Bytes::new())
} else {
Err(anyhow::anyhow!(
"payload may not be larger than {} bytes",
Expand Down
21 changes: 9 additions & 12 deletions core/src/net/packet/s2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,9 @@ impl LoginQueryReq {
}

#[inline]
pub fn channel(&self) -> &Id {
pub fn channel_id(&self) -> &Id {
&self.channel
}

#[inline]
pub fn payload(&self) -> &[u8] {
&self.payload
}
}

impl Encode for LoginQueryReq {
Expand Down Expand Up @@ -219,7 +214,9 @@ impl<'de> Decode<'de> for LoginQueryReq {

let remaining = buf.remaining();
if remaining <= super::QUERY_MAX_PAYLOAD_LEN {
let payload = buf.copy_to_bytes(remaining);
// this was changed in 1.20.2
buf.advance(remaining);
let payload = Bytes::new();
Ok(Self {
query_id,
channel,
Expand All @@ -238,11 +235,11 @@ impl<L> super::Packet<L> for LoginQueryReq where L: listener::Accept<Self> {}

//TODO: LoginSuccessS2CPacket and authlib's GameProfile implementation

pub struct QueryPong {
pub struct PingResult {
start_time: u64,
}

impl QueryPong {
impl PingResult {
#[inline]
pub fn new(start_time: u64) -> Self {
Self { start_time }
Expand All @@ -254,7 +251,7 @@ impl QueryPong {
}
}

impl Encode for QueryPong {
impl Encode for PingResult {
#[inline]
fn encode<B>(&self, buf: &mut B) -> anyhow::Result<()>
where
Expand All @@ -265,7 +262,7 @@ impl Encode for QueryPong {
}
}

impl<'de> Decode<'de> for QueryPong {
impl<'de> Decode<'de> for PingResult {
type Output = Self;

#[inline]
Expand All @@ -279,6 +276,6 @@ impl<'de> Decode<'de> for QueryPong {
}
}

impl<L> super::Packet<L> for QueryPong where L: listener::Accept<Self> {}
impl<L> super::Packet<L> for PingResult where L: listener::Accept<Self> {}

//TODO: QueryResponseS2CPacket and ServerMetadata
4 changes: 2 additions & 2 deletions core/src/registry/tag.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rimecraft_primitives::Id;

static KEYS_CACHE: once_cell::sync::Lazy<rimecraft_caches::ArcCaches<Id>> =
once_cell::sync::Lazy::new(rimecraft_caches::ArcCaches::new);
static KEYS_CACHE: once_cell::sync::Lazy<rimecraft_caches::arc::Caches<Id>> =
once_cell::sync::Lazy::new(rimecraft_caches::arc::Caches::new);

/// Represents a tag key.
pub struct Key<T> {
Expand Down
20 changes: 14 additions & 6 deletions core/src/util/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::ops::Deref;
/// A box with double-valued coords.
/// The box is axis-aligned and the coords are minimum inclusive and maximum exclusive.
#[derive(Clone, Copy, PartialEq)]
pub struct Box {
pub struct BoundingBox {
pub min_x: f64,
pub min_y: f64,
pub min_z: f64,
Expand All @@ -12,8 +12,9 @@ pub struct Box {
pub max_z: f64,
}

impl Box {
impl BoundingBox {
/// Creates a box of the given positions (in (x, y, z)) as corners.
#[inline]
pub fn new<T: Into<(f64, f64, f64)>>(pos1: T, pos2: T) -> Self {
let p1 = pos1.into();
let p2 = pos2.into();
Expand Down Expand Up @@ -72,16 +73,19 @@ impl Box {
self
}

#[inline]
pub fn expand(mut self, x: f64, y: f64, z: f64) -> Self {
self.min_x -= x;
self.min_y -= y;
self.min_z -= z;
self.max_x += x;
self.max_y += y;
self.max_z += z;

self
}

#[inline]
pub fn expand_all(self, value: f64) -> Self {
self.expand(value, value, value)
}
Expand Down Expand Up @@ -146,6 +150,7 @@ impl Box {

/// Creates a box that is translated by the given offset
/// on each axis from this box.
#[inline]
pub fn offset(mut self, x: f64, y: f64, z: f64) -> Self {
self.min_x += x;
self.min_y += y;
Expand All @@ -156,6 +161,7 @@ impl Box {
self
}

#[inline]
pub fn is_nan(self) -> bool {
self.min_x.is_nan()
|| self.min_y.is_nan()
Expand All @@ -166,7 +172,8 @@ impl Box {
}
}

impl std::hash::Hash for Box {
impl std::hash::Hash for BoundingBox {
#[inline]
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
state.write_u64(self.min_x.to_bits());
state.write_u64(self.min_y.to_bits());
Expand All @@ -177,9 +184,10 @@ impl std::hash::Hash for Box {
}
}

impl Eq for Box {}
impl Eq for BoundingBox {}

impl From<glam::DVec3> for Box {
impl From<glam::DVec3> for BoundingBox {
#[inline]
fn from(value: glam::DVec3) -> Self {
Self {
min_x: value.x,
Expand All @@ -192,7 +200,7 @@ impl From<glam::DVec3> for Box {
}
}

impl std::fmt::Display for Box {
impl std::fmt::Display for BoundingBox {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Box[")?;

Expand Down
2 changes: 1 addition & 1 deletion core/src/world/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ where
})
.sum::<usize>()
}
Inner::Indexed(_) => VarI32(0).len(),
Inner::Indexed(_) => 0,
Inner::Singular(option) => {
if let Some(entry) = option {
VarI32(self.ids.index_of(&entry).map(|e| e as i32).unwrap_or(-1)).len()
Expand Down
2 changes: 1 addition & 1 deletion primitives/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Primitive types and traits for building Rimecraft applications.

## Status

Targeting MCJE version: `1.20.1`
Targeting MCJE version: `1.20.2`

## Features

Expand Down
Loading

0 comments on commit d7e698e

Please sign in to comment.