-
Notifications
You must be signed in to change notification settings - Fork 0
/
meBridge.lua
187 lines (156 loc) · 5.81 KB
/
meBridge.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
--[[
toastonrye's AE2 Setpoint Controller
Developed with:
-Basalt UI: https://basalt.madefor.cc/#/
-minecraft-forge 1.19.2-43.1.47
-cc-tweaked-1.19.2-1.101.1.jar
-appliedenergistics2-forge-12.8.4.jar
-AdvancedPeripherals-0.7.21b.jar
v0.1 - 2022-11-06 - partially working proof of concept. YT: https://www.youtube.com/watch?v=5YyxhZpZ1pk
bugs:
- when clicking off an input box, without entering a number leaves it blank. Can't figure out onLoseFocus event..
- many more
--]]
-- -------------------------------------------------------------------------------------------------------------------
local filePath = "basalt.lua"
if not(fs.exists(filePath))then
shell.run("wget run https://basalt.madefor.cc/install.lua packed basalt.lua master")
end
-- -------------------------------------------------------------------------------------------------------------------
local basalt = require("basalt")
local me = peripheral.find("meBridge")
if not me then error("meBridge not found") end
-- meBridge peripheral, isItemCraftable table variable. Bug?: Only returns pattern item info if item amount >=1
local parsedData = {}
local w, h = term.getSize()
local mainF = basalt.createFrame():show():setBackground(colours.purple)
local pollF = mainF:addFrame("test"):setSize(w-2,h):setPosition(2,5):setBackground(colours.yellow)
local pollingProgress = mainF:addProgressbar():setPosition(1,4):setBackground(colours.white):setProgress(10)
-- -------------------------------------------------------------------------------------------------------------------
local function fancyButton(self, event, button, x, y)
self:onClick(function()
self:setBackground(colours.black)
self:setForeground(colours.lightGrey)
end)
self:onClickUp(function()
self:setBackground(colours.grey)
self:setForeground(colours.black)
end)
self:onLoseFocus(function()
self:setBackground(colours.grey)
self:setForeground(colours.black)
end)
end
local function updateConfig(parsed)
local f = fs.open("meBridge.txt", "w")
f.write(textutils.serialize(parsed))
f.close()
basalt.debug("Updated!")
end
-- -------------------------------------------------------------------------------------------------------------------
local function loadInterface(parsed, frame) -- parsedData, pollF
local items = {}
local labelName = {}
local labelAmount = {}
local inputSetpoint = {}
local w, h = frame:getSize()
local tempParsed = parsed
for i=1, #tempParsed do
items[i] = frame:addFrame():setBackground(colours.cyan):setPosition(2,i*2):setSize(w-4,1)
labelName = items[i]:addLabel():setText(tempParsed[i].name)
labelAmount = items[i]:addLabel():setText(tempParsed[i].amount):setPosition(w*0.75, 1)
inputSetpoint = items[i]:addInput():setInputType("number"):setValue(tempParsed[i].setpoint)
:setPosition(w*0.8, 1):setBackground(colours.lightBlue)
:onKey(function(self, event, key)
if key == keys.enter or key == keys.numPadEnter then
local t = self.getValue()
if t == "" or t < 0 or t > 10000 then
t = 0
self:setValue(0)
end
tempParsed[i].setpoint = t
updateConfig(tempParsed)
end
end)
--[[:onLoseFocus(function(self)
if key == keys.enter or key == keys.numPadEnter then
local t = self.getValue()
if t == "" or t < 0 or t > 10000 then
t = 0
self:setValue(0)
end
tempParsed[i].setpoint = t
updateConfig(tempParsed)
end
end)--]]
:onClick(function(self)
self:setValue("")
end)
end
--return items
end
-- -------------------------------------------------------------------------------------------------------------------
local function loadConfig()
local cfg
local f = fs.open("meBridge.txt", "r")
if f ~= nil then -- if meBridge.txt exists, load it
local data = f.readAll()
f.close()
cfg = textutils.unserialize(data)
end
return cfg
end
-- scans ae2 for craftable patterns and loads user setpoints if they exist or set to 1
-- quirk: redstone to pulse AE2 temporary inventory on/off because me.listCraftableItems doesn't find a pattern if there is 0 items...
local function scanCraftableItems(frame)
local parsed = {} -- new table: name, amount, setpoint
local config = loadConfig()
local ae2Raw, loadedSetpoint
--basalt.debug("Redstone On")
redstone.setOutput("back", true)
os.sleep(1)
ae2Raw = me.listCraftableItems()
for k, v in pairs(ae2Raw) do
if config ~= nil then
for k2, v2 in pairs(config) do
if v.name == v2.name then
loadedSetpoint = v2.setpoint
end
end
end
table.insert(parsed, {name = v.name, amount = v.amount, setpoint = loadedSetpoint or 1})
end
redstone.setOutput("back", false)
--basalt.debug("Redstone Off")
local f = fs.open("meBridge.txt", "w")
f.write(textutils.serialize(parsed))
f.close()
basalt.debug("Saved Setpoints!")
local interface = loadInterface(parsed, frame) -- parsedData and pollF
return parsed
end
local function testCraft()
basalt.debug("calling testCraft()")
for k, v in pairs(parsedData) do
if v.amount < v.setpoint then
basalt.debug(v.amount, v.setpoint)
me.craftItem(v)
os.sleep(1)
end
end
end
local buttonRescan = mainF:addButton():onClick(basalt.schedule(function() parsedData = scanCraftableItems(pollF) end)):setSize(10,3):setPosition(1,1):setValue("Rescan")
fancyButton(buttonRescan)
local buttonTestCraft = mainF:addButton():onClick(basalt.schedule(function() testCraft() end)):setSize(10,3):setPosition(15,1):setValue("Test Craft")
fancyButton(buttonRescan)
local function myMain() -- why can this function see parsedData, shouldn't it be out of scope?
while true do
os.sleep(1)
--[[for k, v in pairs(parsedData) do
basalt.debug(k ,v.name, v.setpoint)
end--]]
end
end
parsedData = scanCraftableItems(pollF)
parallel.waitForAll(basalt.autoUpdate, myMain)
--basalt.schedule(function() basalt.debug("testting") os.sleep(1)end)