Search in sources :

Example 21 with MBlockPos

use of com.github.lunatrius.core.util.math.MBlockPos in project defrag by Edouard127.

the class SchematicAlpha method writeToNBT.

@Override
public boolean writeToNBT(final NBTTagCompound tagCompound, final ISchematic schematic) {
    final NBTTagCompound tagCompoundIcon = new NBTTagCompound();
    final ItemStack icon = schematic.getIcon();
    icon.writeToNBT(tagCompoundIcon);
    tagCompound.setTag(Names.NBT.ICON, tagCompoundIcon);
    tagCompound.setShort(Names.NBT.WIDTH, (short) schematic.getWidth());
    tagCompound.setShort(Names.NBT.LENGTH, (short) schematic.getLength());
    tagCompound.setShort(Names.NBT.HEIGHT, (short) schematic.getHeight());
    final int size = schematic.getWidth() * schematic.getLength() * schematic.getHeight();
    final byte[] localBlocks = new byte[size];
    final byte[] localMetadata = new byte[size];
    final byte[] extraBlocks = new byte[size];
    final byte[] extraBlocksNibble = new byte[(int) Math.ceil(size / 2.0)];
    boolean extra = false;
    final MBlockPos pos = new MBlockPos();
    final Map<String, Short> mappings = new HashMap<String, Short>();
    for (int x = 0; x < schematic.getWidth(); x++) {
        for (int y = 0; y < schematic.getHeight(); y++) {
            for (int z = 0; z < schematic.getLength(); z++) {
                final int index = x + (y * schematic.getLength() + z) * schematic.getWidth();
                final IBlockState blockState = schematic.getBlockState(pos.set(x, y, z));
                final Block block = blockState.getBlock();
                final int blockId = Block.REGISTRY.getIDForObject(block);
                localBlocks[index] = (byte) blockId;
                localMetadata[index] = (byte) block.getMetaFromState(blockState);
                if ((extraBlocks[index] = (byte) (blockId >> 8)) > 0) {
                    extra = true;
                }
                final String name = String.valueOf(Block.REGISTRY.getNameForObject(block));
                if (!mappings.containsKey(name)) {
                    mappings.put(name, (short) blockId);
                }
            }
        }
    }
    int count = 20;
    final NBTTagList tileEntitiesList = new NBTTagList();
    for (final TileEntity tileEntity : schematic.getTileEntities()) {
        try {
            final NBTTagCompound tileEntityTagCompound = NBTHelper.writeTileEntityToCompound(tileEntity);
            tileEntitiesList.appendTag(tileEntityTagCompound);
        } catch (final Exception e) {
            final BlockPos tePos = tileEntity.getPos();
            final int index = tePos.getX() + (tePos.getY() * schematic.getLength() + tePos.getZ()) * schematic.getWidth();
            if (--count > 0) {
                final IBlockState blockState = schematic.getBlockState(tePos);
                final Block block = blockState.getBlock();
                Reference.logger.error("Block {}[{}] with TileEntity {} failed to save! Replacing with bedrock...", block, block != null ? Block.REGISTRY.getNameForObject(block) : "?", tileEntity.getClass().getName(), e);
            }
            localBlocks[index] = (byte) Block.REGISTRY.getIDForObject(Blocks.BEDROCK);
            localMetadata[index] = 0;
            extraBlocks[index] = 0;
        }
    }
    for (int i = 0; i < extraBlocksNibble.length; i++) {
        if (i * 2 + 1 < extraBlocks.length) {
            extraBlocksNibble[i] = (byte) ((extraBlocks[i * 2 + 0] << 4) | extraBlocks[i * 2 + 1]);
        } else {
            extraBlocksNibble[i] = (byte) (extraBlocks[i * 2 + 0] << 4);
        }
    }
    final NBTTagList entityList = new NBTTagList();
    final List<Entity> entities = schematic.getEntities();
    for (final Entity entity : entities) {
        try {
            final NBTTagCompound entityCompound = NBTHelper.writeEntityToCompound(entity);
            if (entityCompound != null) {
                entityList.appendTag(entityCompound);
            }
        } catch (final Throwable t) {
            Reference.logger.error("Entity {} failed to save, skipping!", entity, t);
        }
    }
    final PreSchematicSaveEvent event = new PreSchematicSaveEvent(schematic, mappings);
    MinecraftForge.EVENT_BUS.post(event);
    final NBTTagCompound nbtMapping = new NBTTagCompound();
    for (final Map.Entry<String, Short> entry : mappings.entrySet()) {
        nbtMapping.setShort(entry.getKey(), entry.getValue());
    }
    tagCompound.setString(Names.NBT.MATERIALS, Names.NBT.FORMAT_ALPHA);
    tagCompound.setByteArray(Names.NBT.BLOCKS, localBlocks);
    tagCompound.setByteArray(Names.NBT.DATA, localMetadata);
    if (extra) {
        tagCompound.setByteArray(Names.NBT.ADD_BLOCKS, extraBlocksNibble);
    }
    tagCompound.setTag(Names.NBT.ENTITIES, entityList);
    tagCompound.setTag(Names.NBT.TILE_ENTITIES, tileEntitiesList);
    tagCompound.setTag(Names.NBT.MAPPING_SCHEMATICA, nbtMapping);
    final NBTTagCompound extendedMetadata = event.extendedMetadata;
    return true;
}
Also used : Entity(net.minecraft.entity.Entity) TileEntity(net.minecraft.tileentity.TileEntity) 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) Block(net.minecraft.block.Block) BlockPos(net.minecraft.util.math.BlockPos) MBlockPos(com.github.lunatrius.core.util.math.MBlockPos) ItemStack(net.minecraft.item.ItemStack) HashMap(java.util.HashMap) Map(java.util.Map) PreSchematicSaveEvent(com.github.lunatrius.schematica.api.event.PreSchematicSaveEvent)

Aggregations

MBlockPos (com.github.lunatrius.core.util.math.MBlockPos)21 IBlockState (net.minecraft.block.state.IBlockState)13 TileEntity (net.minecraft.tileentity.TileEntity)10 Block (net.minecraft.block.Block)9 BlockPos (net.minecraft.util.math.BlockPos)8 ISchematic (com.github.lunatrius.schematica.api.ISchematic)6 Schematic (com.github.lunatrius.schematica.world.storage.Schematic)6 ItemStack (net.minecraft.item.ItemStack)5 Vec3i (net.minecraft.util.math.Vec3i)4 HashMap (java.util.HashMap)3 Entity (net.minecraft.entity.Entity)3 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)3 NBTTagList (net.minecraft.nbt.NBTTagList)3 NBTConversionException (com.github.lunatrius.schematica.nbt.NBTConversionException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 BlockLiquid (net.minecraft.block.BlockLiquid)2 BlockSlab (net.minecraft.block.BlockSlab)2 ResourceLocation (net.minecraft.util.ResourceLocation)2 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)2