@@ -7,6 +7,7 @@ fn main() {
77 load_packets ( ) ;
88 load_blocks ( ) ;
99 load_items ( ) ;
10+ load_item_to_block ( ) ;
1011}
1112
1213fn load_packets ( ) {
@@ -126,4 +127,48 @@ fn load_items() {
126127
127128 let out_path = Path :: new ( & env:: var ( "OUT_DIR" ) . unwrap ( ) ) . join ( "items.rs" ) ;
128129 fs:: write ( out_path, out) . unwrap ( ) ;
130+ }
131+
132+ fn load_item_to_block ( ) {
133+ let manifest = env:: var ( "CARGO_MANIFEST_DIR" ) . unwrap ( ) ;
134+ let blocks_json_path = Path :: new ( & manifest) . join ( "src/assets/blocks.json" ) ;
135+ let items_json_path = Path :: new ( & manifest) . join ( "src/assets/items.json" ) ;
136+
137+ let block_bytes = fs:: read ( & blocks_json_path) . expect ( "Failed to read blocks.json" ) ;
138+ let item_bytes = fs:: read ( & items_json_path) . expect ( "Failed to read items.json" ) ;
139+
140+ let blocks: HashMap < String , serde_json:: Value > =
141+ serde_json:: from_slice ( & block_bytes) . expect ( "Failed to parse blocks.json" ) ;
142+ let items: HashMap < String , serde_json:: Value > =
143+ serde_json:: from_slice ( & item_bytes) . expect ( "Failed to parse items.json" ) ;
144+
145+ let mut out = String :: new ( ) ;
146+ out. push_str ( "pub fn item_to_block(item_id: i32) -> Option<u16> {\n " ) ;
147+ out. push_str ( " match item_id {\n " ) ;
148+
149+ for ( ik, iv) in items[ "entries" ] . as_object ( ) . unwrap ( ) {
150+ if let Some ( block) = blocks. get ( ik) {
151+ let default_state = block[ "states" ]
152+ . as_array ( )
153+ . unwrap ( )
154+ . iter ( )
155+ . find ( |state| state[ "default" ] . as_bool ( ) . unwrap_or ( false ) )
156+ . expect ( "Block is missing a default state" ) ;
157+
158+ out. push_str (
159+ & format ! (
160+ " {} => Some({}),\n " ,
161+ iv[ "protocol_id" ] . as_u64( ) . unwrap( ) ,
162+ default_state[ "id" ] . as_u64( ) . unwrap( )
163+ )
164+ ) ;
165+ }
166+ }
167+
168+ out. push_str ( " _ => None,\n " ) ;
169+ out. push_str ( " }\n " ) ;
170+ out. push_str ( "}\n " ) ;
171+
172+ let out_path = Path :: new ( & env:: var ( "OUT_DIR" ) . unwrap ( ) ) . join ( "item_to_block.rs" ) ;
173+ fs:: write ( out_path, out) . unwrap ( ) ;
129174}
0 commit comments