Advertisement
Guest User

Prison.lua

a guest
Aug 2nd, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.89 KB | None | 0 0
  1. prison_file_path='/usr/mobController/tables/prisons.tbl'
  2. chest_str = 'copper'
  3.  
  4. function readChest(periph)
  5.     chest = {}
  6.    
  7.     for i=1, periph.size() do
  8.         if periph.getItemMeta(i)~=nil then
  9.             table.insert(chest, {slot=i, meta=periph.getItemMeta(i)})
  10.         end
  11.     end
  12. --    print('readChest:', textutils.serialise(chest))
  13.     return true, chest
  14. end
  15.  
  16. function getPrisons()
  17.     if not fs.exists(prison_file_path) then
  18.         return false, 'prison_file_path is incorrect:\n'..prison_file_path
  19.     end
  20.     file = fs.open(prison_file_path, 'r')
  21.     if not file then
  22.         return false, 'Prisons table not found'
  23.     end
  24.     data = file.readAll()
  25.     file.close()
  26.     if not data or #data==0 then
  27.         return false, 'Prisons data not found'
  28.     end
  29.     prisons=textutils.unserialise(data)
  30.     return true, prisons
  31. end
  32.  
  33. function writePrisons(prisons)
  34.     file = fs.open(prison_file_path, 'w')
  35.     file.write(textutils.serialize(prisons))
  36.     file.flush()
  37.     file.close()
  38.     print('file closed and flushed')
  39. end
  40.  
  41. function findByMeta(meta)
  42.     for k,v in pairs(getPrisons()) do
  43.         if v.meta = meta then
  44.             return true, v
  45.         end
  46.     end
  47. end
  48.  
  49. function addPrison(prison, slot)
  50.     if not periph then
  51.         return false, 'peripheral not found'
  52.     end
  53.     if not slot then
  54.         return false, 'slot not provided'
  55.     end
  56.     if not periph.getItem(slot) then
  57.         return false, 'nothing in slot:'..tostring(slot)
  58.     end
  59.     scs, prisons = getPrisons()
  60.     table.insert(prisons, {slot=slot, meta=prison})
  61.     return scs, prisons
  62. end
  63.  
  64. function deletePrison(prisons, chest, del_item)
  65.     scs, prison = chest.pushItems('up', del_item.slot, 1)
  66.     if not scs then
  67.         return false, 'unable to push '..prison.name
  68.     end
  69.     prisons.pop(del_item)
  70.     return scs, prison
  71. end
  72.  
  73. function pullPrison(periph)
  74.     item_cnt = periph.pullItem('down', 7, 1, periph.size())
  75.     if item_cnt = 0 then
  76.         return false, 'Unable to pull item'
  77.     end
  78.     item = periph.getItemMeta(chest.size())
  79.     if not item then
  80.         return false, 'Unable to read item in ',chest.size())
  81.     end
  82.     prison = findByMeta(item)
  83.     if not prison then
  84.         return false, 'Not a known prison.'
  85.     end
  86.     periph.pushItem('down',chest.size(), 1, 7)
  87.     periph.pullItem('down',7, 1, prison.slot)
  88.     return true, slot, prison    
  89. end
  90.  
  91. function putPrison()
  92. end
  93.  
  94. function resetPrisons(chest)
  95.     scs, prisons = readChest(chest)
  96.     if scs then
  97.         writePrisons(prisons)
  98.     end
  99.     return scs, prisons
  100. end
  101.  
  102.  
  103. -------------------------------------
  104. chest = nil
  105.  
  106. for k,v in pairs(peripheral.getNames()) do
  107.     if string.find(v, chest_str) then
  108.     chest = peripheral.wrap(v)
  109.     break
  110.     end
  111. end
  112.  
  113. scs, prisons = getPrisons()
  114. src = 'getPrisons'
  115. if not scs then
  116.     scs, prisons = readChest(chest)
  117.     src = 'readChest'
  118. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement