world_transmuter_engine/
lib.rs1mod convert;
2mod utils;
3
4pub use crate::convert::*;
5pub use crate::utils::*;
6use java_string::JavaString;
7
8pub type JCompound = valence_nbt::Compound<JavaString>;
9pub type JList = valence_nbt::List<JavaString>;
10pub type JValue = valence_nbt::Value<JavaString>;
11pub type JValueRef<'a> = valence_nbt::value::ValueRef<'a, JavaString>;
12pub type JValueMut<'a> = valence_nbt::value::ValueMut<'a, JavaString>;
13
14pub fn value_to_java(value: valence_nbt::Value) -> JValue {
15 match value {
16 valence_nbt::Value::Byte(v) => JValue::Byte(v),
17 valence_nbt::Value::Short(v) => JValue::Short(v),
18 valence_nbt::Value::Int(v) => JValue::Int(v),
19 valence_nbt::Value::Long(v) => JValue::Long(v),
20 valence_nbt::Value::Float(v) => JValue::Float(v),
21 valence_nbt::Value::Double(v) => JValue::Double(v),
22 valence_nbt::Value::ByteArray(v) => JValue::ByteArray(v),
23 valence_nbt::Value::String(v) => JValue::String(JavaString::from(v)),
24 valence_nbt::Value::List(v) => JValue::List(list_to_java(v)),
25 valence_nbt::Value::Compound(v) => JValue::Compound(compound_to_java(v)),
26 valence_nbt::Value::IntArray(v) => JValue::IntArray(v),
27 valence_nbt::Value::LongArray(v) => JValue::LongArray(v),
28 }
29}
30
31fn list_to_java(list: valence_nbt::List) -> JList {
32 match list {
33 valence_nbt::List::End => JList::End,
34 valence_nbt::List::Byte(v) => JList::Byte(v),
35 valence_nbt::List::Short(v) => JList::Short(v),
36 valence_nbt::List::Int(v) => JList::Int(v),
37 valence_nbt::List::Long(v) => JList::Long(v),
38 valence_nbt::List::Float(v) => JList::Float(v),
39 valence_nbt::List::Double(v) => JList::Double(v),
40 valence_nbt::List::ByteArray(v) => JList::ByteArray(v),
41 valence_nbt::List::String(v) => {
42 JList::String(v.into_iter().map(JavaString::from).collect())
43 }
44 valence_nbt::List::List(v) => JList::List(v.into_iter().map(list_to_java).collect()),
45 valence_nbt::List::Compound(v) => {
46 JList::Compound(v.into_iter().map(compound_to_java).collect())
47 }
48 valence_nbt::List::IntArray(v) => JList::IntArray(v),
49 valence_nbt::List::LongArray(v) => JList::LongArray(v),
50 }
51}
52
53pub fn compound_to_java(compound: valence_nbt::Compound) -> JCompound {
54 let mut result = JCompound::with_capacity(compound.len());
55 for (key, value) in compound {
56 result.insert(JavaString::from(key), value_to_java(value));
57 }
58 result
59}
60
61#[cfg(test)]
62mod tests {
63 use crate::{
64 convert_map_in_map, convert_object_in_map, map_data_converter_func, map_data_walker,
65 value_data_converter_func, value_to_java, AbstractMapDataType, IdDataType, JCompound,
66 JValue, MapDataType, ObjectDataType,
67 };
68 use java_string::JavaString;
69
70 fn make_map(string: &str) -> JCompound {
71 let value =
72 value_to_java(valence_nbt::snbt::from_snbt_str(string).expect("snbt syntax error"));
73 match value {
74 JValue::Compound(compound) => compound,
75 _ => panic!("snbt was not a compound"),
76 }
77 }
78
79 #[test]
80 fn rename_key() {
81 let mut map = make_map(r#"{"hello": "world"}"#);
82 crate::rename_key(&mut map, "hello", "Hello");
83 assert!(map.contains_key("Hello"));
84 assert!(!map.contains_key("hello"));
85 }
86
87 #[test]
88 fn simple_conversion() {
89 let mut map = make_map(r#"{"test": 42}"#);
90 simple_converted_type().convert(&mut map, 0.into(), 1.into());
91 assert!(matches!(map.get("test"), Some(valence_nbt::Value::String(str)) if str == "42"));
92 }
93
94 #[test]
95 fn simple_walker() {
96 let mut map = make_map(r#"{"inner": {"test": 42}}"#);
97 let mut typ = MapDataType::new("Outer");
98 let inner_type = simple_converted_type();
99 typ.add_structure_walker(
100 1,
101 map_data_walker(move |data, from_version, to_version| {
102 convert_map_in_map(&inner_type, data, "inner", from_version, to_version)
103 }),
104 );
105 typ.convert(&mut map, 0.into(), 1.into());
106 assert!(
107 matches!(map.get("inner"), Some(valence_nbt::Value::Compound(inner)) if matches!(inner.get("test"), Some(valence_nbt::Value::String(str)) if str == "42"))
108 );
109 }
110
111 #[test]
112 fn simple_id_walker() {
113 let mut map1 = make_map(r#"{"id": "foo", "test": 42}"#);
114 let mut map2 = make_map(r#"{"id": "bar", "test": 42}"#);
115 let mut inner_type = ObjectDataType::new("Inner");
116 inner_type.add_structure_converter(
117 1,
118 value_data_converter_func(|data, _from_version, _to_version| {
119 if let valence_nbt::value::ValueMut::Int(i) = data {
120 **i = 69;
121 }
122 }),
123 );
124 let mut typ = IdDataType::new("Test");
125 typ.add_walker_for_id(
126 1,
127 "foo",
128 map_data_walker(move |data, from_version, to_version| {
129 convert_object_in_map(&inner_type, data, "test", from_version, to_version);
130 }),
131 );
132
133 typ.convert(&mut map1, 0.into(), 1.into());
134 typ.convert(&mut map2, 0.into(), 1.into());
135 assert_eq!(69, map1.get("test").unwrap().as_i64().unwrap());
136 assert_eq!(42, map2.get("test").unwrap().as_i64().unwrap());
137 }
138
139 fn simple_converted_type() -> MapDataType<'static> {
140 let mut ret = MapDataType::new("Test");
141 ret.add_structure_converter(
142 1,
143 map_data_converter_func(|data, _from_version, _to_version| {
144 if let Some(JValue::Int(i)) = data.get("test") {
145 data.insert("test", JValue::String(JavaString::from(i.to_string())));
146 }
147 }),
148 );
149 ret
150 }
151}