Search in sources :

Example 11 with MBlockPos

use of com.github.lunatrius.core.util.math.MBlockPos in project Spark-Client by Spark-Client-Development.

the class RotationHelper method rotate.

public Schematic rotate(final ISchematic schematic, final EnumFacing axis, final boolean forced) throws RotationException {
    final Vec3i dimensionsRotated = rotateDimensions(axis, schematic.getWidth(), schematic.getHeight(), schematic.getLength());
    final Schematic schematicRotated = new Schematic(schematic.getIcon(), dimensionsRotated.getX(), dimensionsRotated.getY(), dimensionsRotated.getZ(), schematic.getAuthor());
    final MBlockPos tmp = new MBlockPos();
    for (final MBlockPos pos : BlockPosHelper.getAllInBox(0, 0, 0, schematic.getWidth() - 1, schematic.getHeight() - 1, schematic.getLength() - 1)) {
        final IBlockState blockState = schematic.getBlockState(pos);
        final IBlockState blockStateRotated = rotateBlock(blockState, axis, forced);
        schematicRotated.setBlockState(rotatePos(pos, axis, dimensionsRotated, tmp), blockStateRotated);
    }
    final List<TileEntity> tileEntities = schematic.getTileEntities();
    for (final TileEntity tileEntity : tileEntities) {
        final BlockPos pos = tileEntity.getPos();
        tileEntity.setPos(new BlockPos(rotatePos(pos, axis, dimensionsRotated, tmp)));
        schematicRotated.setTileEntity(tileEntity.getPos(), tileEntity);
    }
    return schematicRotated;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) Vec3i(net.minecraft.util.math.Vec3i) IBlockState(net.minecraft.block.state.IBlockState) MBlockPos(com.github.lunatrius.core.util.math.MBlockPos) BlockPos(net.minecraft.util.math.BlockPos) MBlockPos(com.github.lunatrius.core.util.math.MBlockPos) ISchematic(com.github.lunatrius.schematica.api.ISchematic) Schematic(com.github.lunatrius.schematica.world.storage.Schematic)

Example 12 with MBlockPos

use of com.github.lunatrius.core.util.math.MBlockPos in project Spark-Client by Spark-Client-Development.

the class SchematicWorld method replaceBlock.

@SuppressWarnings({ "rawtypes", "unchecked" })
public int replaceBlock(final BlockStateMatcher matcher, final BlockStateReplacer replacer, final Map<IProperty, Comparable> properties) {
    int count = 0;
    for (final MBlockPos pos : BlockPosHelper.getAllInBox(0, 0, 0, getWidth(), getHeight(), getLength())) {
        final IBlockState blockState = this.schematic.getBlockState(pos);
        // TODO: add support for tile entities?
        if (blockState.getBlock().hasTileEntity(blockState)) {
            continue;
        }
        if (matcher.apply(blockState)) {
            final IBlockState replacement = replacer.getReplacement(blockState, properties);
            // TODO: add support for tile entities?
            if (replacement.getBlock().hasTileEntity(replacement)) {
                continue;
            }
            if (this.schematic.setBlockState(pos, replacement)) {
                notifyBlockUpdate(pos.add(this.position), blockState, replacement, 3);
                count++;
            }
        }
    }
    return count;
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) MBlockPos(com.github.lunatrius.core.util.math.MBlockPos)

Example 13 with MBlockPos

use of com.github.lunatrius.core.util.math.MBlockPos 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)

Example 14 with MBlockPos

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

the class BlockList method getList.

