SHARE
TWEET

PrintShop (ComputerCraft)

BombBloke Oct 31st, 2015 (edited) 247 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- +---------------------+-------------+---------------------+
  2. -- |                     |             |                     |
  3. -- |                     |  PrintShop  |                     |
  4. -- |                     |             |                     |
  5. -- +---------------------+-------------+---------------------+
  6.  
  7. local version = "Version 1.0.0"
  8.  
  9. -- Offers printed books to MineCraft players in exchange for XP levels.
  10. -- http://www.computercraft.info/forums2/index.php?/topic/25037-cprint-print-books-with-command-computers/
  11.  
  12. ---------------------------------------------
  13. ------------Variable Declarations------------
  14. ---------------------------------------------
  15.  
  16. if not fs.exists(shell.resolve("printshop.cfg")) then shell.run("pastebin get CWWeJk9g printshop.cfg") end
  17.  
  18. if not cprint then
  19.         if not (fs.exists("cprint") or fs.exists(shell.resolve("cprint"))) then
  20.                 shell.run("pastebin get QP8yFSPW cprint")
  21.                 os.loadAPI(shell.resolve("cprint"))
  22.         else os.loadAPI(fs.exists("cprint") and "cprint" or shell.resolve("cprint")) end
  23. end
  24.  
  25. local cost, cooldown, myEvent, xSize, ySize, printList
  26.  
  27. do
  28.         local arg = {...}
  29.         cost, cooldown = tonumber(arg[1]) or 5, tonumber(arg[2]) or 30
  30. end
  31.  
  32. local cursor = {{">>  ","  <<"},{"> > "," < <"},{" >> "," << "},{"> > "," < <"}}
  33.  
  34. ---------------------------------------------
  35. ------------       Functions     ------------
  36. ---------------------------------------------
  37.  
  38. local function clear(textCol, backCol)
  39.         if textCol then term.setTextColour(textCol) end
  40.         if backCol then term.setBackgroundColour(backCol) end
  41.         for i = 4, ySize - 3 do
  42.                 term.setCursorPos(1, i)
  43.                 term.clearLine()
  44.         end
  45. end
  46.  
  47. -- Returns whether a click was performed at a given location.
  48. -- If one parameter is passed, it checks to see if y is [1].
  49. -- If two parameters are passed, it checks to see if x is [1] and y is [2].
  50. -- If three parameters are passed, it checks to see if x is between [1]/[2] (non-inclusive) and y is [3].
  51. -- If four paramaters are passed, it checks to see if x is between [1]/[2] and y is between [3]/[4] (non-inclusive).
  52. local function clickedAt(...)
  53.         if myEvent[1] ~= "mouse_click" then return false end
  54.         if #arg == 1 then return (arg[1] == myEvent[4])
  55.         elseif #arg == 2 then return (myEvent[3] == arg[1] and myEvent[4] == arg[2])
  56.         elseif #arg == 3 then return (myEvent[3] > arg[1] and myEvent[3] < arg[2] and myEvent[4] == arg[3])
  57.         else return (myEvent[3] > arg[1] and myEvent[3] < arg[2] and myEvent[4] > arg[3] and myEvent[4] < arg[4]) end
  58. end
  59.  
  60. -- Returns whether one of a given set of keys was pressed.
  61. local function pressedKey(...)
  62.         if myEvent[1] ~= "key" then return false end
  63.         for i=1,#arg do if arg[i] == myEvent[2] then return true end end
  64.         return false
  65. end
  66.  
  67. local function writeAt(text, x, y, tCol, bCol)
  68.         if not (x and y) then
  69.                 local curX, curY = term.getCursorPos()
  70.                 x, y = x or curX, y or curY
  71.         end
  72.        
  73.         term.setCursorPos(x, y)
  74.         if tCol then term.setTextColour(tCol) end
  75.         if bCol then term.setBackgroundColour(bCol) end
  76.         term.write(text)
  77. end
  78.  
  79. local function downloadPaste(pasteID)
  80.         if type(pasteID) ~= "string" then error("package.downloadPaste: Expected: (string) paste ID", 2) end
  81.  
  82.         local webHandle = http.get("http://pastebin.com/raw.php?i=" .. textutils.urlEncode(pasteID))
  83.  
  84.         if webHandle then
  85.                 local incoming = webHandle.readAll()
  86.                 webHandle.close()
  87.                 return incoming
  88.         else error("Connection to pastebin failed. http API config in ComputerCraft.cfg is enabled, but may be set to block pastebin - or pastebin servers may be busy.") end
  89. end
  90.  
  91. local function fileBrowser()
  92.         local bump = math.floor((xSize - 49) / 2) + 1
  93.  
  94.         while true do
  95.                 local position, lastPosition, animationTimer, curCount, gapTimer, lastProgress = 1, 0, os.startTimer(0), 1
  96.                 if #shell.resolve(".") > 0 then printList[1] = ".." end
  97.  
  98.                 while true do
  99.                         myEvent = {os.pullEvent()}
  100.  
  101.                         -- Track animations (bouncing cursor + scrolling marquee).
  102.                         if myEvent[1] == "timer" and myEvent[2] == animationTimer then
  103.                                 curCount = curCount == 4 and 1 or (curCount + 1)
  104.                                 animationTimer = os.startTimer(0.5)
  105.                                 myEvent[1] = "cabbage"
  106.  
  107.                         -- Move down the list.
  108.                         elseif pressedKey(keys.down, keys.s) or (myEvent[1] == "mouse_scroll" and myEvent[2] == 1) then
  109.                                 position = position == #printList and 1 or (position + 1)
  110.  
  111.                         -- Move up the list.
  112.                         elseif pressedKey(keys.up, keys.w) or (myEvent[1] == "mouse_scroll" and myEvent[2] == -1) then
  113.                                 position = position == 1 and #printList or (position - 1)
  114.  
  115.                         -- Select something.
  116.                         elseif pressedKey(keys.enter, keys.space) or clickedAt(math.floor(ySize / 2) + 1) then
  117.                                 return printList[position]
  118.                        
  119.                         -- User clicked somewhere on the file list; move that entry to the currently-selected position.
  120.                         elseif clickedAt(0, xSize + 1, 3, ySize - 2) then
  121.                                 position = position + myEvent[4] - math.floor(ySize / 2) - 1
  122.                                 position = position > #printList and #printList or position
  123.                                 position = position < 1 and 1 or position
  124.                         end
  125.  
  126.                         -- Update other screen stuff.
  127.                         if myEvent[1] ~= "timer" then
  128.                                 -- File list.
  129.                                 term.setBackgroundColour(colours.black)
  130.                                 for y = position == lastPosition and (math.floor(ySize / 2) + 1) or 4, position == lastPosition and (math.floor(ySize / 2) + 1) or (ySize - 3) do
  131.                                         local thisLine = y + position - math.floor(ySize / 2) - 1
  132.  
  133.                                         if printList[thisLine] then
  134.                                                 local thisString = printList[thisLine][1]
  135.                                                
  136.                                                 if thisLine == position then
  137.                                                         term.setCursorPos(math.floor((xSize - #thisString - 8) / 2) + 1, y)
  138.                                                         term.clearLine()
  139.                                                         term.setTextColour(colours.brown)
  140.                                                         term.write(cursor[curCount][1])
  141.                                                         term.setTextColour(colours.purple)
  142.                                                         term.write(thisString)
  143.                                                         term.setTextColour(colours.brown)
  144.                                                         term.write(cursor[curCount][2])
  145.                                                 else
  146.                                                         term.setCursorPos(math.floor((xSize - #thisString) / 2) + 1, y)
  147.                                                         term.clearLine()
  148.  
  149.                                                         if y == 4 or y == ySize - 3 then term.setTextColour(blackText)
  150.                                                         elseif y == 5 or y == ySize - 4 then term.setTextColour(colours.grey)
  151.                                                         elseif y == 6 or y == ySize - 5 then term.setTextColour(colours.lightGrey)
  152.                                                         else term.setTextColour(colours.white) end
  153.  
  154.                                                         term.write(thisString)
  155.                                                 end
  156.                                         else
  157.                                                 term.setCursorPos(1, y)
  158.                                                 term.clearLine()
  159.                                         end
  160.                                 end
  161.  
  162.                                 lastPosition = position
  163.                         end
  164.                 end
  165.         end
  166. end
  167.  
  168. ---------------------------------------------
  169. ------------         Init        ------------
  170. ---------------------------------------------
  171.  
  172. if term.current().setTextScale then
  173.         local setTextScale = term.current().setTextScale
  174.        
  175.         setTextScale(0.5)
  176.         local scale = 5
  177.        
  178.         repeat
  179.                 setTextScale(scale)
  180.                 scale = scale - 0.5
  181.                 local xSize, ySize = term.getSize()
  182.         until (xSize >= 50 and ySize >= 19) or scale == 0
  183. end
  184.  
  185. xSize, ySize = term.getSize()
  186.  
  187. if xSize < 50 or ySize < 19 or not term.isColour() then error("Sorry, a 50x19 colour display is required at minimum.") end
  188.  
  189. do
  190.         local input = fs.open("printshop.cfg", "r")
  191.         printList = textutils.unserialise(input.readAll())
  192.         input.close()
  193. end
  194.  
  195. term.setTextColour(colours.black)
  196. term.setBackgroundColour(colours.green)
  197.  
  198. for j = 0, 1 do for i = 1, 3 do
  199.         term.setCursorPos(1, i + (j * (ySize - 3)))
  200.         term.clearLine()
  201. end end
  202.  
  203. term.setCursorPos(5, 2)
  204. term.write("PrintShop", 5, 2)
  205.  
  206. paintutils.drawPixel(1, 1, colours.blue)
  207. paintutils.drawPixel(2, 1, colours.green)
  208. paintutils.drawPixel(3, 1, colours.yellow)
  209. paintutils.drawPixel(1, 2, colours.grey)
  210. paintutils.drawPixel(2, 2, colours.white)
  211. paintutils.drawPixel(3, 2, colours.lightBlue)
  212. paintutils.drawPixel(1, 3, colours.orange)
  213. paintutils.drawPixel(3, 3, colours.red)
  214. paintutils.drawPixel(2, 3, colours.lime)
  215.  
  216. ---------------------------------------------
  217. ------------  Main Program Loop  ------------
  218. ---------------------------------------------
  219.  
  220. while true do
  221.         local thisBook, copies = fileBrowser(), 1
  222.        
  223.         clear(colours.black, colours.lightGrey)
  224.         writeAt(thisBook[1], math.floor((51 - #thisBook[1]) / 2) + 1, 6)
  225.         writeAt("How many copies?", 18, 9, colours.grey)
  226.         writeAt("XP Level Cost: " .. tostring(cost * copies), 18, 13)
  227.        
  228.         writeAt(" < ", 18, 11, colours.lightGrey, colours.grey)
  229.         writeAt(" > ", 26, 11)
  230.         writeAt(" Ok ", 31, 11)
  231.         writeAt("   ", 22, 11)
  232.         writeAt(" Cancel ", 22, 15)
  233.        
  234.         local thisString = tostring(copies)
  235.         writeAt(thisString, 22 + math.floor((4 - #thisString) / 2), 11, colours.lime)
  236.        
  237.         repeat
  238.                 myEvent = {os.pullEvent("mouse_click")}
  239.                
  240.                 if clickedAt(17, 21, 11) and copies > 1 then
  241.                         copies = copies - 1
  242.                 elseif clickedAt(25, 29, 11) then
  243.                         copies = copies + 1
  244.                 end
  245.                
  246.                 writeAt("   ", 22, 11, colours.lime, colours.grey)
  247.                 thisString = tostring(copies)
  248.                 writeAt(thisString, 22 + math.floor((4 - #thisString) / 2), 11)
  249.                
  250.                 writeAt(tostring(cost * copies) .. "      ", 33, 13, colours.grey, colours.lightGrey)
  251.         until clickedAt(30, 35, 11) or clickedAt(21, 30, 15)
  252.        
  253.         if clickedAt(30, 35, 11) then
  254.                 if commands.testfor("@p[lm=" .. tostring(cost * copies) .. "]") then
  255.                         commands.xp("-" .. tostring(cost * copies) .."L", "@p")
  256.                        
  257.                         local book = cprint.newPrintableBook(thisBook[1])
  258.  
  259.                         local oldTerm = term.redirect(book)
  260.                         print(downloadPaste(thisBook[2]))
  261.                         term.redirect(oldTerm)
  262.  
  263.                         book.printBook("@p", copies)
  264.                        
  265.                         writeAt("Book(s) printed! Cooling down...", 10, 15)
  266.                         sleep(cooldown)
  267.                 else
  268.                         writeAt("You don't have enough XP levels!", 10, 15, colours.red)
  269.                         sleep(5)
  270.                 end
  271.         end
  272. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top