-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmoker.lua
74 lines (63 loc) · 1.67 KB
/
smoker.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
print('Launching Furnace Monitoring')
os.loadAPI('clientserver.lua')
local server=(getfenv())["clientserver.lua"]
local furnaces
function getConnectedFurnaces ()
local p = peripheral.getNames()
local furnaces = {}
for i = 1, #p do
s, e = string.find(p[i], 'container_furnace' );
if s ~= nil and s == 1 then
furnaces[ p[i] ] = peripheral.wrap( p[i] )
end
end
return furnaces
end
function setSmoker (burning)
redstone.setOutput('right', not burning)
end
function updateSmoker ()
-- set smoker state
local isBurning = false
for n, p in pairs(furnaces) do
isBurning = p.isBurning() or isBurning
--print( 'Found: ' .. n .. ', burning: ' .. (p.isBurning() and 'true' or 'false') )
end
setSmoker( isBurning )
server.send('smoking|' .. tostring(isBurning))
end
local args = { ... }
local type = args[1]
local protocol = args[2]
local clients = {}
function onSmoking (sid, isSmoking)
clients[sid] = { time = os.time(), isSmoking = isSmoking == 'true' }
updateSmokeStack();
end
function updateSmokeStack ()
local turnOn = false
local time = os.time()
local toRemove = {}
for k,v in pairs(clients) do
if math.abs( v.time - time ) > 0.1 then
table.insert(toRemove, k)
--clients[k] = nil
else
turnOn = turnOn or v.isSmoking
end
end
for i=1,#toRemove do
clients[toRemove[i]] = nill
end
redstone.setOutput('top', not turnOn)
end
if type == 'client' then
furnaces = getConnectedFurnaces()
server.run('client', protocol or 'jo-factory', updateSmoker, 2)
elseif type == 'server' then
local commandTable = {
smoking = onSmoking
}
server.addCommandTable('server', commandTable)
server.run('server', protocol or 'jo-factory', updateSmokeStack, 10);
end