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)
- scs, tbl = getPrisons()
- for k,v in pairs(tbl) do
- if v.meta.nbtHash == meta.nbtHash then
- return true, v
- end
- end
- return false
- 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)
- if not periph then
- return false, 'no peripheral sent'
- end
- trade_slot = periph.size()
- scs, check_item = periph.getItemMeta(trade_slot)
- if check_item then
- return false, check_item.name..' is present in the last slot of the chest'
- end
- move_cnt = periph.pullItems('down', 7, 1, trade_slot)
- if move_cnt == 0 then
- return false, 'Unable to pull item'
- end
- item = periph.getItemMeta(trade_slot)
- if not item then
- return false, 'Unable to read item in ', trade_slot
- end
- scs, prison = findByMeta(item)
- if not prison then
- return false, 'Not a known prison.'
- end
- periph.pushItems('down',trade_slot, 1, 7)
- periph.pullItems('down',7, 1, prison.slot)
- return true, slot, prison
- end
- function putPrison(periph, prison)
- move_cnt=periph.pushItems('down', prison.slot, 1, 7)
- if move_cnt==0 then
- return false, move_cnt
- else
- return true, move_cnt, prison
- end
- end
- function resetPrisons(chest)
- pullPrison(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
Add Comment
Please, Sign In to add comment