public List<WrappedItemStack> getList(final EntityPlayer player, final SchematicWorld world, final World mcWorld) {
    final List<WrappedItemStack> blockList = new ArrayList<WrappedItemStack>();
    if (world == null) {
        return blockList;
    }
    final RayTraceResult rtr = new RayTraceResult(player);
    final MBlockPos mcPos = new MBlockPos();
    for (final MBlockPos pos : BlockPosHelper.getAllInBox(BlockPos.ORIGIN, new BlockPos(world.getWidth() - 1, world.getHeight() - 1, world.getLength() - 1))) {
        if (!world.layerMode.shouldUseLayer(world, pos.getY())) {
            continue;
        }
        final IBlockState blockState = world.getBlockState(pos);
        final Block block = blockState.getBlock();
        if (block == Blocks.AIR || world.isAirBlock(pos)) {
            continue;
        }
        mcPos.set(world.position.add(pos));
        final IBlockState mcBlockState = mcWorld.getBlockState(mcPos);
        final boolean isPlaced = BlockStateHelper.areBlockStatesEqual(blockState, mcBlockState);
        ItemStack stack = ItemStack.EMPTY;
        try {
            stack = block.getPickBlock(blockState, rtr, world, pos, player);
        } catch (final Exception e) {
            Reference.logger.warn("Could not get the pick block for: {}", blockState, e);
        }
        if (block instanceof IFluidBlock || block instanceof BlockLiquid) {
            final IFluidHandler fluidHandler = FluidUtil.getFluidHandler(world, pos, null);
            final FluidActionResult fluidActionResult = FluidUtil.tryFillContainer(new ItemStack(Items.BUCKET), fluidHandler, 1000, null, false);
            if (fluidActionResult.isSuccess()) {
                final ItemStack result = fluidActionResult.getResult();
                if (!result.isEmpty()) {
                    stack = result;
                }
            }
        }
        if (stack == null) {
            Reference.logger.error("Could not find the item for: {} (getPickBlock() returned null, this is a bug)", blockState);
            continue;
        }
        if (stack.isEmpty()) {
            Reference.logger.warn("Could not find the item for: {}", blockState);
            continue;
        }
        int count = 1;
        // TODO: this has to be generalized for all blocks; just a temporary "fix"
        if (block instanceof BlockSlab) {
            if (((BlockSlab) block).isDouble()) {
                count = 2;
            }
        }
        final WrappedItemStack wrappedItemStack = findOrCreateWrappedItemStackFor(blockList, stack);
        if (isPlaced) {
            wrappedItemStack.placed += count;
        }
        wrappedItemStack.total += count;
    }
    for (WrappedItemStack wrappedItemStack : blockList) {
        if (player.capabilities.isCreativeMode) {
            wrappedItemStack.inventory = -1;
        } else {
            wrappedItemStack.inventory = EntityHelper.getItemCountInInventory(player.inventory, wrappedItemStack.itemStack.getItem(), wrappedItemStack.itemStack.getItemDamage());
        }
    }
    return blockList;
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) MBlockPos(com.github.lunatrius.core.util.math.MBlockPos) ArrayList(java.util.ArrayList) RayTraceResult(net.minecraft.util.math.RayTraceResult) FluidActionResult(net.minecraftforge.fluids.FluidActionResult) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) BlockLiquid(net.minecraft.block.BlockLiquid) BlockSlab(net.minecraft.block.BlockSlab) IFluidBlock(net.minecraftforge.fluids.IFluidBlock) IFluidBlock(net.minecraftforge.fluids.IFluidBlock) Block(net.minecraft.block.Block) BlockPos(net.minecraft.util.math.BlockPos) MBlockPos(com.github.lunatrius.core.util.math.MBlockPos) ItemStack(net.minecraft.item.ItemStack)

Example 15 with MBlockPos

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

the class FlipHelper method flip.

public Schematic flip(final ISchematic schematic, final EnumFacing axis, final boolean forced) throws FlipException {
    final Vec3i dimensionsFlipped = new Vec3i(schematic.getWidth(), schematic.getHeight(), schematic.getLength());
    final Schematic schematicFlipped = new Schematic(schematic.getIcon(), dimensionsFlipped.getX(), dimensionsFlipped.getY(), dimensionsFlipped.getZ(), schematic.getAuthor());
    final MBlockPos tmp = new MBlockPos();
    for (final MBlockPos pos : BlockPosHelper.getAllInBox(0, 0, 0, schematic.getWidth() - 1, schematic.getHeight() - 1, schematic.getLength() - 1)) {
        final IBlockState blockState = schematic.getBlockState(pos);
        final IBlockState blockStateFlipped = flipBlock(blockState, axis, forced);
        schematicFlipped.setBlockState(flipPos(pos, axis, dimensionsFlipped, tmp), blockStateFlipped);
    }
    final List<TileEntity> tileEntities = schematic.getTileEntities();
    for (final TileEntity tileEntity : tileEntities) {
        final BlockPos pos = tileEntity.getPos();
        tileEntity.setPos(new BlockPos(flipPos(pos, axis, dimensionsFlipped, tmp)));
        schematicFlipped.setTileEntity(tileEntity.getPos(), tileEntity);
    }
    return schematicFlipped;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) Vec3i(net.minecraft.util.math.Vec3i) IBlockState(net.minecraft.block.state.IBlockState) MBlockPos(com.github.lunatrius.core.util.math.MBlockPos) BlockPos(net.minecraft.util.math.BlockPos) MBlockPos(com.github.lunatrius.core.util.math.MBlockPos) ISchematic(com.github.lunatrius.schematica.api.ISchematic) Schematic(com.github.lunatrius.schematica.world.storage.Schematic)

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