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);
}
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);
}
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());
}
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;
}
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);
}
Aggregations