Guest User

startup

a guest
May 24th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.92 KB | None | 0 0
  1. os.loadAPI("f") -- a monitor API made by myself, it makes my life easier
  2. os.loadAPI("getInput") -- search_bar
  3. local p = peripheral.find("draconic_chest") -- can change for "chest" instead of "draconic_chest"
  4. local m = peripheral.find("monitor") -- running on a monitor
  5. local q = peripheral.find("peripheral")
  6. m.setTextScale(1)
  7. local side,rside = "north","top" -- IMPORTANT
  8. local list_items,items,stq = {},{},{}
  9. local a,b,c = 1,1,1
  10. local selected,last_dest = 0,"none"
  11. local chestSize = p.getInventorySize()
  12. local w,h = m.getSize()
  13.  
  14. local bg = window.create(m,1,1,w,h)
  15. local list = window.create(m,1,2,w*3/5-1,chestSize)
  16. local up_bar = window.create(m,1,1,w,1)
  17. local down_bar = window.create(m,1,h,w,h)
  18. local scroll_bar = window.create(m,w*(3/5),2,1,h-1)
  19. local bg2 = window.create(m,w*0.6+1,2,w*0.4+1,h-1)
  20. local pbg2 = {bg2.getPosition()} sbg2 = {bg2.getSize()}
  21. local button1 = window.create(bg2,2,3,sbg2[1]-3,3)
  22. local pb1 = {button1.getPosition()} sb1 = {button1.getSize()}
  23. local img = paintutils.loadImage("/Charm")
  24. --local switch1 = window.create(bg2,
  25. print("[",os.time(),"] ",os.day())
  26.  
  27. function reset()
  28.   bg.setBackgroundColor(colors.black)
  29.   bg.clear()
  30.   list.setBackgroundColor(colors.black)
  31.   list.clear()
  32.   up_bar.setBackgroundColor(colors.lightGray)
  33.   up_bar.clear()
  34.   down_bar.setBackgroundColor(colors.lightGray)
  35.   down_bar.clear()
  36.   scroll_bar.setBackgroundColor(colors.lightGray)
  37.   scroll_bar.clear()
  38.   bg2.setBackgroundColor(colors.gray)
  39.   bg2.clear()
  40.   button1.setBackgroundColor(colors.red)
  41.   button1.clear()
  42.   f.centerText(up_bar,1,"selectionner une destination","black")
  43.   f.cprint(down_bar,1,1,"dernier tp : ","black")
  44.   f.cprint(down_bar,14,1,last_dest,"blue","lightGray")
  45.   f.cprint(scroll_bar,1,1,"^","black","gray")
  46.   f.cprint(scroll_bar,1,h-2,"v","black","gray")
  47.   f.cprint(bg2,2,1,"Status: ","white","gray")
  48.   f.cprint(bg2,2,7,"Rechercher:","white","gray")
  49.   f.centerText(button1,2,"fermer","black")
  50.   f.drawLine(bg2,2,9,sbg2[1]-2.4,"lightGray")
  51.   if q ~= nil then
  52.     local stq = q.getAllStacks()
  53.     if stq[1] ~= nil then f.cprint(bg2,10,1,"ouvert","green","gray")
  54.     else f.cprint(bg2,10,1,"en veille","yellow","gray") end
  55.   end
  56.   term.redirect(bg2)
  57.   paintutils.drawImage(img,sbg2[1]/2-3,11)
  58. end
  59.  
  60. function pulse()
  61.   redstone.setAnalogOutput(rside,15)
  62.   sleep(0.3)
  63.   redstone.setAnalogOutput(rside,0)
  64. end
  65.  
  66. function getItems()
  67.   b = 1
  68.   items = {}
  69.   if chestSize ~= nil then
  70.     for a=1,chestSize do
  71.       list_items[a] = p.getStackInSlot(a)
  72.       if list_items[a] ~= nil and list_items[a].name == "teleporterMKI" then
  73.         items[b] = {}
  74.         items[b][1] = a
  75.         items[b][2] = list_items[a].display_name
  76.         b = b + 1
  77.       end
  78.     end
  79.   end
  80.   table.sort(items, function(a, b) return a[2] < b[2] end)
  81.   table.insert(items,1,{1,">>> retour au Spawn <<<","spawn"})
  82.   if display == nil then display = items end
  83. end
  84.  
  85. function list_display()
  86.   for c=1,#display do
  87.     if c == selected then
  88.       f.drawLine(list,1,c,w,"gray")
  89.       f.centerTextRight(list,c,"ouvrir","white")
  90.     else
  91.       list.setBackgroundColor(colors.black)
  92.     end
  93.     if display[c][3] == nil then
  94.       f.cprint(list,1,c,display[c][2],"blue")
  95.     elseif display[c][3] == "spawn" then
  96.       f.centerText(list,c,display[c][2],"cyan")
  97.     end
  98.   end
  99. end
  100.  
  101. function scroll(direction)
  102.   local x,y = list.getPosition()
  103.   if direction == "up" and y <=1 then
  104.     list.reposition(1,y+2)
  105.   elseif direction == "down" and y+#items > h-1 then
  106.     list.reposition(1,y-2)
  107.   end
  108. end
  109.  
  110. function search()
  111.   bg2.setCursorPos(2,9)
  112.   word = getInput.read(bg2,30)
  113.   display = {}
  114.   for i,v in pairs(items) do
  115.     if string.find(v[2],word) ~= nil then
  116.       table.insert(display,v)
  117.     end
  118.   end
  119. end
  120.  
  121. pulse()
  122.  
  123. while true do
  124.   reset()
  125.   getItems()
  126.   list_display()
  127.   down_bar.redraw()
  128.   up_bar.redraw()
  129.   x,y = list.getPosition()
  130.   j,k = scroll_bar.getPosition()
  131.   local event = {os.pullEvent()}
  132.   if event[1] == "monitor_touch" then
  133.     if event[4]-y+1 == selected and display[selected] ~= nil and event[3] < j-1 then
  134.       up_bar.clear()
  135.       f.centerText(up_bar,1,"ouverture...","black")
  136.       pulse()
  137.       sleep(0.5)
  138.       p.pushItem(side,display[selected][1])
  139.       last_dest = display[selected][2]
  140.       getItems()
  141.       close = os.startTimer(3)
  142.     elseif event[4] > 1 and event[3] < j-1 then
  143.       selected = event[4]-y+1
  144.     elseif event[4] == 2 and event[3] == j then
  145.       scroll("up")
  146.     elseif event[4] == h-1 and event[3] == j then
  147.       scroll("down")
  148.     elseif f.itrv(event[3],pbg2[1],pbg2[1]+sbg2[1]) and f.itrv(event[4],pbg2[2],pbg2[2]+sbg2[2]) then
  149.       xc = event[3]-pbg2[1] yc = event[4]-pbg2[2]
  150.       if f.itrv(xc,pb1[1],pb1[1]+sb1[1]) and f.itrv(yc,pb1[2],pb1[2]+sb1[2]-1) then
  151.         pulse()
  152.       elseif f.itrv(xc,2,sbg2[1]) and f.itrv(yc,6,9) then
  153.         search()
  154.       end
  155.     end
  156.   end
  157.   if event[1] == "timer" and event[2] == close then
  158.     pulse()
  159.   end
  160. end
Advertisement
Add Comment
Please, Sign In to add comment