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("pow 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 __init__(self, x=None, y=None, z=None):
self.x = x
self.y = y
self.z = z
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()
)
)
dispatcher.execute("teleport 24 51 -632", {})
License
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
brigadier.py-1.0.0.tar.gz
(17.0 kB
view hashes)
Built Distribution
Close
Hashes for brigadier.py-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 98aae4b64e8a4e36d4efbe486737668a7620db1b0a7e884b72cf287303352ce6 |
|
MD5 | f7146f92eca3b3df2c9160f47ca2ec84 |
|
BLAKE2b-256 | 7e07fc567d6891dbd1aa1b5c1d64534e84a9ee5c424a149e5340283c3e1b490d |