Skip to content

Repository files navigation

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 = "$private"action = "allow"# change here deny to allow
  2. Install & start python language server

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

    wget http://127.0.0.1:8000 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:

fromccimportdisk, osdisk.eject('right')
print(os.getComputerLabel())

Opening a file:

fromccimportfswithfs.open('filename', 'r') asf:
forlineinf:
print(line)

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

fromccimportostimer_id=os.startTimer(2)
foreinos.captureEvent('timer'):
ife[0] ==timer_id:
print('Timer reached')
break

Using modems:

fromccimportperipheralmodem=peripheral.wrap('back')
listen_channel=5# this automatically opens and closes modem on listen_channelformsginmodem.receive(listen_channel):
print(repr(msg))
ifmsg.content=='stop':
breakelse:
modem.transmit(msg.reply_channel, listen_channel, msg.content)

Using parallel:

fromccimportparallel, osdeffn():
os.sleep(2)
print('done')
parallel.waitForAll(fn, fn, fn)

Importing in-game files as modules:

fromccimportimport_filep=import_file('/disk/program.py') # absolutem=import_file('lib.py', __file__) # relative to current file

More examples can be found in this repository: examples/ and tests/cc_programs/

About

Pythonization of ComputerCraft Minecraft mod. Write Python instead Lua!

Topics

Resources

Stars

82 stars

Watchers

6 watching

Forks

Releases

Packages

Used by

Contributors

Languages