Search in sources :

Example 1 with DataQuery

use of org.spongepowered.api.data.DataQuery in project SpongeCommon by SpongePowered.

the class JsonDataFormat method readView.

private static void readView(JsonReader reader, DataView view) throws IOException {
    reader.beginObject();
    while (reader.hasNext()) {
        DataQuery key = of(reader.nextName());
        if (reader.peek() == JsonToken.BEGIN_OBJECT) {
            // Check this early so we don't need to copy the view
            readView(reader, view.createView(key));
        } else {
            view.set(key, read(reader));
        }
    }
    reader.endObject();
}
Also used : DataQuery(org.spongepowered.api.data.DataQuery)

Example 2 with DataQuery

use of org.spongepowered.api.data.DataQuery in project SpongeCommon by SpongePowered.

the class NbtTranslator method containerToCompound.

private static void containerToCompound(final DataView container, final NBTTagCompound compound) {
    // We don't need to get deep values since all nested DataViews will be found
    // from the instance of checks.
    checkNotNull(container);
    checkNotNull(compound);
    for (Map.Entry<DataQuery, Object> entry : container.getValues(false).entrySet()) {
        Object value = entry.getValue();
        String key = entry.getKey().asString('.');
        if (value instanceof DataView) {
            NBTTagCompound inner = new NBTTagCompound();
            containerToCompound(container.getView(entry.getKey()).get(), inner);
            compound.setTag(key, inner);
        } else if (value instanceof Boolean) {
            compound.setTag(key + BOOLEAN_IDENTIFER, new NBTTagByte(((Boolean) value) ? (byte) 1 : 0));
        } else {
            compound.setTag(key, getBaseFromObject(value));
        }
    }
}
Also used : DataView(org.spongepowered.api.data.DataView) NBTTagByte(net.minecraft.nbt.NBTTagByte) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) DataQuery(org.spongepowered.api.data.DataQuery) NBTTagString(net.minecraft.nbt.NBTTagString) Map(java.util.Map)

Example 3 with DataQuery

use of org.spongepowered.api.data.DataQuery in project SpongeCommon by SpongePowered.

the class SchematicTranslator method translate.

@Override
public Schematic translate(DataView view) throws InvalidDataException {
    int version = view.getInt(DataQueries.Schematic.VERSION).get();
    // TODO version conversions
    if (version != VERSION) {
        throw new InvalidDataException(String.format("Unknown schematic version %d (current version is %d)", version, VERSION));
    }
    DataView metadata = view.getView(DataQueries.Schematic.METADATA).orElse(null);
    if (metadata != null) {
        Optional<DataView> dot_data = metadata.getView(DataQuery.of("."));
        if (dot_data.isPresent()) {
            DataView data = dot_data.get();
            for (DataQuery key : data.getKeys(false)) {
                if (!metadata.contains(key)) {
                    metadata.set(key, data.get(key).get());
                }
            }
        }
    }
    // TODO error handling for these optionals
    int width = view.getShort(DataQueries.Schematic.WIDTH).get();
    int height = view.getShort(DataQueries.Schematic.HEIGHT).get();
    int length = view.getShort(DataQueries.Schematic.LENGTH).get();
    if (width > MAX_SIZE || height > MAX_SIZE || length > MAX_SIZE) {
        throw new InvalidDataException(String.format("Schematic is larger than maximum allowable size (found: (%d, %d, %d) max: (%d, %<d, %<d)", width, height, length, MAX_SIZE));
    }
    int[] offset = (int[]) view.get(DataQueries.Schematic.OFFSET).orElse(null);
    if (offset == null) {
        offset = new int[3];
    }
    if (offset.length != 3) {
        throw new InvalidDataException("Schematic offset was not of length 3");
    }
    BlockPalette palette;
    Optional<DataView> paletteData = view.getView(DataQueries.Schematic.PALETTE);
    int palette_max = view.getInt(DataQueries.Schematic.PALETTE_MAX).orElse(0xFFFF);
    if (paletteData.isPresent()) {
        // If we had a default palette_max we don't want to allocate all
        // that space for nothing so we use a sensible default instead
        palette = new BimapPalette(palette_max != 0xFFFF ? palette_max : 64);
        DataView paletteMap = paletteData.get();
        Set<DataQuery> paletteKeys = paletteMap.getKeys(false);
        for (DataQuery key : paletteKeys) {
            BlockState state = Sponge.getRegistry().getType(BlockState.class, key.getParts().get(0)).get();
            ((BimapPalette) palette).assign(state, paletteMap.getInt(key).get());
        }
    } else {
        palette = GlobalPalette.instance;
    }
    MutableBlockVolume buffer = new ArrayMutableBlockBuffer(palette, new Vector3i(-offset[0], -offset[1], -offset[2]), new Vector3i(width, height, length));
    byte[] blockdata = (byte[]) view.get(DataQueries.Schematic.BLOCK_DATA).get();
    int index = 0;
    int i = 0;
    int value = 0;
    int varint_length = 0;
    while (i < blockdata.length) {
        value = 0;
        varint_length = 0;
        while (true) {
            value |= (blockdata[i] & 127) << (varint_length++ * 7);
            if (varint_length > 5) {
                throw new RuntimeException("VarInt too big (probably corrupted data)");
            }
            if ((blockdata[i] & 128) != 128) {
                i++;
                break;
            }
            i++;
        }
        // index = (y * length + z) * width + x
        int y = index / (width * length);
        int z = (index % (width * length)) / width;
        int x = (index % (width * length)) % width;
        BlockState state = palette.get(value).get();
        buffer.setBlock(x - offset[0], y - offset[1], z - offset[2], state);
        index++;
    }
    Map<Vector3i, TileEntityArchetype> tiles = Maps.newHashMap();
    List<DataView> tiledata = view.getViewList(DataQueries.Schematic.TILEENTITY_DATA).orElse(null);
    if (tiledata != null) {
        for (DataView tile : tiledata) {
            int[] pos = (int[]) tile.get(DataQueries.Schematic.TILEENTITY_POS).get();
            if (offset.length != 3) {
                throw new InvalidDataException("Schematic tileentity pos was not of length 3");
            }
            TileEntityType type = TileEntityTypeRegistryModule.getInstance().getForClass(TileEntity.REGISTRY.getObject(new ResourceLocation(tile.getString(DataQuery.of("id")).get())));
            TileEntityArchetype archetype = new SpongeTileEntityArchetypeBuilder().state(buffer.getBlock(pos[0] - offset[0], pos[1] - offset[1], pos[2] - offset[2])).tileData(tile).tile(type).build();
            tiles.put(new Vector3i(pos[0] - offset[0], pos[1] - offset[1], pos[2] - offset[2]), archetype);
        }
    }
    Schematic schematic = new SpongeSchematic(buffer, tiles, metadata);
    return schematic;
}
Also used : BlockPalette(org.spongepowered.api.world.schematic.BlockPalette) MutableBlockVolume(org.spongepowered.api.world.extent.MutableBlockVolume) BimapPalette(org.spongepowered.common.world.schematic.BimapPalette) ArrayMutableBlockBuffer(org.spongepowered.common.util.gen.ArrayMutableBlockBuffer) SpongeTileEntityArchetypeBuilder(org.spongepowered.common.block.SpongeTileEntityArchetypeBuilder) DataView(org.spongepowered.api.data.DataView) SpongeSchematic(org.spongepowered.common.world.schematic.SpongeSchematic) BlockState(org.spongepowered.api.block.BlockState) TileEntityType(org.spongepowered.api.block.tileentity.TileEntityType) ResourceLocation(net.minecraft.util.ResourceLocation) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) Vector3i(com.flowpowered.math.vector.Vector3i) DataQuery(org.spongepowered.api.data.DataQuery) TileEntityArchetype(org.spongepowered.api.block.tileentity.TileEntityArchetype) Schematic(org.spongepowered.api.world.schematic.Schematic) SpongeSchematic(org.spongepowered.common.world.schematic.SpongeSchematic)

