Skip to content

Commit

Permalink
dbg_impl & remove Frame edcode
Browse files Browse the repository at this point in the history
  • Loading branch information
0123456789-jpg committed Mar 18, 2024
1 parent 4ab032e commit ead853f
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 58 deletions.
47 changes: 47 additions & 0 deletions crates/core/advancement/src/dbg_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use crate::{AdvancementCx, DisplayInfo, NewDisplayInfoDescriptor};
use std::fmt::Debug;

impl<Cx> Debug for DisplayInfo<'_, Cx>
where
Cx: AdvancementCx + Debug,
Cx::Content: Debug,
Cx::Id: Debug,
Cx::Compound: Debug,
Cx::StyleExt: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DisplayInfo")
.field("title", &self.title)
.field("description", &self.description)
.field("icon", &self.icon)
.field("background", &self.background)
.field("frame", &self.frame)
.field("show_toast", &self.show_toast)
.field("announce_to_chat", &self.announce_to_chat)
.field("hidden", &self.hidden)
.field("pos", &self.pos)
.finish()
}
}

impl<Cx> Debug for NewDisplayInfoDescriptor<'_, Cx>
where
Cx: AdvancementCx + Debug,
Cx::Content: Debug,
Cx::Id: Debug,
Cx::Compound: Debug,
Cx::StyleExt: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("NewDisplayInfoDescriptor")
.field("title", &self.title)
.field("description", &self.description)
.field("icon", &self.icon)
.field("background", &self.background)
.field("frame", &self.frame)
.field("show_toast", &self.show_toast)
.field("announce_to_chat", &self.announce_to_chat)
.field("hidden", &self.hidden)
.finish()
}
}
28 changes: 1 addition & 27 deletions crates/core/advancement/src/edcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,7 @@ pub trait AdvancementEdcodeCx:
AdvancementCx + for<'a> WriteNbt<Option<&'a Self::Compound>>
{
/// Given [`FrameData::name`], returns corresponding [`Frame`].
fn frame_fmt(name: &str) -> Frame<Self>;
}

impl<Cx> Encode for Frame<Cx>
where
Cx: AdvancementEdcodeCx,
{
fn encode<B>(&self, buf: B) -> Result<(), std::io::Error>
where
B: rimecraft_edcode::bytes::BufMut,
{
self.data.name.encode(buf)?;
Ok(())
}
}

impl<Cx> Decode for Frame<Cx>
where
Cx: AdvancementEdcodeCx,
{
fn decode<B>(mut buf: B) -> Result<Self, std::io::Error>
where
B: rimecraft_edcode::bytes::Buf,
{
let name = decode_cow_str(&mut buf)?;
Ok(Cx::frame_fmt(&name))
}
fn frame_fmt(name: &str) -> Frame;
}

impl<Cx> Encode for DisplayInfo<'_, Cx>
Expand Down
64 changes: 33 additions & 31 deletions crates/core/advancement/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#[cfg(feature = "edcode")]
mod edcode;

use std::{fmt::Debug, marker::PhantomData};
mod dbg_impl;

use rimecraft_fmt::Formatting;
use rimecraft_item::{stack::ItemStackCx, ItemStack};
Expand Down Expand Up @@ -40,7 +40,7 @@ where
description: Text<Cx>,
icon: ItemStack<'r, Cx>,
background: Option<Cx::Id>,
frame: Frame<Cx>,
frame: Frame,
/// Send message in chat when obtained.
show_toast: bool,
announce_to_chat: bool,
Expand All @@ -62,7 +62,7 @@ where
/// See [`DisplayInfo::background`].
pub background: Option<Cx::Id>,
/// See [`DisplayInfo::frame`].
pub frame: Frame<Cx>,
pub frame: Frame,
/// See [`DisplayInfo::show_toast`].
pub show_toast: bool,
/// See [`DisplayInfo::announce_to_chat`].
Expand Down Expand Up @@ -115,40 +115,42 @@ where
/// Describes how an advancement will be announced in the chat.
/// # MCJE Reference
/// `net.minecraft.advancement.AdvancementFrame` in yarn.
#[derive(Debug)]
pub struct Frame<Cx: AdvancementCx> {
pub(crate) data: &'static FrameData,
kim: PhantomData<Cx>,
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum Frame {
/// Regular task.
Task = 0,
/// A hard challenge, sometimes hidden.
Challenge = 1,
/// A regular goal.
Goal = 2,
}

impl Frame {
/// Turns [`Frame`] into corresponding [`FrameData`].
pub fn data(&self) -> FrameData {
match self {
Self::Task => FrameData {
name: "task",
fmt: Formatting::Green,
},
Self::Challenge => FrameData {
name: "challenge",
fmt: Formatting::DarkPurple,
},
Self::Goal => FrameData {
name: "goal",
fmt: Formatting::Green,
},
}
}
}

/// Inner type of [`Frame`].
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct FrameData {
/// Chat announcement text id.
pub name: &'static str,
/// Color
pub fmt: Formatting,
}

impl<Cx> Debug for DisplayInfo<'_, Cx>
where
Cx: AdvancementCx + Debug,
Cx::Content: Debug,
Cx::Id: Debug,
Cx::Compound: Debug,
Cx::StyleExt: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DisplayInfo")
.field("title", &self.title)
.field("description", &self.description)
.field("icon", &self.icon)
.field("background", &self.background)
.field("frame", &self.frame)
.field("show_toast", &self.show_toast)
.field("announce_to_chat", &self.announce_to_chat)
.field("hidden", &self.hidden)
.field("pos", &self.pos)
.finish()
}
}

0 comments on commit ead853f

Please sign in to comment.