MarkFergus

spawner/GUI (15/07/2020)

Jul 15th, 2020 (edited)
779
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local version = "Dev" --(26/06 2020)
  2. os.loadAPI("/spawner/f") -- a monitor API made by myself, it makes my life easier
  3. os.loadAPI("/spawner/lib/API") -- search_bar
  4. os.loadAPI("/spawner/lib/APIjson")
  5. local list_items,items = {},{}
  6. local a,b,c,chestSize = 1,1,1,100
  7. local Pcheck = false
  8.  
  9. function getPeriphs()
  10.   p = peripheral.find("diamond") -- can change for "chest" instead of "draconic_chest"
  11.   if p == nil then isChest = false else isChest = true end
  12.   m = peripheral.find("monitor") -- running on a monitor
  13.   if m == nil then m = term.current() isMonitor = false else m.setTextScale(0.5) isMonitor = true end
  14.   fxd = peripheral.find("thermaldynamics_fluxduct")
  15.   w,h = m.getSize()
  16. end
  17.  
  18. function addWin(m,x1,y1,x2,y2,name)
  19.   local b = window.create(m,x1,y1,x2,y2)
  20.   b.pos = {b.getPosition()}
  21.   b.size = {b.getSize()}
  22.   b.name = name
  23.   function b:getName() return b.name end
  24.   function b:isClicked(x,y) return f.itrv(x,b.pos[1],b.pos[1]+b.size[1]) and f.itrv(y,b.pos[2],b.pos[2]+b.size[2]-1) end
  25.   return b
  26. end
  27.  
  28. function setWindows()
  29.   bg = addWin(m,1,1,w,h) bg.reset = {bg_color="black"}
  30.   list = addWin(m,1,2,w*3/5-1,chestSize) list.reset = {bg_color="black"}
  31.   up_bar = addWin(m,1,1,w,1) up_bar.reset = {bg_color="lightGray"}
  32.   down_bar = addWin(m,1,h,w,h) down_bar.reset = {bg_color="lightGray",printText = function()
  33.     f.centerTextRight(down_bar,1,"v"..version,"black","lightGray") end}
  34.   scroll_bar = addWin(m,w*(3/5),2,1,h-1) scroll_bar.reset = {bg_color="lightGray",printText = function()
  35.     f.cprint(scroll_bar,1,1,"^","black","gray")
  36.     f.cprint(scroll_bar,1,h-2,"v","black","gray") end}
  37.   bg2 = addWin(m,w*0.6+1,2,w*0.4+1,h-1) bg2.reset = {bg_color="gray",printText = function()
  38.     f.cprint(bg2,2,1,"Mob: ","white","gray")
  39.     f.cprint(bg2,8,10,"Status: ","white","gray") end}
  40.   b1 = addWin(m,2,13,bg2.size[1]-2,3,"button1") b1.reset = {bg_color="red",printText = function()
  41.     f.centerText(b1,2,"i/o","black","red") end}
  42.   b2 = addWin(m,2,9,5,3,"button2") b2.reset = {bg_color="red",printText = function()
  43.     f.centerText(b2,"Rafraichir","black","purple") end}
  44.   b = {}
  45.   b[1] = addWin(m,2,3,5,3) b[2] = addWin(m,10,3,5,3) b[3] = addWin(m,18,3,5,3)
  46.   for i=1,3 do
  47.     b[i]["notSelected"]={bg_color="black",printText=function() f.cprint(b[i],3,2,i,"lightGray","black") end}
  48.     b[i]["selected"] = {bg_color="magenta",printText=function() f.cprint(b[i],3,2,i,"black","magenta") end}
  49.   end
  50. end
  51.  
  52. function apply(obj,name)
  53.   if f.vn(obj[name]) then
  54.     local metatable = {__Index = obj[name]}
  55.     setmetatable(index, metatable)
  56.     obj.setBackgroundColor(index.bg_color)
  57.     obj.clear()
  58.   else error("object not found") end
  59. end
  60.  
  61. local default = {
  62.   spawner1={side="east",curr_item=""},
  63.   spawner2={side="north",curr_item=""},
  64.   spawner3={side="west",curr_item=""},
  65.   rside="right",
  66.   enable = true,
  67.   player_name="MarkFergus",
  68.   server="infinity.mineaurion.com",
  69.   selected = 0, rubrique = 1
  70. }
  71.  
  72. if fs.exists("/spawner/config") == false then f1 = fs.open("/spawner/config","w") f1.write(textutils.serialise(default)) f1.close() end
  73. f1 = fs.open("/spawner/config","r") index = textutils.unserialize(f1.readAll()) f1.close()
  74. if fs.exists("/spawner/nbt") == false then error("missing 'nbt file'") end
  75. f2 = fs.open("/spawner/nbt","r") nbt_index = textutils.unserialize(f2.readAll()) f2.close()
  76. rubrique = index.rubrique selected = index.selected
  77.  
  78. function reset()
  79.   apply(bg,"reset") apply(bg2,"reset") apply(list,"reset")
  80.   apply(up_bar,"reset") apply(down_bar,"reset") apply(scroll_bar,"reset")
  81.   apply(b1,"reset")  apply(b2,"reset")
  82.   --for i=1,3 do
  83.   --  if rubrique == i then b[i].apply("selected") f.cprint(bb[i],3,2,i,"black","magenta")
  84.   --  else b[i].apply("notSelected") end
  85.   --end
  86.   if f.vn(fxd) then f.cprint(down_bar,1,1,fxd.getMaxEnergyStored()-fxd.getEnergyStored().." RF","red","lightGray") end
  87.   f.cprint(bg2,7,1,index["spawner"..rubrique].curr_item,"purple","gray")
  88.   if index.enable then f.cprint(bg2,16,10,"actif  ","lime","gray") else
  89.   f.cprint(bg2,16,10,"inactif","purple","gray") end
  90.   f.cprint(up_bar,1,1,coordinates,"lime","lightGray")
  91. end
  92.  
  93. function getItems(check1)
  94.   if check1 == true then
  95.   b = 1
  96.   items = {}
  97.   if chestSize ~= nil then
  98.     for a=1,chestSize do
  99.       item = p.getStackInSlot(a)
  100.       if item ~= nil and item.name == "mobSoul" then
  101.         items[b] = {}
  102.         items[b][1] = a
  103.         for ab=1,#nbt_index do
  104.           if item.nbt_hash == nbt_index[ab].nbt_hash then items[b][2] = nbt_index[ab].name end
  105.         end
  106.         items[b]["qty"] = item.qty
  107.         b = b + 1
  108.       end
  109.     end
  110.   end
  111.   if display == nil then display = items end
  112.   end
  113. end
  114.  
  115. function list_display(check1)
  116.   if check1 == true then
  117.   for c=1,#display do
  118.     if c == selected then
  119.       f.drawLine(list,1,c,w,"lightBlue")
  120.       f.centerTextRight(list,c,tostring(items[c].qty),"black")
  121.     else
  122.       list.setBackgroundColor(colors.black)
  123.     end
  124.     if display[c][3] == nil then
  125.       f.centerText(list,c,display[c][2],"purple")
  126.     end
  127.   end
  128.   else f.centerText(list,1,"No chest found","red","black") end
  129. end
  130.  
  131. function scroll(direction)
  132.   local x,y = list.getPosition()
  133.   if direction == "up" and y <=1 then
  134.     list.reposition(1,y+2)
  135.   elseif direction == "down" and y+#items > h-1 then
  136.     list.reposition(1,y-2)
  137.   end
  138. end
  139.  
  140. function checkPlist()
  141.   str = APIjson.decode(http.get("http://api.mineaurion.com/v1/serveurs/"..index.server).readAll())
  142.   local Pcheck = false
  143.   for i,v in pairs(str.joueurs) do
  144.     if v == index.player_name then Pcheck = true end
  145.   end
  146.   if (Pcheck==true and index.enable==true) then rs.setAnalogOutput(index.rside,15) else rs.setAnalogOutput(index.rside,0) end
  147. end
  148.  
  149. function updateIndex()
  150.   shell.run("rm","/spawner/config")
  151.   index.rubrique = rubrique index.selected = selected
  152.   f3 = fs.open("/spawner/config","w") f3.write(textutils.serialise(index)) f3.close()
  153. end
  154.  
  155.  getPeriphs() setWindows()
  156.  
  157. while true do
  158.   reset() getItems(isChest) list_display(isChest)
  159.   down_bar.redraw() up_bar.redraw()
  160.   checkPlist() updateIndex()
  161.   refresh = os.startTimer(20)
  162.   event = {os.pullEvent()}
  163.   if event[1] == "peripheral_detach" or event[1] == "peripheral" then getPeriphs() setWindows() end
  164.   if event[1] == "monitor_touch" or event[1] == "mouse_click" then
  165.     x = event[3] y = event[4]
  166.     coordinates = tostring(x..";"..y..","..selected..","..j)
  167.     if y-list.pos[1]+1 == selected and display[selected] ~= nil and x < scroll_bar.pos[1]-1 then
  168.       up_bar.clear()
  169.       f.centerText(up_bar,1,"reconfiguration...","black")
  170.       sleep(0.5)
  171.       p.pushItem(index["spawner"..rubrique].side,display[selected][1],1)
  172.       index["spawner"..rubrique].curr_item = display[selected][2]
  173.       getItems(isChest)
  174.     elseif y > 1 and x < scroll_bar.pos[1]-1 then
  175.       selected = y-yl+1
  176.     elseif y == 2 and x == scroll_bar.pos[1] then
  177.       scroll("up")
  178.     elseif list.pos[2] == h-1 and event[3] == scroll_bar.pos[1] then
  179.       scroll("down")
  180.     elseif bg2.isClicked(x,y) then
  181.       xc = x-pbg2[1]+1 yc = y-pbg2[2]+1
  182.       for i=1,3 do if b[i].isClicked(xc,yc) then rubrique = i end end
  183.       if b1.isClicked(xc,yc) then
  184.         if index.enable == true then index.enable = not index.enable end
  185.       end
  186.       if b2.isClicked(xc,yc) then
  187.         display = nil
  188.         sleep(0.4)
  189.       end
  190.     end
  191.   end
  192. end
Add Comment
Please, Sign In to add comment