-- +---------------------+------------+---------------------+ -- | | | | -- | | cPrint | | -- | | | | -- +---------------------+------------+---------------------+ local version = "Version 1.0.1" -- An API for generating Printed Books via Command Computers. -- http://www.computercraft.info/forums2/index.php?/topic/25037-cprint-print-books-with-command-computers/ -- ---------------------------------------------------------- local blank1, blank2, colourNum = string.rep(" ", 25), string.rep("f", 25), {} do local hex, counter = "0123456789abcdef", 1 for i = 1, 16 do colourNum[counter] = hex:sub(i, i) counter = counter * 2 end end local function safeString(text) local newText = {} for i = 1, #text do local val = text:byte(i) newText[i] = (val > 31 and val < 127) and val or 63 end return string.char(unpack(newText)) end local function printBook(target, count, page) local output, display = {"pages:" .. tostring(math.ceil(#page / 21))}, {} if page.title ~= " " then display[1] = "Name:\"" .. page.title .. "\"" end if page.lore then display[#display + 1] = "Lore:[\"" .. page.lore .. "\"]" end if #display > 0 then output[2] = "display:{" .. table.concat(display, ",").. "}" end if type(count) ~= "number" then count = 1 end for i = 1, #page do output[#output + 1] = "line" .. tostring(i - 1) .. ":\"" .. page[i].text:gsub("\"", "\\\"") .. "\"" output[#output + 1] = "colour" .. tostring(i - 1) .. ":\"" .. page[i].fg .. "\"" end output = "{" .. table.concat(output, ",") .. "}" if target then commands.give(target, "ComputerCraft:printout", count, 2, output) else local x, y, z = commands.getBlockPosition() 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 commands.setblock(x, y + 2, z, "minecraft:hopper", 0, "replace", "{Items:[{Slot:0,id:4099,Damage:2,Count:"..tostring(count)..",tag:"..output.."}]}") sleep(count * 0.5) commands.setblock(x, y + 2, z, "minecraft:air") end end function newPrintableBook(title) if title == "" or not title then title = " " end local book, page, xPos, yPos, tCol, bCol = {}, {["title"] = title}, 1, 1, colours.black, colours.white for i = 1, 21 do page[i] = {["text"] = blank1, ["fg"] = blank2} end book.blit = function(text, fgCol) if xPos > 25 or xPos + #text - 1 < 1 or yPos < 1 then return end if yPos > #page then for i = #page + 1, yPos do page[i] = {["text"] = blank1, ["fg"] = blank2} end end text = safeString(text) if xPos < 1 then text = text:sub(2 - xPos) fgCol = fgCol:sub(2 - xPos) xPos = 1 end if xPos + #text > 26 then text = text:sub(1, 26 - xPos) fgCol = fgCol:sub(1, 26 - xPos) end page[yPos].text = page[yPos].text:sub(1, xPos - 1) .. text .. page[yPos].text:sub(xPos + #text) page[yPos].fg = page[yPos].fg:sub(1, xPos - 1) .. fgCol .. page[yPos].fg:sub(xPos + #fgCol) xPos = xPos + #text end book.write = function(text) text = tostring(text) book.blit(text, string.rep(colourNum[tCol], #text)) end book.clearLine = function() page[yPos].text = blank1 end book.clear = function() for i = 1, #page do page[i].text = blank1 end end book.getSize = function() return 25, math.huge end book.setCursorPos = function(x, y) xPos, yPos = math.floor(x), math.floor(y) end book.getCursorPos = function() return xPos, yPos end book.setTextColour = function(col) tCol = col end book.setTextColor = book.setTextColour book.setBackgroundColour = function(col) bCol = col end book.setBackgroundColor = book.setBackgroundColour book.getTextColour = function() return tCol end book.getTextColor = book.getTextColour book.getBackgroundColour = function() return bCol end book.getBackgroundColor = book.getBackgroundColour book.isColour = function() return true end book.isColor = book.isColour local dummyFunc = function() end book.scroll, book.setCursorBlink = dummyFunc, dummyFunc book.setLore = function(lore) page.lore = lore and tostring(lore) end book.printBook = function(target, count) printBook(target, count, page) end return book end