MarkFergus

spawner/GUI (18/06/2020)

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