/** * Gets the {@link DataManager} instance to register * {@link DataSerializable}s, and get the related {@link DataBuilder}s. * * @return The serialization service */ default DataManager getDataManager() { return Sponge.getDataManager(); }
@Override public <T> Optional<List<T>> getObjectList(DataQuery path, Class<T> objectClass) { return getViewList(path).flatMap(viewList -> Sponge.getDataManager().getTranslator(objectClass).map(serializer -> viewList.stream() .map(serializer::translate) .collect(Collectors.toList()) ) ); }
private Text getText(Object textView) { if (!(textView instanceof DataView)) { throw new InvalidDataException("Expected DataView"); } return Sponge.getDataManager().deserialize(Text.class, (DataView) textView).get(); }
@Override public <T> Optional<T> getObject(DataQuery path, Class<T> objectClass) { return getView(path).flatMap(view -> Sponge.getDataManager().getTranslator(objectClass) .flatMap(serializer -> Optional.of(serializer.translate(view))) ); }
@SuppressWarnings("unchecked") @Override public <T extends DataSerializable> Optional<List<T>> getSerializableList(DataQuery path, Class<T> clazz) { checkNotNull(path, "path"); checkNotNull(clazz, "clazz"); return Stream.<Supplier<Optional<List<T>>>>of( () -> { if (clazz.isAssignableFrom(CatalogType.class)) { return (Optional<List<T>>) (Optional<?>) getCatalogTypeList(path, (Class<? extends CatalogType>) clazz); } return Optional.empty(); }, () -> getViewList(path).flatMap(list -> Sponge.getDataManager().getBuilder(clazz).map(builder -> list.stream() .map(builder::build) .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toList()) ) ) ) .map(Supplier::get) .filter(Optional::isPresent) .map(Optional::get) .findFirst(); }
@SuppressWarnings("unchecked") @Override public <T extends DataSerializable> Optional<T> getSerializable(DataQuery path, Class<T> clazz) { checkNotNull(path, "path"); checkNotNull(clazz, "clazz"); if (clazz.isAssignableFrom(CatalogType.class)) { final Optional<T> catalog = (Optional<T>) getCatalogType(path, ((Class<? extends CatalogType>) clazz)); if (catalog.isPresent()) { return catalog; } } return getUnsafeView(path).flatMap(view -> Sponge.getDataManager().getBuilder(clazz) .flatMap(builder -> builder.build(view)) ); }
manager = Sponge.getDataManager(); } catch (Exception e) { manager = null;
@Override public final Optional<T> build(DataView container) throws InvalidDataException { if (container.contains(Queries.CONTENT_VERSION)) { final int contentVersion = container.getInt(Queries.CONTENT_VERSION).get(); if (contentVersion < this.supportedVersion) { Optional<DataContentUpdater> updater = Sponge.getDataManager().getWrappedContentUpdater(this.requiredClass, contentVersion, this.supportedVersion); if (!updater.isPresent()) { throw new InvalidDataException("Could not get an updater for " + this.requiredClass.getName() + " data from the version: " + contentVersion + " to " + this.supportedVersion + ". Please notify the SpongePowered developers of this issue!"); } container = updater.get().update(container); } } try { return buildContent(container); } catch (Exception e) { throw new InvalidDataException("Could not deserialize something correctly, likely due to bad type data.", e); } } }
@Override public Text deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { StringWriter writer = new StringWriter(); GsonConfigurationLoader gsonLoader = GsonConfigurationLoader.builder() .setIndent(0) .setSink(() -> new BufferedWriter(writer)) .setHeaderMode(HeaderMode.NONE) .build(); try { gsonLoader.save(value); } catch (IOException e) { throw new ObjectMappingException(e); } return Sponge.getDataManager().deserialize(Text.class, DataContainer.createNew().set(Queries.JSON, writer.toString())).get(); }
manager = Sponge.getDataManager(); } catch (Exception e) { manager = null;
@Override public <T> Optional<List<T>> getObjectList(DataQuery path, Class<T> objectClass) { return getViewList(path).flatMap(viewList -> Sponge.getDataManager().getTranslator(objectClass).map(serializer -> viewList.stream() .map(serializer::translate) .collect(Collectors.toList()) ) ); }
private Text getText(Object textView) { if (!(textView instanceof DataView)) { throw new InvalidDataException("Expected DataView"); } return Sponge.getDataManager().deserialize(Text.class, (DataView) textView).get(); }
@Override public <T> Optional<T> getObject(DataQuery path, Class<T> objectClass) { return getView(path).flatMap(view -> Sponge.getDataManager().getTranslator(objectClass) .flatMap(serializer -> Optional.of(serializer.translate(view))) ); }
@SuppressWarnings("unchecked") @Override public <T extends DataSerializable> Optional<T> getSerializable(DataQuery path, Class<T> clazz) { checkNotNull(path, "path"); checkNotNull(clazz, "clazz"); if (clazz.isAssignableFrom(CatalogType.class)) { final Optional<T> catalog = (Optional<T>) getCatalogType(path, ((Class<? extends CatalogType>) clazz)); if (catalog.isPresent()) { return catalog; } } return getUnsafeView(path).flatMap(view -> Sponge.getDataManager().getBuilder(clazz) .flatMap(builder -> builder.build(view)) ); }
@SuppressWarnings("unchecked") @Override public <T extends DataSerializable> Optional<List<T>> getSerializableList(DataQuery path, Class<T> clazz) { checkNotNull(path, "path"); checkNotNull(clazz, "clazz"); return Stream.<Supplier<Optional<List<T>>>>of( () -> { if (clazz.isAssignableFrom(CatalogType.class)) { return (Optional<List<T>>) (Optional<?>) getCatalogTypeList(path, (Class<? extends CatalogType>) clazz); } return Optional.empty(); }, () -> getViewList(path).flatMap(list -> Sponge.getDataManager().getBuilder(clazz).map(builder -> list.stream() .map(builder::build) .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toList()) ) ) ) .map(Supplier::get) .filter(Optional::isPresent) .map(Optional::get) .findFirst(); }
public static void registerICType(ICType<? extends IC> ic) { registeredICTypes.add(ic); if (ic.getFactory() instanceof SerializedICFactory) { Sponge.getDataManager().registerBuilder(((SerializedICFactory) ic.getFactory()).getRequiredClass(), ((SerializedICFactory) ic.getFactory())); } }
@Override public final Optional<T> build(DataView container) throws InvalidDataException { if (container.contains(Queries.CONTENT_VERSION)) { final int contentVersion = container.getInt(Queries.CONTENT_VERSION).get(); if (contentVersion < this.supportedVersion) { Optional<DataContentUpdater> updater = Sponge.getDataManager().getWrappedContentUpdater(this.requiredClass, contentVersion, this.supportedVersion); if (!updater.isPresent()) { throw new InvalidDataException("Could not get an updater for ItemEnchantment data from the version: " + contentVersion + " to " + this.supportedVersion + ". Please notify the SpongePowered developers of this issue!"); } container = updater.get().update(container); } } try { return buildContent(container); } catch (Exception e) { throw new InvalidDataException("Could not deserialize something correctly, likely due to bad type data.", e); } } }
@Override public Text deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { StringWriter writer = new StringWriter(); GsonConfigurationLoader gsonLoader = GsonConfigurationLoader.builder() .setIndent(0) .setSink(() -> new BufferedWriter(writer)) .setHeaderMode(HeaderMode.NONE) .build(); try { gsonLoader.save(value); } catch (IOException e) { throw new ObjectMappingException(e); } return Sponge.getDataManager().deserialize(Text.class, new MemoryDataContainer().set(Queries.JSON, writer.toString())).get(); }
final DisplayNameData itemName = Sponge.getDataManager().getManipulatorBuilder(DisplayNameData.class).get().create(); itemName.set(Keys.DISPLAY_NAME, ITEM_DISPLAY); final LoreData loreData = Sponge.getDataManager().getManipulatorBuilder(LoreData.class).get().create(); final ListValue<Text> lore = loreData.lore(); lore.addAll(LORE); .build(); final TradeOfferData tradeOfferData = Sponge.getDataManager().getManipulatorBuilder(TradeOfferData.class).get().create(); tradeOfferData.set(tradeOfferData.tradeOffers() .add(TradeOffer.builder()
@Listener public void onDataRegistration(GameRegistryEvent.Register<DataRegistration<?, ?>> event) { 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.HOME_DATA_REGISTRATION = DataRegistration.builder() .dataClass(HomeData.class) .immutableClass(ImmutableHomeData.class) .dataImplementation(HomeDataImpl.class) .immutableImplementation(ImmutableHomeDataImpl.class) .dataName("Home Data") .manipulatorId("myhomes:home") .buildAndRegister(this.container); // Friends stuff this.FRIENDS_DATA_REGISTRATION = DataRegistration.builder() .dataClass(FriendsData.class) .immutableClass(ImmutableFriendsData.class) .dataImplementation(FriendsDataImpl.class) .immutableImplementation(ImmutableFriendsDataImpl.class) .dataName("Friends Data") .manipulatorId("myhomes:friends") .buildAndRegister(this.container); }