Skortioth

[OC]: Draconic Ev. - Portal Changing Script

Jul 11th, 2020 (edited)
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.42 KB | None | 0 0
  1. local component = require("component")
  2. local term = require("term")
  3. local event = require("event")
  4. local sides = {0,1,2,3,4,5}
  5. local transposer = component.transposer
  6. local gpu = component.gpu
  7. local w, h = gpu.getResolution()
  8.  
  9. local dislocatorReceptacleName = "draconicevolution:dislocator_receptacle"
  10. local dislocatorReceptacleSide = -1
  11.  
  12. local portals = {}
  13.  
  14. local infoMessage = ""
  15.  
  16. function Message(text)
  17.     gpu.fill(5,49,125,1," ")
  18.     local newText = string.sub("[Info]: "..text, 1, 120)
  19.     gpu.set(5,49,newText)
  20.     infoMessage = text
  21. end
  22.  
  23. function FindDislocatorReceptacle()
  24.     for k,v in pairs(sides)do
  25.         if(transposer.getInventoryName(v)==dislocatorReceptacleName)then
  26.             dislocatorReceptacleSide = v
  27.             return true
  28.         end
  29.     end
  30.     return false
  31. end
  32.  
  33. function CheckDislocatorReceptacle()
  34.     local found = false
  35.     if(dislocatorReceptacleSide >= 0)then
  36.         if(transposer.getInventoryName(dislocatorReceptacleSide)==dislocatorReceptacleName)then
  37.             found = true
  38.         end
  39.     end
  40.     if not(found)then
  41.         if(FindDislocatorReceptacle())then
  42.             found = true
  43.         else
  44.             Message("Can't find Dislocator Receptacle!")
  45.         end
  46.     end
  47.     return found
  48. end
  49.  
  50. function ScanInventorysForAvailablePortals()
  51.     portals = {}
  52.     for k,v in pairs(sides)do
  53.         local stacks = transposer.getAllStacks(v)
  54.         if(stacks~=nil)then
  55.             for a,b in pairs(stacks.getAll())do
  56.                 if(b.name=="draconicevolution:dislocator")then
  57.                     if(transposer.getInventoryName(v)==dislocatorReceptacleName)then
  58.                         table.insert(portals, {name=b.label, active=true, slot = a, side=v})
  59.                     else
  60.                         table.insert(portals, {name=b.label, active=false, slot = a, side=v})
  61.                     end
  62.                 end
  63.             end
  64.         end
  65.     end
  66. end
  67.  
  68. function GetFreeInventorySlot()
  69. local side, slot
  70.     for k,v in pairs(sides)do
  71.         local stacks = transposer.getAllStacks(v)
  72.         if(stacks~=nil)then
  73.             for a,b in pairs(stacks.getAll())do
  74.                 if(b.name=="minecraft:air")then
  75.                     return v, a
  76.                 end
  77.             end
  78.         end
  79.     end
  80.  
  81. return side, slot
  82. end
  83.  
  84. function GetPortal(side,slot)
  85.     for k,v in pairs(portals)do
  86.         if(v.side==side)and(v.slot==slot)then
  87.             return v
  88.         end
  89.     end
  90.     return nil
  91. end
  92.  
  93. function RemoveActivePortal()
  94.     if(CheckDislocatorReceptacle())then
  95.         local side, slot = GetFreeInventorySlot()
  96.         if(side~=nil)and(slot~=nil)then
  97.             local portal = GetPortal(dislocatorReceptacleSide, 1)
  98.             if(portal~=nil)then
  99.                 if(transposer.transferItem(dislocatorReceptacleSide, side, 1, 1, slot))then
  100.                     portal.side = side
  101.                     portal.slot = slot
  102.                     portal.active = false
  103.                     return true
  104.                 else
  105.                     Message("Removing portal didn't work!")
  106.                 end
  107.             else
  108.                 Message("Can't find portal entry!")
  109.             end
  110.         else
  111.             Message("Inventory is full!")
  112.         end
  113.     end
  114.     return false
  115. end
  116.  
  117. function IsPortalActive()
  118.     if(CheckDislocatorReceptacle())then
  119.         local item = transposer.getStackInSlot(dislocatorReceptacleSide, 1)
  120.         if(item ~= nil)then
  121.             return true
  122.         else
  123.             return false
  124.         end
  125.     end
  126.     return nil
  127. end
  128.  
  129. function ItemStillExist(portal)
  130. local item = transposer.getStackInSlot(portal.side, portal.slot)
  131.  
  132.     if(item.label == portal.name)then
  133.         return true
  134.     end
  135.     return false
  136. end
  137.  
  138. function InsertPortal(portal)
  139.     if(CheckDislocatorReceptacle())then
  140.         local item = ItemStillExist(portal)
  141.         if(item)then
  142.             if not(transposer.transferItem(portal.side, dislocatorReceptacleSide, 1, portal.slot, 1))then
  143.                 Message("Inserting a portal didn't work!")
  144.             else
  145.                 portal.active = true
  146.                 portal.side = dislocatorReceptacleSide
  147.                 portal.slot = 1
  148.             end
  149.         else
  150.             Message("Can't find item in inventory!")
  151.         end
  152.     end
  153. end
  154.  
  155.  
  156. function CreateOrUpdateGUI()
  157. local x = 15
  158. local y = 5
  159.  
  160. local xAddr = (w-40)/4
  161.  
  162.  
  163.     gpu.fill(1, 1, w, h, " ")
  164.     gpu.setForeground(0xFFFFFF)
  165.     gpu.setBackground(0x000000)
  166.     if(#portals>0)then
  167.         for k,v in pairs(portals)do
  168.             if(v.active)then
  169.                 gpu.setBackground(0x32CD32)
  170.             else
  171.                 gpu.setBackground(0xFF0000)
  172.             end
  173.             local portalName = string.sub(v.name, 1, 16)
  174.             gpu.fill(x-1,y-1,string.len(portalName)+2,3," ")
  175.             gpu.set(x,y,portalName)
  176.             gpu.setBackground(0x000000)
  177.             x = x+xAddr
  178.             if(x >= w-40/4)then
  179.                 x = 15
  180.                 y = y+6
  181.             end
  182.         end
  183.         gpu.setBackground(0x32CD32)
  184.         gpu.fill(w-8,h-2,8,3," ")
  185.         gpu.set(w-7,h-1,"Reload")
  186.         gpu.setBackground(0x000000)
  187.     end
  188.     if(string.len(infoMessage)>0)then
  189.         Message(infoMessage)
  190.     end
  191. end
  192.  
  193. function TouchEvent()
  194. local x = 15
  195. local y = 5
  196.  
  197. local xAddr = (w-40)/4
  198.  
  199.     local _, _, xInput, yInput = event.pull("touch")
  200.     if(#portals>0)then
  201.         for k,v in pairs(portals)do
  202.             local portalName = string.sub(v.name, 1, 16)
  203.             if(xInput >= x-1)and(xInput <= x+string.len(portalName)+1)and(yInput >= y-1)and(yInput <= y+1)then
  204.                 if not(v.active)then
  205.                     if(IsPortalActive())then
  206.                         if(RemoveActivePortal())then
  207.                             InsertPortal(v)
  208.                         end
  209.                     else
  210.                         InsertPortal(v)
  211.                     end
  212.                     os.sleep(0.25)
  213.                 end
  214.             end
  215.  
  216.             x = x+xAddr
  217.             if(x >= w-40/4)then
  218.                 x = 15
  219.                 y = y+6
  220.             end
  221.         end
  222.  
  223.         if(xInput>= w-8)and(xInput<=w)and(yInput>=h-3)and(yInput<=h)then
  224.             ScanInventorysForAvailablePortals()
  225.         end
  226.     else
  227.         ScanInventorysForAvailablePortals()
  228.     end
  229. end
  230.  
  231. CheckDislocatorReceptacle()
  232. ScanInventorysForAvailablePortals()
  233.  
  234. while true do
  235.     CreateOrUpdateGUI()
  236.     TouchEvent()
  237. end
Add Comment
Please, Sign In to add comment