Skip to content

Commit

Permalink
Merge branch 'Snowiiii:master' into nix-flake
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaschiasson authored Nov 16, 2024
2 parents cf21e83 + 00af2b9 commit 2eca090
Show file tree
Hide file tree
Showing 17 changed files with 305 additions and 36 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
FROM rust:1-alpine3.20 AS builder
ARG GIT_VERSION=Docker
ENV GIT_VERSION=$GIT_VERSION
ENV RUSTFLAGS="-C target-feature=-crt-static -C target-cpu=native"
RUN apk add --no-cache musl-dev

Expand Down
2 changes: 1 addition & 1 deletion pumpkin-core/src/text/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Here we build Mojang's Textcomponent, Which is used across many places, Often wh

### Features
- Colors
- [ ] RBG
- [x] RBG
- [x] Black
- [x] Dark Blue
- [x] Dark Green
Expand Down
49 changes: 43 additions & 6 deletions pumpkin-core/src/text/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub enum Color {
#[default]
Reset,
/// RGB Color
Rgb(u32),
Rgb(RGBColor),
/// One of the 16 named Minecraft colors
Named(NamedColor),
}
Expand All @@ -25,10 +25,21 @@ impl<'de> Deserialize<'de> for Color {

if s == "reset" {
Ok(Color::Reset)
} else if s.starts_with('#') {
let rgb = u32::from_str_radix(&s.replace("#", ""), 16)
.map_err(|_| serde::de::Error::custom("Invalid hex color"))?;
Ok(Color::Rgb(rgb))
} else if let Some(hex) = s.strip_prefix('#') {
if s.len() != 7 {
return Err(serde::de::Error::custom(
"Hex color must be in the format '#RRGGBB'",
));
}

let r = u8::from_str_radix(&hex[0..2], 16)
.map_err(|_| serde::de::Error::custom("Invalid red component in hex color"))?;
let g = u8::from_str_radix(&hex[2..4], 16)
.map_err(|_| serde::de::Error::custom("Invalid green component in hex color"))?;
let b = u8::from_str_radix(&hex[4..6], 16)
.map_err(|_| serde::de::Error::custom("Invalid blue component in hex color"))?;

return Ok(Color::Rgb(RGBColor::new(r, g, b)));
} else {
Ok(Color::Named(NamedColor::try_from(s.as_str()).map_err(
|_| serde::de::Error::custom("Invalid named color"),
Expand Down Expand Up @@ -59,11 +70,37 @@ impl Color {
NamedColor::Yellow => text.bright_yellow(),
NamedColor::White => text.white(),
},
Color::Rgb(_) => todo!(),
// TODO: Check if terminal supports true color
Color::Rgb(color) => text.truecolor(color.red, color.green, color.blue),
}
}
}

#[derive(Debug, Deserialize, Clone, Copy, Eq, Hash, PartialEq)]
pub struct RGBColor {
red: u8,
green: u8,
blue: u8,
}

impl RGBColor {
pub fn new(red: u8, green: u8, blue: u8) -> Self {
RGBColor { red, green, blue }
}
}

impl Serialize for RGBColor {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&format!(
"#{:02X}{:02X}{:02X}",
self.red, self.green, self.blue
))
}
}

/// Named Minecraft color
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
Expand Down
2 changes: 1 addition & 1 deletion pumpkin-core/src/text/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::Text;
#[serde(tag = "action", content = "contents", rename_all = "snake_case")]
pub enum HoverEvent<'a> {
/// Displays a tooltip with the given text.
ShowText(Text<'a>),
ShowText(Cow<'a, str>),
/// Shows an item.
ShowItem {
/// Resource identifier of the item
Expand Down
13 changes: 13 additions & 0 deletions pumpkin-core/src/text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub mod style;

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(transparent)]
// TODO: Use this instead of TextComponent alone to allow for example text with different colors
// TODO: Allow to mix TextComponent and String
pub struct Text<'a>(pub Box<TextComponent<'a>>);

// Represents a Text component
Expand Down Expand Up @@ -74,6 +76,12 @@ impl<'a> TextComponent<'a> {
if style.strikethrough.is_some() {
text = text.strikethrough().to_string();
}
if style.click_event.is_some() {
if let Some(ClickEvent::OpenUrl(url)) = style.click_event {
//TODO: check if term supports hyperlinks before
text = format!("\x1b]8;;{}\x1b\\{}\x1b]8;;\x1b\\", url, text).to_string()
}
}
text
}
}
Expand All @@ -98,6 +106,11 @@ impl<'a> TextComponent<'a> {
self
}

pub fn color_rgb(mut self, color: color::RGBColor) -> Self {
self.style.color = Some(Color::Rgb(color));
self
}

