EngineHub / CommandHelper

Rapid scripting and command aliases for Minecraft owners
https://methodscript.com
Other
119 stars 71 forks source link

Add support for custom tags in item meta #1366

Closed PseudoKnight closed 1 year ago

PseudoKnight commented 1 year ago

This is a proposal solution for adding custom tags from Bukkit's PersistentDataContainers in item meta. This is for preserving tags added by other plugins as well as allowing scripters more control over their own custom data stored in items.

I'm using array objects to store NBT types along with the values. Due to API limitations we're not able to tell what type of data is under each key, so all types are checked, ordered by an estimation of frequency of use. This can be optimized later, but performance actually seemed adequate and doesn't rely on reflection.

API naming worth discussing is "type", "value", and "tags". We could also go for something like "custom" instead of "tags", "data" instead of "value"

Item array example:

{
    "name": "IRON_PICKAXE",
    "meta": {
        "tags": {
            "commandhelper:mytag": {
                "type": "STRING",
                "value": "something or other",
            },
            "mcmmo:super_ability_boosted": {
                "type": "INTEGER",
                "value": 0,
            }
        }
    }
}

Explosive pick example:

bind('block_break', null, null, @event) {
    @item = pinv(player(), null);
    if(@item 
    && string_ends_with(@item['name'], 'PICKAXE')
    && @item['meta'] 
    && @item['meta']['tags'] 
    && array_index_exists(@item['meta']['tags'], 'commandhelper:explosive_pick')) {
        @size = @item['meta']['tags']['commandhelper:explosive_pick']['value'];
        explosion(@event['location'], @size, false, false, puuid());
    }
}

Creating explosive pick:

pgive_item(array(
    name: 'DIAMOND_PICKAXE',
    meta: array(
        tags: array(
            'explosive_pick': array( // commandhelper namespace is added by default
                type: 'BYTE', 
                value: 1
            )
        )
    )
))