@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())); }
private void setMap(String key, Map<?, ?> value) { DataView view = createView(of(key)); for (Map.Entry<?, ?> entry : value.entrySet()) { view.set(of(entry.getKey().toString()), entry.getValue()); } }
@Override protected Optional<RespawnLocation> buildContent(DataView container) throws InvalidDataException { final String worldString = container.getString(Queries.WORLD_ID).get(); final UUID worldId = UUID.fromString(worldString); final double xPos = container.getDouble(Queries.POSITION_X).get(); final double yPos = container.getDouble(Queries.POSITION_Y).get(); final double zPos = container.getDouble(Queries.POSITION_Z).get(); final Vector3d position = new Vector3d(xPos, yPos, zPos); final boolean forcedSpawn = container.getBoolean(Queries.FORCED_SPAWN).orElse(false); final Builder builder = new Builder(); builder.world = worldId; builder.position = position; builder.forced = forcedSpawn; return Optional.of(new RespawnLocation(builder)); }
@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); } } }
entity.set(DataQueries.WorldUuid, location.getString(DataQueries.WorldUuid).get()); location.remove(DataQueries.WorldUuid); entity.set(DataQueries.Position, location); DataView unsafe = entity.getView(DataQueries.UnsafeData).get(); coordinates.add(location.getDouble(DataQueries.X).get()); coordinates.add(location.getDouble(DataQueries.Y).get()); coordinates.add(location.getDouble(DataQueries.Z).get()); unsafe.set(DataQueries.Pos, coordinates); DataView rotation = entity.getView(DataQueries.Rotation).get(); unsafe.set(DataQueries.Rotation, rotation); entity.set(DataQueries.UnsafeData, unsafe);
@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); }
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);
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; }
@Test public void testGetKeys() { Set<DataQuery> queries = Sets.newHashSet(); queries.add(of("foo")); queries.add(of("foo", "bar")); queries.add(of("foo", "bar", "baz")); queries.add(of("bar")); DataView view = DataContainer.createNew(); view.set(of("foo"), "foo"); view.set(of("foo", "bar"), "foobar"); view.set(of("foo", "bar", "baz"), "foobarbaz"); view.set(of("bar"), 1); Set<DataQuery> testQueries = Sets.newHashSet(); testQueries.add(of("foo")); testQueries.add(of("bar")); Set<DataQuery> shallowKeys = view.getKeys(false); assertTrue(shallowKeys.equals(testQueries)); Set<DataQuery> deepKeys = view.getKeys(true); assertTrue(deepKeys.containsAll(queries)); }
@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); }
@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(); } }
@Test public void testGetMaps() { DataView view = DataContainer.createNew(); view.set(of("foo", "bar", "foo"), "foo"); view.set(of("foo", "bar", "bar"), "foobar"); view.set(of("foo", "bar", "baz"), "foobarbaz"); view.set(of("bar"), 1); shallowMap.put(of("foo"), intermediateMap); Map<DataQuery, Object> shallowValues = view.getValues(false); assertTrue(shallowValues.entrySet().equals(shallowMap.entrySet())); deepMap.put(of("foo", "bar"), internalView); Map<DataQuery, Object> deepValues = view.getValues(true); assertTrue(deepValues.keySet().equals(deepMap.keySet())); assertTrue(deepValues.entrySet().equals(deepMap.entrySet()));
@Override public DataView createView(DataQuery path, Map<?, ?> map) { checkNotNull(path, "path"); DataView section = createView(path); for (Map.Entry<?, ?> entry : map.entrySet()) { if (entry.getValue() instanceof Map) { section.createView(of('.', entry.getKey().toString()), (Map<?, ?>) entry.getValue()); } else { section.set(of('.', entry.getKey().toString()), entry.getValue()); } } return section; }
@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 public DataView update(DataView content) { if (!content.contains(Home.WORLD_QUERY, Home.POSITION_QUERY)) { throw new InvalidDataException("Invalid data for a home!"); } UUID uuid = content.getObject(Home.WORLD_QUERY, UUID.class).get(); Vector3d pos = content.getObject(Home.POSITION_QUERY, Vector3d.class).get(); String name = Sponge.getServer().getWorldProperties(uuid) .map(WorldProperties::getWorldName) .orElse(uuid.toString().substring(0, 9)); name = String.format("%s-%d,%d,%d", name, pos.getFloorX(), pos.getFloorY(), pos.getFloorZ()); content.set(Home.NAME_QUERY, name); return content; } }
@Override protected Optional<PlcStateData> buildContent(DataView container) throws InvalidDataException { PlcStateData state = new PlcStateData(); state.state = container.getBooleanList(DataQuery.of("State")).orElse(new ArrayList<>()); state.languageName = container.getString(DataQuery.of("LanguageName")).orElse("Perlstone"); state.codeString = container.getString(DataQuery.of("Code")).orElse(""); state.error = container.getBoolean(DataQuery.of("Error")).orElse(false); state.errorCode = container.getString(DataQuery.of("ErrorCode")).orElse(""); return Optional.of(state); }
@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(); } }
@Test public void testMaps() { Map<String, Object> myMap = Maps.newHashMap(); myMap.put("foo", "bar"); myMap.put("myNumber", 1); List<String> stringList = Lists.newArrayList(); for (int i = 0; i < 100; i++) { stringList.add("Foo" + i); } myMap.put("myList", stringList); DataView view = DataContainer.createNew(); view.set(of("Foo"), myMap); Map<?, ?> retrievedMap = view.getMap(of("Foo")).get(); assertTrue(myMap.keySet().equals(retrievedMap.keySet())); assertTrue(myMap.entrySet().equals(retrievedMap.entrySet())); }