Search in sources :

Example 1 with DataManager

use of org.spongepowered.api.data.DataManager in project SpongeCommon by SpongePowered.

the class NBTTranslationTest method testContainerToNBT.

@Test
public void testContainerToNBT() {
    DataManager service = Mockito.mock(DataManager.class);
    DataBuilder<FakeSerializable> builder = new FakeBuilder();
    when(service.getBuilder(FakeSerializable.class)).thenReturn(Optional.of(builder));
    DataContainer container = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
    container.set(DataQuery.of("foo"), "bar");
    FakeSerializable temp = new FakeSerializable("bar", 7, 10.0D, "nested");
    container.set(DataQuery.of("myFake"), temp);
    NBTTagCompound compound = NbtTranslator.getInstance().translateData(container);
    DataView translatedContainer = NbtTranslator.getInstance().translateFrom(compound);
    assertEquals(container, translatedContainer);
}
Also used : DataView(org.spongepowered.api.data.DataView) DataContainer(org.spongepowered.api.data.DataContainer) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) DataManager(org.spongepowered.api.data.DataManager) Test(org.junit.Test)

Example 2 with DataManager

use of org.spongepowered.api.data.DataManager in project SpongeCommon by SpongePowered.

the class MyHomes method onDataRegistration.

@Listener
public void onDataRegistration(GameRegistryEvent.Register<DataRegistration<?, ?>> event) {
    this.logger.info("onDataRegistration");
    final DataManager dataManager = Sponge.getDataManager();
    // Home stuff
    dataManager.registerBuilder(Home.class, new HomeBuilder());
    dataManager.registerContentUpdater(Home.class, new HomeBuilder.NameUpdater());
    dataManager.registerContentUpdater(HomeData.class, new HomeDataBuilder.HomesUpdater());
    this.homeDataRegistration = DataRegistration.builder().dataClass(HomeData.class).immutableClass(ImmutableHomeData.class).dataImplementation(HomeDataImpl.class).immutableImplementation(ImmutableHomeDataImpl.class).builder(new HomeDataBuilder()).dataName("Home Data").manipulatorId("home").buildAndRegister(this.container);
    // Friends stuff
    this.friendsDataRegistration = DataRegistration.builder().dataClass(FriendsData.class).immutableClass(ImmutableFriendsData.class).dataImplementation(FriendsDataImpl.class).immutableImplementation(ImmutableFriendsDataImpl.class).builder(new FriendsDataBuilder()).dataName("Friends Data").manipulatorId("friends").buildAndRegister(this.container);
}
Also used : ImmutableFriendsData(org.spongepowered.test.myhomes.data.friends.ImmutableFriendsData) ImmutableFriendsDataImpl(org.spongepowered.test.myhomes.data.friends.impl.ImmutableFriendsDataImpl) ImmutableHomeDataImpl(org.spongepowered.test.myhomes.data.home.impl.ImmutableHomeDataImpl) HomeDataBuilder(org.spongepowered.test.myhomes.data.home.impl.HomeDataBuilder) FriendsDataBuilder(org.spongepowered.test.myhomes.data.friends.impl.FriendsDataBuilder) ImmutableHomeData(org.spongepowered.test.myhomes.data.home.ImmutableHomeData) DataManager(org.spongepowered.api.data.DataManager) HomeBuilder(org.spongepowered.test.myhomes.data.home.impl.HomeBuilder) Listener(org.spongepowered.api.event.Listener)

Example 3 with DataManager

use of org.spongepowered.api.data.DataManager in project SpongeCommon by SpongePowered.

