Search in sources :

Example 81 with BlockState

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

the class SpongeBlockSnapshot method with.

@Override
public Optional<BlockSnapshot> with(ImmutableDataManipulator<?, ?> valueContainer) {
    if (((IMixinBlock) this.blockState.getType()).supports((Class<ImmutableDataManipulator<?, ?>>) valueContainer.getClass())) {
        final BlockState newState;
        boolean changeState = false;
        if (this.blockState.supports((Class<ImmutableDataManipulator<?, ?>>) valueContainer.getClass())) {
            newState = this.blockState.with(valueContainer).get();
            changeState = true;
        } else {
            newState = this.blockState;
        }
        if (changeState) {
            return Optional.of(createBuilder().blockState(newState).build());
        }
        final SpongeBlockSnapshotBuilder builder = createBuilder();
        builder.add(valueContainer);
        return Optional.of(builder.build());
    }
    return Optional.of(createBuilder().add(valueContainer).build());
}
Also used : IMixinBlock(org.spongepowered.common.interfaces.block.IMixinBlock) BlockState(org.spongepowered.api.block.BlockState) IBlockState(net.minecraft.block.state.IBlockState) ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator)

Example 82 with BlockState

use of org.spongepowered.api.block.BlockState 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 83 with BlockState

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

the class MinecartBlockDataProcessor method getValues.

@Override
protected Map<Key<?>, ?> getValues(EntityMinecart entity) {
    BlockState state = (BlockState) entity.getDisplayTile();
    int offset = entity.getDisplayTileOffset();
    return ImmutableMap.of(Keys.REPRESENTED_BLOCK, state, Keys.OFFSET, offset);
}
Also used : BlockState(org.spongepowered.api.block.BlockState) IBlockState(net.minecraft.block.state.IBlockState)

Example 84 with BlockState

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

the class MinecartBlockDataProcessor method set.

@Override
protected boolean set(EntityMinecart entity, Map<Key<?>, Object> keyValues) {
    BlockState type = (BlockState) keyValues.get(Keys.REPRESENTED_BLOCK);
    int offset = (Integer) keyValues.get(Keys.OFFSET);
    entity.setDisplayTileOffset(offset);
    entity.setDisplayTile((IBlockState) type);
    return true;
}
Also used : BlockState(org.spongepowered.api.block.BlockState) IBlockState(net.minecraft.block.state.IBlockState)

Example 85 with BlockState

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

the class MinecartBlockDataProcessor method fill.

@Override
public Optional<MinecartBlockData> fill(DataContainer container, MinecartBlockData data) {
    if (!container.contains(Keys.REPRESENTED_BLOCK.getQuery()) || !container.contains(Keys.OFFSET.getQuery())) {
        return Optional.empty();
    }
    BlockState block = container.getSerializable(Keys.REPRESENTED_BLOCK.getQuery(), BlockState.class).get();
    int offset = container.getInt(Keys.OFFSET.getQuery()).get();
    data.set(Keys.REPRESENTED_BLOCK, block);
    data.set(Keys.OFFSET, offset);
    return Optional.of(data);
}
Also used : BlockState(org.spongepowered.api.block.BlockState) IBlockState(net.minecraft.block.state.IBlockState)

Aggregations

BlockState (org.spongepowered.api.block.BlockState)133 World (org.spongepowered.api.world.World)39 IBlockState (net.minecraft.block.state.IBlockState)29 BlockType (org.spongepowered.api.block.BlockType)27 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)22 Direction (org.spongepowered.api.util.Direction)21 Optional (java.util.Optional)20 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)20 Vector3i (com.flowpowered.math.vector.Vector3i)19 Location (org.spongepowered.api.world.Location)18 ItemStack (org.spongepowered.api.item.inventory.ItemStack)17 LocatableBlock (org.spongepowered.api.world.LocatableBlock)14 Sponge (org.spongepowered.api.Sponge)13 ItemType (org.spongepowered.api.item.ItemType)13 ArrayList (java.util.ArrayList)12 Player (org.spongepowered.api.entity.living.player.Player)12 List (java.util.List)11 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)11 Vector3d (com.flowpowered.math.vector.Vector3d)10 Listener (org.spongepowered.api.event.Listener)10