This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Bomb Bloke's profile picture

cPrint: Print books with Command Computers

Started by Bomb Bloke, 31 October 2015 - 06:54 AM
Bomb Bloke #1
Posted 31 October 2015 - 07:54 AM
cPrint API: pastebin get QP8yFSPW cprint

PrintShop script: pastebin get juQeX2fS printshop

People keep complaining about the difficulty of creating printed books. It occurred to me that Command Computers could be used to "cheat", making the process much simpler.

So here we have two bits of code: an API that returns terminal objects that can be converted into books, and a script that uses said API to offer books to players.

cPrint API Usagecprint.newPrintableBook([string book title]) => table book terminal object
Returns a terminal object that you can term.redirect() to. Dimensions are 25 columns by an effectively unlimited number of rows.

These objects offers two additional functions over regular ones:

book.printBook([string target], [number amount])
If a target is specified (eg "@p", which would refer to whoever's closest to the computer), places a printed book into their inventory. If no target is specified, the book goes into a chest above the computer. Be aware that this may destroy the two blocks which were previously above it, if any were present!

If an amount is specified, that many copies of the book will be generated. Defaults to one.

book.setLore([string tooltip])
Sets the tooltip that will be used if the book is printed.

For example:

os.loadAPI("cprint")

local myBook = cprint.newPrintableBook("This is my book")

local oldTerm = term.redirect(myBook)

term.setTextColour(colours.red)
print("Hello, world!")
print()
term.setTextColour(colours.black)
print("This is my great new book!")

term.redirect(oldTerm)

myBook.setLore("by Bomb Bloke")
myBook.printBook("@p")

PrintShop Script Usageprintshop [XP cost] [cooldown]

When first run, PrintShop will download a small list of books for you (along with the cPrint API, if it's missing):

pastebin get CWWeJk9g printshop.cfg

It'll then display this list on-screen for users to pick from. Editing "printshop.cfg" allows you to change the selection to suit yourself.

As regular players cannot directly interact with Command Computers, if you wish to offer books to your regular users, you will need to connect the computer to an Advanced Monitor (3x2 blocks minimum) and run it through that:

monitor <monitorside> printshop

When users attempt to print books, they will be charged 5 XP levels for each (taken from whoever's standing closest to the computer when it produces the order). You can specify a different amount if you wish.

After each print job, the printer will also "cooldown" for a default period of 30 seconds. These two mechanisms are intended to prevent rogue players from using the system to generate copious amounts of books.

If just using the script for yourself, or if you simply wish to do without these limitations, you'd just set them to 0 at the command line. Eg, by running the script with:

printshop 0 0

Videohttp://youtu.be/bw1eYBMQVCc

Version History2015/10/31
1.0.0
Initial release.

2015/11/02
cPrint 1.0.1
Bug fixes.
Edited on 02 November 2015 - 02:23 AM
The_Puzzlemaker #2
Posted 01 November 2015 - 12:49 PM
Any screenshots? I want to see how the PrintShop API would look like.
Awe2K #3
Posted 01 November 2015 - 05:30 PM
Alright, it seems to be a cool idea to allow player to simultaneously print whole book.
However, I've experienced some problems trying it (your program just printed book contents and tag info to chat):
Screenshot in spoiler.
Spoiler
Bomb Bloke #4
Posted 02 November 2015 - 03:20 AM
Any screenshots? I want to see how the PrintShop API would look like.

The PrintShop boils down to a basic "here's a list"-interface, so it's not very interesting. I've gone ahead an added a short video to the OP anyway.

However, I've experienced some problems trying it (your program just printed book contents and tag info to chat):

Seems I forgot to re-escape quotes before passing the text to the command system - I didn't test the particular book you tried it with (even though it happened to be first on the list), and somehow the books I did try didn't have them!

If you "rm cprint" and allow it to be redownloaded, it should be fixed now. Thanks for the report! :)/>
elopus001 #5
Posted 02 November 2015 - 03:38 AM
Thanks for making this. I'm working on using it for generating logs for my other scripts. Is there a way to make these place in a chest?
Bomb Bloke #6
Posted 02 November 2015 - 06:33 AM
If you don't specify a target when printing, the API will generate a chest above the Command Computer (assuming something isn't already there), and then a hopper with the book(s) above that (which gets removed once the books have dropped to the chest below).

It's done this way as there isn't a method of directly adding items to an inventory using commands - at least, not under MC 1.7.10. Attempting to place a new chest each time would delete its prior contents.

If you wanted to have the PrintShop script call the API in this manner, change the line around 263 to:

book.printBook(nil, copies)

Books may not be the best way to store logs, however - they're a pretty way of distributing information to players, but you might be better off storing data you actually want to "use" into files on your system's drives (which you can send off to pastebin, if you're having trouble accessing them from there). Eg:

local output = fs.open("myLog.txt", "w")  -- Or use "a" instead of "w", if you want "append" mode.
output.writeLine("stuff")
output.close()
elopus001 #7
Posted 02 November 2015 - 01:38 PM
Books may not be the best way to store logs, however - they're a pretty way of distributing information to players, but you might be better off storing data you actually want to "use" into files on your system's drives (which you can send off to pastebin, if you're having trouble accessing them from there). Eg:

local output = fs.open("myLog.txt", "w")  -- Or use "a" instead of "w", if you want "append" mode.
output.writeLine("stuff")
output.close()

Ok. I'm already using the file method to store all the block locations. I'm just using this to create a book of what type of building is at what chunk in my city generator.
Edited on 02 November 2015 - 12:40 PM