DataManipulatorGenerator

Did you register it? And then offer() it?

Register where? I couldn’t find documentation regarding where to register my keys.

In pre-initialization, you must call DataManager#register.

Ah, duh. Thanks pie.

Actually, do you have any example working code with custom data registering? I can’t figure out how to do it properly.

game.getDataManager().register(TestData.class, TestData.Immutable.class, new TestData.Builder());
1 Like

I’ll note that the generated code doesn’t expose the builder constructor.

Additionally, after properly registering my data, it still gets rejected for whatever reason.

A single log message isn’t extremely helpful - can you provide a bit more information (registration code, data insertion code, DMG config)?

And protected is also package access.

Anyone trying to use the example code and failing - Just noticed an incorrect bracket, updated the OP to fix that.

I’d totally send you the code right now but I’m an idiot and haven’t fixed the remote desktop on my PC at home yet. :stuck_out_tongue:

But here’s just a summary of what I’ve done.
I’ve generated an Inventory dstamanipulator with keys and such. I register it with the code snippet you posted with my.class instead of the one you sent. When I offer it to a player, the player automatically fails it for some reason. I’m not sure why.

fields = [
	{
		type=Inventory
		name=spicyStorage
		key {
			name=SPICY_STORAGE
			display-name="Spicy Storage"
		}
	}
]
class=DespiceData
package=io.lokiraut.despice
plugin-id=despice

package io.lokiraut.despice.data;

import org.spongepowered.api.Sponge;
import org.spongepowered.api.data.DataContainer;
import org.spongepowered.api.data.DataHolder;
import org.spongepowered.api.data.DataView;
import org.spongepowered.api.data.manipulator.DataManipulatorBuilder;
import org.spongepowered.api.data.manipulator.immutable.common.AbstractImmutableData;
import org.spongepowered.api.data.manipulator.mutable.common.AbstractData;
import org.spongepowered.api.data.merge.MergeFunction;
import org.spongepowered.api.data.persistence.AbstractDataBuilder;
import org.spongepowered.api.data.persistence.InvalidDataException;
import org.spongepowered.api.data.value.immutable.ImmutableValue;
import org.spongepowered.api.data.value.mutable.Value;
import org.spongepowered.api.item.inventory.Inventory;

import javax.annotation.Generated;
import java.util.Optional;

@Generated(value = "flavor.pie.generator.data.DataManipulatorGenerator", date = "2017-02-27T03:27:32.565Z")
public class DespiceData extends AbstractData<DespiceData, DespiceData.Immutable> {

    private Inventory spicyStorage;

    {
        registerGettersAndSetters();
    }

    DespiceData() {
    }

    DespiceData(Inventory spicyStorage) {
        this.spicyStorage = spicyStorage;
    }

    @Override
    protected void registerGettersAndSetters() {
        registerFieldGetter(DespiceKeys.SPICY_STORAGE, this::getSpicyStorage);
        registerFieldSetter(DespiceKeys.SPICY_STORAGE, this::setSpicyStorage);
        registerKeyValue(DespiceKeys.SPICY_STORAGE, this::spicyStorage);
    }

    public Inventory getSpicyStorage() {
        return spicyStorage;
    }

    public void setSpicyStorage(Inventory spicyStorage) {
        this.spicyStorage = spicyStorage;
    }

    public Value<Inventory> spicyStorage() {
        return Sponge.getRegistry().getValueFactory().createValue(DespiceKeys.SPICY_STORAGE, spicyStorage);
    }

    @Override
    public Optional<DespiceData> fill(DataHolder dataHolder, MergeFunction overlap) {
        dataHolder.get(DespiceData.class).ifPresent(that -> {
                DespiceData data = overlap.merge(this, that);
                this.spicyStorage = data.spicyStorage;
        });
        return Optional.of(this);
    }

    @Override
    public Optional<DespiceData> from(DataContainer container) {
        return from((DataView) container);
    }

    public Optional<DespiceData> from(DataView container) {
        container.getObject(DespiceKeys.SPICY_STORAGE.getQuery(), Inventory.class).ifPresent(v -> spicyStorage = v);
        return Optional.of(this);
    }

    @Override
    public DespiceData copy() {
        return new DespiceData(spicyStorage);
    }

    @Override
    public Immutable asImmutable() {
        return new Immutable(spicyStorage);
    }

    @Override
    public int getContentVersion() {
        return 1;
    }