Example 4 with DataQuery

use of org.spongepowered.api.data.DataQuery in project SpongeCommon by SpongePowered.

the class MemoryDataTest method testToString.

@Test
public void testToString() {
    DataContainer container = DataContainer.createNew();
    DataQuery testQuery = of("foo", "bar", "baz");
    List<Integer> intList = ImmutableList.of(1, 2, 3, 4);
    container.set(testQuery, intList);
    assertTrue(container.getIntegerList(testQuery).isPresent());
    assertTrue(container.getIntegerList(testQuery).get().equals(intList));
    List<Double> doubleList = ImmutableList.of(1.0D, 2.0D, 3.0D, 4.0D);
    container.set(testQuery, doubleList);
    assertTrue(container.getDoubleList(testQuery).isPresent());
    assertTrue(container.getDoubleList(testQuery).get().equals(doubleList));
    List<Short> shortList = ImmutableList.of((short) 1, (short) 2, (short) 3, (short) 4);
    container.set(testQuery, shortList);
    assertTrue(container.getShortList(testQuery).isPresent());
    assertTrue(container.getShortList(testQuery).get().equals(shortList));
    List<Byte> byteList = ImmutableList.of((byte) 1, (byte) 2, (byte) 3, (byte) 4);
    container.set(testQuery, byteList);
    List<SimpleData> list = Lists.newArrayList();
    for (int i = 0; i < 1000; i++) {
        String number = Integer.toString(i);
        list.add(new SimpleData(i, 0.1 * i, "i", Lists.asList(number, new String[] { " foo", "bar" })));
    }
    container.set(of("SimpleData"), list);
    container.toString();
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer) DataQuery(org.spongepowered.api.data.DataQuery) Test(org.junit.Test)

Example 5 with DataQuery

use of org.spongepowered.api.data.DataQuery in project SpongeCommon by SpongePowered.

the class MemoryDataTest method testString.

@Test
public void testString() {
    DataContainer container = DataContainer.createNew();
    DataQuery testQuery = of("foo", "bar");
    container.set(testQuery, "foo");
    Optional<String> stringOptional = container.getString(testQuery);
    assertTrue(stringOptional.isPresent());
    assertTrue(stringOptional.get().equals("foo"));
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer) DataQuery(org.spongepowered.api.data.DataQuery) Test(org.junit.Test)

Aggregations

DataQuery (org.spongepowered.api.data.DataQuery)41 DataContainer (org.spongepowered.api.data.DataContainer)23 DataView (org.spongepowered.api.data.DataView)20 Test (org.junit.Test)14 Map (java.util.Map)12 ArrayList (java.util.ArrayList)7 List (java.util.List)7 DataTypeSerializer (org.lanternpowered.server.data.persistence.DataTypeSerializer)7 ImmutableMap (com.google.common.collect.ImmutableMap)4 TypeToken (com.google.common.reflect.TypeToken)4 Collection (java.util.Collection)4 HashMap (java.util.HashMap)4 Set (java.util.Set)4 Collectors (java.util.stream.Collectors)4 Nullable (javax.annotation.Nullable)4 CatalogType (org.spongepowered.api.CatalogType)4 DataSerializable (org.spongepowered.api.data.DataSerializable)4 Vector3i (com.flowpowered.math.vector.Vector3i)3 Lists (com.google.common.collect.Lists)3 LinkedHashMap (java.util.LinkedHashMap)3