1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00

merge main

This commit is contained in:
mat 2023-03-14 01:33:18 -05:00
commit fcc257a0cc
13 changed files with 137 additions and 100 deletions

53
Cargo.lock generated
View file

@ -458,6 +458,7 @@ dependencies = [
"azalea-buf",
"azalea-registry-macros",
"enum-as-inner",
"serde",
]
[[package]]
@ -788,9 +789,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.23"
version = "0.4.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16b0a3d9ed01224b22057780a37bb8c5dbfe1be8ba48678e7bf57ec4b385411f"
checksum = "4e3c5919066adf22df73762e50cffcde3a758f2a848b113b586d1f86728b673b"
dependencies = [
"num-integer",
"num-traits",
@ -1832,18 +1833,18 @@ dependencies = [
[[package]]
name = "proc-macro2"
version = "1.0.51"
version = "1.0.52"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6"
checksum = "1d0e1ae9e836cc3beddd63db0df682593d7e2d3d891ae8c9083d2113e1744224"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.23"
version = "1.0.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b"
checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc"
dependencies = [
"proc-macro2",
]
@ -2075,9 +2076,9 @@ dependencies = [
[[package]]
name = "semver"
version = "1.0.16"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58bc9567378fc7690d6b2addae4e60ac2eeea07becb2c64b9f218b53865cba2a"
checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed"
[[package]]
name = "serde"
@ -2353,9 +2354,9 @@ checksum = "3ab8ed2edee10b50132aed5f331333428b011c99402b5a534154ed15746f9622"
[[package]]
name = "toml_edit"
version = "0.19.5"
version = "0.19.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7082a95d48029677a28f181e5f6422d0c8339ad8396a39d3f33d62a90c1f6c30"
checksum = "08de71aa0d6e348f070457f85af8bd566e2bc452156a423ddf22861b3a953fae"
dependencies = [
"indexmap",
"toml_datetime",
@ -2732,9 +2733,9 @@ dependencies = [
[[package]]
name = "windows-targets"
version = "0.42.1"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e2522491fbfcd58cc84d47aeb2958948c4b8982e9a2d8a2a35bbaed431390e7"
checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
@ -2747,45 +2748,45 @@ dependencies = [
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.42.1"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608"
checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8"
[[package]]
name = "windows_aarch64_msvc"
version = "0.42.1"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7"
checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43"
[[package]]
name = "windows_i686_gnu"
version = "0.42.1"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640"
checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f"
[[package]]
name = "windows_i686_msvc"
version = "0.42.1"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605"
checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060"
[[package]]
name = "windows_x86_64_gnu"
version = "0.42.1"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45"
checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.42.1"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463"
checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3"
[[package]]
name = "windows_x86_64_msvc"
version = "0.42.1"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd"
checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0"
[[package]]
name = "winnow"

View file

@ -271,7 +271,7 @@ fn process_packet_events(ecs: &mut World) {
// brand
local_player.write_packet(
ServerboundCustomPayloadPacket {
identifier: ResourceLocation::new("brand").unwrap(),
identifier: ResourceLocation::new("brand"),
// they don't have to know :)
data: "vanilla".into(),
}

View file

@ -1,5 +1,6 @@
#![doc = include_str!("../README.md")]
#![feature(int_roundings)]
#![feature(const_for)]
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

View file

@ -6,9 +6,6 @@ use std::io::{Cursor, Write};
#[cfg(feature = "serde")]
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
// TODO: make a `resourcelocation!("minecraft:overworld")` macro that checks if
// it's correct at compile-time.
#[derive(Hash, Clone, PartialEq, Eq)]
pub struct ResourceLocation {
pub namespace: String,
@ -19,7 +16,7 @@ static DEFAULT_NAMESPACE: &str = "minecraft";
// static REALMS_NAMESPACE: &str = "realms";
impl ResourceLocation {
pub fn new(resource_string: &str) -> Result<ResourceLocation, BufReadError> {
pub fn new(resource_string: &str) -> ResourceLocation {
let sep_byte_position_option = resource_string.chars().position(|c| c == ':');
let (namespace, path) = if let Some(sep_byte_position) = sep_byte_position_option {
if sep_byte_position == 0 {
@ -33,10 +30,10 @@ impl ResourceLocation {
} else {
(DEFAULT_NAMESPACE, resource_string)
};
Ok(ResourceLocation {
ResourceLocation {
namespace: namespace.to_string(),
path: path.to_string(),
})
}
}
}
@ -54,7 +51,7 @@ impl std::fmt::Debug for ResourceLocation {
impl McBufReadable for ResourceLocation {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let location_string = String::read_from(buf)?;
ResourceLocation::new(&location_string)
Ok(ResourceLocation::new(&location_string))
}
}
impl McBufWritable for ResourceLocation {
@ -81,10 +78,7 @@ impl<'de> Deserialize<'de> for ResourceLocation {
{
let s = String::deserialize(deserializer)?;
if s.contains(':') {
match ResourceLocation::new(&s) {
Ok(r) => Ok(r),
Err(e) => Err(de::Error::custom(e)),
}
Ok(ResourceLocation::new(&s))
} else {
Err(de::Error::invalid_value(
de::Unexpected::Str(&s),
@ -100,25 +94,25 @@ mod tests {
#[test]
fn basic_resource_location() {
let r = ResourceLocation::new("abcdef:ghijkl").unwrap();
let r = ResourceLocation::new("abcdef:ghijkl");
assert_eq!(r.namespace, "abcdef");
assert_eq!(r.path, "ghijkl");
}
#[test]
fn no_namespace() {
let r = ResourceLocation::new("azalea").unwrap();
let r = ResourceLocation::new("azalea");
assert_eq!(r.namespace, "minecraft");
assert_eq!(r.path, "azalea");
}
#[test]
fn colon_start() {
let r = ResourceLocation::new(":azalea").unwrap();
let r = ResourceLocation::new(":azalea");
assert_eq!(r.namespace, "minecraft");
assert_eq!(r.path, "azalea");
}
#[test]
fn colon_end() {
let r = ResourceLocation::new("azalea:").unwrap();
let r = ResourceLocation::new("azalea:");
assert_eq!(r.namespace, "azalea");
assert_eq!(r.path, "");
}
@ -127,7 +121,6 @@ mod tests {
fn mcbuf_resource_location() {
let mut buf = Vec::new();
ResourceLocation::new("minecraft:dirt")
.unwrap()
.write_into(&mut buf)
.unwrap();
@ -135,7 +128,7 @@ mod tests {
assert_eq!(
ResourceLocation::read_from(&mut buf).unwrap(),
ResourceLocation::new("minecraft:dirt").unwrap()
ResourceLocation::new("minecraft:dirt")
);
}
}

View file

@ -351,7 +351,7 @@ mod tests {
fn test_gravity() {
let mut app = make_test_app();
let _world_lock = app.world.resource_mut::<InstanceContainer>().insert(
ResourceLocation::new("minecraft:overworld").unwrap(),
ResourceLocation::new("minecraft:overworld"),
384,
-64,
);
@ -367,7 +367,7 @@ mod tests {
z: 0.,
},
azalea_registry::EntityKind::Zombie,
ResourceLocation::new("minecraft:overworld").unwrap(),
ResourceLocation::new("minecraft:overworld"),
),
MinecraftEntityId(0),
Local,
@ -403,7 +403,7 @@ mod tests {
fn test_collision() {
let mut app = make_test_app();
let world_lock = app.world.resource_mut::<InstanceContainer>().insert(
ResourceLocation::new("minecraft:overworld").unwrap(),
ResourceLocation::new("minecraft:overworld"),
384,
-64,
);
@ -425,7 +425,7 @@ mod tests {
z: 0.5,
},
azalea_registry::EntityKind::Player,
ResourceLocation::new("minecraft:overworld").unwrap(),
ResourceLocation::new("minecraft:overworld"),
),
MinecraftEntityId(0),
Local,
@ -462,7 +462,7 @@ mod tests {
fn test_slab_collision() {
let mut app = make_test_app();
let world_lock = app.world.resource_mut::<InstanceContainer>().insert(
ResourceLocation::new("minecraft:overworld").unwrap(),
ResourceLocation::new("minecraft:overworld"),
384,
-64,
);
@ -484,7 +484,7 @@ mod tests {
z: 0.5,
},
azalea_registry::EntityKind::Player,
ResourceLocation::new("minecraft:overworld").unwrap(),
ResourceLocation::new("minecraft:overworld"),
),
MinecraftEntityId(0),
Local,
@ -516,7 +516,7 @@ mod tests {
fn test_top_slab_collision() {
let mut app = make_test_app();
let world_lock = app.world.resource_mut::<InstanceContainer>().insert(
ResourceLocation::new("minecraft:overworld").unwrap(),
ResourceLocation::new("minecraft:overworld"),
384,
-64,
);
@ -538,7 +538,7 @@ mod tests {
z: 0.5,
},
azalea_registry::EntityKind::Player,
ResourceLocation::new("minecraft:overworld").unwrap(),
ResourceLocation::new("minecraft:overworld"),
),
MinecraftEntityId(0),
Local,
@ -569,7 +569,7 @@ mod tests {
fn test_weird_wall_collision() {
let mut app = make_test_app();
let world_lock = app.world.resource_mut::<InstanceContainer>().insert(
ResourceLocation::new("minecraft:overworld").unwrap(),
ResourceLocation::new("minecraft:overworld"),
384,
-64,
);
@ -591,7 +591,7 @@ mod tests {
z: 0.5,
},
azalea_registry::EntityKind::Player,
ResourceLocation::new("minecraft:overworld").unwrap(),
ResourceLocation::new("minecraft:overworld"),
),
MinecraftEntityId(0),
Local,
@ -627,7 +627,7 @@ mod tests {
fn test_negative_coordinates_weird_wall_collision() {
let mut app = make_test_app();
let world_lock = app.world.resource_mut::<InstanceContainer>().insert(
ResourceLocation::new("minecraft:overworld").unwrap(),
ResourceLocation::new("minecraft:overworld"),
384,
-64,
);
@ -649,7 +649,7 @@ mod tests {
z: -7.5,
},
azalea_registry::EntityKind::Player,
ResourceLocation::new("minecraft:overworld").unwrap(),
ResourceLocation::new("minecraft:overworld"),
),
MinecraftEntityId(0),
Local,

View file

@ -364,7 +364,7 @@ mod tests {
node_type: NodeType::Argument {
name: "position".to_string(),
parser: BrigadierParser::Vec3,
suggestions_type: Some(ResourceLocation::new("minecraft:test_suggestion").unwrap()),
suggestions_type: Some(ResourceLocation::new("minecraft:test_suggestion")),
},
};
let mut buf = Vec::new();

View file

@ -95,12 +95,24 @@ pub mod registry {
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "strict_registry", serde(deny_unknown_fields))]
pub struct RegistryRoot {
#[cfg(feature = "strict_registry")]
#[serde(rename = "minecraft:chat_type")]
pub chat_type: RegistryType<ChatTypeElement>,
#[cfg(not(feature = "strict_registry"))]
#[serde(rename = "minecraft:chat_type")]
pub chat_type: Tag,
#[serde(rename = "minecraft:dimension_type")]
pub dimension_type: RegistryType<DimensionTypeElement>,
#[cfg(feature = "strict_registry")]
#[serde(rename = "minecraft:worldgen/biome")]
pub world_type: RegistryType<WorldTypeElement>,
#[cfg(not(feature = "strict_registry"))]
#[serde(rename = "minecraft:worldgen/biome")]
pub world_type: Tag,
}
/// A collection of values for a certain type of registry data.
@ -170,8 +182,9 @@ pub mod registry {
}
/// Dimension attributes.
#[cfg(feature = "strict_registry")]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "strict_registry", serde(deny_unknown_fields))]
#[serde(deny_unknown_fields)]
pub struct DimensionTypeElement {
pub ambient_light: f32,
#[serde(with = "Convert")]
@ -203,6 +216,14 @@ pub mod registry {
pub ultrawarm: bool,
}
/// Dimension attributes.
#[cfg(not(feature = "strict_registry"))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DimensionTypeElement {
pub height: u32,
pub min_y: i32,
}
/// The light level at which monsters can spawn.
///
/// This can be either a single minimum value, or a formula with a min and
@ -327,7 +348,7 @@ pub mod registry {
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "strict_registry", serde(deny_unknown_fields))]
pub struct SoundId {
pub sound_id: ResourceLocation,
pub sound_id: azalea_registry::SoundEvent,
}
/// Biome particles.
@ -412,29 +433,22 @@ pub mod registry {
#[cfg(test)]
mod tests {
use super::registry::{
ChatTypeElement, DimensionTypeElement, RegistryHolder, RegistryRoot, RegistryType,
WorldTypeElement,
};
use super::registry::{DimensionTypeElement, RegistryHolder, RegistryRoot, RegistryType};
use azalea_core::ResourceLocation;
use azalea_nbt::Tag;
#[test]
fn test_convert() {
// Do NOT use Tag::End, they should be Tag::Compound.
// This is just for testing.
let registry = RegistryHolder {
root: RegistryRoot {
chat_type: RegistryType::<ChatTypeElement> {
kind: ResourceLocation::new("minecraft:chat_type").unwrap(),
value: Vec::new(),
},
chat_type: Tag::End,
dimension_type: RegistryType::<DimensionTypeElement> {
kind: ResourceLocation::new("minecraft:dimension_type").unwrap(),
value: Vec::new(),
},
world_type: RegistryType::<WorldTypeElement> {
kind: ResourceLocation::new("minecraft:worldgen/biome").unwrap(),
kind: ResourceLocation::new("minecraft:dimension_type"),
value: Vec::new(),
},
world_type: Tag::End,
},
};
@ -447,14 +461,6 @@ mod tests {
.as_compound()
.unwrap();
let chat = root
.get("minecraft:chat_type")
.unwrap()
.as_compound()
.unwrap();
let chat_type = chat.get("type").unwrap().as_string().unwrap();
assert!(chat_type == "minecraft:chat_type");
let dimension = root
.get("minecraft:dimension_type")
.unwrap()
@ -462,13 +468,5 @@ mod tests {
.unwrap();
let dimension_type = dimension.get("type").unwrap().as_string().unwrap();
assert!(dimension_type == "minecraft:dimension_type");
let world = root
.get("minecraft:worldgen/biome")
.unwrap()
.as_compound()
.unwrap();
let world_type = world.get("type").unwrap().as_string().unwrap();
assert!(world_type == "minecraft:worldgen/biome");
}
}

View file

@ -125,7 +125,7 @@ mod tests {
let packet = ClientboundUpdateAdvancementsPacket {
reset: true,
added: [(
ResourceLocation::new("minecraft:test").unwrap(),
ResourceLocation::new("minecraft:test"),
Advancement {
parent_id: None,
display: Some(DisplayInfo {
@ -145,11 +145,11 @@ mod tests {
)]
.into_iter()
.collect(),
removed: vec![ResourceLocation::new("minecraft:test2").unwrap()],
removed: vec![ResourceLocation::new("minecraft:test2")],
progress: [(
ResourceLocation::new("minecraft:test3").unwrap(),
ResourceLocation::new("minecraft:test3"),
[(
ResourceLocation::new("minecraft:test4").unwrap(),
ResourceLocation::new("minecraft:test4"),
CriterionProgress {
date: Some(123456789),
},
@ -171,12 +171,12 @@ mod tests {
let advancement = packet
.added
.get(&ResourceLocation::new("minecraft:test").unwrap())
.get(&ResourceLocation::new("minecraft:test"))
.unwrap()
.clone();
let read_advancement = read_packet
.added
.get(&ResourceLocation::new("minecraft:test").unwrap())
.get(&ResourceLocation::new("minecraft:test"))
.unwrap()
.clone();
assert_eq!(advancement.parent_id, read_advancement.parent_id);

View file

@ -183,9 +183,7 @@ impl McBufWritable for Recipe {
RecipeData::Stonecutting(_) => "minecraft:stonecutting",
RecipeData::Smithing(_) => "minecraft:smithing",
};
ResourceLocation::new(resource_location)
.unwrap()
.write_into(buf)?;
ResourceLocation::new(resource_location).write_into(buf)?;
self.identifier.write_into(buf)?;
self.data.write_without_id(buf)?;
Ok(())

View file

@ -9,6 +9,11 @@ version = "0.6.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
azalea-buf = {path = "../azalea-buf", version = "^0.6.0" }
azalea-registry-macros = {path = "./azalea-registry-macros", version = "^0.6.0" }
azalea-buf = { path = "../azalea-buf", version = "^0.6.0" }
azalea-registry-macros = { path = "./azalea-registry-macros", version = "^0.6.0" }
enum-as-inner = "0.5.1"
serde = { version = "1.0.155", optional = true }
[features]
serde = ["dep:serde", "azalea-registry-macros/serde"]
default = ["serde"]

View file

@ -15,3 +15,6 @@ proc-macro = true
proc-macro2 = "1.0.39"
quote = "1.0.18"
syn = "1.0.95"
[features]
serde = []

View file

@ -136,12 +136,16 @@ pub fn registry(input: TokenStream) -> TokenStream {
// Display that uses registry ids
let mut display_items = quote! {};
let mut from_str_items = quote! {};
for item in &input.items {
let name = &item.name;
let id = &item.id;
display_items.extend(quote! {
Self::#name => write!(f, #id),
});
from_str_items.extend(quote! {
#id => Ok(Self::#name),
});
}
generated.extend(quote! {
impl std::fmt::Display for #name {
@ -151,7 +155,40 @@ pub fn registry(input: TokenStream) -> TokenStream {
}
}
}
impl std::str::FromStr for #name {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
#from_str_items
_ => Err(format!("{s:?} is not a valid {name}", s = s, name = stringify!(#name))),
}
}
}
});
#[cfg(feature = "serde")]
{
generated.extend(quote! {
impl serde::Serialize for #name {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&self.to_string())
}
}
impl<'de> serde::Deserialize<'de> for #name {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
s.parse().map_err(serde::de::Error::custom)
}
}
});
}
generated.into()
}

View file

@ -5,9 +5,10 @@
// auto-generated (so you can add doc comments to the registry enums if you
// want)
use std::io::{Cursor, Write};
use azalea_buf::{BufReadError, McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable};
use azalea_registry_macros::registry;
use std::io::{Cursor, Write};
pub trait Registry
where