Fork of Python for ComputerCraft
Go to file
2020-07-03 02:08:49 +03:00
computercraft Rednet 2020-07-03 02:08:49 +03:00
examples Remove global state, remove autorestarting 2016-12-17 21:32:58 +03:00
.gitignore Tests & fixes for keys and os api 2020-06-28 08:19:01 +03:00
README.md Bump version; Update description 2020-06-27 01:40:18 +03:00
requirements.txt Use pythocloc for development 2020-06-26 11:57:45 +03:00
setup.cfg Flake8 settings 2020-06-27 01:39:52 +03:00
setup.py Bump version; Update description 2020-06-27 01:40:18 +03:00
testmod.py Rednet 2020-07-03 02:08:49 +03:00

Pythonized CC Tweaked (ComputerCraft) API

  1. 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. Create module named examplemod.py:

    async def hello(api):
        await api.print('Hello world!')
    
  3. Start a server:

    python -m computercraft.server examplemod
    
  4. In minecraft, open up any computer and type:

    wget http://127.0.0.1:8080/ py
    py hello
    

py is short Lua program that interacts with the server. Argument is the name of coroutine inside the module. api object contains almost everything as is in ComputerCraft documentation:

async def program(api):
    await api.disk.eject('right')
    await api.print(await api.os.getComputerLabel())
    # ...

Using python coroutines allows launching commands in parallel, effectively replacing parallel API:

async def program(api):
    # Since os.sleep is mostly waiting for events, it doesn't block execution of parallel threads
    # and this snippet takes approximately 2 seconds to complete.
    await asyncio.gather(api.os.sleep(2), api.os.sleep(2))