Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mon = peripheral.wrap("right")
- mon.setBackgroundColor(colors.black)
- mon.clear()
- w,h = mon.getSize()
- tw, th = term.getSize()
- items = {}
- print(tw)
- changeCostButton = {id = "changeCost", color = colors.green, text1 = {text = "Change", ydiff = 1}, text2 = {text = "Cost", ydiff = 2}, x1 = (tw/2)-(8/2)+1, y1 = 5, x2 = (tw/2)-(8/2)+1+7, y2 = 8}
- changeNameButton = {id = "changeName", color = colors.blue, text1 = {text = "Change", ydiff = 1}, text2 = {text = "Name", ydiff = 2}, x1 = (tw/2)-(8/2)+1, y1 = 11, x2 = (tw/2)-(8/2)+1+7, y2 = 14}
- tempMoving = 0
- function updateItems()
- items = {}
- file = io.open("items", "r")
- for line in file:lines() do
- if line ~= "" then
- itemN, priceN = line:match("([^:]+):([^:]+)")
- table.insert(items, {item = itemN, price = tonumber(priceN)})
- end
- end
- file:close()
- end
- function updateMonitor()
- mon.setBackgroundColor(colors.black)
- mon.clear()
- for i, v in pairs(items) do
- mon.setCursorPos(1,i)
- if i%2 == 0 then
- mon.setBackgroundColor(colors.gray)
- mon.clearLine()
- else
- mon.setBackgroundColor(colors.lightGray)
- mon.clearLine()
- end
- mon.write(string.sub(v["item"],1,(w-string.len(" - $"..v["price"]))).." - $"..v["price"])
- end
- end
- function showPassword()
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- term.write("Password: ")
- pass = read("*")
- if pass == "temp" then
- print("Access Granted")
- sleep(0.5)
- return true
- elseif pass == "term" then
- error("Terminating")
- else
- print("Wrong Password!")
- sleep(3)
- return false
- end
- end
- function updateScreen()
- term.setBackgroundColor(colors.black)
- term.clear()
- last = 0
- for i, v in pairs(items) do
- term.setCursorPos(1,i)
- if i%2 == 0 then
- term.setBackgroundColor(colors.gray)
- term.clearLine()
- else
- term.setBackgroundColor(colors.lightGray)
- term.clearLine()
- end
- term.write(v["item"].." - $"..v["price"])
- last = i
- end
- if last < th then
- term.setCursorPos(1, last+1)
- term.setBackgroundColor(colors.green)
- term.clearLine()
- term.write("ADD ITEM")
- end
- end
- function showDetails(num)
- term.setBackgroundColor(colors.black)
- term.clear()
- item = items[num]
- term.setCursorPos((tw/2)-(string.len(item["item"])/2)+1, 2)
- term.write(item["item"])
- term.setCursorPos((tw/2)-(string.len("$"..item["price"])/2)+1, 3)
- term.write("$"..item["price"])
- for i, v in pairs({changeCostButton, changeNameButton}) do
- term.setBackgroundColor(v["color"])
- for x = v["x1"], v["x2"] do
- for y = v["y1"], v["y2"] do
- term.setCursorPos(x,y)
- term.write(" ")
- end
- end
- term.setCursorPos(v["x1"]+(v["x2"]-v["x1"])/2-string.len(v["text1"]["text"])/2,v["y1"]+v["text1"]["ydiff"])
- term.write(v["text1"]["text"])
- term.setCursorPos(v["x1"]+(v["x2"]-v["x1"])/2-string.len(v["text2"]["text"])/2,v["y1"]+v["text2"]["ydiff"])
- term.write(v["text2"]["text"])
- end
- end
- function changeDetail(det, num)
- if det == "changeName" then
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- term.write("New Name: ")
- newName = read()
- items[num]["item"] = newName
- elseif det == "changeCost" then
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- term.write("New Price: $")
- newPrice = read()
- if tonumber(newPrice) ~= nil then
- items[num]["price"] = tonumber(newPrice)
- end
- end
- saveItems()
- end
- function saveItems()
- file = io.open("items", "w")
- for i, v in pairs(items) do
- file:write(v["item"]..":"..v["price"].."\n")
- end
- file:close()
- updateItems()
- updateMonitor()
- end
- function addItem()
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- term.write("Item Name: ")
- newName = read()
- term.write("Item Price: $")
- newPrice = read()
- if tonumber(newPrice) ~= nil then
- table.insert(items, {item = newName, price = tonumber(newPrice)})
- end
- saveItems()
- end
- updateItems()
- updateMonitor()
- --updateScreen()
- while true do
- if showPassword() then
- ev = ""
- while ev ~= "key" do
- updateScreen()
- ev, d1, d2, d3 = os.pullEvent()
- if ev == "key" then
- elseif ev == "mouse_click" then
- if d1 == 1 then
- if d3 == #items+1 then
- addItem()
- elseif d3 <= #items then
- showDetails(d3)
- subev, subd1, subd2, subd3 = os.pullEvent()
- if subev == "key" then
- elseif subev == "mouse_click" then
- for i, v in pairs({changeCostButton, changeNameButton}) do
- if subd2 > v["x1"] and subd2 < v["x2"] then
- if subd3 > v["y1"] and subd3 < v["y2"] then
- changeDetail(v["id"], d3)
- print("Changed")
- sleep(1)
- end
- end
- end
- end
- end
- elseif d1 == 2 then
- if d3 <= #items then
- table.remove(items, d3)
- saveItems()
- end
- elseif d1 == 3 then
- if d3 <= #items then
- tempMoving = d3
- subbd3 = 0
- while subbd3 == 0 or subbd3 > #items or subbd3 == tempMoving do
- subbev, subbd1, subbd2, subbd3 = os.pullEvent("mouse_click")
- end
- temp = items[tempMoving]
- items[tempMoving] = items[subbd3]
- items[subbd3] = temp
- saveItems()
- end
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement