SethBling / cbscript

CBScript for Minecraft
1.13k stars 28 forks source link

'Entity' not in the scriptlex token list #38

Open JhonTabio opened 1 month ago

JhonTabio commented 1 month ago

I'd like to preface first that I just started looking into CBScript the other day so I not well-versed in this project at all. Alongside that, I am using this as a means to learn Datapacks too, so a 2-in-1 noobie when it comes to this project.

So, I started a project to learn both CBScript and Datapacks overall. I'm using scripts provided in the repo as a somewhat guide on how to use CBS and couldn't help but notice that there were only examples for blocks when it came to loot tables (I think this means when mining a block, the loot table of what's dropped? Correct me if I am wrong).

Turns out there was no direct support for anything but "block" in the parser. Below are the changes I made:

Within scriptparse.py

def p_loot_table_type(p):
    '''loot_table_type : block
                        |   entity''' # Added this for yaac
    types = {
        'block': 'blocks',
        'entity': 'entities', # Added this for type translation
    }
    if p[1] not in types:
        raise SyntaxError(f'Invalid loot table type "{p[1]}" at line {p.lineno(1)}.')

    p[0] = types[p[1]]

And one small addition to scriptlex.py

keywords = (
    'for', 'dir', 'desc', 'in', 'end', 'not', 'and', 'or', 'to', 'by', 'import',
    'name', 'with', 'macros',
    'at', 'as', 'on', 'facing', 'rotated', 'align', 'here', 'the_end', 'the_nether', 'overworld',
    'move', 'create', 'tell', 'title', 'subtitle', 'actionbar',
    'reset', 'clock', 'function', 'if', 'unless', 'then', 'do', 'else', 'switch', 'case', 'default',
    'return', 'while', 'macro', 'block', 'block_data', 'block_tag', 'entity_tag', 'item_tag', 'define', 'array', 'remove', 'success', 'result',
    'return', 'while', 'macro', 'entity' # Right over here, 'block', 'block_data', 'block_tag', 'entity_tag', 'item_tag', 'define', 'array', 'remove', 'success', 'result',
    'shaped', 'recipe', 'keys', 'eyes', 'feet', 'advancement', 'loot_table', 'predicate', "item_modifier",
    'push', 'pop', 'true', 'false',
)

So now you should be able to define an entity loot table as follows:

loot_table entity minecraft:entity_type_here {
    # All the JSON goodies go here
}

Now I'd like to say again how I am quite new to all of this, so maybe there was a properly defined method that I just hadn't known. But considering that the parser's tokens didn't even include any form of entity (aside from entity tags, which I don't think help in this case[?]), that leads me to believe that it was not supported. Another thing to consider is this was not implemented for a reason, but this band-aid patch seems to work for all my needs and brings up no issues thus far.