Search in sources :

Example 1 with MBlockPos

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

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 2 with MBlockPos

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

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)

Example 3 with MBlockPos

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

the class CommonProxy method copyChunkToSchematic.

public void copyChunkToSchematic(final ISchematic schematic, final World world, final int chunkX, final int chunkZ, final int minX, final int maxX, final int minY, final int maxY, final int minZ, final int maxZ) {
    final MBlockPos pos = new MBlockPos();
    final MBlockPos localPos = new MBlockPos();
    final int localMinX = minX < (chunkX << 4) ? 0 : (minX & 15);
    final int localMaxX = maxX > ((chunkX << 4) + 15) ? 15 : (maxX & 15);
    final int localMinZ = minZ < (chunkZ << 4) ? 0 : (minZ & 15);
    final int localMaxZ = maxZ > ((chunkZ << 4) + 15) ? 15 : (maxZ & 15);
    for (int chunkLocalX = localMinX; chunkLocalX <= localMaxX; chunkLocalX++) {
        for (int chunkLocalZ = localMinZ; chunkLocalZ <= localMaxZ; chunkLocalZ++) {
            for (int y = minY; y <= maxY; y++) {
                final int x = chunkLocalX | (chunkX << 4);
                final int z = chunkLocalZ | (chunkZ << 4);
                final int localX = x - minX;
                final int localY = y - minY;
                final int localZ = z - minZ;
                pos.set(x, y, z);
                localPos.set(localX, localY, localZ);
                try {
                    final IBlockState blockState = world.getBlockState(pos);
                    final Block block = blockState.getBlock();
                    final boolean success = schematic.setBlockState(localPos, blockState);
                    if (success && block.hasTileEntity(blockState)) {
                        final TileEntity tileEntity = world.getTileEntity(pos);
                        if (tileEntity != null) {
                            try {
                                final TileEntity reloadedTileEntity = NBTHelper.reloadTileEntity(tileEntity, minX, minY, minZ);
                                schematic.setTileEntity(localPos, reloadedTileEntity);
                            } catch (final NBTConversionException nce) {
                                Reference.logger.error("Error while trying to save tile entity '{}'!", tileEntity, nce);
                                schematic.setBlockState(localPos, Blocks.BEDROCK.getDefaultState());
                            }
                        }
                    }
                } catch (final Exception e) {
                    Reference.logger.error("Something went wrong!", e);
                }
            }
        }
    }
    final int minX1 = localMinX | (chunkX << 4);
    final int minZ1 = localMinZ | (chunkZ << 4);
    final int maxX1 = localMaxX | (chunkX << 4);
    final int maxZ1 = localMaxZ | (chunkZ << 4);
    final AxisAlignedBB bb = new AxisAlignedBB(minX1, minY, minZ1, maxX1 + 1, maxY + 1, maxZ1 + 1);
    final List<Entity> entities = world.getEntitiesWithinAABB(Entity.class, bb);
    for (final Entity entity : entities) {
        try {
            final Entity reloadedEntity = NBTHelper.reloadEntity(entity, minX, minY, minZ);
            schematic.addEntity(reloadedEntity);
        } catch (final NBTConversionException nce) {
            Reference.logger.error("Error while trying to save entity '{}'!", entity, nce);
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) Entity(net.minecraft.entity.Entity) TileEntity(net.minecraft.tileentity.TileEntity) IBlockState(net.minecraft.block.state.IBlockState) MBlockPos(com.github.lunatrius.core.util.math.MBlockPos) Block(net.minecraft.block.Block) NBTConversionException(com.github.lunatrius.schematica.nbt.NBTConversionException) NBTConversionException(com.github.lunatrius.schematica.nbt.NBTConversionException) IOException(java.io.IOException)

Example 4 with MBlockPos

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

the class Printer method OnUpdate.

@SubscribeEvent
void OnUpdate(PlayerUpdateEvent event) {
    if (ClientProxy.schematic == null)
        return;
    if (cooldown > 0) {
        cooldown--;
        return;
    }
    final double dX = ClientProxy.playerPosition.x - ClientProxy.schematic.position.x;
    final double dY = ClientProxy.playerPosition.y - ClientProxy.schematic.position.y;
    final double dZ = ClientProxy.playerPosition.z - ClientProxy.schematic.position.z;
    final int x = (int) Math.floor(dX);
    final int y = (int) Math.floor(dY);
    final int z = (int) Math.floor(dZ);
    final int range = 5;
    final int minX = Math.max(0, x - range);
    final int maxX = Math.min(ClientProxy.schematic.getWidth() - 1, x + range);
    int minY = Math.max(0, y - range);
    int maxY = Math.min(ClientProxy.schematic.getHeight() - 1, y + range);
    final int minZ = Math.max(0, z - range);
    final int maxZ = Math.min(ClientProxy.schematic.getLength() - 1, z + range);
    if (minX > maxX || minY > maxY || minZ > maxZ) {
        return;
    }
    for (final MBlockPos p : BlockPosHelper.getAllInBoxXZY(minX, minY, minZ, maxX, maxY, maxZ)) {
        BlockPos worldpos = p.add(ClientProxy.schematic.position);
        if (mc.world.getBlockState(worldpos).getBlock().material.isReplaceable()) {
            Block b = ClientProxy.schematic.getBlockState(p).getBlock();
            if (b != Blocks.AIR) {
                BlockInteractUtil.BlockPlaceResult res = BlockInteractUtil.tryPlaceBlock(p, new SpecBlockSwitchItem(Blocks.TNT), true, true, 4, false);
                if (res != BlockInteractUtil.BlockPlaceResult.FAILED) {
                    if (res == BlockInteractUtil.BlockPlaceResult.PLACED) {
                        if (render.getValue())
                            new FadePos(p, fill, true);
                        cooldown = delay.getValue();
                    }
                    return;
                }
            }
        }
    }
}
Also used : FadePos(me.wallhacks.spark.util.objects.FadePos) BlockInteractUtil(me.wallhacks.spark.util.player.BlockInteractUtil) MBlockPos(com.github.lunatrius.core.util.math.MBlockPos) Block(net.minecraft.block.Block) BlockPos(net.minecraft.util.math.BlockPos) MBlockPos(com.github.lunatrius.core.util.math.MBlockPos) SpecBlockSwitchItem(me.wallhacks.spark.util.player.itemswitcher.itemswitchers.SpecBlockSwitchItem) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 5 with MBlockPos

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

the class SchematicAlpha method readFromNBT.

@Override
public Object readFromNBT(final NBTTagCompound tagCompound) {
    final ItemStack icon = SchematicUtil.getIconFromNBT(tagCompound);
    final byte[] localBlocks = tagCompound.getByteArray(Names.NBT.BLOCKS);
    final byte[] localMetadata = tagCompound.getByteArray(Names.NBT.DATA);
    boolean extra = false;
    byte[] extraBlocks = null;
    byte[] extraBlocksNibble = null;
    if (tagCompound.hasKey(Names.NBT.ADD_BLOCKS)) {
        extra = true;
        extraBlocksNibble = tagCompound.getByteArray(Names.NBT.ADD_BLOCKS);
        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(Names.NBT.ADD_BLOCKS_SCHEMATICA)) {
        extra = true;
        extraBlocks = tagCompound.getByteArray(Names.NBT.ADD_BLOCKS_SCHEMATICA);
    }
    final short width = tagCompound.getShort(Names.NBT.WIDTH);
    final short length = tagCompound.getShort(Names.NBT.LENGTH);
    final short height = tagCompound.getShort(Names.NBT.HEIGHT);
    Short id = null;
    final Map<Short, Short> oldToNew = new HashMap<Short, Short>();
    if (tagCompound.hasKey(Names.NBT.MAPPING_SCHEMATICA)) {
        final NBTTagCompound mapping = tagCompound.getCompoundTag(Names.NBT.MAPPING_SCHEMATICA);
        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(Names.NBT.TILE_ENTITIES, 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

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