hyndgrinder

Prison

Aug 2nd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     scs, tbl = getPrisons()
  43.     for k,v in pairs(tbl) do
  44.         if v.meta.nbtHash == meta.nbtHash then
  45.             return true, v
  46.         end
  47.     end
  48.     return false
  49. end
  50.  
  51. function addPrison(prison, slot)
  52.     if not periph then
  53.         return false, 'peripheral not found'
  54.     end
  55.     if not slot then
  56.         return false, 'slot not provided'
  57.     end
  58.     if not periph.getItem(slot) then
  59.         return false, 'nothing in slot:'..tostring(slot)
  60.     end
  61.     scs, prisons = getPrisons()
  62.     table.insert(prisons, {slot=slot, meta=prison})
  63.     return scs, prisons
  64. end
  65.  
  66. function deletePrison(prisons, chest, del_item)
  67.     scs, prison = chest.pushItems('up', del_item.slot, 1)
  68.     if not scs then
  69.         return false, 'unable to push '..prison.name
  70.     end
  71.     prisons.pop(del_item)
  72.     return scs, prison
  73. end
  74.  
  75. function pullPrison(periph)
  76.     if not periph then
  77.         return false, 'no peripheral sent'
  78.     end
  79.     trade_slot = periph.size()
  80.     scs, check_item = periph.getItemMeta(trade_slot)
  81.     if check_item then
  82.         return false, check_item.name..' is present in the last slot of the chest'
  83.     end
  84.    
  85.     move_cnt = periph.pullItems('down', 7, 1, trade_slot)
  86.     if move_cnt == 0 then
  87.         return false, 'Unable to pull item'
  88.     end
  89.     item = periph.getItemMeta(trade_slot)
  90.     if not item then
  91.         return false, 'Unable to read item in ', trade_slot
  92.     end
  93.     scs, prison = findByMeta(item)
  94.     if not prison then
  95.         return false, 'Not a known prison.'
  96.     end
  97.     periph.pushItems('down',trade_slot, 1, 7)
  98.     periph.pullItems('down',7, 1, prison.slot)
  99.     return true, slot, prison    
  100. end
  101.  
  102. function putPrison(periph, prison)
  103.     move_cnt=periph.pushItems('down', prison.slot, 1, 7)
  104.     if move_cnt==0 then
  105.         return false, move_cnt
  106.     else
  107.         return true, move_cnt, prison
  108.     end
  109. end
  110.  
  111. function resetPrisons(chest)
  112.         pullPrison(chest)
  113.     scs, prisons = readChest(chest)
  114.     if scs then
  115.         writePrisons(prisons)
  116.     end
  117.     return scs, prisons
  118. end
  119.  
  120.  
  121. -------------------------------------
  122. chest = nil
  123.  
  124. for k,v in pairs(peripheral.getNames()) do
  125.     if string.find(v, chest_str) then
  126.     chest = peripheral.wrap(v)
  127.     break
  128.     end
  129. end
  130.  
  131. scs, prisons = getPrisons()
  132. src = 'getPrisons'
  133. if not scs then
  134.     scs, prisons = readChest(chest)
  135.     src = 'readChest'
  136. end
Add Comment
Please, Sign In to add comment