/// Makes the text bold
pub fn bold(mut self) -> Self {
self.style.bold = Some(1);
Expand Down
1 change: 1 addition & 0 deletions pumpkin-core/src/text/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use super::{
};

#[derive(Clone, Debug, Serialize, Deserialize, Default, PartialEq, Eq, Hash)]
#[serde(rename_all = "camelCase")]
pub struct Style<'a> {
/// Changes the color to render the content
#[serde(default, skip_serializing_if = "Option::is_none")]
Expand Down
16 changes: 16 additions & 0 deletions pumpkin-protocol/src/client/play/c_transfer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::VarInt;
use pumpkin_macros::client_packet;
use serde::Serialize;

#[derive(Serialize)]
#[client_packet("play:transfer")]
pub struct CTransfer<'a> {
host: &'a str,
port: &'a VarInt,
}

impl<'a> CTransfer<'a> {
pub fn new(host: &'a str, port: &'a VarInt) -> Self {
Self { host, port }
}
}
2 changes: 2 additions & 0 deletions pumpkin-protocol/src/client/play/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ mod c_subtitle;
mod c_sync_player_position;
mod c_system_chat_message;
mod c_teleport_entity;
mod c_transfer;
mod c_unload_chunk;
mod c_update_entity_pos;
mod c_update_entity_pos_rot;
Expand Down Expand Up @@ -113,6 +114,7 @@ pub use c_subtitle::*;
pub use c_sync_player_position::*;
pub use c_system_chat_message::*;
pub use c_teleport_entity::*;
pub use c_transfer::*;
pub use c_unload_chunk::*;
pub use c_update_entity_pos::*;
pub use c_update_entity_pos_rot::*;
Expand Down
3 changes: 3 additions & 0 deletions pumpkin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ png = "0.17.14"

# logging
simple_logger = { version = "5.0.0", features = ["threads"] }
time = "0.3.36"
sys-info = "0.9.1"

# commands
async-trait = "0.1.83"

[build-dependencies]
winresource = "0.1.17"
git-version = "0.3.9"
26 changes: 18 additions & 8 deletions pumpkin/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
fn main() {
if cfg!(target_os = "windows") {
let mut res = winresource::WindowsResource::new();
res.set_icon("../assets/icon.ico");
res.set_language(0x0009); // English
res.compile().unwrap();
}
}
use git_version::git_version;
use std::env;

fn main() {
if cfg!(target_os = "windows") {
let mut res = winresource::WindowsResource::new();
res.set_icon("../assets/icon.ico");
res.set_language(0x0009); // English
res.compile().unwrap();
}

let version = git_version!(fallback = "unknown");
let git_version = match version {
"unknown" => env::var("GIT_VERSION").unwrap_or("unknown".to_string()),
_ => version.to_string(),
};
println!("cargo:rustc-env=GIT_VERSION={}", git_version);
}
17 changes: 10 additions & 7 deletions pumpkin/src/command/commands/cmd_pumpkin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
args::ConsumedArgs, tree::CommandTree, CommandError, CommandExecutor, CommandSender,
},
server::CURRENT_MC_VERSION,
GIT_VERSION,
};

const NAMES: [&str; 2] = ["pumpkin", "version"];
Expand All @@ -15,6 +16,9 @@ const DESCRIPTION: &str = "Display information about Pumpkin.";

struct PumpkinExecutor;

const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
const CARGO_PKG_DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION");

#[async_trait]
impl CommandExecutor for PumpkinExecutor {
async fn execute<'a>(
Expand All @@ -23,13 +27,12 @@ impl CommandExecutor for PumpkinExecutor {
_server: &crate::server::Server,
_args: &ConsumedArgs<'a>,
) -> Result<(), CommandError> {
let version = env!("CARGO_PKG_VERSION");
let description = env!("CARGO_PKG_DESCRIPTION");

sender.send_message(TextComponent::text(
&format!("Pumpkin {version}, {description} (Minecraft {CURRENT_MC_VERSION}, Protocol {CURRENT_MC_PROTOCOL})")
).color_named(NamedColor::Green)).await;

sender
.send_message(
TextComponent::text(&format!("Pumpkin {CARGO_PKG_VERSION} ({GIT_VERSION}), {CARGO_PKG_DESCRIPTION} (Minecraft {CURRENT_MC_VERSION}, Protocol {CURRENT_MC_PROTOCOL})", ))
.color_named(NamedColor::Green),
)
.await;
Ok(())
}
}
Expand Down
6 changes: 0 additions & 6 deletions pumpkin/src/command/commands/cmd_say.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ impl CommandExecutor for SayExecutor {
server: &crate::server::Server,
args: &ConsumedArgs<'a>,
) -> Result<(), CommandError> {
let sender = match sender {
CommandSender::Console => "Console",
CommandSender::Rcon(_) => "Rcon",
CommandSender::Player(player) => &player.gameprofile.name,
};

let Some(Arg::Msg(msg)) = args.get(ARG_MESSAGE) else {
return Err(InvalidConsumption(Some(ARG_MESSAGE.into())));
};
Expand Down
Loading

0 comments on commit 2eca090

Please sign in to comment.