Search in sources :

Example 11 with DataView

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

the class SchematicTranslator method translate.

@Override
public DataContainer translate(final Schematic schematic) throws InvalidDataException {
    final DataContainer data = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
    final DataView view = data.createView(Constants.Sponge.Schematic.SCHEMATIC);
    this.addTo(schematic, view);
    return data;
}
Also used : DataView(org.spongepowered.api.data.persistence.DataView) DataContainer(org.spongepowered.api.data.persistence.DataContainer)

Example 12 with DataView

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

the class DataUtil method getPosition3d.

public static Vector3d getPosition3d(final DataView view, @Nullable final DataQuery query) {
    final DataView internal = query == null ? view : view.getView(query).orElseThrow(DataUtil.dataNotFound());
    final double x = internal.getDouble(Queries.POSITION_X).orElseThrow(DataUtil.dataNotFound());
    final double y = internal.getDouble(Queries.POSITION_Y).orElseThrow(DataUtil.dataNotFound());
    final double z = internal.getDouble(Queries.POSITION_Z).orElseThrow(DataUtil.dataNotFound());
    return new Vector3d(x, y, z);
}
Also used : DataView(org.spongepowered.api.data.persistence.DataView) Vector3d(org.spongepowered.math.vector.Vector3d)

Example 13 with DataView

use of org.spongepowered.api.data.persistence.DataView 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)

Example 14 with DataView

use of org.spongepowered.api.data.persistence.DataView 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
            final DataView subView = view.createView(key);
            JsonDataFormat.readView(reader, subView);
            // handle special array types
            subView.getString(DataQuery.of(ARRAYTYPE)).ifPresent(type -> {
                view.remove(key);
                view.set(key, JsonDataFormat.readArray(type, subView));
            });
        } else {
            view.set(key, JsonDataFormat.read(reader));
        }
    }
    reader.endObject();
}
Also used : DataView(org.spongepowered.api.data.persistence.DataView) DataQuery(org.spongepowered.api.data.persistence.DataQuery)

Example 15 with DataView

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

the class NBTTranslator method setInternal.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static void setInternal(Tag base, byte type, DataView view, String key) {
    checkNotNull(base);
    checkNotNull(view);
    checkNotNull(key);
    checkArgument(!key.isEmpty());
    checkArgument(type > Constants.NBT.TAG_END && type <= Constants.NBT.TAG_INT_ARRAY);
    switch(type) {
        case Constants.NBT.TAG_BYTE:
            if (key.contains(NBTTranslator.BOOLEAN_IDENTIFIER)) {
                view.set(of(key.replace(NBTTranslator.BOOLEAN_IDENTIFIER, "")), (((ByteTag) base).getAsByte() != 0));
            } else {
                view.set(of(key), ((ByteTag) base).getAsByte());
            }
            break;
        case Constants.NBT.TAG_SHORT:
            view.set(of(key), ((ShortTag) base).getAsShort());
            break;
        case Constants.NBT.TAG_INT:
            view.set(of(key), ((IntTag) base).getAsInt());
            break;
        case Constants.NBT.TAG_LONG:
            view.set(of(key), ((LongTag) base).getAsLong());
            break;
        case Constants.NBT.TAG_FLOAT:
            view.set(of(key), ((FloatTag) base).getAsFloat());
            break;
        case Constants.NBT.TAG_DOUBLE:
            view.set(of(key), ((DoubleTag) base).getAsDouble());
            break;
        case Constants.NBT.TAG_BYTE_ARRAY:
            view.set(of(key), ((ByteArrayTag) base).getAsByteArray());
            break;
        case Constants.NBT.TAG_STRING:
            view.set(of(key), base.getAsString());
            break;
        case Constants.NBT.TAG_LIST:
            ListTag list = (ListTag) base;
            byte listType = list.getElementType();
            int count = list.size();
            List objectList = Lists.newArrayListWithCapacity(count);
            for (final Tag inbt : list) {
                objectList.add(NBTTranslator.fromTagBase(inbt, listType));
            }
            view.set(of(key), objectList);
            break;
        case Constants.NBT.TAG_COMPOUND:
            DataView internalView = view.createView(of(key));
            CompoundTag compound = (CompoundTag) base;
            for (String internalKey : compound.getAllKeys()) {
                Tag internalBase = compound.get(internalKey);
                byte internalType = internalBase.getId();
                // Basically.... more recursion.
                // Reasoning: This avoids creating a new DataContainer which would
                // then be copied in to the owning DataView anyways. We can internally
                // set the actual data directly to the child view instead.
                NBTTranslator.setInternal(internalBase, internalType, internalView, internalKey);
            }
            break;
        case Constants.NBT.TAG_INT_ARRAY:
            view.set(of(key), ((IntArrayTag) base).getAsIntArray());
            break;
        case Constants.NBT.TAG_LONG_ARRAY:
            view.set(of(key), ((LongArrayTag) base).getAsLongArray());
            break;
        default:
            throw new IllegalArgumentException("Unknown NBT type " + type);
    }
}
Also used : DataView(org.spongepowered.api.data.persistence.DataView) List(java.util.List) LongArrayTag(net.minecraft.nbt.LongArrayTag) DoubleTag(net.minecraft.nbt.DoubleTag) Tag(net.minecraft.nbt.Tag) StringTag(net.minecraft.nbt.StringTag) ByteTag(net.minecraft.nbt.ByteTag) IntTag(net.minecraft.nbt.IntTag) IntArrayTag(net.minecraft.nbt.IntArrayTag) FloatTag(net.minecraft.nbt.FloatTag) ByteArrayTag(net.minecraft.nbt.ByteArrayTag) LongTag(net.minecraft.nbt.LongTag) ShortTag(net.minecraft.nbt.ShortTag) CompoundTag(net.minecraft.nbt.CompoundTag) ListTag(net.minecraft.nbt.ListTag) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

DataView (org.spongepowered.api.data.persistence.DataView)32 DataQuery (org.spongepowered.api.data.persistence.DataQuery)16 DataContainer (org.spongepowered.api.data.persistence.DataContainer)10 CompoundTag (net.minecraft.nbt.CompoundTag)9 ResourceKey (org.spongepowered.api.ResourceKey)8 Map (java.util.Map)6 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)6 Optional (java.util.Optional)5 Nullable (org.checkerframework.checker.nullness.qual.Nullable)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Collection (java.util.Collection)3 DataContentUpdater (org.spongepowered.api.data.persistence.DataContentUpdater)3 DataSerializable (org.spongepowered.api.data.persistence.DataSerializable)3 DataStore (org.spongepowered.api.data.persistence.DataStore)3 DataTranslator (org.spongepowered.api.data.persistence.DataTranslator)3 Vector3i (org.spongepowered.math.vector.Vector3i)3 ImmutableList (com.google.common.collect.ImmutableList)2 TypeToken (io.leangen.geantyref.TypeToken)2