How to create a Key.builder()?


[Google trad]
I would like to know if with Key.builder() one can create a key which stores in one block a BlockType of another block or a string like value?
I can not find an example to do it.
Thank you


j’aimerais savoir si avec Key.builder() on peut créer une clé qui stocke dans un bloc un BlockType d’un autre bloc ou une chaine de caractère comme valeur ?

Je ne trouve pas d'exemple pour le faire.



Here is a good example of how to make your own custom keys. Its by @pie_flavor


[Google trad]
I can have an example please, I really can not do it.

Je peux avoir un exemple SVP, je n’y arrive vraiment pas.


That was an example. This is the file with the keys.

[Google translate]
chapeau était un exemple. Ceci est le fichier avec les clés.


[Google translate]
I saw this file, but the key does not add:

J’ai vu ce fichier, mais la clé ne s’ajoute pas:

        BlockState bloc = player.getLocation().getBlock();
        Key<Value<String>> DATA = Key.builder()
                .name("Block Origin ID")
                .query(DataQuery.of('.', "block_origin_id"))
        bloc.with(DATA, "test");

        if(bloc.get(DATA).isPresent()) {
            Tool.getLogger().info("DATA => " + player.get(DATA).get());


Its sadly not that simple. You need a mutableDataManipulator and an ImmutableDataManipulator as well as a builder and you need to register the 3 as well as the key to get it to work.

This is the french version of the sponge docs on the page that tells you how it works.


Unfortunately, you can’t add custom data to a BlockState. You can only add custom data to ItemStack, Entity, and TileEntity.


[Google translate]
If i had a key to a personalized chest because the chest as an getTileEntity, why i lost the key when i get back the ItemStack’s chest Because when I click on the chest, it has the key, when I break it has the key, but the ItemStack no longer.

Si j’ajoute une key personnalisé a un coffre car le coffre dispose d’une getTileEntity, pourquoi il perd la key quand je récupère du coffre ItemStack ? Car quand je clic sur le coffre il a la key, quand je le casse il a la key, mais l’ItemStack ne la plus.


This is due to the fact that the instance of the item stack you originally had is not the same as the one you picked up.

You need to make your custom key support both tile entity and item stack. After that use events to detect if/when an item stack with your key turn into a tile entity or when a tile entity turn to a Item entity. And then apply the data to the newly created instance