Search in sources :

Example 1 with BlockBPMultipart

use of com.bluepowermod.block.BlockBPMultipart in project BluePower by Qmunity.

the class BPEventHandler method blockHighlightEvent.

@OnlyIn(Dist.CLIENT)
@SubscribeEvent
public void blockHighlightEvent(DrawHighlightEvent event) {
    PlayerEntity player = Minecraft.getInstance().player;
    if (player == null) {
        return;
    }
    World world = player.level;
    if (world == null) {
        return;
    }
    RayTraceResult mop = event.getTarget();
    if (mop instanceof BlockRayTraceResult) {
        BlockPos pos = ((BlockRayTraceResult) mop).getBlockPos();
        BlockState state = world.getBlockState(pos);
        if (state.getBlock() instanceof BlockBPMultipart) {
            BlockState partstate = MultipartUtils.getClosestState(player, pos);
            IVertexBuilder builder = event.getBuffers().getBuffer(RenderType.lines());
            if (partstate != null) {
                VoxelShape shape = partstate.getShape(world, pos, ISelectionContext.of(player));
                Vector3d projectedView = event.getInfo().getPosition();
                double d0 = pos.getX() - projectedView.x();
                double d1 = pos.getY() - projectedView.y();
                double d2 = pos.getZ() - projectedView.z();
                Matrix4f matrix4f = event.getMatrix().last().pose();
                shape.forAllEdges((startX, startY, startZ, endX, endY, endZ) -> {
                    builder.vertex(matrix4f, (float) (startX + d0), (float) (startY + d1), (float) (startZ + d2)).color(0.0F, 0.0F, 0.0F, 0.4F).endVertex();
                    builder.vertex(matrix4f, (float) (endX + d0), (float) (endY + d1), (float) (endZ + d2)).color(0.0F, 0.0F, 0.0F, 0.4F).endVertex();
                });
                event.setCanceled(true);
            }
        }
    }
}
Also used : Matrix4f(net.minecraft.util.math.vector.Matrix4f) BlockState(net.minecraft.block.BlockState) VoxelShape(net.minecraft.util.math.shapes.VoxelShape) Vector3d(net.minecraft.util.math.vector.Vector3d) RayTraceResult(net.minecraft.util.math.RayTraceResult) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) BlockBPMultipart(com.bluepowermod.block.BlockBPMultipart) BlockPos(net.minecraft.util.math.BlockPos) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) World(net.minecraft.world.World) PlayerEntity(net.minecraft.entity.player.PlayerEntity) IVertexBuilder(com.mojang.blaze3d.vertex.IVertexBuilder) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent) OnlyIn(net.minecraftforge.api.distmarker.OnlyIn)

Example 2 with BlockBPMultipart

use of com.bluepowermod.block.BlockBPMultipart in project BluePower by Qmunity.

the class ItemBPPart method place.

@Override
public ActionResultType place(BlockItemUseContext context) {
    BlockState state = context.getLevel().getBlockState(context.getClickedPos());
    BlockState thisState = getBlock().getStateForPlacement(context);
    if (state.getBlock() instanceof IBPPartBlock && thisState != null && !AABBUtils.testOcclusion(((IBPPartBlock) thisState.getBlock()).getOcclusionShape(thisState), state.getShape(context.getLevel(), context.getClickedPos()))) {
        // Save the Tile Entity Data
        CompoundNBT nbt = new CompoundNBT();
        TileEntity tileEntity = context.getLevel().getBlockEntity(context.getClickedPos());
        if (tileEntity != null) {
            nbt = tileEntity.save(nbt);
        }
        // Replace with Multipart
        context.getLevel().setBlockAndUpdate(context.getClickedPos(), BPBlocks.multipart.defaultBlockState());
        tileEntity = context.getLevel().getBlockEntity(context.getClickedPos());
        if (tileEntity instanceof TileBPMultipart) {
            // Add the original State to the Multipart
            ((TileBPMultipart) tileEntity).addState(state);
            // Restore the Tile Entity Data
            TileEntity tile = ((TileBPMultipart) tileEntity).getTileForState(state);
            if (tile != null)
                tile.load(state, nbt);
            // Add the new State
            ((TileBPMultipart) tileEntity).addState(thisState);
            thisState.getBlock().setPlacedBy(context.getLevel(), context.getClickedPos(), thisState, context.getPlayer(), context.getItemInHand());
        }
        // Update Self
        state.neighborChanged(context.getLevel(), context.getClickedPos(), state.getBlock(), context.getClickedPos(), false);
        context.getItemInHand().shrink(1);
        // Place Sound
        context.getLevel().playSound(null, context.getClickedPos(), SoundEvents.STONE_PLACE, SoundCategory.BLOCKS, 1.0F, 1.0F);
        return ActionResultType.SUCCESS;
    } else if (state.getBlock() instanceof BlockBPMultipart && thisState != null && !AABBUtils.testOcclusion(((IBPPartBlock) thisState.getBlock()).getOcclusionShape(thisState), state.getShape(context.getLevel(), context.getClickedPos()))) {
        // Add to the Existing Multipart
        TileEntity tileEntity = context.getLevel().getBlockEntity(context.getClickedPos());
        if (tileEntity instanceof TileBPMultipart) {
            ((TileBPMultipart) tileEntity).addState(thisState);
            thisState.getBlock().setPlacedBy(context.getLevel(), context.getClickedPos(), thisState, context.getPlayer(), context.getItemInHand());
            // Update Neighbors
            for (Direction dir : Direction.values()) {
                context.getLevel().getBlockState(context.getClickedPos().relative(dir)).neighborChanged(context.getLevel(), context.getClickedPos().relative(dir), state.getBlock(), context.getClickedPos(), false);
            }
            // Update Self
            state.neighborChanged(context.getLevel(), context.getClickedPos(), state.getBlock(), context.getClickedPos(), false);
            context.getItemInHand().shrink(1);
            // Place Sound
            context.getLevel().playSound(null, context.getClickedPos(), SoundEvents.STONE_PLACE, SoundCategory.BLOCKS, 1.0F, 1.0F);
            return ActionResultType.SUCCESS;
        }
    }
    return super.place(context);
}
Also used : IBPPartBlock(com.bluepowermod.api.multipart.IBPPartBlock) TileEntity(net.minecraft.tileentity.TileEntity) BlockState(net.minecraft.block.BlockState) CompoundNBT(net.minecraft.nbt.CompoundNBT) BlockBPMultipart(com.bluepowermod.block.BlockBPMultipart) TileBPMultipart(com.bluepowermod.tile.TileBPMultipart) Direction(net.minecraft.util.Direction)

Example 3 with BlockBPMultipart

use of com.bluepowermod.block.BlockBPMultipart in project BluePower by Qmunity.

the class Renderers method init.

public static void init() {
    ClientRegistry.bindTileEntityRenderer(BPTileEntityType.LAMP, RenderLamp::new);
    ClientRegistry.bindTileEntityRenderer(BPTileEntityType.ENGINE, RenderEngine::new);
    for (Item item : BPItems.itemList) {
        if (item instanceof IBPColoredItem) {
            Minecraft.getInstance().getItemColors().register(new BPItemColor(), item);
        }
    }
    for (Block block : BPBlocks.blockList) {
        if (block instanceof IBPColoredBlock) {
            Minecraft.getInstance().getBlockColors().register(new BPBlockColor(), block);
            Minecraft.getInstance().getItemColors().register(new BPBlockColor(), Item.byBlock(block));
        }
        if (block instanceof BlockLampSurface || block instanceof BlockGateBase || block instanceof BlockBattery)
            RenderTypeLookup.setRenderLayer(block, RenderType.cutout());
        if (block instanceof BlockBPGlass || block instanceof BlockBPMicroblock || block instanceof BlockBPMultipart)
            RenderTypeLookup.setRenderLayer(block, RenderType.translucent());
    }
    RenderTypeLookup.setRenderLayer(BPBlocks.indigo_flower, RenderType.cutout());
    RenderTypeLookup.setRenderLayer(BPBlocks.flax_crop, RenderType.cutout());
    RenderTypeLookup.setRenderLayer(BPBlocks.cracked_basalt_lava, RenderType.cutout());
    RenderTypeLookup.setRenderLayer(BPBlocks.cracked_basalt_decorative, RenderType.cutout());
    RenderTypeLookup.setRenderLayer(BPBlocks.rubber_leaves, RenderType.cutout());
    RenderTypeLookup.setRenderLayer(BPBlocks.rubber_sapling, RenderType.cutout());
    RenderTypeLookup.setRenderLayer(BPBlocks.tube, RenderType.cutout());
}
Also used : BlockBPMultipart(com.bluepowermod.block.BlockBPMultipart) BlockLampSurface(com.bluepowermod.block.lighting.BlockLampSurface) BlockBPMicroblock(com.bluepowermod.block.BlockBPMicroblock) BlockBattery(com.bluepowermod.block.power.BlockBattery) BlockBPGlass(com.bluepowermod.block.worldgen.BlockBPGlass) Item(net.minecraft.item.Item) Block(net.minecraft.block.Block) BlockGateBase(com.bluepowermod.block.gates.BlockGateBase)

Aggregations

BlockBPMultipart (com.bluepowermod.block.BlockBPMultipart)3 BlockState (net.minecraft.block.BlockState)2 IBPPartBlock (com.bluepowermod.api.multipart.IBPPartBlock)1 BlockBPMicroblock (com.bluepowermod.block.BlockBPMicroblock)1 BlockGateBase (com.bluepowermod.block.gates.BlockGateBase)1 BlockLampSurface (com.bluepowermod.block.lighting.BlockLampSurface)1 BlockBattery (com.bluepowermod.block.power.BlockBattery)1 BlockBPGlass (com.bluepowermod.block.worldgen.BlockBPGlass)1 TileBPMultipart (com.bluepowermod.tile.TileBPMultipart)1 IVertexBuilder (com.mojang.blaze3d.vertex.IVertexBuilder)1 Block (net.minecraft.block.Block)1 PlayerEntity (net.minecraft.entity.player.PlayerEntity)1 Item (net.minecraft.item.Item)1 CompoundNBT (net.minecraft.nbt.CompoundNBT)1 TileEntity (net.minecraft.tileentity.TileEntity)1 Direction (net.minecraft.util.Direction)1 BlockPos (net.minecraft.util.math.BlockPos)1 BlockRayTraceResult (net.minecraft.util.math.BlockRayTraceResult)1 RayTraceResult (net.minecraft.util.math.RayTraceResult)1 VoxelShape (net.minecraft.util.math.shapes.VoxelShape)1