Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- This slab of code has no documentation, no coments, and makes no sense, in the event of needing to fix it
- -- may the lord be with you, not even I, it's creator, understand how it works, I only know it works,
- -- so if you have entered here to optimize or fix something, you have my most sincere condolences.
- local modem = peripheral.find("modem") or error("No modem attached", 0)
- local inputChest = peripheral.wrap("minecraft:chest_8") or error("No input chest found", 0)
- local crafter = peripheral.wrap("turtle_0")
- local inputChetName = peripheral.getName(inputChest)
- local query = ""
- local page = 1
- local craftMode = false
- local chests = {}
- local items = {}
- local buttons = {}
- function Draw()
- -- cache the buttons data
- buttons = {}
- local iy=2
- local counter = 1
- for name, amount in pairs(items) do
- local index = string.find(name, ":")
- name = string.sub(name,index+1,string.len(name))
- if (string.find(name, query) ~= nil) then
- if (counter >= page) then
- amount = tostring(amount)
- table.insert(buttons, {name=name, amount=amount})
- iy=iy+1
- if (iy>19) then
- break
- end
- end
- end
- counter=counter+1
- end
- term.setBackgroundColor(colors.white)
- term.clear()
- term.setTextColor(colors.black)
- -- Page
- if (page > 1) then
- term.setCursorPos(51,2)
- term.write("\24")
- end
- if (page < #items-17) then
- term.setCursorPos(51,18)
- term.write("\25")
- end
- -- Scrollbar
- if (#items-17 > 1) then
- local scrollbarSize = (18/#items)*16
- local scrollbarPos = (page/#items)+3
- paintutils.drawLine(51,scrollbarPos, 51,17, colors.orange)
- --paintutils.drawLine(51,scrollbarPos, 51,scrollbarSize+2, colors.orange)
- end
- -- item buttons
- for i, button in ipairs(buttons) do
- paintutils.drawLine(2,i+1, 46,i+1, colors.yellow)
- term.setCursorPos(2,i+1)
- term.write(button.name)
- -- Quantity
- term.setCursorPos(47-string.len(button.amount),i+1)
- term.write(button.amount)
- -- x64
- paintutils.drawLine(48,i+1, 50,i+1, colors.green)
- term.setCursorPos(48,i+1)
- term.write("x64")
- end
- term.setTextColor(colors.white)
- -- Clear button
- paintutils.drawLine(47,1, 51,1, colors.red)
- term.setCursorPos(47,1)
- term.write("Clear")
- -- query
- paintutils.drawLine(1,1, 46,1, colors.black)
- term.setCursorPos(1,1)
- print(query)
- term.setCursorPos(string.len(query)+1,1)
- term.setCursorBlink(true)
- end
- function GetItems()
- items = {}
- chests = modem.getNamesRemote()
- for i, c in ipairs(chests) do
- if (c ~= inputChetName and string.find(c, "chest")) then
- local chest = peripheral.wrap(c)
- for slot, item in pairs(chest.list()) do
- if (items[item.name] == nil) then
- items[item.name]=item.count
- else
- items[item.name]=items[item.name] + item.count
- end
- end
- end
- end
- end
- function KeyProcess(key)
- if (keys.getName(key) == "backspace") then
- query = string.sub(query,1,string.len(query)-1)
- elseif (keys.getName(key) == "space") then
- query = query .. "_"
- elseif (string.len(keys.getName(key)) == 1) then
- query = query .. keys.getName(key)
- end
- end
- function PullItem(name,amount)
- for i, c in ipairs(chests) do
- if (string.find(c, "chest")) then
- local chest = peripheral.wrap(c)
- for slot, item in pairs(chest.list()) do
- if (item.name == name) then
- chest.pushItems(inputChetName, slot, amount)
- return
- end
- end
- end
- end
- end
- function StoreItems()
- for slot, item in pairs(inputChest.list()) do
- if (item ~= nil) then
- for i, c in ipairs(chests) do
- if (c ~= inputChetName and string.find(c, "chest")) then
- local amount = inputChest.pushItems(c, slot)
- if (amount == item.count) then
- break
- end
- end
- end
- end
- end
- end
- function Click(x,y)
- if (y==1 and x>=47) then
- StoreItems()
- return
- end
- local iy=2
- for name, amount in pairs(items) do
- if (string.find(name, query) ~= nil) then
- if (iy==y and x>=48) then
- PullItem(name,64)
- return
- elseif (iy==y) then
- PullItem(name,1)
- return
- end
- iy=iy+1
- end
- end
- end
- function CraftItem(quantity)
- local recipeScheme = {"", "", "",
- "", "", "",
- "", "", ""}
- -- here we need to recover the recipe from the JSON file (One file per recipe? Nah)
- local aditioner = 0
- for i, item in ipairs(recipeScheme) do
- -- to ignore the slots not used to craft
- if (i == 4 or i == 8) then
- aditioner = aditioner + 1
- end
- local slot = i + aditioner
- chest.pushItems(crafter, fromslot, quantity, slot)
- end
- modem.transmit(1, 2, "CRAFT")
- end
- --main
- GetItems()
- Draw()
- parallel.waitForAny(
- function()
- while (true) do
- local event, key, is_held = os.pullEvent("key")
- KeyProcess(key)
- Draw()
- end
- end,
- function()
- while (true) do
- local event, dir, x, y = os.pullEvent("mouse_scroll")
- page = page + dir
- if (page < 1) then
- page = 1
- elseif (#buttons > 18) then
- page = #buttons
- Draw()
- else
- Draw()
- end
- end
- end,
- function()
- while (true) do
- local event, button, x, y = os.pullEvent("mouse_click")
- Click(x,y)
- GetItems()
- Draw()
- end
- end
- )
Add Comment
Please, Sign In to add comment