Create pumpkin-core, and move pumpkin-text
This commit is contained in:
1 parent
5d023e696c
commit
8eb894ef13
31 files changed
+43
-41
No files matched your search
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "pumpkin-core"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
fastnbt = { git = "https://github.com/owengage/fastnbt.git" }
|
||||
uuid.workspace = true
|
||||
@@ -0,0 +1 @@
|
||||
pub mod text;
|
||||
@@ -0,0 +1,46 @@
|
||||
### Pumpkin Text
|
||||
Here we build Mojang's Textcomponent, Which is used across many places, Often where text is send to the player.
|
||||
|
||||
### Features
|
||||
- Colors
|
||||
- [ ] RBG
|
||||
- [x] Black
|
||||
- [x] Dark Blue
|
||||
- [x] Dark Green
|
||||
- [x] Dark Aqua
|
||||
- [x] Dark Red
|
||||
- [x] Dark Purple
|
||||
- [x] Gold
|
||||
- [x] Gray
|
||||
- [x] Dark Gray
|
||||
- [x] Blue
|
||||
- [x] Green
|
||||
- [x] Aqua
|
||||
- [x] Red
|
||||
- [x] Light Purple
|
||||
- [x] Yellow
|
||||
- [x] White
|
||||
- Style
|
||||
- [x] Bold
|
||||
- [x] Italic
|
||||
- [x] Underlined
|
||||
- [x] Strikethrough
|
||||
- [x] Obfuscated
|
||||
- [x] Insertion
|
||||
- Click Event
|
||||
- [x] Open URL
|
||||
- [x] Run Command
|
||||
- [x] Suggest Command
|
||||
- [x] Change Pange (Book)
|
||||
- [x] Copy to Clipboard
|
||||
- Hover Event
|
||||
- [x] Show Text
|
||||
- [x] Show Item
|
||||
- [x] ShowEntity
|
||||
- Fonts
|
||||
- [x] Default
|
||||
- [ ] Uniform (Unicode)
|
||||
- [ ] Alt
|
||||
- [ ] Illageralt
|
||||
|
||||
Reference: https://wiki.vg/Text_formatting
|
||||
@@ -0,0 +1,21 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Action to take on click of the text.
|
||||
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
|
||||
#[serde(tag = "action", content = "value", rename_all = "snake_case")]
|
||||
pub enum ClickEvent<'a> {
|
||||
/// Opens a URL
|
||||
OpenUrl(Cow<'a, str>),
|
||||
/// Works in signs, but only on the root text component
|
||||
RunCommand(Cow<'a, str>),
|
||||
/// Replaces the contents of the chat box with the text, not necessarily a
|
||||
/// command.
|
||||
SuggestCommand(Cow<'a, str>),
|
||||
/// Only usable within written books. Changes the page of the book. Indexing
|
||||
/// starts at 1.
|
||||
ChangePage(i32),
|
||||
/// Copies the given text to system clipboard
|
||||
CopyToClipboard(Cow<'a, str>),
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Text color
|
||||
#[derive(Default, Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum Color {
|
||||
/// The default color for the text will be used, which varies by context
|
||||
/// (in some cases, it's white; in others, it's black; in still others, it
|
||||
/// is a shade of gray that isn't normally used on text).
|
||||
#[default]
|
||||
Reset,
|
||||
/// One of the 16 named Minecraft colors
|
||||
Named(NamedColor),
|
||||
}
|
||||
|
||||
/// Named Minecraft color
|
||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum NamedColor {
|
||||
Black = 0,
|
||||
DarkBlue,
|
||||
DarkGreen,
|
||||
DarkAqua,
|
||||
DarkRed,
|
||||
DarkPurple,
|
||||
Gold,
|
||||
Gray,
|
||||
DarkGray,
|
||||
Blue,
|
||||
Green,
|
||||
Aqua,
|
||||
Red,
|
||||
LightPurple,
|
||||
Yellow,
|
||||
White,
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::Text;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(tag = "action", content = "contents", rename_all = "snake_case")]
|
||||
#[allow(clippy::enum_variant_names)]
|
||||
pub enum HoverEvent<'a> {
|
||||
/// Displays a tooltip with the given text.
|
||||
ShowText(Text<'a>),
|
||||
/// Shows an item.
|
||||
ShowItem {
|
||||
/// Resource identifier of the item
|
||||
id: Cow<'a, str>,
|
||||
/// Number of the items in the stack
|
||||
count: Option<i32>,
|
||||
/// NBT information about the item (sNBT format)
|
||||
tag: Cow<'a, str>,
|
||||
},
|
||||
/// Shows an entity.
|
||||
ShowEntity {
|
||||
/// The entity's UUID
|
||||
id: uuid::Uuid,
|
||||
/// Resource identifier of the entity
|
||||
#[serde(rename = "type")]
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
kind: Option<Cow<'a, str>>,
|
||||
/// Optional custom name for the entity
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
name: Option<Text<'a>>,
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
use core::str;
|
||||
use std::borrow::Cow;
|
||||
|
||||
use click::ClickEvent;
|
||||
use color::Color;
|
||||
use fastnbt::SerOpts;
|
||||
use hover::HoverEvent;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use style::Style;
|
||||
|
||||
pub mod click;
|
||||
pub mod color;
|
||||
pub mod hover;
|
||||
pub mod style;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct Text<'a>(pub Box<TextComponent<'a>>);
|
||||
|
||||
// Represents a Text component
|
||||
// Reference: https://wiki.vg/Text_formatting#Text_components
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct TextComponent<'a> {
|
||||
/// The actual text
|
||||
#[serde(flatten)]
|
||||
pub content: TextContent<'a>,
|
||||
/// Style of the text. Bold, Italic, underline, Color...
|
||||
/// Also has `ClickEvent
|
||||
#[serde(flatten)]
|
||||
pub style: Style<'a>,
|
||||
}
|
||||
|
||||
impl<'a> TextComponent<'a> {
|
||||
pub fn text(text: &'a str) -> Self {
|
||||
Self {
|
||||
content: TextContent::Text { text: text.into() },
|
||||
style: Style::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> serde::Serialize for TextComponent<'a> {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
serializer.serialize_bytes(self.encode().as_slice())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TextComponent<'a> {
|
||||
pub fn color(mut self, color: Color) -> Self {
|
||||
self.style.color = Some(color);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn color_named(mut self, color: color::NamedColor) -> Self {
|
||||
self.style.color = Some(Color::Named(color));
|
||||
self
|
||||
}
|
||||
|
||||
/// Makes the text bold
|
||||
pub fn bold(mut self) -> Self {
|
||||
self.style.bold = Some(1);
|
||||
self
|
||||
}
|
||||
|
||||
/// Makes the text italic
|
||||
pub fn italic(mut self) -> Self {
|
||||
self.style.italic = Some(1);
|
||||
self
|
||||
}
|
||||
|
||||
/// Makes the text underlined
|
||||
pub fn underlined(mut self) -> Self {
|
||||
self.style.underlined = Some(1);
|
||||
self
|
||||
}
|
||||
|
||||
/// Makes the text strikethrough
|
||||
pub fn strikethrough(mut self) -> Self {
|
||||
self.style.strikethrough = Some(1);
|
||||
self
|
||||
}
|
||||
|
||||
/// Makes the text obfuscated
|
||||
pub fn obfuscated(mut self) -> Self {
|
||||
self.style.obfuscated = Some(1);
|
||||
self
|
||||
}
|
||||
|
||||
/// When the text is shift-clicked by a player, this string is inserted in their chat input. It does not overwrite any existing text the player was writing. This only works in chat messages
|
||||
pub fn insertion(mut self, text: String) -> Self {
|
||||
self.style.insertion = Some(text);
|
||||
self
|
||||
}
|
||||
|
||||
/// Allows for events to occur when the player clicks on text. Only work in chat.
|
||||
pub fn click_event(mut self, event: ClickEvent<'a>) -> Self {
|
||||
self.style.click_event = Some(event);
|
||||
self
|
||||
}
|
||||
|
||||
/// Allows for a tooltip to be displayed when the player hovers their mouse over text.
|
||||
pub fn hover_event(mut self, event: HoverEvent<'a>) -> Self {
|
||||
self.style.hover_event = Some(event);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn encode(&self) -> Vec<u8> {
|
||||
// TODO: Somehow fix this ugly mess
|
||||
#[derive(serde::Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct TempStruct<'a> {
|
||||
#[serde(flatten)]
|
||||
text: &'a TextContent<'a>,
|
||||
#[serde(flatten)]
|
||||
style: &'a Style<'a>,
|
||||
}
|
||||
let astruct = TempStruct {
|
||||
text: &self.content,
|
||||
style: &self.style,
|
||||
};
|
||||
// dbg!(&serde_json::to_string(&astruct));
|
||||
|
||||
fastnbt::to_bytes_with_opts(&astruct, SerOpts::network_nbt()).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum TextContent<'a> {
|
||||
/// Raw Text
|
||||
Text { text: Cow<'a, str> },
|
||||
/// Translated text
|
||||
Translate {
|
||||
translate: Cow<'a, str>,
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
with: Vec<Text<'a>>,
|
||||
},
|
||||
/// Displays the name of one or more entities found by a selector.
|
||||
EntityNames {
|
||||
selector: Cow<'a, str>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
separator: Option<Cow<'a, str>>,
|
||||
},
|
||||
/// A keybind identifier
|
||||
/// https://minecraft.fandom.com/wiki/Controls#Configurable_controls
|
||||
Keybind { keybind: Cow<'a, str> },
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::{
|
||||
click::ClickEvent,
|
||||
color::{self, Color},
|
||||
hover::HoverEvent,
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
|
||||
pub struct Style<'a> {
|
||||
/// Changes the color to render the content
|
||||
pub color: Option<Color>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub bold: Option<u8>,
|
||||
/// Whether to render the content in italic.
|
||||
/// Keep in mind that booleans are represented as bytes in nbt
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub italic: Option<u8>,
|
||||
/// Whether to render the content in underlined.
|
||||
/// Keep in mind that booleans are represented as bytes in nbt
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub underlined: Option<u8>,
|
||||
/// Whether to render the content in strikethrough.
|
||||
/// Keep in mind that booleans are represented as bytes in nbt
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub strikethrough: Option<u8>,
|
||||
/// Whether to render the content in obfuscated.
|
||||
/// Keep in mind that booleans are represented as bytes in nbt
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub obfuscated: Option<u8>,
|
||||
/// When the text is shift-clicked by a player, this string is inserted in their chat input. It does not overwrite any existing text the player was writing. This only works in chat messages
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub insertion: Option<String>,
|
||||
/// Allows for events to occur when the player clicks on text. Only work in chat.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub click_event: Option<ClickEvent<'a>>,
|
||||
/// Allows for a tooltip to be displayed when the player hovers their mouse over text.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub hover_event: Option<HoverEvent<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> Style<'a> {
|
||||
pub fn color(mut self, color: Color) -> Self {
|
||||
self.color = Some(color);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn color_named(mut self, color: color::NamedColor) -> Self {
|
||||
self.color = Some(Color::Named(color));
|
||||
self
|
||||
}
|
||||
|
||||
/// Makes the text bold
|
||||
pub fn bold(mut self) -> Self {
|
||||
self.bold = Some(1);
|
||||
self
|
||||
}
|
||||
|
||||
/// Makes the text italic
|
||||
pub fn italic(mut self) -> Self {
|
||||
self.italic = Some(1);
|
||||
self
|
||||
}
|
||||
|
||||
/// Makes the text underlined
|
||||
pub fn underlined(mut self) -> Self {
|
||||
self.underlined = Some(1);
|
||||
self
|
||||
}
|
||||
|
||||
/// Makes the text strikethrough
|
||||
pub fn strikethrough(mut self) -> Self {
|
||||
self.strikethrough = Some(1);
|
||||
self
|
||||
}
|
||||
|
||||
/// Makes the text obfuscated
|
||||
pub fn obfuscated(mut self) -> Self {
|
||||
self.obfuscated = Some(1);
|
||||
self
|
||||
}
|
||||
|
||||
/// When the text is shift-clicked by a player, this string is inserted in their chat input. It does not overwrite any existing text the player was writing. This only works in chat messages
|
||||
pub fn insertion(mut self, text: String) -> Self {
|
||||
self.insertion = Some(text);
|
||||
self
|
||||
}
|
||||
|
||||
/// Allows for events to occur when the player clicks on text. Only work in chat.
|
||||
pub fn click_event(mut self, event: ClickEvent<'a>) -> Self {
|
||||
self.click_event = Some(event);
|
||||
self
|
||||
}
|
||||
|
||||
/// Allows for a tooltip to be displayed when the player hovers their mouse over text.
|
||||
pub fn hover_event(mut self, event: HoverEvent<'a>) -> Self {
|
||||
self.hover_event = Some(event);
|
||||
self
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user