Search in sources :

Example 6 with TileEntityArchetype

use of org.spongepowered.api.block.tileentity.TileEntityArchetype in project SpongeCommon by SpongePowered.

the class SchematicTranslator method addTo.

@Override
public DataView addTo(Schematic schematic, DataView data) {
    final int xMin = schematic.getBlockMin().getX();
    final int yMin = schematic.getBlockMin().getY();
    final int zMin = schematic.getBlockMin().getZ();
    final int width = schematic.getBlockSize().getX();
    final int height = schematic.getBlockSize().getY();
    final int length = schematic.getBlockSize().getZ();
    if (width > MAX_SIZE || height > MAX_SIZE || length > MAX_SIZE) {
        throw new IllegalArgumentException(String.format("Schematic is larger than maximum allowable size (found: (%d, %d, %d) max: (%d, %<d, %<d)", width, height, length, MAX_SIZE));
    }
    data.set(DataQueries.Schematic.WIDTH, width);
    data.set(DataQueries.Schematic.HEIGHT, height);
    data.set(DataQueries.Schematic.LENGTH, length);
    data.set(DataQueries.Schematic.VERSION, VERSION);
    for (DataQuery metaKey : schematic.getMetadata().getKeys(false)) {
        data.set(DataQueries.Schematic.METADATA.then(metaKey), schematic.getMetadata().get(metaKey).get());
    }
    int[] offset = new int[] { -xMin, -yMin, -zMin };
    data.set(DataQueries.Schematic.OFFSET, offset);
    BlockPalette palette = schematic.getPalette();
    ByteArrayOutputStream buffer = new ByteArrayOutputStream(width * height * length);
    for (int y = 0; y < height; y++) {
        int y0 = yMin + y;
        for (int z = 0; z < length; z++) {
            int z0 = zMin + z;
            for (int x = 0; x < width; x++) {
                int x0 = xMin + x;
                BlockState state = schematic.getBlock(x0, y0, z0);
                int id = palette.getOrAssign(state);
                while ((id & -128) != 0) {
                    buffer.write(id & 127 | 128);
                    id >>>= 7;
                }
                buffer.write(id);
            }
        }
    }
    data.set(DataQueries.Schematic.BLOCK_DATA, buffer.toByteArray());
    if (palette.getType() == BlockPaletteTypes.LOCAL) {
        DataQuery paletteQuery = DataQueries.Schematic.PALETTE;
        for (BlockState state : palette.getEntries()) {
            // getOrAssign to skip the optional, it will never assign
            data.set(paletteQuery.then(state.getId()), palette.getOrAssign(state));
        }
        data.set(DataQueries.Schematic.PALETTE_MAX, palette.getHighestId());
    }
    List<DataView> tileEntities = Lists.newArrayList();
    for (Map.Entry<Vector3i, TileEntityArchetype> entry : schematic.getTileEntityArchetypes().entrySet()) {
        Vector3i pos = entry.getKey();
        DataContainer tiledata = entry.getValue().getTileData();
        int[] apos = new int[] { pos.getX() - xMin, pos.getY() - yMin, pos.getZ() - zMin };
        tiledata.set(DataQueries.Schematic.TILEENTITY_POS, apos);
        if (!tiledata.contains(DataQueries.CONTENT_VERSION)) {
            // Set a default content version of 1
            tiledata.set(DataQueries.CONTENT_VERSION, 1);
        }
        tileEntities.add(tiledata);
    }
    data.set(DataQueries.Schematic.TILEENTITY_DATA, tileEntities);
    return data;
}
Also used : BlockPalette(org.spongepowered.api.world.schematic.BlockPalette) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataView(org.spongepowered.api.data.DataView) DataContainer(org.spongepowered.api.data.DataContainer) BlockState(org.spongepowered.api.block.BlockState) Vector3i(com.flowpowered.math.vector.Vector3i) DataQuery(org.spongepowered.api.data.DataQuery) TileEntityArchetype(org.spongepowered.api.block.tileentity.TileEntityArchetype) Map(java.util.Map)

Example 7 with TileEntityArchetype

use of org.spongepowered.api.block.tileentity.TileEntityArchetype in project SpongeCommon by SpongePowered.

