Skip to main content

Implementation of Mojang's brigadier in Python

Project description

brigadier.py

Implementation of Mojang/brigadier in Python.

Installing

Requires Python 3.7 or higher

# For Windows
py -3 -m pip install brigadier.py

# For linux or macOS
python3 -m pip install brigadier.py

Examples

Registering a simple command

from brigadier import CommandDispatcher
from brigadier.builder import literal, argument
from brigadier.arguments import integer

def pow2_command(ctx):
    number = ctx.get_argument("number")
    return pow(number, 2)

def pow_command(ctx):
    number = ctx.get_argument("number")
    power = ctx.get_argument("power")
    return pow(number, power)

# Register the command
dispatcher = CommandDispatcher()
dispatcher.register(
    literal("pow2").then(
        argument("number", integer).executes(power_command)
    )
)

dispatcher.register(
    literal("pow").then(
        argument("number", integer).then(
            argument("power", integer).executes(pow_command)
        )
    )
)

# Execute the command
print(dispatcher.execute("pow2 2", {}))
print(dispatcher.execute("pow 3 4", {}))

Using a custom argument type

from brigadier import CommandDispatcher
from brigadier.builder import literal, argument
from brigadier.suggestion import empty_suggestion

class Vector3:
    def parse(self, reader):
        self.x = reader.read_int()
        reader.skip()
        self.y = reader.read_int()
        reader.skip()
        self.z = reader.read_int()
        return self

    def list_suggestions(self, builder):
        return empty_suggestion()

    def get_examples(self):
        return ["2 3 1", "0 5 0"]

def teleport_command(ctx):
    location = ctx.get_argument("location")
    x = location.x
    y = location.y
    z = location.z
    print(f"You've been teleported to {x}, {y}, {z}")
    return 1

dispatcher = CommandDispatcher()
dispatcher.register(
    literal("teleport").then(
        argument("location", Vector3()).executes(teleport_command)
    )
)

dispatcher.execute("teleport 24 51 -632", {})

License

MIT

Project details


Download files

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

Source Distribution

brigadier.py-1.1.0.tar.gz (17.1 kB view hashes)

Uploaded Source

Built Distribution

brigadier.py-1.1.0-py3-none-any.whl (27.8 kB view hashes)

Uploaded Python 3

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