-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorter_mega.lua
More file actions
157 lines (102 loc) · 3.42 KB
/
sorter_mega.lua
File metadata and controls
157 lines (102 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
-- SORTER
-- version 1.0.1
-- for use with pipeworks lua tube
--
-- Supports 4 additional functions:
-- 1. find - just a re-implemented string.find, because normal one
-- is disabled in luacontroller
-- 2. array_find - search item name (or something else) in the array
-- of elements; array is just an indexed table
-- 3. is_mod - checks whether a string contains selected modname
-- 4. string_begins - checks whether a string begins with some substring
--
--
-- License: GNU AGPL
-- Copyright Ghaydn (ghaydn@ya.ru), 2023
--
-- https://github.com/Ghaydn/misc_minetest_scripts/blob/main/sorter_mega.lua
--
--- ports
--- you can name these as you want, depending on what's on the end of pipes
local cobble_processor = "red"
local signs_bot_chest = "green"
local plant_drawer = "blue"
local ta4_chest = "white"
-- default
local default = "black"
--------------------------------------------------------------------------------
-- string.find is disabled in luacontroller, so let's write custom one
local function find(str, pattern)
-- check length
local pattern_len = pattern:len()
local str_len = str:len()
if str_len < pattern_len then return nil end
local max_pos = str_len - pattern_len + 1
-- searching here
for pos = 1, max_pos do
if str:sub(pos, pos + pattern_len) == pattern then return pos end
end
-- not found
return nil
end
--------------------------------------------------------------------------------
-- search for the first exact matching value in a table that looks like an array
local function array_find(array, pattern)
for i, v in ipairs(array) do
if v == pattern then return i end
end
return nil
end
--------------------------------------------------------------------------------
-- check if this item name belongs to a specific mod
-- it's simpler than using find
local function is_mod(str, modname)
local modlength = modname:len() + 1
local sub = str:sub(1, modlength)
return (sub == modname..":")
end
--------------------------------------------------------------------------------
local function string_begins(str, pattern)
local length = pattern:len()
local sub = str:sub(1, length)
return (sub == pattern)
end
--------------------------------------------------------------------------------
----- SORTING EXAMPLE -------------------------------------------------------
--------------------------------------------------------------------------------
if event.type == "item" then
local item = event.item.name
---- Example of "array_find" use
local send_to_cobble = {
"default:cobble",
"default:gravel",
"techage:bauxite_cobble",
"techage:bauxite_gravel"
}
if array_find(send_to_cobble, item)
then
return cobble_processor
end
---- send ores to process
-- example of "is_mod" use
if is_mod(item, "signs_bot")
then
return signs_bot_chest
end
---- example of "find" use
if find(item, "sapling")
or find(item, "wood")
or find(item, "planks")
or find(item, "leaves")
or find(item, "shroom") -- MUshroom or ethereal ILLUMIshroom
or find(item, "fung") -- fungUS, fungAL etc
then
return plant_drawer
end
---- example if "string_begins" use
if string_begins(item, "techage:ta4")
then
return ta4_chest
end
return default
end