Advertisement
osmarks

SpatialSwap

Dec 5th, 2018
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.92 KB | None | 0 0
  1. local storage = "actuallyadditions:giantchest_1"
  2. local spatial = "back"
  3. local state_file = "state.tbl"
  4. local storage_to_spatial = "down"
  5. local spatial_to_storage = "up"
  6.  
  7. local function save()
  8.     local f = fs.open(state_file, "w")
  9.     f.write(textutils.serialise(state))
  10.     f.close()
  11. end
  12.  
  13. local function load()
  14.     if not fs.exists(state_file) then return end
  15.     local f = fs.open(state_file, "r")
  16.     local d = textutils.unserialise(f.readAll())
  17.     f.close()
  18.     return d
  19. end
  20.  
  21. local state = {} or load()
  22.  
  23. local cells = {
  24.     ["7ce8"] = "empty",
  25.     ["f361"] = "test"
  26. }
  27.  
  28. local function find_by_hash(inventory, hash)
  29.     for slot, item in pairs(inventory) do
  30.         if item.nbtHash:match(hash) then return slot end
  31.     end
  32. end
  33.  
  34. local function find_by_name(inventory, name)
  35.     for hash, cname in pairs(cells) do
  36.         if cname == name then
  37.             return find_by_hash(inventory, hash)
  38.         end
  39.     end
  40. end
  41.  
  42. local function load_cell(slot)
  43.     peripheral.call(storage, "pushItems", storage_to_spatial, slot, 1, 1)
  44. end
  45.  
  46. local function unload_cell()
  47.     peripheral.call(spatial, "pushItems", spatial_to_storage, 2, 1)
  48. end
  49.  
  50. local function reload_cell()
  51.     peripheral.call(spatial, "pushItems", "self", 2, 1, 1)
  52. end
  53.  
  54. local function pulse_spatial()
  55.     redstone.setOutput(spatial, true)
  56.     sleep(0.2)
  57.     redstone.setOutput(spatial, false)
  58.     sleep(0.2)
  59. end
  60.  
  61. while true do
  62.     write "Cell: "
  63.     local cell = read()
  64.     local inv = peripheral.call(storage, "list")
  65.     local slot = find_by_name(inv, cell)
  66.     if not slot then printError(cell .. " not found.")
  67.     elseif state.currently_open == cell then
  68.         print(cell, "already loaded")
  69.     else
  70.         if state.currently_open then
  71.             print("Unloading", state.currently_open)
  72.             load_cell(find_by_name(inv, state.currently_open))
  73.             pulse_spatial()
  74.             unload_cell()
  75.             state.currently_open = nil
  76.             save()
  77.         end
  78.         print("Loading", cell)
  79.         load_cell(slot)
  80.         pulse_spatial()
  81.         unload_cell()
  82.         state.currently_open = cell
  83.         save()
  84.     end
  85. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement