DubstepDentist icon

Computer Craft - Books

DubstepDentist | PRO | 03/30/15 05:54:04 AM UTC | 0 ⭐ | 423 👁️ | Never ⏰ | []
Lua |

1.42 KB

|

None

|

0 👍

/

0 👎

local data = {}
rednet.open("left")
p = peripheral.wrap("bottom")
 
function clearInv()
   for i = 1,16 do
      turtle.select(i)
      turtle.dropDown()
   end
end
 
function checkSlots(id)
   clearInv()
   data = {}
   local slot
   local name
   local slots = p.getAllStacks()
   for i,j in pairs(slots) do
      slot = i
      name = j["myst_book"]["destination"]
--      print(name)
      data[slot]=name
   end
   rednet.send(id,"done")
end
 
function removeSlot(slot)
   p.pushItem("up", slot, 1)
   rs.setOutput("left", true)
   sleep(2)
   rs.setOutput("left", false)
end
 
function book(slot,id)
   p.pushItem("up", slot, 1)
   turtle.select(1)
   turtle.drop()
   sleep(5)
   getBook()
   turtle.select(1)
   turtle.dropDown()
   rednet.send(tonumber(id), "done")
end
 
function getBook()
   turtle.suck()
end
 
function getNames(id)
   local nameTbl = textutils.serialize(data)
   rednet.send(tonumber(id), nameTbl)
end
 
clearInv()
 
while true do
   local id, msg, dis = rednet.receive()
   local newmsg = string.match(msg, "%a+")
--   print(msg)
   if newmsg == "checkSlots" then
     checkSlots(id) 
   elseif newmsg == "getNames" then
     getNames(id)
   elseif newmsg == "remove" then
     removeSlot(tonumber(string.match(msg, "%d+")))
     rednet.send(id,"done")
   elseif newmsg == "books" then
     slot = string.match(msg, "%d+")
     book(tonumber(slot), id)
   end
end

Comments