simon816 / Command-Block-Assembly

Compile high-level code into Minecraft commands
https://www.simon816.com/minecraft/assembler
MIT License
271 stars 29 forks source link

Possible bug in the C text API #17

Closed sarahmence closed 4 years ago

sarahmence commented 4 years ago

I am having trouble with the C Text API, specifically the function text_set_click_run. I have this code:

#include <stdio.h>
#include <text.h>

void say_hello() {
    printf("Hello!");
}

void main() {
    text_begin()
    text_set_text("Test");
    text_set_color("yellow");
    text_set_click_run(say_hello);
    text_end(TEXT_TELLRAW);
}

When I compile this file to object code, then to a datapack, then run the main function (by entering /function test:sub_main, the clickable text "Test" shows up in yellow as expected. However, when I click it, Minecraft displays an error stating "Expected whitespace to end one argument, but found trailing data". Minecraft then shows that instead of MCC compiling the click event function as /function test:sub_say_hello, it compiled it as /function $func:say_hello$. This made the datapack not work.

Is this a bug, and if not, how do I fix it?

Thanks!

Edit: I am using the latest version of the compiler, git cloned from the repository.

simon816 commented 4 years ago

That'll be a bug in the compiler. I used to use $func:...$ as a placeholder for a function name but removed it as part of a rewrite. I must have forgotten to update text_set_click_run.

May I suggest trying out CBL? I haven't worked on the C compiler in a while and CBL has pretty much replaced it. I haven't actually added formatting and click handlers to its Text API, but it wouldn't be hard to do.

I guess for the time being, you could embed the tellraw command directly:

#include <mclib.h>
CMD(tellraw @s {"text":"Test","color":"yellow","clickEvent":{"action":"run_command","value":"/function namespace:sub_say_hello"}});

Note that the fully qualified name (i.e. with namespace) of the function needs to be given for this to work.

sarahmence commented 4 years ago

Thanks for replying so quickly! I would rather not switch my entire project from C to CBL, as I have around 1000 lines of C in it currently, and I have a very productive C workflow set up that would not work with CBL. Is there any way you could fast-track fixing this bug? I would be happy to try to fix it myself, but I would need some pointers as to what source files deal with text_set_click_run.

Thanks!

simon816 commented 4 years ago

Well I'm impressed you've been able to use the C compiler in its current state. I've kind of neglected it since working on CBL (the main motivating factor for creating CBL was because there were just too many issues an limitations with both the C language and the compiler).

I may be able to do a quick bugfix tomorrow if it's not too involved. The source for text_set_click_run is implemented in Python: https://github.com/simon816/Command-Block-Assembly/blob/afd53530d81dbede2727b59e755d9afbc39f6658/c_comp/lib/text.py#L27-L34 I haven't documented the compiler internals, so it would take longer to explain the crazy inner workings than to fix it! You could get away with just patching it to something like this:

'value': '/function namespace:sub_%s' % name