|
1 | | --- Merges table2's contents into table1. |
| 1 | +--- Searches table for given value |
| 2 | +--- Returns index if found, nil otherwise |
| 3 | +local function table_find(table1, value) |
| 4 | + for index, value_1 in pairs(table1) do |
| 5 | + if type(value) == "table" then |
| 6 | + local is_table_same = true |
| 7 | + |
| 8 | + -- Iterate inner table |
| 9 | + for _, inner_value in pairs(value) do |
| 10 | + if not table_find(value_1, inner_value) then |
| 11 | + is_table_same = false |
| 12 | + end |
| 13 | + end |
| 14 | + |
| 15 | + -- Table is same as value table, thus the value is found |
| 16 | + if is_table_same then |
| 17 | + return index |
| 18 | + end |
| 19 | + else |
| 20 | + if value_1 == value then |
| 21 | + return index |
| 22 | + end |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + return nil; |
| 27 | +end |
| 28 | + |
| 29 | +-- Inserts the new item into the table only if it doesn't already exist. (Index optional.) |
| 30 | +function bobmods.lib.safe_insert(array, new_item, index) |
| 31 | + if not table_find(array, new_item) then |
| 32 | + if index then |
| 33 | + table.insert(array, index, new_item) |
| 34 | + else |
| 35 | + table.insert(array, new_item) |
| 36 | + end |
| 37 | + end |
| 38 | +end |
| 39 | + |
| 40 | +--- Safely inserts new items when index is numerical, replaces non-numeric matching indices |
| 41 | +local function safe_insert_and_replace(array, new_item, index) |
| 42 | + if type(index) == "number" then |
| 43 | + bobmods.lib.safe_insert(array, new_item, index) |
| 44 | + else |
| 45 | + array[index] = new_item |
| 46 | + end |
| 47 | +end |
| 48 | + |
| 49 | +--- Merges table2's contents into table1. |
| 50 | +--- This will append non-existing values and replace values with matching non-numeric key |
2 | 51 | function bobmods.lib.table_merge(table1, table2) |
3 | 52 | for index, value in pairs(table2) do |
4 | 53 | if type(value) == "table" then |
5 | 54 | if type(table1[index]) == "table" then |
6 | 55 | bobmods.lib.table_merge(table1[index], table2[index]) |
7 | 56 | else |
8 | | - table1[index] = util.table.deepcopy(table2[index]) |
| 57 | + safe_insert_and_replace(table1, util.table.deepcopy(table2[index]), index) |
9 | 58 | end |
10 | 59 | else |
11 | | - table1[index] = value |
| 60 | + safe_insert_and_replace(table1, value, index) |
12 | 61 | end |
13 | 62 | end |
14 | 63 | end |
@@ -38,23 +87,6 @@ function bobmods.lib.belt_speed_ips(ips) |
38 | 87 | return ips * 1 / 480 |
39 | 88 | end |
40 | 89 |
|
41 | | ---Inserts the new item into the table only if it doesn't already exist. (Index optional. Designed to insert strings only.) |
42 | | -function bobmods.lib.safe_insert(array, new_item, index) |
43 | | - local addit = true |
44 | | - for i, item in pairs(array) do |
45 | | - if item == new_item then |
46 | | - addit = false |
47 | | - end |
48 | | - end |
49 | | - if addit then |
50 | | - if index then |
51 | | - table.insert(array, index, new_item) |
52 | | - else |
53 | | - table.insert(array, new_item) |
54 | | - end |
55 | | - end |
56 | | -end |
57 | | - |
58 | 90 | --takes an item/fluid/entity(maybe even recipe) and returns a complete icons array. |
59 | 91 | --if it has no icons= tag, it builds one from icon and icon_size. |
60 | 92 | --Example use bobmods.lib.icons_from_item(data.raw.item.wood) |
|
0 commit comments