Search in sources :

Example 1 with DataQuery

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

the class MemoryDataView method contains.

@Override
public boolean contains(final DataQuery path, final DataQuery... paths) {
    Objects.requireNonNull(path, "DataQuery cannot be null!");
    Objects.requireNonNull(paths, "DataQuery varargs cannot be null!");
    if (paths.length == 0) {
        return this.contains(path);
    }
    final List<DataQuery> queries = new ArrayList<>();
    queries.add(path);
    for (final DataQuery query : paths) {
        queries.add(Objects.requireNonNull(query, "No null queries!"));
    }
    for (final DataQuery query : queries) {
        if (!this.contains(query)) {
            return false;
        }
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) DataQuery(org.spongepowered.api.data.persistence.DataQuery)

Example 2 with DataQuery

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

the class MemoryDataView method createView.

@Override
public DataView createView(final DataQuery path) {
    Objects.requireNonNull(path, "path");
    final List<String> queryParts = path.parts();
    final int sz = queryParts.size();
    checkArgument(sz != 0, "The size of the query must be at least 1");
    final String key = queryParts.get(0);
    final DataQuery keyQuery = DataQuery.of(key);
    if (sz == 1) {
        final DataView result = new MemoryDataView(this, keyQuery, this.safety);
        this.map.put(key, result);
        return result;
    }
    final DataQuery subQuery = path.popFirst();
    DataView subView = (DataView) this.map.get(key);
    if (subView == null) {
        subView = new MemoryDataView(this.parent, keyQuery, this.safety);
        this.map.put(key, subView);
    }
    return subView.createView(subQuery);
}
Also used : DataView(org.spongepowered.api.data.persistence.DataView) DataQuery(org.spongepowered.api.data.persistence.DataQuery)

Example 3 with DataQuery

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

the class SchematicTranslator method deserializeBlockContainer.

private static void deserializeBlockContainer(final DataView view, final SpongeArchetypeVolume archetypeVolume, final int width, final int length, final Vector3i offset, final boolean needsFixers) {
    final MutableBimapPalette<BlockState, BlockType> palette;
    final DataView paletteMap = view.getView(Constants.Sponge.Schematic.BLOCK_PALETTE).orElseThrow(() -> new InvalidDataException("Missing BlockPalette as required by Schematic Specification"));
    final Set<DataQuery> paletteKeys = paletteMap.keys(false);
    // 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 MutableBimapPalette<>(PaletteTypes.BLOCK_STATE_PALETTE.get(), Sponge.game().registry(RegistryTypes.BLOCK_TYPE), RegistryTypes.BLOCK_TYPE, paletteKeys.size());
    for (final DataQuery key : paletteKeys) {
        final BlockState state = BlockStateSerializerDeserializer.deserialize(key.parts().get(0)).orElseGet(() -> BlockTypes.BEDROCK.get().defaultState());
        palette.assign(state, paletteMap.getInt(key).orElseThrow(() -> new IllegalStateException("Somehow got a missing biome in the palette map for schematic")));
    }
    final byte[] blockData = (byte[]) view.get(Constants.Sponge.Schematic.BLOCK_DATA).orElseThrow(() -> new InvalidDataException("Missing BlockData for Schematic"));
    SchematicTranslator.readByteArrayData(width, (width * length), offset, palette, blockData, archetypeVolume, BlockVolume.Modifiable::setBlock);
    view.getViewList(Constants.Sponge.Schematic.BLOCKENTITY_CONTAINER).ifPresent(tileData -> tileData.forEach(SchematicTranslator.deserializeBlockEntities(offset, archetypeVolume, needsFixers)));
}
Also used : DataView(org.spongepowered.api.data.persistence.DataView) BlockState(org.spongepowered.api.block.BlockState) BlockType(org.spongepowered.api.block.BlockType) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) DataQuery(org.spongepowered.api.data.persistence.DataQuery)

Example 4 with DataQuery

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

the class MapUtil method getMapCanvasFromContainer.