the class MemoryDataView method setCollection.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void setCollection(String key, Collection<?> value) {
    ImmutableList.Builder<Object> builder = ImmutableList.builder();
    @Nullable DataManager manager;
    try {
        manager = Sponge.getDataManager();
    } catch (Exception e) {
        manager = null;
    }
    for (Object object : value) {
        if (object instanceof DataSerializable) {
            builder.add(((DataSerializable) object).toContainer());
        } else if (object instanceof DataView) {
            if (this.safety == SafetyMode.ALL_DATA_CLONED || this.safety == SafetyMode.CLONED_ON_SET) {
                MemoryDataView view = new MemoryDataContainer(this.safety);
                DataView internalView = (DataView) object;
                for (Map.Entry<DataQuery, Object> entry : internalView.getValues(false).entrySet()) {
                    view.set(entry.getKey(), entry.getValue());
                }
                builder.add(view);
            } else {
                builder.add(object);
            }
        } else if (object instanceof CatalogType) {
            builder.add(((CatalogType) object).getId());
        } else if (object instanceof Map) {
            builder.add(ensureSerialization((Map) object));
        } else if (object instanceof Collection) {
            builder.add(ensureSerialization((Collection) object));
        } else {
            if (manager != null) {
                final Optional<? extends DataTranslator<?>> translatorOptional = manager.getTranslator(object.getClass());
                if (translatorOptional.isPresent()) {
                    DataTranslator translator = translatorOptional.get();
                    final DataContainer container = translator.translate(object);
                    checkArgument(!container.equals(this), "Cannot insert self-referencing Objects!");
                    builder.add(container);
                } else {
                    builder.add(object);
                }
            } else {
                builder.add(object);
            }
        }
    }
    this.map.put(key, builder.build());
}
Also used : DataTranslator(org.spongepowered.api.data.persistence.DataTranslator) ImmutableList(com.google.common.collect.ImmutableList) DataManager(org.spongepowered.api.data.DataManager) DataSerializable(org.spongepowered.api.data.DataSerializable) DataView(org.spongepowered.api.data.DataView) CatalogType(org.spongepowered.api.CatalogType) DataContainer(org.spongepowered.api.data.DataContainer) Collection(java.util.Collection) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Nullable(javax.annotation.Nullable)

Example 4 with DataManager

use of org.spongepowered.api.data.DataManager in project SpongeCommon by SpongePowered.

the class MemoryDataView method set.

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public DataView set(DataQuery path, Object value) {
    checkNotNull(path, "path");
    checkNotNull(value, "value");
    checkState(this.container != null);
    @Nullable DataManager manager;
    // TODO: this call to getDataManager each set can be cleaned up
    try {
        manager = Sponge.getDataManager();
    } catch (Exception e) {
        manager = null;
    }
    List<String> parts = path.getParts();
    String key = parts.get(0);
    if (parts.size() > 1) {
        DataQuery subQuery = of(key);
        Optional<DataView> subViewOptional = this.getUnsafeView(subQuery);
        DataView subView;
        if (!subViewOptional.isPresent()) {
            this.createView(subQuery);
            subView = (DataView) this.map.get(key);
        } else {
            subView = subViewOptional.get();
        }
        subView.set(path.popFirst(), value);
        return this;
    }
    if (value instanceof DataView) {
        checkArgument(value != this, "Cannot set a DataView to itself.");
        // always have to copy a data view to avoid overwriting existing
        // views and to set the interior path correctly.
        copyDataView(path, (DataView) value);
    } else if (value instanceof DataSerializable) {
        DataContainer valueContainer = ((DataSerializable) value).toContainer();
        checkArgument(!(valueContainer).equals(this), "Cannot insert self-referencing DataSerializable");
        // see above for why this is copied
        copyDataView(path, valueContainer);
    } else if (value instanceof CatalogType) {
        return set(path, ((CatalogType) value).getId());
    } else if (manager != null && manager.getTranslator(value.getClass()).isPresent()) {
        DataTranslator serializer = manager.getTranslator(value.getClass()).get();
        final DataContainer container = serializer.translate(value);
        checkArgument(!container.equals(this), "Cannot insert self-referencing Objects!");
        // see above for why this is copied
        copyDataView(path, container);
    } else if (value instanceof Collection) {
        setCollection(key, (Collection) value);
    } else if (value instanceof Map) {
        setMap(key, (Map) value);
    } else if (value.getClass().isArray()) {
        if (this.safety == SafetyMode.ALL_DATA_CLONED || this.safety == SafetyMode.CLONED_ON_SET) {
            if (value instanceof byte[]) {
                this.map.put(key, ArrayUtils.clone((byte[]) value));
            } else if (value instanceof short[]) {
                this.map.put(key, ArrayUtils.clone((short[]) value));
            } else if (value instanceof int[]) {
                this.map.put(key, ArrayUtils.clone((int[]) value));
            } else if (value instanceof long[]) {
                this.map.put(key, ArrayUtils.clone((long[]) value));
            } else if (value instanceof float[]) {
                this.map.put(key, ArrayUtils.clone((float[]) value));
            } else if (value instanceof double[]) {
                this.map.put(key, ArrayUtils.clone((double[]) value));
            } else if (value instanceof boolean[]) {
                this.map.put(key, ArrayUtils.clone((boolean[]) value));
            } else {
                this.map.put(key, ArrayUtils.clone((Object[]) value));
            }
        } else {
            this.map.put(key, value);
        }
    } else {
        this.map.put(key, value);
    }
    return this;
}
Also used : DataTranslator(org.spongepowered.api.data.persistence.DataTranslator) DataManager(org.spongepowered.api.data.DataManager) DataSerializable(org.spongepowered.api.data.DataSerializable) DataView(org.spongepowered.api.data.DataView) DataContainer(org.spongepowered.api.data.DataContainer) CatalogType(org.spongepowered.api.CatalogType) Collection(java.util.Collection) DataQuery(org.spongepowered.api.data.DataQuery) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Nullable(javax.annotation.Nullable)

