mark2devel / mark2

Minecraft Multi Server Wrapper Written in Python with Twisted; Pull Requests HIGHLY Encouraged, Collaborators Needed Discord: https://discord.gg/zymJygHNpv
Other
205 stars 44 forks source link

Text formatting support for scripts #132

Closed Column01 closed 2 years ago

Column01 commented 2 years ago

Right now, scripts can be run using mark2 events. I'd like to support formatting properties from certain events into the commands that get executed.

For example:

  1. React to the player chat message
  2. Have it execute msg {username} I saw you sent a chat message!
  3. mark2 formats the {username} to be whatever the PlayerChat event had for the username.

As far as I know, it should just be adding something to the args for the execute function that passes the event to it? Then we can just format the cmd with python to fill in placeholders. https://github.com/gsand/mark2/blob/8419151405cb16427371f256f3662a9370a55e0e/mk2/plugins/script.py#L29

Would probably become self.plugin.register(lambda e: self.execute(command, e), event) because AFAIK, e in this instance is the event. Then we'd just need code to format the event properties into the command string

Column01 commented 2 years ago

Wow, that was incredibly easy to implement. Events already had a way to serialize it so it was just a matter of ensuring all values are strings and then using a custom safe dict to ensure missing keys are just returned as {key} instead of raising a ValueError

image

Added in https://github.com/gsand/mark2/commit/2a5a7ee4da775b474e7e8c80314a4b7da47b1a58