@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 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 Optional<SimpleData> build(final DataView container) { Optional<Integer> testInt = container.getInt(DataQuery.of("myInt")); if (!testInt.isPresent()) { throw new InvalidDataException("Missing important data: {myInt}"); } Optional<Double> testDouble = container.getDouble(DataQuery.of("myDouble")); if (!testDouble.isPresent()) { throw new InvalidDataException("Missing important data: {myDouble}"); } Optional<String> testString = container.getString(DataQuery.of("myString")); if (!testString.isPresent()) { throw new InvalidDataException("Missing important data: {myString}"); } Optional<List<String>> testList = container.getStringList(DataQuery.of("myStringList")); if (!testList.isPresent()) { throw new InvalidDataException("Missing important data: {myStringList}"); } return Optional.of(new SimpleData(testInt.get(), testDouble.get(), testString.get(), testList.get())); }
@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<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(); } }
statement.setObject(2, container.getString(DataQueries.EventName).get()); statement.setObject(3, location.getString(DataQueries.WorldUuid).get()); statement.setInt(4, location.getInt(DataQueries.X).get()); statement.setInt(5, location.getInt(DataQueries.Y).get()); statement.setInt(6, location.getInt(DataQueries.Z).get()); statement.setString(7, container.getString(DataQueries.Target).get()); statement.setString(8, playerUUID);
statement.setObject(2, container.getString(DataQueries.EventName).get()); statement.setString(3, TypeUtil.uuidStringToDbString(location.getString(DataQueries.WorldUuid).get())); statement.setInt(4, location.getInt(DataQueries.X).get()); statement.setInt(5, location.getInt(DataQueries.Y).get()); statement.setInt(6, location.getInt(DataQueries.Z).get()); statement.setString(7, container.getString(DataQueries.Target).orElse(null)); statement.setString(8, playerUUID);
@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 Home translate(final DataView content) throws InvalidDataException { content.getInt(Queries.CONTENT_VERSION).ifPresent(version -> { if (version != CONTENT_VERSION) { throw new InvalidDataException("Version incompatible: " + version); } }); if (!content.contains(Home.WORLD_QUERY, Home.POSITION_QUERY, Home.ROTATION_QUERY, Home.NAME_QUERY)) { throw new InvalidDataException("Incomplete data"); } 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 new Home(transform, name); }
public DataView formatBlockData(DataView finalBlock, @Nonnull Object optionalLocation) { Preconditions.checkNotNull(optionalLocation, "The location you are formatting cannot be null."); DataView location = (DataView) optionalLocation; DataView position = DataContainer.createNew(); position.set(DataQueries.X, location.get(DataQueries.X).get()); position.set(DataQueries.Y, location.get(DataQueries.Y).get()); position.set(DataQueries.Z, location.get(DataQueries.Z).get()); finalBlock.set(DataQueries.Position, position); finalBlock.set(DataQueries.WorldUuid, location.get(DataQueries.WorldUuid).get()); int blockStateVersion = finalBlock.getInt(DataQueries.BlockState.then(DataQueries.ContentVersion)).orElse(1); if (blockStateVersion == 1) { // Unsafe data includes coordinates Optional<Object> optionalUnsafeData = finalBlock.get(DataQueries.UnsafeData); if (optionalUnsafeData.isPresent()) { DataView unsafeData = (DataView) optionalUnsafeData.get(); unsafeData.set(DataQueries.X, location.get(DataQueries.X).get()); unsafeData.set(DataQueries.Y, location.get(DataQueries.Y).get()); unsafeData.set(DataQueries.Z, location.get(DataQueries.Z).get()); finalBlock.set(DataQueries.UnsafeData, unsafeData); } } return finalBlock; }