@@ -78,6 +78,31 @@ struct EnumString ModApiMapgen::es_Rotation[] =
78
78
};
79
79
80
80
81
+ static void read_schematic_replacements (lua_State *L, DecoSchematic *dschem, int index)
82
+ {
83
+ lua_pushnil (L);
84
+ while (lua_next (L, index )) {
85
+ // key at index -2 and value at index -1
86
+ std::string replace_from;
87
+ std::string replace_to;
88
+ if (lua_istable (L, -1 )) { // Old {{"x", "y"}, ...} format
89
+ lua_rawgeti (L, -1 , 1 );
90
+ replace_from = lua_tostring (L, -1 );
91
+ lua_pop (L, 1 );
92
+ lua_rawgeti (L, -1 , 2 );
93
+ replace_to = lua_tostring (L, -1 );
94
+ lua_pop (L, 1 );
95
+ } else { // New {x = "y", ...} format
96
+ replace_from = lua_tostring (L, -2 );
97
+ replace_to = lua_tostring (L, -1 );
98
+ }
99
+ dschem->replacements [replace_from] = replace_to;
100
+ // removes value, keeps key for next iteration
101
+ lua_pop (L, 1 );
102
+ }
103
+ }
104
+
105
+
81
106
// get_mapgen_object(objectname)
82
107
// returns the requested object used during map generation
83
108
int ModApiMapgen::l_get_mapgen_object (lua_State *L)
@@ -414,20 +439,7 @@ int ModApiMapgen::l_register_decoration(lua_State *L)
414
439
415
440
lua_getfield (L, index , " replacements" );
416
441
if (lua_istable (L, -1 )) {
417
- int i = lua_gettop (L);
418
- lua_pushnil (L);
419
- while (lua_next (L, i) != 0 ) {
420
- // key at index -2 and value at index -1
421
- lua_rawgeti (L, -1 , 1 );
422
- std::string replace_from = lua_tostring (L, -1 );
423
- lua_pop (L, 1 );
424
- lua_rawgeti (L, -1 , 2 );
425
- std::string replace_to = lua_tostring (L, -1 );
426
- lua_pop (L, 1 );
427
- dschem->replacements [replace_from] = replace_to;
428
- // removes value, keeps key for next iteration
429
- lua_pop (L, 1 );
430
- }
442
+ read_schematic_replacements (L, dschem, lua_gettop (L));
431
443
}
432
444
lua_pop (L, 1 );
433
445
@@ -602,19 +614,7 @@ int ModApiMapgen::l_place_schematic(lua_State *L)
602
614
dschem.rotation = (Rotation)rot;
603
615
604
616
if (lua_istable (L, 4 )) {
605
- lua_pushnil (L);
606
- while (lua_next (L, 4 ) != 0 ) {
607
- // key at index -2 and value at index -1
608
- lua_rawgeti (L, -1 , 1 );
609
- std::string replace_from = lua_tostring (L, -1 );
610
- lua_pop (L, 1 );
611
- lua_rawgeti (L, -1 , 2 );
612
- std::string replace_to = lua_tostring (L, -1 );
613
- lua_pop (L, 1 );
614
- dschem.replacements [replace_from] = replace_to;
615
- // removes value, keeps key for next iteration
616
- lua_pop (L, 1 );
617
- }
617
+ read_schematic_replacements (L, &dschem, 4 );
618
618
}
619
619
620
620
bool force_placement = true ;
0 commit comments