Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("libgo")
- local primaryMon = nil
- local colors = {
- black = 0x000000,
- white = 0xFFFFFF,
- blue = 0x1E88E5,
- darkBlue = 0x1565C0,
- faintBlue = 0xE3F2FD,
- blueGrey = 0x607D8B,
- blackish = 0x212121,
- red = 0xF44336
- }
- local theme = {
- headerBg = colors.blue,
- headerText = colors.faintBlue,
- headerAccent = colors.darkBlue,
- bodyBg = colors.white,
- bodyText = colors.blueGrey
- }
- local function setup()
- -- Primary monitor
- for _, side in pairs(redstone.getSides()) do
- if peripheral.isPresent(side) and peripheral.getType(side) == "monitor" then
- print("Monitor side: " .. side)
- primaryMon = peripheral.wrap(side)
- end
- end
- if not primaryMon then
- print("No monitor could be found.")
- return
- end
- -- Theming
- local i = 0
- for name, hex in pairs(theme) do
- primaryMon.setPaletteColor(2^i, hex)
- theme[name] = 2^i
- i = i + 1
- end
- end
- local addressWidth = 12
- local priceWidth = 11
- local stockWidth = 6
- local function writeItem(m, item)
- local x, y = m.getCursorPos()
- local w, h = m.getSize()
- m.setBackgroundColor(theme.bodyBg)
- m.setTextColor(theme.bodyText)
- m.setCursorPos(3, y)
- m.write(item.name)
- m.setCursorPos(w - 1 - addressWidth - 1 - priceWidth - 1 - stockWidth, y)
- m.write(tostring(item.stock))
- m.setTextColor(theme.headerBg)
- m.setCursorPos(w - 1 - addressWidth - 1 - priceWidth, y)
- m.write(tostring(item.price) .. " KST/ea")
- m.setTextColor(theme.headerAccent)
- m.setCursorPos(w - 1 - addressWidth, y)
- m.write(item.address)
- end
- local function writeHeader(m)
- local x, y = m.getCursorPos()
- local w, h = m.getSize()
- m.setBackgroundColor(theme.headerAccent)
- m.setTextColor(theme.bodyBg)
- m.clearLine()
- m.setCursorPos(3, y)
- m.write("Item")
- m.setCursorPos(w - 1 - addressWidth - 1 - priceWidth - 1 - stockWidth, y)
- m.write("Stock")
- m.setCursorPos(w - 1 - addressWidth - 1 - priceWidth, y)
- m.write("Price")
- m.setCursorPos(w - 1 - addressWidth, y)
- m.write("Address")
- end
- local function renderShop(m, items)
- m.setTextScale(0.5)
- local w, h = m.getSize()
- -- Draw header
- local headerHeight = 3
- m.setBackgroundColor(theme.headerBg)
- m.setTextColor(theme.headerText)
- m.setCursorPos(1, 1)
- m.clearLine()
- m.setCursorPos(3, 2)
- m.clearLine()
- m.write("Cloud's Cache")
- m.setCursorPos(1, 3)
- m.clearLine()
- m.setCursorPos(1, 4)
- writeHeader(m)
- -- Draw table body
- m.setBackgroundColor(theme.bodyBg)
- for i = headerHeight + 2, h do
- m.setCursorPos(1, i)
- m.clearLine()
- end
- m.setCursorPos(3, headerHeight + 2)
- m.setTextColor(theme.bodyText)
- for i = headerHeight + 2, headerHeight + 1 + #items do
- m.setCursorPos(1, i)
- writeItem(m, items[i - headerHeight - 1])
- end
- end
- local database = {
- ["minecraft:cobblestone"] = {
- name = "Cobblestone",
- price = 0.05,
- address = "cbs@klua.kst"
- }
- }
- local itemUpdateChan = "item_update"
- local function scanInv()
- local itemMap = {}
- for slot = 1, 16 do
- local stock = turtle.getItemCount(slot)
- if stock > 0 then
- local item = turtle.getItemDetail(slot)
- if not itemMap[item.name] then
- itemMap[item.name] = {stock = stock}
- else
- local current = itemMap[item.name]
- current.stock = current.stock + stock
- end
- end
- end
- local items = {}
- for itemID, item in pairs(itemMap) do
- if database[itemID] then
- local entry = database[itemID]
- table.insert(items, {
- name = entry.name,
- price = entry.price,
- address = entry.address,
- stock = item.stock
- })
- end
- end
- return items
- end
- local function main()
- setup()
- go(function()
- while true do
- renderShop(primaryMon, scanInv())
- sleep(5)
- end
- end)
- end
- libgo.runDispatcher(main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement