Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.unloadAPI("button")
- sleep(0.5)
- os.loadAPI("button")
- mon = peripheral.wrap("left")
- rednet.open("back")
- items = {}
- menu = "index"
- label = ""
- inputName = ""
- intputId = ""
- intputMeta = ""
- indexLetter = ""
- currentPage = 1 --Page number in letter index
- hasNextPage = false
- currentItem = ""
- withdrawAmount = ""
- craftAmount = ""
- function save(table, name)
- local file = fs.open(name, "w")
- file.write(textutils.serialize(table))
- file.close()
- end
- function load(name)
- local file = fs.open(name, "r")
- if file ~= nil then
- local data = file.readAll()
- file.close()
- return textutils.unserialize(data)
- else
- return nil
- end
- end
- function split(pString, pPattern) --("A_Small_Dog", "_")
- local Table = {}
- local fpat = "(.-)" .. pPattern
- local last_end = 1
- local s, e, cap = pString:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(Table, cap)
- end
- last_end = e + 1
- s, e, cap = pString:find(fpat, last_end)
- end
- if last_end <= #pString then
- cap = pString:sub(last_end)
- table.insert(Table, cap)
- end
- return Table
- end
- function getReply(uuid)
- id, message = rednet.receive()
- m = split(message, " ")
- if m[1] == "inv" then
- if m[2] == "replyAmount" then
- if tonumber(m[3]) == tonumber(uuid) then
- return m[4]
- end
- end
- end
- return nil
- end
- function selectIndex(selected)
- print("Selected " ..selected .." in " ..menu)
- button.flash(selected)
- if selected == "Menu" then
- menu = "index"
- currentPage = 0
- drawMenu()
- elseif menu == "index" then
- if selected == "Add Item" then
- inputName = ""
- menu = "addItemName"
- drawKeyboard("Enter Item Name: ")
- selected = ""
- elseif string.len(selected) == 1 and menu == "index" then
- currentPage = 1
- hasNextPage = false
- menu = "itemList"
- indexLetter = selected
- drawList(selected)
- end
- elseif menu == "itemList" then
- if selected == "Prev" then
- if currentPage > 1 then
- currentPage = currentPage - 1
- drawList(indexLetter)
- else
- c = string.byte(indexLetter) - 1
- if c >= 65 then
- currentPage = 1
- hasNextPage = false
- menu = "itemList"
- indexLetter = string.char(c)
- drawList(string.char(c))
- else
- currentPage = 1
- hasNextPage = false
- hasNextPage = false
- menu = "itemList"
- indexLetter = "Z"
- drawList("Z")
- end
- end
- elseif selected == "Next" then
- if hasNextPage then
- print("Has next")
- currentPage = currentPage + 1
- drawList(indexLetter)
- else
- print("No Next next")
- c = string.byte(indexLetter) + 1
- if c <= 90 then
- currentPage = 1
- hasNextPage = false
- menu = "itemList"
- indexLetter = string.char(c)
- drawList(string.char(c))
- else
- currentPage = 1
- hasNextPage = false
- menu = "itemList"
- indexLetter = "A"
- drawList("A")
- end
- end
- else
- print("Clicked item")
- menu = "item"
- currentItem = selected
- drawItem(selected)
- end
- elseif menu == "item" then
- if selected == "Withdraw" then
- withdrawAmount = ""
- menu = "withdraw"
- drawNumpad("Enter Amount: ")
- selected = ""
- end
- elseif menu == "addItemName" then
- print("In Add Item Name")
- if selected ~= "Backspace" and selected ~= "Menu" and string.len(selected) == 1 then
- mon.setCursorPos(2, 8)
- mon.clearLine()
- inputName = inputName ..string.lower(selected)
- mon.write(label ..inputName)
- selected = ""
- elseif selected == "Backspace" then
- mon.setCursorPos(2, 8)
- mon.clearLine()
- inputName = string.sub(inputName, 1, string.len(inputName) - 1)
- mon.write(label ..inputName)
- selected = ""
- elseif selected == "OK" and menu == "addItemName" then
- inputId = ""
- drawNumpad("Enter Item ID:")
- menu = "addItemId"
- end
- elseif menu == "withdraw" then
- if selected ~= "Backspace" and selected ~= "Menu" and string.len(selected) == 1 then
- mon.setCursorPos(2, 8)
- mon.clearLine()
- if withdrawAmount == nil then
- withdrawAmount = string.lower(selected)
- else
- withdrawAmount = withdrawAmount ..string.lower(selected)
- end
- mon.write(label ..withdrawAmount)
- selected = ""
- elseif selected == "Backspace" then
- mon.setCursorPos(2, 8)
- mon.clearLine()
- withdrawAmount = string.sub(withdrawAmount, 1, string.len(withdrawAmount) - 1)
- mon.write(label ..withdrawAmount)
- selected = ""
- elseif selected == "OK" and menu == "withdraw" then
- uuid = tostring(items[currentItem]["id"] + items[currentItem]["meta"] * 32768)
- amount = nil
- rednet.broadcast("inv getAmount " ..uuid)
- while amount == nil do
- amount = getReply(uuid)
- end
- if tonumber(amount) > tonumber(withdrawAmount) + 1 then
- rednet.broadcast("inv withdraw " ..uuid .." " ..withdrawAmount)
- drawItem(currentItem)
- menu = "item"
- else
- button.clearTable()
- button.setTable("Menu", selectIndex, 2, 80, 2, 4)
- button.label(2, 8, "Not enough items to withdraw!")
- button.label(2, 9, tostring(amount - 1) .." " ..currentItem .."(s) left in storage.")
- button.screen()
- end
- selected = ""
- end
- elseif menu == "addItemId" then
- print("In Add Item Name")
- if selected ~= "Backspace" and selected ~= "Menu" and string.len(selected) == 1 then
- print("In Charachet")
- mon.setCursorPos(2, 8)
- mon.clearLine()
- if inputId == nil then
- inputId = string.lower(selected)
- else
- inputId = inputId ..string.lower(selected)
- end
- mon.write(label ..inputId)
- selected = ""
- elseif selected == "Backspace" then
- mon.setCursorPos(2, 8)
- mon.clearLine()
- inputId = string.sub(inputId, 1, string.len(inputId) - 1)
- mon.write(label ..inputId)
- selected = ""
- elseif selected == "OK" and menu == "addItemId" then
- inputMeta = ""
- drawNumpad("Enter Item Meta:")
- menu = "addItemMeta"
- selected = ""
- end
- elseif menu == "addItemMeta" then
- print("In Add Item Meta")
- if selected ~= "Backspace" and selected ~= "Menu" and string.len(selected) == 1 then
- mon.setCursorPos(2, 8)
- mon.clearLine()
- if inputMeta == nil then
- inputMeta = string.lower(selected)
- else
- inputMeta = inputMeta ..string.lower(selected)
- end
- mon.write(label ..inputMeta)
- selected = ""
- elseif selected == "Backspace" then
- mon.setCursorPos(2, 8)
- mon.clearLine()
- inputMeta = string.sub(inputMeta, 1, string.len(inputMeta) - 1)
- mon.write(label ..inputMeta)
- selected = ""
- elseif selected == "OK" and menu == "addItemMeta" then
- createItem()
- end
- end
- end
- function pairsByKeys (t, f)
- local a = {}
- for n in pairs(t) do table.insert(a, n) end
- table.sort(a, f)
- local i = 0 -- iterator variable
- local iter = function () -- iterator function
- i = i + 1
- if a[i] == nil then return nil
- else return a[i], t[a[i]]
- end
- end
- return iter
- end
- function sortList(list)
- sortedList = {}
- for key, value in pairsByKeys(list) do
- sortedList[key] = value
- end
- return sortedList
- end
- function drawItem(name)
- button.clearTable()
- button.setTable("Prev", selectIndex, 2, 10, 2, 4)
- button.setTable("Menu", selectIndex, 12, 70, 2, 4)
- button.setTable("Next", selectIndex, 72, 80, 2, 4)
- button.setTable("Craft", selectIndex, 2, 40, 26, 32)
- button.setTable("Withdraw", selectIndex, 42, 80, 26, 32)
- id = items[name]["id"]
- meta = items[name]["meta"]
- uuid = id + meta * 32768
- amount = nil
- rednet.broadcast("inv getAmount " ..uuid)
- while amount == nil do
- amount = getReply(uuid)
- end
- button.label(2, 6, "Item: " ..name)
- button.label(2, 10, "ID: " ..tostring(id))
- button.label(2, 14, "Meta: " ..tostring(meta))
- button.label(2, 18, "UUID: " ..tostring(uuid))
- button.label(2, 22, "Amount: " ..amount)
- button.screen()
- end
- function addItem(name, id, meta)
- if name == nil then
- print("ITEM NAME IS NULL")
- end
- print("NAME: " ..name)
- items[name] = {}
- items[name]["id"] = tonumber(id)
- items[name]["meta"] = tonumber(meta)
- itemName = ""
- itemId = ""
- itemMeta = ""
- items = sortList(items)
- save(items, "dictionary")
- end
- function createItem()
- button.clearTable()
- addItem(inputName, inputId, inputMeta)
- button.setTable("Menu", selectIndex, 2, 80, 2, 4)
- button.label(2, 8, "Added " ..inputName .." to item dictionary!")
- for k, v in pairs(items) do
- print(k)
- end
- menu = "addedItem"
- button.screen()
- end
- function drawList(letter)
- print("Drawing Item List!")
- button.clearTable()
- button.setTable("Prev", selectIndex, 2, 10, 2, 4)
- button.setTable("Menu", selectIndex, 12, 70, 2, 4)
- button.setTable("Next", selectIndex, 72, 80, 2, 4)
- lastLetter = ""
- totalItemCount = 0
- itemsInColumn = 0
- column = 1
- pageItemIsOn = 1
- hasNextPage = false
- for k, v in pairsByKeys(items) do
- --print("Subbed: " .. string.sub(k, 1, 1))
- if lastLetter == string.lower(letter) and string.sub(k, 1, 1) ~= string.lower(letter) then
- print("Breaking")
- break
- elseif string.sub(k, 1, 1) == string.lower(letter) then
- pageItemIsOn = math.ceil(totalItemCount / 14)
- --print("pageItemIsOn: " ..pageItemIsOn .. " Current Page: " ..currentPage)
- if pageItemIsOn < 1 then
- pageItemIsOn = 1
- end
- --print("Page " ..currentPage)
- if pageItemIsOn == currentPage then
- if itemsInColumn == 7 then
- itemsInColumn = 0;
- if column == 1 then
- column = 2
- else
- column = 1
- end
- end
- if column == 1 then
- -- print("Adding to column 1")
- button.setTable(k, selectIndex, 2, 40, 6 + itemsInColumn * 4, 6 + itemsInColumn * 4 + 2)
- else
- --print("Adding to column 2")
- button.setTable(k, selectIndex, 42, 80, 6 + itemsInColumn * 4, 6 + itemsInColumn * 4 + 2)
- end
- itemsInColumn = itemsInColumn + 1
- elseif pageItemIsOn > currentPage then
- hasNextPage = true
- end
- totalItemCount = totalItemCount + 1
- lastLetter = string.lower(letter)
- end
- end
- print("Next? ", hasNextPage)
- button.screen()
- end
- function drawMenu()
- button.clearTable()
- button.setTable("Menu", selectIndex, 2, 80, 2, 4)
- button.setTable("A", selectIndex, 2, 10, 6, 11)
- button.setTable("B", selectIndex, 12, 20, 6, 11)
- button.setTable("C", selectIndex, 22, 30, 6, 11)
- button.setTable("D", selectIndex, 32, 40, 6, 11)
- button.setTable("E", selectIndex, 42, 50, 6, 11)
- button.setTable("F", selectIndex, 52, 60, 6, 11)
- button.setTable("G", selectIndex, 62, 70, 6, 11)
- button.setTable("H", selectIndex, 72, 80, 6, 11)
- button.setTable("I", selectIndex, 2, 10, 13, 18)
- button.setTable("J", selectIndex, 12, 20, 13, 18)
- button.setTable("K", selectIndex, 22, 30, 13, 18)
- button.setTable("L", selectIndex, 32, 40, 13, 18)
- button.setTable("M", selectIndex, 42, 50, 13, 18)
- button.setTable("N", selectIndex, 52, 60, 13, 18)
- button.setTable("O", selectIndex, 62, 70, 13, 18)
- button.setTable("P", selectIndex, 72, 80, 13, 18)
- button.setTable("Q", selectIndex, 2, 10, 20, 25)
- button.setTable("R", selectIndex, 12, 20, 20, 25)
- button.setTable("S", selectIndex, 22, 30, 20, 25)
- button.setTable("T", selectIndex, 32, 40, 20, 25)
- button.setTable("U", selectIndex, 42, 50, 20, 25)
- button.setTable("V", selectIndex, 52, 60, 20, 25)
- button.setTable("W", selectIndex, 62, 70, 20, 25)
- button.setTable("X", selectIndex, 72, 80, 20, 25)
- button.setTable("Y", selectIndex, 2, 10, 27, 32)
- button.setTable("Z", selectIndex, 12, 20, 27, 32)
- button.setTable("Add Item", selectIndex, 22, 80, 27, 32)
- button.screen()
- end
- function drawKeyboard(tempLabel)
- button.clearTable()
- label = tempLabel
- button.setTable("Menu", selectIndex, 2, 80, 2, 4)
- button.setTable("A", selectIndex, 2, 10, 12, 17)
- button.setTable("B", selectIndex, 12, 20, 12, 17)
- button.setTable("C", selectIndex, 22, 30, 12, 17)
- button.setTable("D", selectIndex, 32, 40, 12, 17)
- button.setTable("E", selectIndex, 42, 50, 12, 17)
- button.setTable("F", selectIndex, 52, 60, 12, 17)
- button.setTable("G", selectIndex, 62, 70, 12, 17)
- button.setTable("H", selectIndex, 72, 80, 12, 17)
- button.setTable("I", selectIndex, 2, 10, 19, 24)
- button.setTable("J", selectIndex, 12, 20, 19, 24)
- button.setTable("K", selectIndex, 22, 30, 19, 24)
- button.setTable("L", selectIndex, 32, 40, 19, 24)
- button.setTable("M", selectIndex, 42, 50, 19, 24)
- button.setTable("N", selectIndex, 52, 60, 19, 24)
- button.setTable("O", selectIndex, 62, 70, 19, 24)
- button.setTable("P", selectIndex, 72, 80, 19, 24)
- button.setTable("Q", selectIndex, 2, 10, 26, 28)
- button.setTable("R", selectIndex, 12, 20, 26, 28)
- button.setTable("S", selectIndex, 22, 30, 26, 28)
- button.setTable("T", selectIndex, 32, 40, 26, 28)
- button.setTable("U", selectIndex, 42, 50, 26, 28)
- button.setTable("V", selectIndex, 2, 10, 30, 32)
- button.setTable("W", selectIndex, 12, 20, 30, 32)
- button.setTable("X", selectIndex, 22, 30, 30, 32)
- button.setTable("Y", selectIndex, 32, 40, 30, 32)
- button.setTable("Z", selectIndex, 42, 50, 30, 32)
- button.setTable(" ", selectIndex, 52, 60, 26, 32)
- button.setTable("Backspace", selectIndex, 62, 70, 26, 32)
- button.setTable("OK", selectIndex, 72, 80, 26, 32)
- mon.setCursorPos(2, 8)
- mon.write(label)
- button.screen()
- end
- function drawNumpad(tempLabel)
- button.clearTable()
- label = tempLabel
- button.setTable("Menu", selectIndex, 2, 80, 2, 4)
- button.setTable("0", selectIndex, 2, 10, 13, 32)
- button.setTable("1", selectIndex, 12, 20, 13, 18)
- button.setTable("2", selectIndex, 22, 30, 13, 18)
- button.setTable("3", selectIndex, 32, 40, 13, 18)
- button.setTable("4", selectIndex, 12, 20, 20, 25)
- button.setTable("5", selectIndex, 22, 30, 20, 25)
- button.setTable("6", selectIndex, 32, 40, 20, 25)
- button.setTable("7", selectIndex, 12, 20, 27, 32)
- button.setTable("8", selectIndex, 22, 30, 27, 32)
- button.setTable("9", selectIndex, 32, 40, 27, 32)
- button.setTable("Backspace", selectIndex, 42, 60, 13, 32)
- button.setTable("OK", selectIndex, 62, 80, 13, 32)
- mon.setCursorPos(2, 8)
- mon.write(label)
- button.screen()
- end
- function getClick()
- event, side, x, y = os.pullEvent("monitor_touch")
- button.checkxy(x, y)
- end
- items = load("dictionary")
- if items == nil then
- items = {}
- end
- drawMenu()
- while true do
- getClick()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement