Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- prison_file_path='/usr/mobController/tables/prisons.tbl'
- chest_str = 'copper'
- function readChest(periph)
- chest = {}
- for i=1, periph.size() do
- if periph.getItemMeta(i)~=nil then
- table.insert(chest, {slot=i, meta=periph.getItemMeta(i)})
- end
- end
- -- print('readChest:', textutils.serialise(chest))
- return true, chest
- end
- function getPrisons()
- if not fs.exists(prison_file_path) then
- return false, 'prison_file_path is incorrect:\n'..prison_file_path
- end
- file = fs.open(prison_file_path, 'r')
- if not file then
- return false, 'Prisons table not found'
- end
- data = file.readAll()
- file.close()
- if not data or #data==0 then
- return false, 'Prisons data not found'
- end
- prisons=textutils.unserialise(data)
- return true, prisons
- end
- function writePrisons(prisons)
- file = fs.open(prison_file_path, 'w')
- file.write(textutils.serialize(prisons))
- file.flush()
- file.close()
- print('file closed and flushed')
- end
- function findByMeta(meta)
- for k,v in pairs(getPrisons()) do
- if v.meta = meta then
- return true, v
- end
- end
- end
- function addPrison(prison, slot)
- if not periph then
- return false, 'peripheral not found'
- end
- if not slot then
- return false, 'slot not provided'
- end
- if not periph.getItem(slot) then
- return false, 'nothing in slot:'..tostring(slot)
- end
- scs, prisons = getPrisons()
- table.insert(prisons, {slot=slot, meta=prison})
- return scs, prisons
- end
- function deletePrison(prisons, chest, del_item)
- scs, prison = chest.pushItems('up', del_item.slot, 1)
- if not scs then
- return false, 'unable to push '..prison.name
- end
- prisons.pop(del_item)
- return scs, prison
- end
- function pullPrison(periph)
- item_cnt = periph.pullItem('down', 7, 1, periph.size())
- if item_cnt = 0 then
- return false, 'Unable to pull item'
- end
- item = periph.getItemMeta(chest.size())
- if not item then
- return false, 'Unable to read item in ',chest.size())
- end
- prison = findByMeta(item)
- if not prison then
- return false, 'Not a known prison.'
- end
- periph.pushItem('down',chest.size(), 1, 7)
- periph.pullItem('down',7, 1, prison.slot)
- return true, slot, prison
- end
- function putPrison()
- end
- function resetPrisons(chest)
- scs, prisons = readChest(chest)
- if scs then
- writePrisons(prisons)
- end
- return scs, prisons
- end
- -------------------------------------
- chest = nil
- for k,v in pairs(peripheral.getNames()) do
- if string.find(v, chest_str) then
- chest = peripheral.wrap(v)
- break
- end
- end
- scs, prisons = getPrisons()
- src = 'getPrisons'
- if not scs then
- scs, prisons = readChest(chest)
- src = 'readChest'
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement