Skip to main content

A powerful Minecraft command library

Project description

logo

Mecha

GitHub Actions PyPI PyPI - Python Version Code style: black Discord

A powerful Minecraft command library.

from mecha import Mecha

mc = Mecha()

function = """
    execute
        as @a                        # For each "player",
        at @s                        # start at their feet.
        anchored eyes                # Looking through their eyes,
        facing 0 0 0                 # face perfectly at the target
        anchored feet                # (go back to the feet)
        positioned ^ ^ ^1            # and move one block forward.
        rotated as @s                # Face the direction the player
                                     # is actually facing,
        positioned ^ ^ ^-1           # and move one block back.
        if entity @s[distance=..0.6] # Check if we're close to the
                                     # player's feet.
        run
            say I'm facing the target!
"""

ast = mc.parse(function, multiline=True)
print(mc.serialize(ast))  # execute as @a at @s anchored eyes facing ...

Introduction

This package provides everything you need for working with Minecraft commands in Python, whether you're looking to process commands or build abstractions on top.

Features

  • Extensible and version-agnostic mcfunction parser
  • Clean, immutable and hashable abstract syntax tree with source location
  • Command config resolver that flattens and enumerates all the valid command prototypes
  • Powerful rule dispatcher for processing specific ast nodes
  • Composable ast visitors and reducers
  • Comes with useful syntactic extensions like relative locations, nesting and implicit execute
  • Compile-time scripting with Bolt, a subset of Python integrated into command syntax
  • Rich function analyzer for keeping track of command statistics
  • Execute arbitrary compilation passes in your beet pipeline
  • (soon) Expressive command API for writing commands in Python

Credits

Installation

The package can be installed with pip.

$ pip install mecha

Command-line utility

$ mecha --help
Usage: mecha [OPTIONS] [SOURCE]...

  Validate data packs and .mcfunction files.

Options:
  -m, --minecraft VERSION  Minecraft version.
  -l, --log LEVEL          Configure output verbosity.
  -s, --stats              Collect statistics.
  -j, --json FILENAME      Output json.
  -v, --version            Show the version and exit.
  -h, --help               Show this message and exit.

You can use the command-line utility to check data packs and function files for errors. The command arguments can be zipped and unzipped data packs, individual function files, and if you specify a directory that's not a data pack it will recursively grab all the .mcfunction files in the directory. You can use the --minecraft option to select between versions 1.16, 1.17, and 1.18.

$ mecha path/to/my_data_pack
Validating with mecha vX.X.X

ERROR  | mecha  Expected curly '}' but got bracket ']'.
       | path/to/my_data_pack/data/demo/functions/foo.mcfunction:5:34
       |      4 |
       |      5 |  say hello @a[scores={foo=1, bar=2]
       |        :                                   ^

Error: Reported 1 error.

The --stats option will output a report that shows how many commands, selectors and scoreboards were used. You can also use the --json option to output the raw statistics in a json file.

INFO   | stats  Analyzed 1 function
       | -------------------------------------------------------------------------------
       | Total commands (1 behind execute)                                      |      4
       | -------------------------------------------------------------------------------
       |        /scoreboard                                                     |      3
       |                    objectives add <objective> <criteria>               |      1
       |                    players set <targets> <objective> <score>           |      1
       |                    players operation <targets> <targetObjective> <o... |      1
       |        /setblock (1 behind execute)                                    |      1
       |        /execute                                                        |      1
       |                 if score <target> <targetObjective> matches <range>... |      1
       |                 as <targets> <subcommand>                              |      1
       |                 run <subcommand>                                       |      1
       | -------------------------------------------------------------------------------
       | Total selectors                                                        |      3
       | -------------------------------------------------------------------------------
       |        @e                                                              |      2
       |           [tag]                                                        |      2
       |           [scores]                                                     |      1
       |        @s                                                              |      1
       |        @e with missing or inverted type                                |      2
       | -------------------------------------------------------------------------------
       | Scoreboard objectives                                                  |      2
       | -------------------------------------------------------------------------------
       |        my_consts (dummy)                                               |      3
       |                  10                                                    |      2
       |        foo                                                             |      3

Github action

You can use mecha to check your data packs and function files for errors without having to install anything using the mcbeet/check-commands github action.

# .github/workflows/check-commands.yml
name: Check commands
on: [push]

jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: mcbeet/check-commands@v1
        with:
          source: .

This allows you to make sure that your commands don't contain any error when you push to your repository. For more details check out the action README.

Contributing

Contributions are welcome. Make sure to first open an issue discussing the problem or the new feature before creating a pull request. The project uses poetry.

$ poetry install

You can run the tests with poetry run pytest.

$ poetry run pytest

The project must type-check with pyright. If you're using VSCode the pylance extension should report diagnostics automatically. You can also install the type-checker locally with npm install and run it from the command-line.

$ npm run watch
$ npm run check

The code follows the black code style. Import statements are sorted with isort.

$ poetry run isort mecha tests
$ poetry run black mecha tests
$ poetry run black --check mecha tests

License - MIT

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mecha-0.62.2.tar.gz (91.1 kB view details)

Uploaded Source

Built Distribution

mecha-0.62.2-py3-none-any.whl (105.6 kB view details)

Uploaded Python 3

File details

Details for the file mecha-0.62.2.tar.gz.

File metadata

  • Download URL: mecha-0.62.2.tar.gz
  • Upload date:
  • Size: 91.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.3 readme-renderer/37.3 requests/2.28.1 requests-toolbelt/0.10.1 urllib3/1.26.12 tqdm/4.64.1 importlib-metadata/5.0.0 keyring/23.11.0 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.9

File hashes

Hashes for mecha-0.62.2.tar.gz
Algorithm Hash digest
SHA256 e8aae95589861c8551fdbe13364fee5e3a6604ffd1d62bf6344d5c940b1984d6
MD5 1093d480d14b929f5bb3eed9ef11442a
BLAKE2b-256 f055325b9a637882a9bbcd5b69b455e17462ae4e99717357e37ac90dba66ef05

See more details on using hashes here.

File details

Details for the file mecha-0.62.2-py3-none-any.whl.

File metadata

  • Download URL: mecha-0.62.2-py3-none-any.whl
  • Upload date:
  • Size: 105.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.3 readme-renderer/37.3 requests/2.28.1 requests-toolbelt/0.10.1 urllib3/1.26.12 tqdm/4.64.1 importlib-metadata/5.0.0 keyring/23.11.0 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.9

File hashes

Hashes for mecha-0.62.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3a1a1902eb45432c92b126f1f2ac942f6ec9aa82cbd73bbfdfd0f94eb9dabf93
MD5 aed578157ebe8b31a95b3dcc819d9166
BLAKE2b-256 6fe2ecf8dd2742a8ee9f88a6d0e5f8e7d4d60c14c5eb1f5c67c62498854588ec

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page