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

Python In Computercraft For Mc 1.6.x

Started by Allos, 03 November 2013 - 01:14 PM
Allos #1
Posted 03 November 2013 - 02:14 PM
Have you ever wanted to use Python in ComputerCraft? Well now's your chance! With CCPy v1.0, you can do just that, with access to the key APIs that make ComputerCraft work, for example the Term API, OS API etc.

Coming very soon

Here's the list of features coming very soon:
  • Sandboxing (it's not very good if viruses could propagate through this mod, when people download other people's scripts)
Coming soon(ish)
  • Turtle support (hopefully, haven't investigated whether it's possible or not yet)
Source Code
Edit: 2014 here's the source code (probs no one reading this now anyway, but I need to delete old files eventually): http://www.mediafire.com/download/stp5bh6oq8kris5/Source.zip

Installation

Download the zip file containing all the necessary files here: http://www.mediafire...h4klsy6ppr84b6l

With the latest forge version installed for your 1.6.x version of Minecraft, drag both jython-standalone-2.5.4-rc1.jar and CCPy.zip to your mods folder, in .minecraft. Finally, drag CCPy_Resource_Pack_v1.0.zip to the resourcepacks folder in .minecraft.

Basics (need to know)

To craft the Python Module block, needed to run Python programs, put some green dye, string, stone and redstone into the crafting bench in any order. Then shift-click it onto the side of your computer.

To start programming in Python, simply type "editpy <program name>", to use the editor, but with syntax highlighting for Python. I recommend appending ".py" to all your Python programs, so that people can differentiate between Python and Lua programs. To run your programs, type "runpy <program name> <arguments>", and it'll start right up. You will need a Python Module attached to your computer for this to work. There is a pause while the Jython Interpreter starts up, but your program will start running after ~1 second.

Programming with the APIs

The equivalent of "rednet.open("right")" in a Python program will be "cc.rednet_open("right")". This pattern applies for all API calls. This is to avoid conflicting names of APIs. For example, Python already has an os module ready for import, which would conflict with the ComputerCraft OS API. The equivalent of "keys.enter" is just "cc.keys["enter"]" in Python. When there are an arbitrary number of paraments, the known ones are normal, but the arbitrary number is stored in a list. For example, peripheral.call in lua always has a side as the first argument, and the function name to call on the peripheral as the second, and then an unknown number of extra arguments. In Python, the first two are the same, and then the extra arguments of unknown length are in a list. However, when there is an optional argument in Lua, for example the timeout on rednet.receive, this is also optional in Python. EXCEPTION: cc.peripheral_call still needs an empty array as the third argument, when there aren't any extra arguments. When there are multiple return values, for example "os.pullEvent()" in Lua, the values are returned in a list aswell. For example, "cc.os_pullEvent()" returns a list with the first element as the event type, and the rest as values from the event.

The full list of the implemented APIs is:
  • Term (without redirect/restore, due to my Java implementation of printing, and would require a lot of rewriten code)
  • Rednet
  • Key
  • Colo(u)r
  • OS
  • Peripheral
  • Redstone
  • Disk
  • And a miscellaneous function, read ("cc.read()") for input.
I didn't include things that I considered "addons", or were to complicated for me to be bothered to port :)/>. These include textutils and paintutils.

Output

Just use the print keyword in Python, like usual. This is the equivalent of the print() function in Lua. You can also use "cc.term_write".

Warning (IMPORTANT!)

This should be able to run on a server, however it isn't at all sandboxed. This means that I haven't prevented the Python programs from doing everything the programmer wants. For example, if you run this on your server on your own computer, then everyone can read and delete your files, run commands, etc. Therefore, only let people you trust on your server, or don't run a server at all.

Don't use input() for input, use cc.read(). If you use input(), your ComputerCraft computer will "crash", and you'll have to restart it, or exit the world and re-enter it.

Bugs

Please post any bugs not already posted below, with the title of "Bug". Include as much info as you can, including output logs, what you did to cause the bug, program code, etc. so I can sort it out ASAP.

Screenshots

Edited on 17 December 2014 - 05:56 PM
Engineer #2
Posted 03 November 2013 - 06:57 PM
I have a question to the OP: is this going to be an open source mod? Or at least, show the source code :P/> (open source and source code are two different things). I am asking this because I'm lately really enjoying to mod minecraft, but I'm not as good as I wished. I'm working on it, and I'm eager to learn it.

Back to the question, I asked it because I'm curious how such a big mod looks/should look. I have a pretty good idea of it, but want to see your implementation of it.


about the peripheral: if this really would work, and IF I knew python I would definitely use it, when it's sandboxed. Because I don't like the idea that I can download scripts that do terrible things to my computer. So I would recommend setting that as a first priority. It's not easy, I think, but has to be done.
Allos #3
Posted 04 November 2013 - 02:02 PM
I have a question to the OP: is this going to be an open source mod? Or at least, show the source code :P/> (open source and source code are two different things). I am asking this because I'm lately really enjoying to mod minecraft, but I'm not as good as I wished. I'm working on it, and I'm eager to learn it.

Back to the question, I asked it because I'm curious how such a big mod looks/should look. I have a pretty good idea of it, but want to see your implementation of it.


