-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmeltery.lua
142 lines (131 loc) · 3.64 KB
/
smeltery.lua
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
-- 5
local config = {}
local Term = nil
local Table = nil
local port = nil
local cursor = 1
local function init()
if fs.exists('var/smeltery.conf') then
local file = fs.open('var/smeltery.conf', 'r')
config = textutils.unserialise(file.readAll())
file.close()
end
if not fs.exists('lib/term.lua') then
shell.run('installer lib/term.lua')
end
if not fs.exists('lib/table.lua') then
shell.run('installer lib/table.lua')
end
Term = require('lib/term')
Table = require('lib/table')
if config.portSide == nil then
setPortSide()
end
if config.metals == nil then
config.metals = {}
end
if config.limit == nil then
config.limit = false
end
port = peripheral.wrap(config.portSide)
end
function writeConfig()
local file = fs.open('var/smeltery.conf', 'w')
file.write(textutils.serialise(config))
file.close()
end
function setPortSide()
term.clear()
print('Which side is the port on?')
config.portSide = read()
writeConfig()
end
local function getCurrent()
return Table.map(port.getController().getMolten(), function(entry)
return {
name = entry.displayName,
amount = entry.amount
}
end)
end
local function display()
local current = getCurrent()
if cursor > #current then
cursor = #current
end
if cursor == 0 then
cursor = 1
end
term.clear()
if config.limit then
Term.out('Limit active', 1, 5)
end
for i = 1, #current do
if cursor == i then
Term.out(">", i + 2, 1)
end
if Table.indexOf(config.metals, current[i].name) > -1 then
Term.out('*', i + 2, 3)
end
Term.out(current[i].name, i + 2, 5)
Term.out(current[i].amount, i + 2, 30)
end
end
function keyListener()
local running = true
while running do
local _, key = os.pullEvent('key')
local current = getCurrent()
if key == keys.up and cursor > 1 then
cursor = cursor - 1
end
if key == keys.down and cursor < #current then
cursor = cursor + 1
end
if key == keys.space then
if Table.indexOf(config.metals, current[cursor].name) > -1 then
config.metals = Table.filter(config.metals, function(item)
return item ~= current[cursor].name
end)
else
Table.push(config.metals, current[cursor].name)
end
writeConfig()
end
if key == keys.enter then
port.getController().selectMolten(cursor)
end
if key == keys.l then
config.limit = not config.limit
writeConfig()
end
if key == keys.q then
running = false
end
display()
end
term.clear()
term.setCursorPos(1, 1)
sleep(0.05)
return
end
function signal()
while true do
local moltenList = port.getController().getMolten()
if (#moltenList > 0) then
local entry = moltenList[1]
local allFlushable = Table.every(moltenList, function(item)
return Table.indexOf(config.metals, item.displayName) > -1
end)
local aboveLimit = (not config.limit) or entry.amount > 20000 or allFlushable
rs.setOutput(config.portSide, Table.indexOf(config.metals, entry.displayName) > -1 and aboveLimit)
else
rs.setOutput(config.portSide, false)
end
display()
end
end
init()
display()
parallel.waitForAny(keyListener, signal)
rs.setOutput(config.portSide, false)