/** * Returns whether this {@link DataView} contains the given {@link Key}'s * defaulted {@link DataQuery}. * * @param key The key to get the data path relative to this data view * @return True if the path exists */ default boolean contains(Key<?> key) { return contains(checkNotNull(key, "Key cannot be null!").getQuery()); }
private Optional<Text> findText(DataView container, DataQuery query) { if (container.contains(query)) { return Optional.of(getText(container.get(query).get())); } return Optional.empty(); }
@Override public final boolean contains(DataQuery path) { checkNotNull(path, "path"); List<String> queryParts = path.getParts(); String key = queryParts.get(0); if (queryParts.size() == 1) { return this.map.containsKey(key); } Optional<DataView> subViewOptional = this.getUnsafeView(key); return subViewOptional.isPresent() && subViewOptional.get().contains(path.popFirst()); }
/** * Returns whether this {@link DataView} contains the given {@link Key}es * defaulted {@link DataQuery}. * * @param key The key to get the data path relative to this data view * @param keys The additional keys to check * @return True if the path exists */ default boolean contains(Key<?> key, Key<?>... keys) { checkNotNull(key, "Key cannot be null!"); checkNotNull(keys, "Keys cannot be null!"); if (keys.length == 0) { return contains(key.getQuery()); } List<DataQuery> queries = new ArrayList<>(); for (Key<?> arrayKey : keys) { queries.add(checkNotNull(arrayKey, "Cannot have a null key!").getQuery()); } return contains(key.getQuery(), queries.toArray(new DataQuery[queries.size()])); }
@Override protected Optional<Color> buildContent(DataView container) throws InvalidDataException { if (!container.contains(Queries.COLOR_RED, Queries.COLOR_GREEN, Queries.COLOR_BLUE)) { return Optional.empty(); } try { final int red = container.getInt(Queries.COLOR_RED).get(); final int green = container.getInt(Queries.COLOR_GREEN).get(); final int blue = container.getInt(Queries.COLOR_BLUE).get(); return Optional.of(Color.ofRgb(red, green, blue)); } catch (Exception e) { throw new InvalidDataException("Could not parse some data.", e); } } }
@Override protected Optional<BookView> buildContent(DataView container) throws InvalidDataException { BookView.Builder builder = BookView.builder(); findText(container, TEXT_TITLE).ifPresent(builder::title); findText(container, TEXT_AUTHOR).ifPresent(builder::author); if (container.contains(TEXT_PAGE_LIST)) { Object pageList = container.get(TEXT_PAGE_LIST).get(); if (!(pageList instanceof List)) { throw new InvalidDataException("Expected List"); } ((List<?>) pageList).forEach(textView -> builder.addPage(getText(textView))); } return Optional.of(builder.build()); }
@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); } } }
/** * Returns whether this {@link DataView} contains the given {@link Key}'s * defaulted {@link DataQuery}. * * @param key The key to get the data path relative to this data view * @return True if the path exists */ default boolean contains(Key<?> key) { return contains(checkNotNull(key, "Key cannot be null!").getQuery()); }
private Optional<Text> findText(DataView container, DataQuery query) { if (container.contains(query)) { return Optional.of(getText(container.get(query).get())); } return Optional.empty(); }
@Override public final boolean contains(DataQuery path) { checkNotNull(path, "path"); List<String> queryParts = path.getParts(); String key = queryParts.get(0); if (queryParts.size() == 1) { return this.map.containsKey(key); } Optional<DataView> subViewOptional = this.getUnsafeView(key); if (!subViewOptional.isPresent()) { return false; } return subViewOptional.get().contains(path.popFirst()); }
@Override protected Optional<FriendsData> buildContent(DataView container) throws InvalidDataException { if (container.contains(MyHomes.FRIENDS)) { List<UUID> friends = container.getObjectList(MyHomes.FRIENDS.getQuery(), UUID.class).get(); return Optional.of(new FriendsDataImpl(friends)); } return Optional.empty(); } }
@Override protected Optional<LastPowerData> buildContent(DataView container) throws InvalidDataException { if (container.contains(CraftBookKeys.LAST_POWER.getQuery())) { return Optional.of(new LastPowerData(container.getInt(CraftBookKeys.LAST_POWER.getQuery()).get())); } return Optional.empty(); } }
@Override protected Optional<KeyLockData> buildContent(DataView container) throws InvalidDataException { if (container.contains(CraftBookKeys.KEY_LOCK.getQuery())) { return Optional.of(new KeyLockData(container.getSerializable(CraftBookKeys.KEY_LOCK.getQuery(), ItemStackSnapshot.class).get())); } return Optional.empty(); } }
@Override protected Optional<NamespaceData> buildContent(DataView container) throws InvalidDataException { if (container.contains(CraftBookKeys.NAMESPACE.getQuery())) { return Optional.of(new NamespaceData(container.getString(CraftBookKeys.NAMESPACE.getQuery()).get())); } return Optional.empty(); } }
@Override protected Optional<EmbeddedBlockBagData> buildContent(DataView container) throws InvalidDataException { if (container.contains(CraftBookKeys.EMBEDDED_BLOCK_BAG.getQuery())) { return Optional.of(new EmbeddedBlockBagData(container.getSerializable(CraftBookKeys.EMBEDDED_BLOCK_BAG.getQuery(), EmbeddedBlockBag.class).get())); } return Optional.empty(); } }
@Override protected Optional<BlockBagData> buildContent(DataView container) throws InvalidDataException { if (container.contains(CraftBookKeys.BLOCK_BAG.getQuery())) { return Optional.of(new BlockBagData(container.getLong(CraftBookKeys.BLOCK_BAG.getQuery()).get())); } return Optional.empty(); } }
@Override protected Optional<ICData> buildContent(DataView container) throws InvalidDataException { if (container.contains(CraftBookKeys.IC_DATA.getQuery())) { try { Class<SerializedICData> clazz = (Class<SerializedICData>) Class.forName(container.getString(DataQuery.of("ICDataClass")).orElse(SerializedICData.class.getName())); return Optional.of(new ICData(container.getSerializable(CraftBookKeys.IC_DATA.getQuery(), clazz).get())); } catch (ClassNotFoundException e) { e.printStackTrace(); } } return Optional.empty(); } }
@Override protected Optional<Home> buildContent(DataView content) throws InvalidDataException { if (!content.contains(Home.WORLD_QUERY, Home.POSITION_QUERY, Home.ROTATION_QUERY, Home.NAME_QUERY)) { return Optional.empty(); } World world = Sponge.getServer().getWorld(content.getObject(Home.WORLD_QUERY, UUID.class).get()).orElseThrow(InvalidDataException::new); Vector3d position = content.getObject(Home.POSITION_QUERY, Vector3d.class).get(); Vector3d rotation = content.getObject(Home.ROTATION_QUERY, Vector3d.class).get(); String name = content.getString(Home.NAME_QUERY).get(); Transform<World> transform = new Transform<>(world, position, rotation); return Optional.of(new Home(transform, name)); }
@Override protected Optional<BookView> buildContent(DataView container) throws InvalidDataException { BookView.Builder builder = BookView.builder(); findText(container, TEXT_TITLE).ifPresent(builder::title); findText(container, TEXT_AUTHOR).ifPresent(builder::author); if (container.contains(TEXT_PAGE_LIST)) { Object pageList = container.get(TEXT_PAGE_LIST).get(); if (!(pageList instanceof List)) { throw new InvalidDataException("Expected List"); } ((List<?>) pageList).forEach(textView -> builder.addPage(getText(textView))); } return Optional.of(builder.build()); }
@Override protected Optional<HomeData> buildContent(DataView container) throws InvalidDataException { if (!container.contains(MyHomes.HOMES)) return Optional.empty(); HomeData data = new HomeDataImpl(); container.getView(MyHomes.HOMES.getQuery()) .get().getKeys(false).forEach(name -> data.homes().put(name.toString(), container.getSerializable(name, Home.class) .orElseThrow(InvalidDataException::new))); container.getSerializable(MyHomes.DEFAULT_HOME.getQuery(), Home.class).ifPresent(home -> { data.set(MyHomes.DEFAULT_HOME, home); }); return Optional.of(data); }