How can set and get custom data of entities?

Hello!

How can I get and set custom parameters of a mob (i. e. properties, which I use in my plugin) ?

As far as I understand, I have to create my own key class, which needs to implement the Key interface or one of its descendants.

Let’s say I want to store a mutable integer value in my mobs. Is it correct that I have to use Value interface for this?

And is there an implementation of this interface already, or do I have to implement it myself?

Thanks in advance

Dmitri Pisarenko

Found out that a key can be created using

KeyFactory.makeSingleKey( Integer::class.java, MutableBoundedValue::class.java, DataQuery.of("MyInformation") )

Now I need to find a way to make my mobs support it.

Just create a custom DataManipulator containing that Key and add it to any entity you want to support.

1 Like

Thanks for your answer.

What’s the best place to learn, how to do it? This or something else?

That Docs Page is for writing Sponge-DataManipulators.
For creating custom data there is no docs page yet.
See Data API Docs · Issue #335 · SpongePowered/SpongeDocs · GitHub

gabizou provided an example in that issue:

Also if you search the forum a bit you might find more examples.

2 Likes

I would say you could poke around in my code, with this being the class it’s trying to save, and with the addition of this in the main plugin file;

Sponge.getDataManager().register(ShopReferenceData.class, ImmutableShopReferenceData.class, new ShopReferenceDataManipulatorBuilder());
Sponge.getDataManager().registerBuilder(ShopReference.class, new ShopReferenceBuilder());

… However, while it works in game, it has problems persisting after reloading the server :stuck_out_tongue: . Best of luck, and if you find anything that works, let me know!

1 Like

@Faithcaio, @MichaelFedora, thanks for your recommendations.

I have created the infrastructure for the values I want to store.

Then I register these item during pre-init event of the plugin:

open class AlmaEconSimPlugin(val cmdFactory:ICommandFactory = CommandFactory()) {
    @Inject lateinit var logger: Logger
    @Inject lateinit var game: Game

    @Listener
    fun onPreInit(event: GamePreInitializationEvent) {
        registerSimData()
        [...]
    }

    [...]

    protected fun registerSimData() {
        Sponge.getDataManager().register(SimData::class.java, ImmutableSimData::class.java, 
            SimDataManipulatorBuilder())
    }

At another place in the code I create a villager and want to set the initial value of DAYS_SINCE_LAST_MEAL to 0.

if (villager.supports(SimKeys.DAYS_SINCE_LAST_MEAL)) {
    val simData = villager.getOrCreate(SimData::class.java).get()
    simData.daysSinceLastMeal().set(0)
}

How can I now set the value of that property (DAYS_SINCE_LAST_MEAL) to some value? Like I did above (simData.daysSinceLastMeal().set(0)) or differently?

Thanks

Dmitri

Kotlin, eh? Can’t help you debug, but I do notice you forgot to register the builder. See the second line of my snippet up there :wink:

Here’s another snippet

ShopReferenceDataManipulatorBuilder builder = (ShopReferenceDataManipulatorBuilder) Sponge.getDataManager().getManipulatorBuilder(ShopReferenceData.class).get();
ShopReferenceData refData = builder.createFrom(new ShopReference(data.ownerId.orElse(null), instance));
DataTransactionResult dtr = sign.offer(refData);
1 Like