chester.lua

by anonymous
381 days agolua
COPY
1
-- touchscreen ender storage configurator with favorites/bookmarks
2
-- this one requires sc-peripherals, which is a switchcraft thing (sc3.io)
3
local chest = peripheral.find("ender_storage")
4
local monitor = peripheral.find("monitor")
5
monitor.setTextScale(0.5)
6
monitor.clear()
7

8
local favorites = {
9
    ["Lava"] = {colors.orange,colors.orange,colors.orange},
10
    ["Lava Return"] = {colors.orange,colors.orange,colors.white},
11
    ["Snow Balls"] = {colors.white,colors.lightBlue,colors.white},
12
    ["Snow Blocks"] = {colors.white,colors.blue,colors.white},
13
    ["Waste"] = {colors.red,colors.red,colors.red},
14
    ["Ender Pearls"] = {colors.white,colors.white,colors.white},
15
    ["Rocks"] = {colors.gray,colors.gray,colors.gray},
16
    ["Cooked Cod"] = {colors.yellow,colors.white,colors.yellow}
17
}
18
local fkeys = {}
19
for k,v in pairs(favorites) do
20
    table.insert(fkeys,k)
21
end
22

23
local online = {}
24
local wkeys = {}
25
local ok,err = pcall(function()
26
    local data = http.get("https://p.sc3.io/THaX6gxqtp").readAll()
27
    for _,v in pairs(textutils.unserialiseJSON(data)) do
28
        online[v.label] = v.colors
29
        table.insert(wkeys,v.label)
30
    end
31
end)
32
if not ok then printError("Online database not loaded: "..err) end
33

34
local selection = 2
35
local channel = {colors.white,colors.white,colors.white}
36
local chs = {"l","m","r"}
37
local tcol = {
38
    colors.white,
39
    colors.orange,
40
    colors.magenta,
41
    colors.lightBlue,
42
    colors.yellow,
43
    colors.lime,
44
    colors.pink,
45
    colors.gray,
46
    colors.lightGray,
47
    colors.cyan,
48
    colors.purple,
49
    colors.blue,
50
    colors.brown,
51
    colors.green,
52
    colors.red,
53
    colors.black
54
}
55

56
local function text(x,y,txt,color,bcolor)
57
    monitor.setCursorPos(x,y)
58
    if not color then color = colors.white end
59
    if not bcolor then bcolor = colors.black end
60
    monitor.setTextColor(color)
61
    monitor.setBackgroundColor(bcolor)
62
    monitor.write(txt)
63
end
64
local function rect(x,y,w,h,color,highlight)
65
    local bg = colors.toBlit(color)
66
    local fg = bg
67
    if highlight then
68
        if bg == "0" then
69
            fg = "f"
70
        else
71
            fg = "0" -- white
72
        end
73
    end
74
    for i=x,x+w do
75
        for j=y,y+h do
76
            monitor.setCursorPos(i,j)
77
            monitor.blit("\127",fg,bg)
78
        end
79
    end
80
end
81

82
local selectionModified = true
83
local show = false
84
local offset = 0
85
parallel.waitForAny(function() while true do
86
    -- drawing
87
    if selectionModified then
88
        selectionModified = false
89
        -- ok a little updating because stuff
90
        chest.setFrequency(channel[1],
91
                           channel[2],
92
                           channel[3])
93
        monitor.clear()
94
        if show then
95
            text(7,1,"\x1E")
96
            for i=1,8 do
97
                text(1,i+1,show[i+offset])
98
            end
99
            text(7,10,"\x1F")
100
        else
101
            for i=1,3 do
102
                local basex = (i*5)-4
103
                local bcol = channel[i]
104
                local col = bcol == colors.black and colors.white or colors.black
105
                if selection == i then
106
                    text(basex,1," >"..chs[i].."< ",col,bcol)
107
                else
108
                    text(basex,1,"  "..chs[i].."  ",col,bcol)
109
                end
110
            end
111
            local opt = channel[selection]
112
            for i=1,15 do
113
                rect(i,2,1,8,tcol[i],opt==2^(i-1))
114
            end
115
            text(1,10,"W",colors.white)
116
            rect(2,10,12,1,tcol[16],opt==2^15)
117
            text(15,10,"F",colors.white)
118
        end
119
    else sleep(0) end
120
end end, function() while true do
121
    -- handle input
122
    local e,s,x,y = os.pullEvent("monitor_touch")
123
    if show then
124
        if y == 1 then
125
            offset = math.max(offset-1,0)
126
        elseif y == 10 then
127
            offset = math.min(offset+1,#show-8)
128
        elseif show[y-1+offset] then
129
            if show == fkeys then
130
                channel = {table.unpack(favorites[show[y-1+offset]])}
131
            elseif show == wkeys then
132
                channel = {table.unpack(online[show[y-1+offset]])}
133
            end
134
            show = false
135
        else
136
            show = false
137
        end
138
    elseif y > 1 and y < 10 then
139
        channel[selection] = tcol[x]
140
    elseif y == 10 then
141
        if x == 1 then
142
            show = wkeys
143
            offset = 0
144
        elseif x == 15 then
145
            show = fkeys
146
            offset = 0
147
        else
148
            channel[selection] = tcol[16]
149
        end
150
    else
151
        if     x <= 5  then selection = 1
152
        elseif x <= 10 then selection = 2
153
        elseif x <= 15 then selection = 3
154
        end
155
    end
156
    selectionModified = true
157
end end)