BombBloke

cPrint API (ComputerCraft)

Oct 31st, 2015
264
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- +---------------------+------------+---------------------+
  2. -- |                     |            |                     |
  3. -- |                     |   cPrint   |                     |
  4. -- |                     |            |                     |
  5. -- +---------------------+------------+---------------------+
  6.  
  7. local version = "Version 1.0.1"
  8.  
  9. -- An API for generating Printed Books via Command Computers.
  10. -- http://www.computercraft.info/forums2/index.php?/topic/25037-cprint-print-books-with-command-computers/
  11.  
  12. -- ----------------------------------------------------------
  13.  
  14. local blank1, blank2, colourNum = string.rep(" ", 25), string.rep("f", 25), {}
  15.  
  16. do
  17.     local hex, counter = "0123456789abcdef", 1
  18.  
  19.     for i = 1, 16 do
  20.         colourNum[counter] = hex:sub(i, i)
  21.         counter = counter * 2
  22.     end
  23. end
  24.  
  25. local function safeString(text)
  26.     local newText = {}
  27.    
  28.     for i = 1, #text do
  29.         local val = text:byte(i)
  30.         newText[i] = (val > 31 and val < 127) and val or 63
  31.     end
  32.    
  33.     return string.char(unpack(newText))
  34. end
  35.  
  36. local function printBook(target, count, page)
  37.     local output, display = {"pages:" .. tostring(math.ceil(#page / 21))}, {}
  38.     if page.title ~= " " then display[1] = "Name:\"" .. page.title .. "\"" end
  39.     if page.lore then display[#display + 1] = "Lore:[\"" .. page.lore .. "\"]" end
  40.     if #display > 0 then output[2] = "display:{" .. table.concat(display, ",").. "}" end
  41.    
  42.     if type(count) ~= "number" then count = 1 end
  43.    
  44.     for i = 1, #page do
  45.         output[#output + 1] = "line" .. tostring(i - 1) .. ":\"" .. page[i].text:gsub("\"", "\\\"") .. "\""
  46.         output[#output + 1] = "colour" .. tostring(i - 1) .. ":\"" .. page[i].fg .. "\""
  47.     end
  48.    
  49.     output = "{" .. table.concat(output, ",") .. "}"
  50.    
  51.     if target then
  52.         commands.give(target, "ComputerCraft:printout", count, 2, output)
  53.     else
  54.         local x, y, z = commands.getBlockPosition()
  55.         if commands.getBlockInfo(x, y + 1, z).name == "minecraft:air" then commands.setblock(x, y + 1, z, "minecraft:chest", commands.getBlockInfo(x, y, z).metadata) end
  56.         commands.setblock(x, y + 2, z, "minecraft:hopper", 0, "replace", "{Items:[{Slot:0,id:4099,Damage:2,Count:"..tostring(count)..",tag:"..output.."}]}")
  57.         sleep(count * 0.5)
  58.         commands.setblock(x, y + 2, z, "minecraft:air")
  59.     end
  60. end
  61.  
  62. function newPrintableBook(title)
  63.     if title == "" or not title then title = " " end
  64.  
  65.     local book, page, xPos, yPos, tCol, bCol = {}, {["title"] = title}, 1, 1, colours.black, colours.white
  66.     for i = 1, 21 do page[i] = {["text"] = blank1, ["fg"] = blank2} end
  67.  
  68.     book.blit = function(text, fgCol)
  69.         if xPos > 25 or xPos + #text - 1 < 1 or yPos < 1 then return end
  70.         if yPos > #page then for i = #page + 1, yPos do page[i] = {["text"] = blank1, ["fg"] = blank2} end end
  71.  
  72.         text = safeString(text)
  73.  
  74.         if xPos < 1 then
  75.             text = text:sub(2 - xPos)
  76.             fgCol = fgCol:sub(2 - xPos)
  77.             xPos = 1
  78.         end
  79.  
  80.         if xPos + #text > 26 then
  81.             text = text:sub(1, 26 - xPos)
  82.             fgCol = fgCol:sub(1, 26 - xPos)
  83.         end
  84.  
  85.         page[yPos].text = page[yPos].text:sub(1, xPos - 1) .. text .. page[yPos].text:sub(xPos + #text)
  86.         page[yPos].fg = page[yPos].fg:sub(1, xPos - 1) .. fgCol .. page[yPos].fg:sub(xPos + #fgCol)
  87.  
  88.         xPos = xPos + #text
  89.     end
  90.  
  91.     book.write = function(text)
  92.         text = tostring(text)
  93.         book.blit(text, string.rep(colourNum[tCol], #text))
  94.     end
  95.  
  96.     book.clearLine = function()
  97.         page[yPos].text = blank1
  98.     end
  99.  
  100.     book.clear = function()
  101.         for i = 1, #page do page[i].text = blank1 end
  102.     end
  103.  
  104.     book.getSize = function()
  105.         return 25, math.huge
  106.     end
  107.  
  108.     book.setCursorPos = function(x, y)
  109.         xPos, yPos = math.floor(x), math.floor(y)
  110.     end
  111.  
  112.     book.getCursorPos = function()
  113.         return xPos, yPos
  114.     end
  115.  
  116.     book.setTextColour = function(col)
  117.         tCol = col
  118.     end
  119.     book.setTextColor = book.setTextColour
  120.  
  121.     book.setBackgroundColour = function(col)
  122.         bCol = col
  123.     end
  124.     book.setBackgroundColor = book.setBackgroundColour
  125.    
  126.     book.getTextColour = function()
  127.         return tCol
  128.     end
  129.     book.getTextColor = book.getTextColour
  130.  
  131.     book.getBackgroundColour = function()
  132.         return bCol
  133.     end
  134.     book.getBackgroundColor = book.getBackgroundColour
  135.  
  136.     book.isColour = function()
  137.         return true
  138.     end
  139.     book.isColor = book.isColour
  140.  
  141.     local dummyFunc = function() end
  142.     book.scroll, book.setCursorBlink = dummyFunc, dummyFunc
  143.    
  144.     book.setLore = function(lore)
  145.         page.lore = lore and tostring(lore)
  146.     end
  147.  
  148.     book.printBook = function(target, count)
  149.         printBook(target, count, page)
  150.     end
  151.  
  152.     return book
  153. end
RAW Paste Data