sandstone-mc / sandstone

Sandstone | Next Generation Framework for Minecraft
https://sandstone.dev/
MIT License
173 stars 16 forks source link

Method for checking if something is in a tag #116

Closed GrantGryczan closed 1 year ago

GrantGryczan commented 2 years ago

tag.has(value) would be a nice alias for this:

tag.values.some(tagValue => (
    (
        typeof tagValue !== 'string'
        && 'id' in tagValue
            ? tagValue.id
            : tagValue
    ).toString() === value.toString()
))

value could be a string, whatever object type the tag is made of (e.g. an MCFunction), or the same type of tag (e.g. a function tag).

Here is an example to demonstrate that this method would be useful in general:

const someAbstraction = () => {
    MCFunction('function', () => {}, {
        tags: [tag]
    });
    // or
    tag.add(
        MCFunction('function', () => {})
    );
};

If someAbstraction is called multiple times, there will necessarily still only be one instance of the created function in-game, but it will be added to tag multiple times regardless. It is necessary to be able to add the same function to one tag multiple times, so preventing that in general is not the solution here. Instead, because it isn't desirable in most cases that calling an abstraction multiple times would add a function it requires to a tag multiple times, it would be better to do this:

const someAbstraction = () => {
    const mcFunction = MCFunction('function', () => {});

    if (!tag.has(mcFunction)) {
        tag.add(mcFunction);
    }
};

An abstraction happening to need a function that needs to be in a function tag is a general use case, and tag.has would make that use case much cleaner to implement, because currently it requires the ugly monster at the top of this post to check if something is in a tag.