about the peripheral: if this really would work, and IF I knew python I would definitely use it, when it's sandboxed. Because I don't like the idea that I can download scripts that do terrible things to my computer. So I would recommend setting that as a first priority. It's not easy, I think, but has to be done.

I am not sure that I will publicly release all the source code, as I don't want people plagiarizing my work. However, I know where you are coming from, as I too had too learn to mod, just to make this mod (I'd never modded before). What I did is I learned from others (online tutorials, other open source mods, wiki pages on the forge site), and then built from this base. The whole Jython thing isn't really related to Minecraft specifically, the only Minecraft specific things are adding the block to the game, attributes etc. I could release the Minecraft specific stuff, such as the block file, main mod file etc., if you wanted.

The security issue it being attended to straight away. I will now add a "Coming very soon" section, to tell others.
ElvishJerricco #4
Posted 04 November 2013 - 07:27 PM
I am not sure that I will publicly release all the source code, as I don't want people plagiarizing my work.

Honestly, this is kind of a weird reason to go closed source. People don't copy code, they copy ideas. And when they do copy code, a lot of shit goes down on them. There are some good reasons for closed source, such as not wanting to deal with supporting people trying to use the repo, build scripts, etc; or if you're trying to monetize on the code and therefore can't release source. But plagiarism isn't a thing to worry about, especially in the world of MC modding. Not saying closed source is bad in any way. This reason just seems weird to me.
Symmetryc #5
Posted 05 November 2013 - 05:13 PM
I am not sure that I will publicly release all the source code, as I don't want people plagiarizing my work.
I'd have to agree with ElvishJerrico; Nobody in the MC community copies other people's code, and if for some reason they do, they'll get in big trouble ;)/>. For example, Buildcraft is an open source mod and its been out for a very long time and there hasn't been anyone who copied and released it as their own that I know of :)/>.
Allos #6
Posted 06 November 2013 - 01:00 PM
I am not sure that I will publicly release all the source code, as I don't want people plagiarizing my work.

Honestly, this is kind of a weird reason to go closed source. People don't copy code, they copy ideas. And when they do copy code, a lot of shit goes down on them. There are some good reasons for closed source, such as not wanting to deal with supporting people trying to use the repo, build scripts, etc; or if you're trying to monetize on the code and therefore can't release source. But plagiarism isn't a thing to worry about, especially in the world of MC modding. Not saying closed source is bad in any way. This reason just seems weird to me.

Ok, I'll release the source code, when the mod's more developed. I want to sort out the issues (sandboxing, inconsistencies in api names) with the code, and comment it, then I'll release it on GitHub.
Symmetryc #7
Posted 06 November 2013 - 02:43 PM
I am not sure that I will publicly release all the source code, as I don't want people plagiarizing my work.

Honestly, this is kind of a weird reason to go closed source. People don't copy code, they copy ideas. And when they do copy code, a lot of shit goes down on them. There are some good reasons for closed source, such as not wanting to deal with supporting people trying to use the repo, build scripts, etc; or if you're trying to monetize on the code and therefore can't release source. But plagiarism isn't a thing to worry about, especially in the world of MC modding. Not saying closed source is bad in any way. This reason just seems weird to me.

Ok, I'll release the source code, when the mod's more developed. I want to sort out the issues (sandboxing, inconsistencies in api names) with the code, and comment it, then I'll release it on GitHub.
Don't feel obligated to if you still don't want to, it's certainly your decision :)/>.
Allos #8
Posted 08 November 2013 - 03:02 PM
I am not sure that I will publicly release all the source code, as I don't want people plagiarizing my work.

Honestly, this is kind of a weird reason to go closed source. People don't copy code, they copy ideas. And when they do copy code, a lot of shit goes down on them. There are some good reasons for closed source, such as not wanting to deal with supporting people trying to use the repo, build scripts, etc; or if you're trying to monetize on the code and therefore can't release source. But plagiarism isn't a thing to worry about, especially in the world of MC modding. Not saying closed source is bad in any way. This reason just seems weird to me.

Ok, I'll release the source code, when the mod's more developed. I want to sort out the issues (sandboxing, inconsistencies in api names) with the code, and comment it, then I'll release it on GitHub.
Don't feel obligated to if you still don't want to, it's certainly your decision :)/>.

Yeah, very true. I'm sure some people want to see how I did it :)/>. But, I want to see how they do it (If they try to do something similar, I want to see if they do it the same way).
ecmaxp #9
Posted 17 November 2013 - 02:07 PM
Can you rewrite with PyPy + socket + json + Java mods? (Not use Jython, mean the use remote process.)

I can write python part's code, for more safely running python code (or other language support?) in server machine. (memory limit are possible! but i don't know how to handle computer get shutdown.)

and check the https://github.com/EcmaXp/PyCraft
trevor403 #10
Posted 01 December 2013 - 07:14 PM
You should make an interactive shell like lua does in computercraft. Just a suggestion because I like to use dir command a lot to learn python apis.
Y2K38 #11
Posted 12 December 2013 - 09:47 PM
Would it be possible to install other python modules and api's into it like PyBrain or something else into the mod?

Also, you could probably make a Python Command Line in Python like this:

print("CCPython Interpreter")
while True:
	srun=input()
	run=str(srun)
	print(exec(run))
I know that this code sample uses input() instead of cc.read() but I was too lazy to make the changes.
Edited on 13 December 2013 - 03:00 PM