Example 5 with DataManager

use of org.spongepowered.api.data.DataManager in project TotalEconomy by Erigitic.

the class TotalEconomy method createAndRegisterData.

/**
 * Create and register custom data
 */
private void createAndRegisterData() {
    DataManager dm = Sponge.getDataManager();
    dm.registerBuilder(Shop.class, new Shop.Builder());
    dm.registerBuilder(ShopItem.class, new ShopItem.Builder());
    dm.registerBuilder(PlayerShopInfo.class, new PlayerShopInfo.Builder());
    DataRegistration.builder().dataClass(ShopData.class).immutableClass(ImmutableShopData.class).builder(new ShopData.Builder()).manipulatorId("shop").dataName("shop").buildAndRegister(pluginContainer);
    DataRegistration.builder().dataClass(ShopItemData.class).immutableClass(ImmutableShopItemData.class).builder(new ShopItemData.Builder()).manipulatorId("shopitem").dataName("shopitem").buildAndRegister(pluginContainer);
    DataRegistration.builder().dataClass(PlayerShopInfoData.class).immutableClass(ImmutablePlayerShopInfoData.class).builder(new PlayerShopInfoData.Builder()).manipulatorId("playershopinfo").dataName("playershopinfo").buildAndRegister(pluginContainer);
}
Also used : DataManager(org.spongepowered.api.data.DataManager)

Aggregations

DataManager (org.spongepowered.api.data.DataManager)6 DataContainer (org.spongepowered.api.data.DataContainer)3 DataView (org.spongepowered.api.data.DataView)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 Collection (java.util.Collection)2 Map (java.util.Map)2 Nullable (javax.annotation.Nullable)2 CatalogType (org.spongepowered.api.CatalogType)2 DataSerializable (org.spongepowered.api.data.DataSerializable)2 DataTranslator (org.spongepowered.api.data.persistence.DataTranslator)2 ImmutableList (com.google.common.collect.ImmutableList)1 Optional (java.util.Optional)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 ObjectMappingException (ninja.leaping.configurate.objectmapping.ObjectMappingException)1 Test (org.junit.Test)1 DataQuery (org.spongepowered.api.data.DataQuery)1 DataBuilder (org.spongepowered.api.data.persistence.DataBuilder)1 Listener (org.spongepowered.api.event.Listener)1 ImmutableFriendsData (org.spongepowered.test.myhomes.data.friends.ImmutableFriendsData)1 FriendsDataBuilder (org.spongepowered.test.myhomes.data.friends.impl.FriendsDataBuilder)1