the class SpongeArchetypeVolume method apply.

@Override
public void apply(Location<World> location, BlockChangeFlag changeFlag) {
    this.backing.getBlockWorker().iterate((v, x, y, z) -> {
        location.getExtent().setBlock(x + location.getBlockX(), y + location.getBlockY(), z + location.getBlockZ(), v.getBlock(x, y, z), changeFlag);
    });
    for (Vector3i pos : this.tiles.keySet()) {
        TileEntityArchetype archetype = this.tiles.get(pos);
        archetype.apply(location.add(pos));
    }
}
Also used : Vector3i(com.flowpowered.math.vector.Vector3i) TileEntityArchetype(org.spongepowered.api.block.tileentity.TileEntityArchetype)

Example 8 with TileEntityArchetype

use of org.spongepowered.api.block.tileentity.TileEntityArchetype in project SpongeCommon by SpongePowered.

the class SpongeSchematicBuilder method build.

@Override
public Schematic build() throws IllegalArgumentException {
    if (this.palette == null) {
        this.palette = this.type.create();
    }
    checkArgument(this.volume != null || this.view != null);
    Vector3i min;
    Vector3i size;
    if (this.volume != null) {
        min = this.volume.getBlockMin();
        size = this.volume.getBlockSize();
    } else {
        min = this.view.getBlockMin();
        size = this.view.getBlockSize();
    }
    if (this.metadata == null) {
        this.metadata = DataContainer.createNew();
    }
    for (Map.Entry<String, Object> entry : this.metaValues.entrySet()) {
        this.metadata.set(DataQuery.of('.', entry.getKey()), entry.getValue());
    }
    if (this.volume == null) {
        final MutableBlockVolume volume = new ArrayMutableBlockBuffer(this.palette, min, size);
        Map<Vector3i, TileEntityArchetype> tiles = Maps.newHashMap();
        this.view.getBlockWorker().iterate((v, x, y, z) -> {
            volume.setBlock(x, y, z, v.getBlock(x, y, z));
            Optional<TileEntity> tile = v.getTileEntity(x, y, z);
            if (tile.isPresent()) {
                tiles.put(new Vector3i(x, y, z), tile.get().createArchetype());
            }
        });
        return new SpongeSchematic(volume, tiles, this.metadata);
    }
    return new SpongeSchematic((SpongeArchetypeVolume) this.volume, this.metadata);
}
Also used : TileEntity(org.spongepowered.api.block.tileentity.TileEntity) MutableBlockVolume(org.spongepowered.api.world.extent.MutableBlockVolume) Vector3i(com.flowpowered.math.vector.Vector3i) TileEntityArchetype(org.spongepowered.api.block.tileentity.TileEntityArchetype) Map(java.util.Map) ArrayMutableBlockBuffer(org.spongepowered.common.util.gen.ArrayMutableBlockBuffer)

Aggregations

TileEntityArchetype (org.spongepowered.api.block.tileentity.TileEntityArchetype)8 Vector3i (com.flowpowered.math.vector.Vector3i)7 BlockState (org.spongepowered.api.block.BlockState)4 DataView (org.spongepowered.api.data.DataView)4 ArrayMutableBlockBuffer (org.spongepowered.common.util.gen.ArrayMutableBlockBuffer)4 Map (java.util.Map)3 ResourceLocation (net.minecraft.util.ResourceLocation)3 TileEntityType (org.spongepowered.api.block.tileentity.TileEntityType)3 MutableBlockVolume (org.spongepowered.api.world.extent.MutableBlockVolume)3 BlockPalette (org.spongepowered.api.world.schematic.BlockPalette)3 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)2 DataContainer (org.spongepowered.api.data.DataContainer)2 DataQuery (org.spongepowered.api.data.DataQuery)2 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)2 SpongeTileEntityArchetypeBuilder (org.spongepowered.common.block.SpongeTileEntityArchetypeBuilder)2 BimapPalette (org.spongepowered.common.world.schematic.BimapPalette)2 SpongeSchematic (org.spongepowered.common.world.schematic.SpongeSchematic)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ITileEntityProvider (net.minecraft.block.ITileEntityProvider)1 BlockType (org.spongepowered.api.block.BlockType)1