@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; }
/** * Serializes the {@code T} object and applies the provided * data to the provided {@link DataView} instead of creating * a new {@link DataContainer}, reducing nested information. * * @param obj The object to serialize * @param dataView The data view to serialize to * @return The provided data view, for chaining */ default DataView addTo(T obj, DataView dataView) { for (Map.Entry<DataQuery, Object> entry : translate(obj).getValues(false).entrySet()) { dataView.set(entry.getKey(), entry); } return dataView; } }
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()); } }
subView = subViewOptional.get(); subView.set(path.popFirst(), value); return this;
@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())); }
@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 public DataView update(DataView content) { content.set(MyHomes.HOMES, ImmutableMap.of()); return content; } }
@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; }
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()); } }
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; }
entity.set(DataQueries.WorldUuid, location.getString(DataQueries.WorldUuid).get()); location.remove(DataQueries.WorldUuid); entity.set(DataQueries.Position, location); coordinates.add(location.getDouble(DataQueries.Y).get()); coordinates.add(location.getDouble(DataQueries.Z).get()); unsafe.set(DataQueries.Pos, coordinates); unsafe.set(DataQueries.Rotation, rotation); entity.set(DataQueries.UnsafeData, unsafe);
@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; } }
metadata.set(DataQuery.of("Name"), name);
subView = subViewOptional.get(); subView.set(path.popFirst(), value); return this;
/** * Helper method for formatting entity container data. * @param entity */ private void writeEntity(Entity entity) { checkNotNull(entity); DataContainer entityData = entity.toContainer(); Optional<DataView> position = entityData.getView(DataQueries.Position); if (position.isPresent()) { position.get().set(DataQueries.WorldUuid, entityData.get(DataQueries.WorldUuid).get()); data.set(DataQueries.Location, position.get()); entityData.remove(DataQueries.Position); entityData.remove(DataQueries.WorldUuid); } Optional<DataView> optionalUnsafeData = entityData.getView(DataQueries.UnsafeData); if (optionalUnsafeData.isPresent()) { DataView unsafeData = optionalUnsafeData.get(); unsafeData.remove(DataQueries.Rotation); unsafeData.remove(DataQueries.Pos); entityData.set(DataQueries.UnsafeData, unsafeData); } data.set(DataQueries.Target, entity.getType().getId().replace("_", " ")); data.set(DataQueries.Entity, entityData); }