public static byte[] getMapCanvasFromContainer(final DataView container) {
    final DataQuery canvasQuery = Constants.Map.MAP_CANVAS;
    final List<Byte> data = container.getByteList(canvasQuery).orElseThrow(() -> new InvalidDataException(canvasQuery + " was not a byte list!"));
    if (data.size() != Constants.Map.MAP_SIZE) {
        throw new InvalidDataException(canvasQuery + "had incorrect length, expected: " + Constants.Map.MAP_SIZE + ", got: " + data.size());
    }
    final Set<Byte> validPixels = new HashSet<>();
    // Ensure the data is valid.
    for (Byte pixel : data) {
        if (validPixels.contains(pixel)) {
            continue;
        }
        MapUtil.getMapColorFromPixelValue(pixel).orElseThrow(() -> new InvalidDataException("Invalid pixel value: " + pixel));
        validPixels.add(pixel);
    }
    return Bytes.toArray(data);
}
Also used : InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) DataQuery(org.spongepowered.api.data.persistence.DataQuery) HashSet(java.util.HashSet)

Example 5 with DataQuery

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

the class SchematicUpdater2_to_3 method update.

@Override
public DataView update(final DataView content) {
    // Move BlockData, BlockPalette, BlockEntities -> Blocks.Data, Blocks.Palette, and Blocks.BlockEntities
    content.getView(Constants.Sponge.Schematic.Versions.V2_BLOCK_PALETTE).ifPresent(dataView -> {
        content.remove(Constants.Sponge.Schematic.Versions.V2_BLOCK_PALETTE);
        final byte[] blockData = (byte[]) content.get(Constants.Sponge.Schematic.BLOCK_DATA).orElseThrow(() -> new InvalidDataException("Missing BlockData for Schematic"));
        content.remove(Constants.Sponge.Schematic.Versions.V2_BLOCK_DATA);
        final List<DataView> blockEntities = content.getViewList(Constants.Sponge.Schematic.BLOCKENTITY_CONTAINER).orElse(Collections.emptyList());
        content.remove(Constants.Sponge.Schematic.BLOCKENTITY_CONTAINER);
        final DataView blockContainer = content.createView(Constants.Sponge.Schematic.BLOCK_CONTAINER);
        blockContainer.set(Constants.Sponge.Schematic.BLOCK_DATA, blockData);
        blockContainer.set(Constants.Sponge.Schematic.BLOCK_PALETTE, dataView);
        blockContainer.set(Constants.Sponge.Schematic.BLOCKENTITY_CONTAINER, blockEntities);
    });
    // Move BiomeData, BiomePalette -> Biomes.Data and Biomes.Palette
    content.get(Constants.Sponge.Schematic.Versions.V2_BIOME_DATA).ifPresent(biomeData -> {
        content.remove(Constants.Sponge.Schematic.Versions.V2_BIOME_DATA);
        // But first, convert from a 2D array to a 3D array, which basically means almost fully deserializing
        // the entirety of the biome into a new buffer.
        final int[] offset = (int[]) content.get(Constants.Sponge.Schematic.OFFSET).orElse(new int[3]);
        if (offset.length != 3) {
            throw new InvalidDataException("Schematic offset was not of length 3");
        }
        final int xOffset = offset[0];
        final int yOffset = offset[1];
        final int zOffset = offset[2];
        final DataView palette = content.getView(Constants.Sponge.Schematic.Versions.V2_BIOME_PALETTE).orElseThrow(() -> new InvalidDataException("Missing Biome Palette for schematic"));
        final int width = content.getShort(Constants.Sponge.Schematic.WIDTH).orElseThrow(() -> new InvalidDataException("Missing value for: " + Constants.Sponge.Schematic.WIDTH));
        final int height = content.getShort(Constants.Sponge.Schematic.HEIGHT).orElseThrow(() -> new InvalidDataException("Missing value for: " + Constants.Sponge.Schematic.HEIGHT));
        final int length = content.getShort(Constants.Sponge.Schematic.LENGTH).orElseThrow(() -> new InvalidDataException("Missing value for: " + Constants.Sponge.Schematic.LENGTH));
        final Set<DataQuery> biomeKeys = palette.keys(false);
        final Registry<Biome> biomeRegistry = VolumeStreamUtils.nativeToSpongeRegistry(BuiltinRegistries.BIOME);
        final MutableBimapPalette<Biome, Biome> biomePalette = new MutableBimapPalette<>(PaletteTypes.BIOME_PALETTE.get(), biomeRegistry, RegistryTypes.BIOME, biomeKeys.size());
        final ByteArrayMutableBiomeBuffer biomeBuffer = new ByteArrayMutableBiomeBuffer(biomePalette, new Vector3i(-xOffset, -yOffset, -zOffset), new Vector3i(width, height, length));
        final DataView biomeView = content.createView(Constants.Sponge.Schematic.BIOME_CONTAINER);
        biomeView.set(Constants.Sponge.Schematic.BLOCK_PALETTE, palette);
        final byte[] biomes = (byte[]) biomeData;
        int biomeIndex = 0;
        int biomeJ = 0;
        int bVal = 0;
        int varIntLength = 0;
        final int yMin = biomeBuffer.min().y();
        final int yMax = biomeBuffer.max().y();
        while (biomeJ < biomes.length) {
            bVal = 0;
            varIntLength = 0;
            while (true) {
                bVal |= (biomes[biomeJ] & 127) << (varIntLength++ * 7);
                if (varIntLength > 5) {
                    throw new RuntimeException("VarInt too big (probably corrupted data)");
                }
                if (((biomes[biomeJ] & 128) != 128)) {
                    biomeJ++;
                    break;
                }
                biomeJ++;
            }
            final int z = (biomeIndex % (width * length)) / width;
            final int x = (biomeIndex % (width * length)) % width;
            final Biome type = biomePalette.get(bVal, Sponge.server()).get();
            for (int y = yMin; y <= yMax; y++) {
                biomeBuffer.setBiome(x - xOffset, y, z - zOffset, type);
            }
            biomeIndex++;
        }
        try (final ByteArrayOutputStream buffer = new ByteArrayOutputStream(width * height * length)) {
            final int xMin = biomeBuffer.min().x();
            final int zMin = biomeBuffer.min().z();
            for (int y = 0; y < height; y++) {
                final int y0 = yMin + y;
                for (int z = 0; z < length; z++) {
                    final int z0 = zMin + z;
                    for (int x = 0; x < width; x++) {
                        final int x0 = xMin + x;
                        final Biome biome = biomeBuffer.biome(x0, y0, z0);
                        SchematicTranslator.writeIdToBuffer(buffer, biomePalette.orAssign(biome));
                    }
                }
            }
            content.set(Constants.Sponge.Schematic.BIOME_DATA, buffer.toByteArray());
        } catch (final IOException e) {
        // Should never reach here.
        }
        content.remove(Constants.Sponge.Schematic.Versions.V2_BIOME_PALETTE);
    });
    content.set(Constants.Sponge.Schematic.VERSION, 3);
    return content;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DataView(org.spongepowered.api.data.persistence.DataView) Biome(org.spongepowered.api.world.biome.Biome) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) Vector3i(org.spongepowered.math.vector.Vector3i) MutableBimapPalette(org.spongepowered.common.world.schematic.MutableBimapPalette) DataQuery(org.spongepowered.api.data.persistence.DataQuery) ByteArrayMutableBiomeBuffer(org.spongepowered.common.world.volume.buffer.biome.ByteArrayMutableBiomeBuffer)

Aggregations

DataQuery (org.spongepowered.api.data.persistence.DataQuery)20 DataView (org.spongepowered.api.data.persistence.DataView)15 ResourceKey (org.spongepowered.api.ResourceKey)6 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)6 CompoundTag (net.minecraft.nbt.CompoundTag)5 DataContainer (org.spongepowered.api.data.persistence.DataContainer)5 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 List (java.util.List)3 Map (java.util.Map)3 Optional (java.util.Optional)3 Nullable (org.checkerframework.checker.nullness.qual.Nullable)3 Biome (org.spongepowered.api.world.biome.Biome)3 Vector3i (org.spongepowered.math.vector.Vector3i)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 TypeToken (io.leangen.geantyref.TypeToken)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 Collection (java.util.Collection)2 Set (java.util.Set)2