hilburn

Mystcraft Page Trader

Jan 9th, 2015
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.16 KB | None | 0 0
  1. peripherals = peripheral.getNames()
  2.  
  3. for i,j in pairs(peripherals) do
  4.         if peripheral.getType(j) == "tradingpost" then trade = peripheral.wrap(j) end
  5.         if peripheral.getType(j) == "container_chest" or peripheral.getType(j) == "iron" then chest = peripheral.wrap(j) end
  6. end
  7.  
  8. emerald_buffer = {["direction"]="north",["slot"]=0}
  9. pages = {}
  10.  
  11. function savePages()
  12.         if fs.exists("pages.dat") then
  13.                 fs.delete("pages.dat")
  14.         end
  15.         file = fs.open("pages.dat", "w")
  16.         file.write(textutils.serialize(pages))
  17.         file.close()
  18. end
  19.  
  20. function loadPages()
  21.         if fs.exists("pages.dat") then
  22.                 file = fs.open("pages.dat", "r")
  23.                 pages = textutils.unserialize(file.readAll())
  24.                 file.close()
  25.         else
  26.                 if not shell.run("pastebin get","pqqGZ2nW","pages.dat") then
  27.                         error("Failed to initialise page list, please check http permissions")
  28.                 end
  29.         end
  30. end
  31.  
  32. function getTradeInfo(id)
  33.         local sold = trade.getItemSold(id,0)
  34.         if sold == nil then
  35.                 sold = {["name"]="nope",["qty"]=0,["rawName"]="nada"}
  36.         end
  37.         return sold.name, sold.qty, sold.rawName, id
  38. end
  39.  
  40. function topUpEmeralds()
  41.         chest.pullItem(emerald_buffer["direction"], emerald_buffer["slot"])
  42. end
  43.  
  44. loadPages()
  45. topUpEmeralds()
  46. while true do
  47.         ids = trade.getVillagerIds()
  48.                 chest.pullItem(emerald_buffer["direction"], emerald_buffer["slot"])
  49.         for index, idnum in pairs(ids) do
  50.                 sold, soldQty, rawName, id = getTradeInfo(idnum)
  51.                 if rawName == "item.myst.page" and (sold == "Link Panel" or pages[sold] ~= true) then
  52.                         if trade.performTrade(id, 0) then
  53.                                 print("New "..sold)
  54.                                 pages[sold] = true
  55.                                 savePages()
  56.                                 topUpEmeralds()
  57.                         else
  58.                                 print("Failed to buy "..sold)
  59.                         end
  60.                 end
  61.         end
  62.         sleep(3)
  63. end
Advertisement
Add Comment
Please, Sign In to add comment