aboutsummaryrefslogtreecommitdiff
path: root/azalea-chat/src
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2023-02-04 19:32:27 -0600
committerGitHub <noreply@github.com>2023-02-04 19:32:27 -0600
commita5672815ccef520b433363ac622dbb6d6af60c91 (patch)
treef9bb1b41876d81423ac3f188f4d368e6d362eed1 /azalea-chat/src
parent7c7446ab1e467c29f86e9bfba260741fc469389a (diff)
downloadazalea-drasl-a5672815ccef520b433363ac622dbb6d6af60c91.tar.xz
Use an ECS (#52)
* add EntityData::kind * start making metadata use hecs * make entity codegen generate ecs stuff * fix registry codegen * get rid of worldhaver it's not even used * add bevy_ecs to deps * rename Component to FormattedText also start making the metadata use bevy_ecs but bevy_ecs doesn't let you query on Bundles so it's annoying * generate metadata.rs correctly for bevy_ecs * start switching more entity stuff to use ecs * more ecs stuff for entity storage * ok well it compiles but it definitely doesn't work * random fixes * change a bunch of entity things to use the components * some ecs stuff in az-client * packet handler uses the ecs now and other fun changes i still need to make ticking use the ecs but that's tricker, i'm considering using bevy_ecs systems for those bevy_ecs systems can't be async but the only async things in ticking is just sending packets which can just be done as a tokio task so that's not a big deal * start converting some functions in az-client into systems committing because i'm about to try something that might go horribly wrong * start splitting client i'm probably gonna change it so azalea entity ids are separate from minecraft entity ids next (so stuff like player ids can be consistent and we don't have to wait for the login packet) * separate minecraft entity ids from azalea entity ids + more ecs stuff i guess i'm using bevy_app now too huh it's necessary for plugins and it lets us control the tick rate anyways so it's fine i think i'm still not 100% sure how packet handling that interacts with the world will work, but i think if i can sneak the ecs world into there it'll be fine. Can't put packet handling in the schedule because that'd make it tick-bound, which it's not (technically it'd still work but it'd be wrong and anticheats might realize). * packet handling now it runs the schedule only when we get a tick or packet :smile: also i systemified some more functions and did other random fixes so az-world and az-physics compile making azalea-client use the ecs is almost done! all the hard parts are done now i hope, i just have to finish writing all the code so it actually works * start figuring out how functions in Client will work generally just lifetimes being annoying but i think i can get it all to work * make writing packets work synchronously* * huh az-client compiles * start fixing stuff * start fixing some packets * make packet handler work i still haven't actually tested any of this yet lol but in theory it should all work i'll probably either actually test az-client and fix all the remaining issues or update the azalea crate next ok also one thing that i'm not particularly happy with is how the packet handlers are doing ugly queries like ```rs let local_player = ecs .query::<&LocalPlayer>() .get_mut(ecs, player_entity) .unwrap(); ``` i think the right way to solve it would be by putting every packet handler in its own system but i haven't come up with a way to make that not be really annoying yet * fix warnings * ok what if i just have a bunch of queries and a single packet handler system * simple example for azalea-client * :bug: * maybe fix deadlock idk can't test it rn lmao * make physicsstate its own component * use the default plugins * azalea compiles lol * use systemstate for packet handler * fix entities basically moved some stuff from being in the world to just being components * physics (ticking) works * try to add a .entity_by function still doesn't work because i want to make the predicate magic * try to make entity_by work well it does work but i couldn't figure out how to make it look not terrible. Will hopefully change in the future * everything compiles * start converting swarm to use builder * continue switching swarm to builder and fix stuff * make swarm use builder still have to fix some stuff and make client use builder * fix death event * client builder * fix some warnings * document plugins a bit * start trying to fix tests * azalea-ecs * azalea-ecs stuff compiles * az-physics tests pass :tada: * fix all the tests * clippy on azalea-ecs-macros * remove now-unnecessary trait_upcasting feature * fix some clippy::pedantic warnings lol * why did cargo fmt not remove the trailing spaces * FIX ALL THE THINGS * when i said 'all' i meant non-swarm bugs * start adding task pool * fix entity deduplication * fix pathfinder not stopping * fix some more random bugs * fix panic that sometimes happens in swarms * make pathfinder run in task * fix some tests * fix doctests and clippy * deadlock * fix systems running in wrong order * fix non-swarm bots
Diffstat (limited to 'azalea-chat/src')
-rwxr-xr-xazalea-chat/src/base_component.rs4
-rwxr-xr-xazalea-chat/src/component.rs104
-rwxr-xr-xazalea-chat/src/lib.rs2
-rwxr-xr-xazalea-chat/src/text_component.rs23
-rwxr-xr-xazalea-chat/src/translatable_component.rs29
5 files changed, 81 insertions, 81 deletions
diff --git a/azalea-chat/src/base_component.rs b/azalea-chat/src/base_component.rs
index ab4f5e5d..43b35aef 100755
--- a/azalea-chat/src/base_component.rs
+++ b/azalea-chat/src/base_component.rs
@@ -1,11 +1,11 @@
-use crate::{style::Style, Component};
+use crate::{style::Style, FormattedText};
use serde::Serialize;
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct BaseComponent {
// implements mutablecomponent
#[serde(skip_serializing_if = "Vec::is_empty")]
- pub siblings: Vec<Component>,
+ pub siblings: Vec<FormattedText>,
#[serde(flatten)]
pub style: Style,
}
diff --git a/azalea-chat/src/component.rs b/azalea-chat/src/component.rs
index 14ec0d0c..59e11bbf 100755
--- a/azalea-chat/src/component.rs
+++ b/azalea-chat/src/component.rs
@@ -17,7 +17,7 @@ use std::{
/// A chat component, basically anything you can see in chat.
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(untagged)]
-pub enum Component {
+pub enum FormattedText {
Text(TextComponent),
Translatable(TranslatableComponent),
}
@@ -28,7 +28,7 @@ pub static DEFAULT_STYLE: Lazy<Style> = Lazy::new(|| Style {
});
/// A chat component
-impl Component {
+impl FormattedText {
pub fn get_base_mut(&mut self) -> &mut BaseComponent {
match self {
Self::Text(c) => &mut c.base,
@@ -44,14 +44,16 @@ impl Component {
}
/// Add a component as a sibling of this one
- fn append(&mut self, sibling: Component) {
+ fn append(&mut self, sibling: FormattedText) {
self.get_base_mut().siblings.push(sibling);
}
/// Get the "separator" component from the json
- fn parse_separator(json: &serde_json::Value) -> Result<Option<Component>, serde_json::Error> {
+ fn parse_separator(
+ json: &serde_json::Value,
+ ) -> Result<Option<FormattedText>, serde_json::Error> {
if json.get("separator").is_some() {
- return Ok(Some(Component::deserialize(
+ return Ok(Some(FormattedText::deserialize(
json.get("separator").unwrap(),
)?));
}
@@ -62,16 +64,17 @@ impl Component {
/// [ANSI string](https://en.wikipedia.org/wiki/ANSI_escape_code), so you
/// can print it to your terminal and get styling.
///
- /// This is technically a shortcut for [`Component::to_ansi_custom_style`]
- /// with a default [`Style`] colored white.
+ /// This is technically a shortcut for
+ /// [`FormattedText::to_ansi_custom_style`] with a default [`Style`]
+ /// colored white.
///
/// # Examples
///
/// ```rust
- /// use azalea_chat::Component;
+ /// use azalea_chat::FormattedText;
/// use serde::de::Deserialize;
///
- /// let component = Component::deserialize(&serde_json::json!({
+ /// let component = FormattedText::deserialize(&serde_json::json!({
/// "text": "Hello, world!",
/// "color": "red",
/// })).unwrap();
@@ -86,7 +89,7 @@ impl Component {
/// Convert this component into an
/// [ANSI string](https://en.wikipedia.org/wiki/ANSI_escape_code).
///
- /// This is the same as [`Component::to_ansi`], but you can specify a
+ /// This is the same as [`FormattedText::to_ansi`], but you can specify a
/// default [`Style`] to use.
pub fn to_ansi_custom_style(&self, default_style: &Style) -> String {
// this contains the final string will all the ansi escape codes
@@ -117,12 +120,12 @@ impl Component {
}
}
-impl IntoIterator for Component {
+impl IntoIterator for FormattedText {
/// Recursively call the function for every component in this component
fn into_iter(self) -> Self::IntoIter {
let base = self.get_base();
let siblings = base.siblings.clone();
- let mut v: Vec<Component> = Vec::with_capacity(siblings.len() + 1);
+ let mut v: Vec<FormattedText> = Vec::with_capacity(siblings.len() + 1);
v.push(self);
for sibling in siblings {
v.extend(sibling.into_iter());
@@ -131,11 +134,11 @@ impl IntoIterator for Component {
v.into_iter()
}
- type Item = Component;
+ type Item = FormattedText;
type IntoIter = std::vec::IntoIter<Self::Item>;
}
-impl<'de> Deserialize<'de> for Component {
+impl<'de> Deserialize<'de> for FormattedText {
fn deserialize<D>(de: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
@@ -143,11 +146,11 @@ impl<'de> Deserialize<'de> for Component {
let json: serde_json::Value = serde::Deserialize::deserialize(de)?;
// we create a component that we might add siblings to
- let mut component: Component;
+ let mut component: FormattedText;
// if it's primitive, make it a text component
if !json.is_array() && !json.is_object() {
- return Ok(Component::Text(TextComponent::new(
+ return Ok(FormattedText::Text(TextComponent::new(
json.as_str().unwrap_or("").to_string(),
)));
}
@@ -155,7 +158,7 @@ impl<'de> Deserialize<'de> for Component {
else if json.is_object() {
if let Some(text) = json.get("text") {
let text = text.as_str().unwrap_or("").to_string();
- component = Component::Text(TextComponent::new(text));
+ component = FormattedText::Text(TextComponent::new(text));
} else if let Some(translate) = json.get("translate") {
let translate = translate
.as_str()
@@ -170,8 +173,8 @@ impl<'de> Deserialize<'de> for Component {
// if it's a string component with no styling and no siblings, just add a
// string to with_array otherwise add the component
// to the array
- let c = Component::deserialize(item).map_err(de::Error::custom)?;
- if let Component::Text(text_component) = c {
+ let c = FormattedText::deserialize(item).map_err(de::Error::custom)?;
+ if let FormattedText::Text(text_component) = c {
if text_component.base.siblings.is_empty()
&& text_component.base.style.is_empty()
{
@@ -179,16 +182,19 @@ impl<'de> Deserialize<'de> for Component {
continue;
}
}
- with_array.push(StringOrComponent::Component(
- Component::deserialize(item).map_err(de::Error::custom)?,
+ with_array.push(StringOrComponent::FormattedText(
+ FormattedText::deserialize(item).map_err(de::Error::custom)?,
));
}
- component =
- Component::Translatable(TranslatableComponent::new(translate, with_array));
+ component = FormattedText::Translatable(TranslatableComponent::new(
+ translate, with_array,
+ ));
} else {
// if it doesn't have a "with", just have the with_array be empty
- component =
- Component::Translatable(TranslatableComponent::new(translate, Vec::new()));
+ component = FormattedText::Translatable(TranslatableComponent::new(
+ translate,
+ Vec::new(),
+ ));
}
} else if let Some(score) = json.get("score") {
// object = GsonHelper.getAsJsonObject(jsonObject, "score");
@@ -210,14 +216,13 @@ impl<'de> Deserialize<'de> for Component {
"keybind text components aren't yet supported",
));
} else {
- let _nbt = if let Some(nbt) = json.get("nbt") {
- nbt
- } else {
+ let Some(_nbt) = json.get("nbt") else {
return Err(de::Error::custom(
- format!("Don't know how to turn {json} into a Component").as_str(),
+ format!("Don't know how to turn {json} into a FormattedText").as_str(),
));
};
- let _separator = Component::parse_separator(&json).map_err(de::Error::custom)?;
+ let _separator =
+ FormattedText::parse_separator(&json).map_err(de::Error::custom)?;
let _interpret = match json.get("interpret") {
Some(v) => v.as_bool().ok_or(Some(false)).unwrap(),
@@ -229,16 +234,15 @@ impl<'de> Deserialize<'de> for Component {
));
}
if let Some(extra) = json.get("extra") {
- let extra = match extra.as_array() {
- Some(r) => r,
- None => return Err(de::Error::custom("Extra isn't an array")),
+ let Some(extra) = extra.as_array() else {
+ return Err(de::Error::custom("Extra isn't an array"));
};
if extra.is_empty() {
return Err(de::Error::custom("Unexpected empty array of components"));
}
for extra_component in extra {
let sibling =
- Component::deserialize(extra_component).map_err(de::Error::custom)?;
+ FormattedText::deserialize(extra_component).map_err(de::Error::custom)?;
component.append(sibling);
}
}
@@ -251,16 +255,18 @@ impl<'de> Deserialize<'de> for Component {
// ok so it's not an object, if it's an array deserialize every item
else if !json.is_array() {
return Err(de::Error::custom(
- format!("Don't know how to turn {json} into a Component").as_str(),
+ format!("Don't know how to turn {json} into a FormattedText").as_str(),
));
}
let json_array = json.as_array().unwrap();
// the first item in the array is the one that we're gonna return, the others
// are siblings
- let mut component = Component::deserialize(&json_array[0]).map_err(de::Error::custom)?;
+ let mut component =
+ FormattedText::deserialize(&json_array[0]).map_err(de::Error::custom)?;
for i in 1..json_array.len() {
component.append(
- Component::deserialize(json_array.get(i).unwrap()).map_err(de::Error::custom)?,
+ FormattedText::deserialize(json_array.get(i).unwrap())
+ .map_err(de::Error::custom)?,
);
}
Ok(component)
@@ -268,18 +274,18 @@ impl<'de> Deserialize<'de> for Component {
}
#[cfg(feature = "azalea-buf")]
-impl McBufReadable for Component {
+impl McBufReadable for FormattedText {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let string = String::read_from(buf)?;
- debug!("Component string: {}", string);
+ debug!("FormattedText string: {}", string);
let json: serde_json::Value = serde_json::from_str(string.as_str())?;
- let component = Component::deserialize(json)?;
+ let component = FormattedText::deserialize(json)?;
Ok(component)
}
}
#[cfg(feature = "azalea-buf")]
-impl McBufWritable for Component {
+impl McBufWritable for FormattedText {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let json = serde_json::to_string(self).unwrap();
json.write_into(buf)?;
@@ -287,31 +293,31 @@ impl McBufWritable for Component {
}
}
-impl From<String> for Component {
+impl From<String> for FormattedText {
fn from(s: String) -> Self {
- Component::Text(TextComponent {
+ FormattedText::Text(TextComponent {
text: s,
base: BaseComponent::default(),
})
}
}
-impl From<&str> for Component {
+impl From<&str> for FormattedText {
fn from(s: &str) -> Self {
Self::from(s.to_string())
}
}
-impl Display for Component {
+impl Display for FormattedText {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
- Component::Text(c) => c.fmt(f),
- Component::Translatable(c) => c.fmt(f),
+ FormattedText::Text(c) => c.fmt(f),
+ FormattedText::Translatable(c) => c.fmt(f),
}
}
}
-impl Default for Component {
+impl Default for FormattedText {
fn default() -> Self {
- Component::Text(TextComponent::default())
+ FormattedText::Text(TextComponent::default())
}
}
diff --git a/azalea-chat/src/lib.rs b/azalea-chat/src/lib.rs
index 97a2580d..d6ff7285 100755
--- a/azalea-chat/src/lib.rs
+++ b/azalea-chat/src/lib.rs
@@ -6,4 +6,4 @@ pub mod style;
pub mod text_component;
pub mod translatable_component;
-pub use component::Component;
+pub use component::FormattedText;
diff --git a/azalea-chat/src/text_component.rs b/azalea-chat/src/text_component.rs
index 44bcbcf1..42932d0e 100755
--- a/azalea-chat/src/text_component.rs
+++ b/azalea-chat/src/text_component.rs
@@ -1,4 +1,4 @@
-use crate::{base_component::BaseComponent, style::ChatFormatting, Component};
+use crate::{base_component::BaseComponent, style::ChatFormatting, FormattedText};
use serde::{ser::SerializeMap, Serialize, Serializer, __private::ser::FlatMapSerializer};
use std::fmt::Display;
@@ -26,7 +26,7 @@ impl Serialize for TextComponent {
const LEGACY_FORMATTING_CODE_SYMBOL: char = 'ยง';
-/// Convert a legacy color code string into a Component
+/// Convert a legacy color code string into a FormattedText
/// Technically in Minecraft this is done when displaying the text, but AFAIK
/// it's the same as just doing it in TextComponent
pub fn legacy_color_code_to_text_component(legacy_color_code: &str) -> TextComponent {
@@ -41,12 +41,9 @@ pub fn legacy_color_code_to_text_component(legacy_color_code: &str) -> TextCompo
while i < legacy_color_code.chars().count() {
if legacy_color_code.chars().nth(i).unwrap() == LEGACY_FORMATTING_CODE_SYMBOL {
let formatting_code = legacy_color_code.chars().nth(i + 1);
- let formatting_code = match formatting_code {
- Some(formatting_code) => formatting_code,
- None => {
- i += 1;
- continue;
- }
+ let Some(formatting_code) = formatting_code else {
+ i += 1;
+ continue;
};
if let Some(formatter) = ChatFormatting::from_code(formatting_code) {
if components.is_empty() || !components.last().unwrap().text.is_empty() {
@@ -98,18 +95,18 @@ impl TextComponent {
}
}
- fn get(self) -> Component {
- Component::Text(self)
+ fn get(self) -> FormattedText {
+ FormattedText::Text(self)
}
}
impl Display for TextComponent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// this contains the final string will all the ansi escape codes
- for component in Component::Text(self.clone()).into_iter() {
+ for component in FormattedText::Text(self.clone()).into_iter() {
let component_text = match &component {
- Component::Text(c) => c.text.to_string(),
- Component::Translatable(c) => c.read()?.to_string(),
+ FormattedText::Text(c) => c.text.to_string(),
+ FormattedText::Translatable(c) => c.read()?.to_string(),
};
f.write_str(&component_text)?;
diff --git a/azalea-chat/src/translatable_component.rs b/azalea-chat/src/translatable_component.rs
index 7819d5ff..edbb5a6d 100755
--- a/azalea-chat/src/translatable_component.rs
+++ b/azalea-chat/src/translatable_component.rs
@@ -1,7 +1,7 @@
use std::fmt::{self, Display, Formatter};
use crate::{
- base_component::BaseComponent, style::Style, text_component::TextComponent, Component,
+ base_component::BaseComponent, style::Style, text_component::TextComponent, FormattedText,
};
use serde::{ser::SerializeMap, Serialize, Serializer, __private::ser::FlatMapSerializer};
@@ -9,7 +9,7 @@ use serde::{ser::SerializeMap, Serialize, Serializer, __private::ser::FlatMapSer
#[serde(untagged)]
pub enum StringOrComponent {
String(String),
- Component(Component),
+ FormattedText(FormattedText),
}
/// A message whose content depends on the client's language.
@@ -42,7 +42,7 @@ impl TranslatableComponent {
}
}
- /// Convert the key and args to a Component.
+ /// Convert the key and args to a FormattedText.
pub fn read(&self) -> Result<TextComponent, fmt::Error> {
let template = azalea_language::get(&self.key).unwrap_or(&self.key);
// decode the % things
@@ -57,12 +57,9 @@ impl TranslatableComponent {
while i < template.len() {
if template.chars().nth(i).unwrap() == '%' {
- let char_after = match template.chars().nth(i + 1) {
- Some(c) => c,
- None => {
- built_text.push(template.chars().nth(i).unwrap());
- break;
- }
+ let Some(char_after) = template.chars().nth(i + 1) else {
+ built_text.push(template.chars().nth(i).unwrap());
+ break;
};
i += 1;
match char_after {
@@ -111,7 +108,7 @@ impl TranslatableComponent {
built_text.push(template.chars().nth(i).unwrap());
}
- i += 1
+ i += 1;
}
if components.is_empty() {
@@ -122,7 +119,7 @@ impl TranslatableComponent {
Ok(TextComponent {
base: BaseComponent {
- siblings: components.into_iter().map(Component::Text).collect(),
+ siblings: components.into_iter().map(FormattedText::Text).collect(),
style: Style::default(),
},
text: "".to_string(),
@@ -133,10 +130,10 @@ impl TranslatableComponent {
impl Display for TranslatableComponent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// this contains the final string will all the ansi escape codes
- for component in Component::Translatable(self.clone()).into_iter() {
+ for component in FormattedText::Translatable(self.clone()).into_iter() {
let component_text = match &component {
- Component::Text(c) => c.text.to_string(),
- Component::Translatable(c) => c.read()?.to_string(),
+ FormattedText::Text(c) => c.text.to_string(),
+ FormattedText::Translatable(c) => c.read()?.to_string(),
};
f.write_str(&component_text)?;
@@ -150,7 +147,7 @@ impl Display for StringOrComponent {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
match self {
StringOrComponent::String(s) => write!(f, "{s}"),
- StringOrComponent::Component(c) => write!(f, "{c}"),
+ StringOrComponent::FormattedText(c) => write!(f, "{c}"),
}
}
}
@@ -159,7 +156,7 @@ impl From<StringOrComponent> for TextComponent {
fn from(soc: StringOrComponent) -> Self {
match soc {
StringOrComponent::String(s) => TextComponent::new(s),
- StringOrComponent::Component(c) => TextComponent::new(c.to_string()),
+ StringOrComponent::FormattedText(c) => TextComponent::new(c.to_string()),
}
}
}