Search in sources :

Example 1 with DataView

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

the class DataUtil method deserializeImmutableManipulatorList.

public static ImmutableList<ImmutableDataManipulator<?, ?>> deserializeImmutableManipulatorList(List<DataView> containers) {
    checkNotNull(containers);
    final ImmutableList.Builder<ImmutableDataManipulator<?, ?>> builder = ImmutableList.builder();
    for (DataView view : containers) {
        view = updateDataViewForDataManipulator(view);
        final String dataId = view.getString(DataQueries.DATA_ID).orElseThrow(DataUtil.dataNotFound());
        final DataView manipulatorView = view.getView(DataQueries.INTERNAL_DATA).orElseThrow(DataUtil.dataNotFound());
        try {
            deserializeManipulator(dataId, manipulatorView).map(DataManipulator::asImmutable).ifPresent(builder::add);
        } catch (Exception e) {
            new InvalidDataException("Could not translate " + dataId + "!", e).printStackTrace();
        }
    }
    return builder.build();
}
Also used : DataView(org.spongepowered.api.data.DataView) ImmutableList(com.google.common.collect.ImmutableList) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator)

Example 2 with DataView

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

the class DataUtil method getPosition3i.

public static Vector3i getPosition3i(DataView view, DataQuery query) {
    checkDataExists(view, DataQueries.SNAPSHOT_WORLD_POSITION);
    final DataView internal = view.getView(DataQueries.SNAPSHOT_WORLD_POSITION).get();
    final int x = internal.getInt(Queries.POSITION_X).get();
    final int y = internal.getInt(Queries.POSITION_Y).get();
    final int z = internal.getInt(Queries.POSITION_Z).get();
    return new Vector3i(x, y, z);
}
Also used : DataView(org.spongepowered.api.data.DataView) Vector3i(com.flowpowered.math.vector.Vector3i)

Example 3 with DataView

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

the class LegacySchematicTranslator method translate.

@Override
public Schematic translate(DataView view) throws InvalidDataException {
    // We default to sponge as the assumption should be that if this tag
    // (which is not in the sponge schematic specification) is not present
    // then it is more likely that its a sponge schematic than a legacy
    // schematic
    String materials = view.getString(DataQueries.Schematic.LEGACY_MATERIALS).orElse("Sponge");
    if ("Sponge".equalsIgnoreCase(materials)) {
        // not a legacy schematic use the new loader instead.
        return DataTranslators.SCHEMATIC.translate(view);
    } else if (!"Alpha".equalsIgnoreCase(materials)) {
        throw new InvalidDataException(String.format("Schematic specifies unknown materials %s", materials));
    }
    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 offsetX = view.getInt(DataQueries.Schematic.LEGACY_OFFSET_X).orElse(0);
    int offsetY = view.getInt(DataQueries.Schematic.LEGACY_OFFSET_Y).orElse(0);
    int offsetZ = view.getInt(DataQueries.Schematic.LEGACY_OFFSET_Z).orElse(0);
    BlockPalette palette = GlobalPalette.instance;
    ArrayMutableBlockBuffer buffer = new ArrayMutableBlockBuffer(new Vector3i(-offsetX, -offsetY, -offsetZ), new Vector3i(width, height, length));
    byte[] block_ids = (byte[]) view.get(DataQueries.Schematic.LEGACY_BLOCKS).get();
    byte[] block_data = (byte[]) view.get(DataQueries.Schematic.LEGACY_BLOCK_DATA).get();
    byte[] add_block = (byte[]) view.get(DataQueries.Schematic.LEGACY_ADD_BLOCKS).orElse(null);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            for (int z = 0; z < length; z++) {
                int index = (y * length + z) * width + x;
                final int default_state_id = block_ids[index];
                final int blockData = block_data[index] & 0xF;
                int palette_id = default_state_id << 4 | blockData;
                if (add_block != null) {
                    palette_id |= add_block[index] << 12;
                }
                Optional<BlockState> blockState = palette.get(palette_id);
                if (!blockState.isPresent()) {
                    // At the very least get the default state id
                    blockState = Optional.of((BlockState) Block.REGISTRY.getObjectById(default_state_id));
                }
                BlockState block = blockState.orElseGet(BlockTypes.COBBLESTONE::getDefaultState);
                buffer.setBlock(x - offsetX, y - offsetY, z - offsetZ, block);
            }
        }
    }
    Map<Vector3i, TileEntityArchetype> tiles = Maps.newHashMap();
    List<DataView> tiledata = view.getViewList(DataQueries.Schematic.LEGACY_TILEDATA).orElse(null);
    if (tiledata != null) {
        for (DataView tile : tiledata) {
            int x = tile.getInt(DataQueries.X_POS).get();
            int y = tile.getInt(DataQueries.Y_POS).get();
            int z = tile.getInt(DataQueries.Z_POS).get();
            final String tileType = tile.getString(TILE_ID).get();
            final ResourceLocation name = new ResourceLocation(tileType);
            TileEntityType type = TileEntityTypeRegistryModule.getInstance().getForClass(TileEntity.REGISTRY.getObject(name));
            final BlockState state = buffer.getBlock(x - offsetX, y - offsetY, z - offsetZ);
            // fixers.
            if (type != null && SpongeImplHooks.hasBlockTileEntity(((Block) state.getType()), BlockUtil.toNative(state))) {
                TileEntityArchetype archetype = new SpongeTileEntityArchetypeBuilder().state(state).tileData(tile).tile(type).build();
                tiles.put(new Vector3i(x - offsetX, y - offsetY, z - offsetZ), archetype);
            }
        }
    }
    SpongeSchematic schematic = new SpongeSchematic(buffer, tiles);
    return schematic;
}
Also used : BlockPalette(org.spongepowered.api.world.schematic.BlockPalette) 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) TileEntityArchetype(org.spongepowered.api.block.tileentity.TileEntityArchetype)

