Search in sources :

Example 6 with Schematic

use of com.github.lunatrius.schematica.world.storage.Schematic in project Spark-Client by Spark-Client-Development.

the class SchematicAlpha method readFromNBT.

@Override
public ISchematic readFromNBT(final NBTTagCompound tagCompound) {
    final ItemStack icon = SchematicUtil.getIconFromNBT(tagCompound);
    final byte[] localBlocks = tagCompound.getByteArray("Blocks");
    final byte[] localMetadata = tagCompound.getByteArray("Data");
    boolean extra = false;
    byte[] extraBlocks = null;
    byte[] extraBlocksNibble = null;
    if (tagCompound.hasKey("AddBlocks")) {
        extra = true;
        extraBlocksNibble = tagCompound.getByteArray("AddBlocks");
        extraBlocks = new byte[extraBlocksNibble.length * 2];
        for (int i = 0; i < extraBlocksNibble.length; i++) {
            extraBlocks[i * 2 + 0] = (byte) ((extraBlocksNibble[i] >> 4) & 0xF);
            extraBlocks[i * 2 + 1] = (byte) (extraBlocksNibble[i] & 0xF);
        }
    } else if (tagCompound.hasKey("Add")) {
        extra = true;
        extraBlocks = tagCompound.getByteArray("Add");
    }
    final short width = tagCompound.getShort("Width");
    final short length = tagCompound.getShort("Length");
    final short height = tagCompound.getShort("Height");
    Short id = null;
    final Map<Short, Short> oldToNew = new HashMap<Short, Short>();
    if (tagCompound.hasKey("SchematicaMapping")) {
        final NBTTagCompound mapping = tagCompound.getCompoundTag("SchematicaMapping");
        final Set<String> names = mapping.getKeySet();
        for (final String name : names) {
            oldToNew.put(mapping.getShort(name), (short) Block.REGISTRY.getIDForObject(Block.REGISTRY.getObject(new ResourceLocation(name))));
        }
    }
    final MBlockPos pos = new MBlockPos();
    final ISchematic schematic = new Schematic(icon, width, height, length);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            for (int z = 0; z < length; z++) {
                final int index = x + (y * length + z) * width;
                int blockID = (localBlocks[index] & 0xFF) | (extra ? ((extraBlocks[index] & 0xFF) << 8) : 0);
                final int meta = localMetadata[index] & 0xFF;
                if ((id = oldToNew.get((short) blockID)) != null) {
                    blockID = id;
                }
                final Block block = Block.REGISTRY.getObjectById(blockID);
                pos.set(x, y, z);
                try {
                    final IBlockState blockState = block.getStateFromMeta(meta);
                    schematic.setBlockState(pos, blockState);
                } catch (final Exception e) {
                    Reference.logger.error("Could not set block state at {} to {} with metadata {}", pos, Block.REGISTRY.getNameForObject(block), meta, e);
                }
            }
        }
    }
    final NBTTagList tileEntitiesList = tagCompound.getTagList("TileEntities", Constants.NBT.TAG_COMPOUND);
    for (int i = 0; i < tileEntitiesList.tagCount(); i++) {
        try {
            final TileEntity tileEntity = NBTHelper.readTileEntityFromCompound(tileEntitiesList.getCompoundTagAt(i));
            if (tileEntity != null) {
                schematic.setTileEntity(tileEntity.getPos(), tileEntity);
            }
        } catch (final Exception e) {
            Reference.logger.error("TileEntity failed to load properly!", e);
        }
    }
    return schematic;
}
Also used : ISchematic(com.github.lunatrius.schematica.api.ISchematic) IBlockState(net.minecraft.block.state.IBlockState) HashMap(java.util.HashMap) MBlockPos(com.github.lunatrius.core.util.math.MBlockPos) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) NBTTagList(net.minecraft.nbt.NBTTagList) TileEntity(net.minecraft.tileentity.TileEntity) ResourceLocation(net.minecraft.util.ResourceLocation) Block(net.minecraft.block.Block) ItemStack(net.minecraft.item.ItemStack) ISchematic(com.github.lunatrius.schematica.api.ISchematic) Schematic(com.github.lunatrius.schematica.world.storage.Schematic)

Aggregations

ISchematic (com.github.lunatrius.schematica.api.ISchematic)6 Schematic (com.github.lunatrius.schematica.world.storage.Schematic)6 TileEntity (net.minecraft.tileentity.TileEntity)6 MBlockPos (com.github.lunatrius.core.util.math.MBlockPos)3 IBlockState (net.minecraft.block.state.IBlockState)3 ItemStack (net.minecraft.item.ItemStack)2 BlockPos (net.minecraft.util.math.BlockPos)2 Vec3i (net.minecraft.util.math.Vec3i)2 HashMap (java.util.HashMap)1 Block (net.minecraft.block.Block)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 NBTTagList (net.minecraft.nbt.NBTTagList)1 ResourceLocation (net.minecraft.util.ResourceLocation)1 Template (net.minecraft.world.gen.structure.template.Template)1