Fork of Python for ComputerCraft
Go to file
2023-01-08 23:05:32 -05:00
computercraft convert spaces to tabs 2023-01-08 23:05:32 -05:00
examples convert spaces to tabs 2023-01-08 23:05:32 -05:00
tests Rewrite lua_eval result processing 2020-07-21 01:32:37 +03:00
.gitignore Tests & fixes for keys and os api 2020-06-28 08:19:01 +03:00
Dockerfile Run in docker 2020-07-18 13:36:19 +03:00
LICENSE Create LICENSE 2020-07-13 08:47:45 +03:00
README.md Add how to run program to readme 2020-07-25 02:41:28 +03:00
requirements.txt Rewrite to greenlets (remove awaits from user programs) 2020-07-13 02:49:07 +03:00
setup.cfg Rewrite to greenlets (remove awaits from user programs) 2020-07-13 02:49:07 +03:00
setup.py Update readme & setup.py: make project installable from pypi again 2020-07-17 23:36:51 +03:00

Pythonized CC Tweaked (ComputerCraft) API

Warning: CPython can't build safe sandboxes for arbitrary untrusted code (read more). Never use code in this repo if you don't trust your players!

  1. Before you start Minecraft, enable localhost in mod server config

    In case of singleplayer it's located inside your saves folder. In case of multiplayer check your server folder.

    Edit computercraft-server.toml

    [[http.rules]]
    	host = "127.0.0.0/8"
    	action = "allow"  # change here deny to allow
    
  2. Install & start python language server

    Choose one of the following:

    Docker way:

    docker run -it -p 8080:8080 neumond/python-computer-craft
    

    Install & run manually:

    pip install computercraft
    python -m computercraft.server
    
  3. Start Minecraft, open up any computer and type:

    wget http://127.0.0.1:8080/ py
    py
    

    Now you have python REPL in computercraft! To quit REPL type exit() and press enter.

    To run any program:

    py program.py  # relative to current dir
    py /path/to/program.py
    

py is short Lua program that interacts with the server. cc module contains almost everything as is in ComputerCraft documentation:

from cc import disk, os

disk.eject('right')
print(os.getComputerLabel())

Opening a file:

from cc import fs

with fs.open('filename', 'r') as f:
    for line in f:
        print(line)

Waiting for event (os.captureEvent instead os.pullEvent):

from cc import os

timer_id = os.startTimer(2)
for e in os.captureEvent('timer'):
    if e[0] == timer_id:
        print('Timer reached')
        break

Using modems:

from cc import peripheral

modem = peripheral.wrap('back')
listen_channel = 5
# this automatically opens and closes modem on listen_channel
for msg in modem.receive(listen_channel):
    print(repr(msg))
    if msg.content == 'stop':
        break
    else:
        modem.transmit(msg.reply_channel, listen_channel, msg.content)

Using parallel:

from cc import parallel, os

def fn():
    os.sleep(2)
    print('done')

parallel.waitForAll(fn, fn, fn)

Importing in-game files as modules:

from cc import import_file

p = import_file('/disk/program.py')  # absolute
m = import_file('lib.py', __file__)  # relative to current file

More examples can be found in this repository.