    @Override
    public DataContainer toContainer() {
        return super.toContainer()
                .set(DespiceKeys.SPICY_STORAGE.getQuery(), spicyStorage);
    }

    @Generated(value = "flavor.pie.generator.data.DataManipulatorGenerator", date = "2017-02-27T03:27:32.587Z")
    public static class Immutable extends AbstractImmutableData<Immutable, DespiceData> {

        private Inventory spicyStorage;
        {
            registerGetters();
        }

        Immutable() {
        }

        Immutable(Inventory spicyStorage) {
            this.spicyStorage = spicyStorage;
        }

        @Override
        protected void registerGetters() {
            registerFieldGetter(DespiceKeys.SPICY_STORAGE, this::getSpicyStorage);
            registerKeyValue(DespiceKeys.SPICY_STORAGE, this::spicyStorage);
        }

        public Inventory getSpicyStorage() {
            return spicyStorage;
        }

        public ImmutableValue<Inventory> spicyStorage() {
            return Sponge.getRegistry().getValueFactory().createValue(DespiceKeys.SPICY_STORAGE, spicyStorage).asImmutable();
        }

        @Override
        public DespiceData asMutable() {
            return new DespiceData(spicyStorage);
        }

        @Override
        public int getContentVersion() {
            return 1;
        }

        @Override
        public DataContainer toContainer() {
            return super.toContainer()
                    .set(DespiceKeys.SPICY_STORAGE.getQuery(), spicyStorage);
        }

    }

    @Generated(value = "flavor.pie.generator.data.DataManipulatorGenerator", date = "2017-02-27T03:27:32.591Z")
    public static class Builder extends AbstractDataBuilder<DespiceData> implements DataManipulatorBuilder<DespiceData, Immutable> {

        public Builder() {
            super(DespiceData.class, 1);
        }

        @Override
        public DespiceData create() {
            return new DespiceData();
        }

        @Override
        public Optional<DespiceData> createFrom(DataHolder dataHolder) {
            return create().fill(dataHolder);
        }

        @Override
        protected Optional<DespiceData> buildContent(DataView container) throws InvalidDataException {
            return create().from(container);
        }

    }
}

package io.lokiraut.despice.data;

import com.google.common.reflect.TypeToken;
import org.spongepowered.api.data.DataQuery;
import org.spongepowered.api.data.key.Key;
import org.spongepowered.api.data.key.KeyFactory;
import org.spongepowered.api.data.value.mutable.Value;
import org.spongepowered.api.item.inventory.Inventory;

import javax.annotation.Generated;

@Generated(value = "flavor.pie.generator.data.DataManipulatorGenerator", date = "2017-02-27T03:27:32.592Z")
public class DespiceKeys {

    private DespiceKeys() {}

    public final static Key<Value<Inventory>> SPICY_STORAGE;
    static {
        TypeToken<Inventory> optionalInventoryToken = new TypeToken<Inventory>(){};
        TypeToken<Value<Inventory>> valueOptionalInventoryToken = new TypeToken<Value<Inventory>>(){};
        SPICY_STORAGE = KeyFactory.makeSingleKey(optionalInventoryToken, valueOptionalInventoryToken, DataQuery.of("spicystorage"), "despice:spicystorage", "spicystorage");
    }
}

Sponge.getDataManager().register(DespiceData.class,DespiceData.Immutable.class,new DespiceData.Builder());

You’ve got the single registration line, but[quote=“pie_flavor, post:10, topic:17080”]
data insertion code
[/quote]

meaning the part where you offer it?

For instance, it’s a caveat of the current implementation that the entire data manipulator must be offered to the player before Keys can be used individually. So if you were using a Key, that would be the problem.

Also, I mentioned that the name in type should be fully qualified; this will auto-import it and such.

1 Like

Oh, I had no idea you had to offer the data manipulator lol

For instance, it’s a caveat of the current implementation that the entire data manipulator must be offered to the player before Keys can be used individually.

Motherf… !

That certainly explains a lot of headaches on the long bumpy road.

2 Likes

Yeah, it confused the shit out of me for a while too.

Note that you still have to offer() after getOrCreate(), even if you don’t modify it; it literally just creates it without setting it.

1 Like

We just need a Data API FAQ or troubleshooting section in the docs.

1 Like

Could you provide a full example implementation? It would be immensely helpful.

Thanks.

any update for api 7.2.0 ? would be mucho appreciated <3

Got a lot on my plate. I’ll try to get around to it eventually.