Example 4 with DataView

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

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

the class NbtTranslator method setInternal.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static void setInternal(NBTBase base, byte type, DataView view, String key) {
    checkNotNull(base);
    checkNotNull(view);
    checkNotNull(key);
    checkArgument(!key.isEmpty());
    checkArgument(type > NbtDataUtil.TAG_END && type <= NbtDataUtil.TAG_INT_ARRAY);
    switch(type) {
        case NbtDataUtil.TAG_BYTE:
            if (key.contains(BOOLEAN_IDENTIFER)) {
                view.set(of(key.replace(BOOLEAN_IDENTIFER, "")), (((NBTTagByte) base).getByte() != 0));
            } else {
                view.set(of(key), ((NBTTagByte) base).getByte());
            }
            break;
        case NbtDataUtil.TAG_SHORT:
            view.set(of(key), ((NBTTagShort) base).getShort());
            break;
        case NbtDataUtil.TAG_INT:
            view.set(of(key), ((NBTTagInt) base).getInt());
            break;
        case NbtDataUtil.TAG_LONG:
            view.set(of(key), ((NBTTagLong) base).getLong());
            break;
        case NbtDataUtil.TAG_FLOAT:
            view.set(of(key), ((NBTTagFloat) base).getFloat());
            break;
        case NbtDataUtil.TAG_DOUBLE:
            view.set(of(key), ((NBTTagDouble) base).getDouble());
            break;
        case NbtDataUtil.TAG_BYTE_ARRAY:
            view.set(of(key), ((NBTTagByteArray) base).getByteArray());
            break;
        case NbtDataUtil.TAG_STRING:
            view.set(of(key), ((NBTTagString) base).getString());
            break;
        case NbtDataUtil.TAG_LIST:
            NBTTagList list = (NBTTagList) base;
            byte listType = (byte) list.getTagType();
            int count = list.tagCount();
            List objectList = Lists.newArrayListWithCapacity(count);
            for (int i = 0; i < count; i++) {
                objectList.add(fromTagBase(list.get(i), listType));
            }
            view.set(of(key), objectList);
            break;
        case NbtDataUtil.TAG_COMPOUND:
            DataView internalView = view.createView(of(key));
            NBTTagCompound compound = (NBTTagCompound) base;
            for (String internalKey : compound.getKeySet()) {
                NBTBase internalBase = compound.getTag(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.
                setInternal(internalBase, internalType, internalView, internalKey);
            }
            break;
        case NbtDataUtil.TAG_INT_ARRAY:
            view.set(of(key), ((NBTTagIntArray) base).getIntArray());
            break;
        default:
            throw new IllegalArgumentException("Unknown NBT type " + type);
    }
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) DataView(org.spongepowered.api.data.DataView) NBTBase(net.minecraft.nbt.NBTBase) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) NBTTagList(net.minecraft.nbt.NBTTagList) List(java.util.List) NBTTagString(net.minecraft.nbt.NBTTagString)

Aggregations

DataView (org.spongepowered.api.data.DataView)100 DataContainer (org.spongepowered.api.data.DataContainer)30 DataQuery (org.spongepowered.api.data.DataQuery)24 ArrayList (java.util.ArrayList)21 Map (java.util.Map)17 List (java.util.List)13 Vector3i (com.flowpowered.math.vector.Vector3i)11 LanternItemStack (org.lanternpowered.server.inventory.LanternItemStack)11 ItemStack (org.spongepowered.api.item.inventory.ItemStack)11 UUID (java.util.UUID)10 Nullable (javax.annotation.Nullable)10 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)10 ImmutableList (com.google.common.collect.ImmutableList)8 IOException (java.io.IOException)8 Test (org.junit.Test)8 CatalogType (org.spongepowered.api.CatalogType)8 Path (java.nio.file.Path)7 DataTypeSerializer (org.lanternpowered.server.data.persistence.DataTypeSerializer)7 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)7 ImmutableMap (com.google.common.collect.ImmutableMap)6