Search in sources :

Example 61 with BlockState

use of net.minecraft.world.level.block.state.BlockState in project MC-Prefab by Brian-Wuest.

the class StructureRenderHandler method renderComponentInWorld.

private static boolean renderComponentInWorld(Level world, BuildBlock buildBlock, MultiBufferSource.BufferSource entityVertexConsumer, PoseStack matrixStack, BlockPos pos) {
    // Don't render this block if it's going to overlay a non-air/water block.
    BlockState targetBlock = world.getBlockState(pos);
    if (targetBlock.getMaterial() != Material.AIR && targetBlock.getMaterial() != Material.WATER) {
        return false;
    }
    StructureRenderHandler.doRenderComponent(world, buildBlock, pos, entityVertexConsumer, matrixStack);
    if (buildBlock.getSubBlock() != null) {
        Block foundBlock = Registry.BLOCK.get(buildBlock.getSubBlock().getResourceLocation());
        BlockState blockState = foundBlock.defaultBlockState();
        BuildBlock subBlock = BuildBlock.SetBlockState(StructureRenderHandler.currentConfiguration, StructureRenderHandler.currentConfiguration.pos, buildBlock.getSubBlock(), foundBlock, blockState, StructureRenderHandler.currentStructure.getClearSpace().getShape().getDirection());
        BlockPos subBlockPos = subBlock.getStartingPosition().getRelativePosition(StructureRenderHandler.currentConfiguration.pos, StructureRenderHandler.currentStructure.getClearSpace().getShape().getDirection(), StructureRenderHandler.currentConfiguration.houseFacing);
        RenderShape subBlockRenderType = subBlock.getBlockState().getRenderShape();
        return StructureRenderHandler.renderComponentInWorld(world, subBlock, entityVertexConsumer, matrixStack, subBlockPos);
    }
    return true;
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) RenderShape(net.minecraft.world.level.block.RenderShape) BuildBlock(com.wuest.prefab.structures.base.BuildBlock) BuildBlock(com.wuest.prefab.structures.base.BuildBlock) Block(net.minecraft.world.level.block.Block) BlockPos(net.minecraft.core.BlockPos)

Example 62 with BlockState

use of net.minecraft.world.level.block.state.BlockState in project MC-Prefab by Brian-Wuest.

the class StructureRenderHandler method renderPlayerLook.

/**
 * This is to render the currently bound structure.
 *
 * @param player The player to render the structure for.
 * @param src    The ray trace for where the player is currently looking.
 */
public static void renderPlayerLook(Player player, HitResult src, PoseStack matrixStack) {
    if (StructureRenderHandler.currentStructure != null && StructureRenderHandler.dimension == player.level.dimensionType().logicalHeight() && StructureRenderHandler.currentConfiguration != null && CommonProxy.proxyConfiguration.serverConfiguration.enableStructurePreview) {
        rendering = true;
        MultiBufferSource.BufferSource entityVertexConsumer = Minecraft.getInstance().renderBuffers().bufferSource();
        Frustum frustum = new Frustum(matrixStack.last().pose(), RenderSystem.getProjectionMatrix());
        BlockPos cameraPos = player.eyeBlockPosition();
        frustum.prepare(cameraPos.getX(), cameraPos.getY(), cameraPos.getZ());
        for (BuildBlock buildBlock : StructureRenderHandler.currentStructure.getBlocks()) {
            Block foundBlock = Registry.BLOCK.get(buildBlock.getResourceLocation());
            if (foundBlock != null) {
                // In order to get the proper relative position I also need the structure's original facing.
                BlockPos pos = buildBlock.getStartingPosition().getRelativePosition(StructureRenderHandler.currentConfiguration.pos, StructureRenderHandler.currentStructure.getClearSpace().getShape().getDirection(), StructureRenderHandler.currentConfiguration.houseFacing);
                // Don't render the block if it isn't visible (cull)
                AABB box = new AABB(pos.getX() - 0.5, pos.getY() - 0.5, pos.getZ() - 0.5, pos.getX() + 1.5, pos.getY() + 1.5, pos.getZ() + 1.5);
                if (!frustum.isVisible(box)) {
                    continue;
                }
                // Get the unique block state for this block.
                BlockState blockState = foundBlock.defaultBlockState();
                buildBlock = BuildBlock.SetBlockState(StructureRenderHandler.currentConfiguration, StructureRenderHandler.currentConfiguration.pos, buildBlock, foundBlock, blockState, StructureRenderHandler.currentStructure.getClearSpace().getShape().getDirection());
                StructureRenderHandler.renderComponentInWorld(player.level, buildBlock, entityVertexConsumer, matrixStack, pos);
            }
        }
        // Draw function.
        entityVertexConsumer.endBatch(Sheets.translucentItemSheet());
        if (!StructureRenderHandler.showedMessage) {
            Minecraft mc = Minecraft.getInstance();
            // Stop narrator from continuing narrating what was in the structure GUI
            Narrator.getNarrator().clear();
            TranslatableComponent message = new TranslatableComponent(GuiLangKeys.GUI_PREVIEW_NOTICE);
            message.setStyle(Style.EMPTY.withColor(ChatFormatting.GREEN));
            mc.gui.handleChat(ChatType.CHAT, message, Util.NIL_UUID);
            message = new TranslatableComponent(GuiLangKeys.GUI_BLOCK_CLICKED);
            message.setStyle(Style.EMPTY.withColor(ChatFormatting.YELLOW));
            mc.gui.handleChat(ChatType.CHAT, message, Util.NIL_UUID);
            StructureRenderHandler.showedMessage = true;
        }
    }
}
Also used : Frustum(net.minecraft.client.renderer.culling.Frustum) BlockState(net.minecraft.world.level.block.state.BlockState) BuildBlock(com.wuest.prefab.structures.base.BuildBlock) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) MultiBufferSource(net.minecraft.client.renderer.MultiBufferSource) BuildBlock(com.wuest.prefab.structures.base.BuildBlock) Block(net.minecraft.world.level.block.Block) BlockPos(net.minecraft.core.BlockPos) Minecraft(net.minecraft.client.Minecraft) AABB(net.minecraft.world.phys.AABB)

Example 63 with BlockState

use of net.minecraft.world.level.block.state.BlockState in project MC-Prefab by Brian-Wuest.

the class IGrassSpreadable method DetermineGrassSpread.

/**
 * Determines if grass should spread to this block.
 *
 * @param state   The state of the current block.
 * @param worldIn The server world the block resides in.
 * @param pos     The position of the block.
 * @param random  The random value used for checking.
 */
default void DetermineGrassSpread(BlockState state, ServerLevel worldIn, BlockPos pos, Random random) {
    if (!worldIn.isClientSide) {
        // This is equivalent to light level 9.
        if (worldIn.getBrightness(pos.above()) >= 0.2727273) {
            for (int i = 0; i < 4; ++i) {
                BlockPos blockpos = pos.offset(random.nextInt(3) - 1, random.nextInt(5) - 3, random.nextInt(3) - 1);
                if (blockpos.getY() >= 0 && blockpos.getY() < 256 && !worldIn.isLoaded(blockpos)) {
                    return;
                }
                BlockState iblockstate1 = worldIn.getBlockState(blockpos);
                if ((iblockstate1.getBlock() == Blocks.GRASS_BLOCK || iblockstate1.getBlock() == ModRegistry.GrassStairs.get() || iblockstate1.getBlock() == ModRegistry.GrassWall.get() || iblockstate1.getBlock() == ModRegistry.GrassSlab.get()) && // This equivalent to light level 4.
                worldIn.getBrightness(blockpos.above()) >= 0.08333334) {
                    BlockState grassState = this.getGrassBlockState(state);
                    worldIn.setBlock(pos, grassState, 3);
                }
            }
        }
    }
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) BlockPos(net.minecraft.core.BlockPos)

Example 64 with BlockState

use of net.minecraft.world.level.block.state.BlockState in project MC-Prefab by Brian-Wuest.

the class BlockPhasing method findNeighborPhasicBlocks.

/**
 * Sets the powered status and updates the block's neighbor.
 *
 * @param worldIn           The world where the block resides.
 * @param pos               The position of the block.
 * @param desiredBlockState The current state of the block at the position.
 * @param cascadeCount      The number of times it has cascaded.
 * @param cascadedBlockPos  The list of cascaded block positions, this is used to determine if this block should
 *                          be processed again.
 * @param setCurrentBlock   Determines if the current block should be set.
 */
private int findNeighborPhasicBlocks(Level worldIn, BlockPos pos, BlockState desiredBlockState, int cascadeCount, ArrayList<BlockPos> cascadedBlockPos, boolean setCurrentBlock) {
    cascadeCount++;
    if (cascadeCount > 100) {
        return cascadeCount;
    }
    if (setCurrentBlock) {
        cascadedBlockPos.add(pos);
    }
    for (Direction facing : Direction.values()) {
        Block neighborBlock = worldIn.getBlockState(pos.relative(facing)).getBlock();
        if (neighborBlock instanceof BlockPhasing) {
            BlockState blockState = worldIn.getBlockState(pos.relative(facing));
            // If the block is already in the correct state or was already checked, there is no need to cascade to
            // it's neighbors.
            EnumPhasingProgress progress = blockState.getValue(Phasing_Progress);
            if (cascadedBlockPos.contains(pos.relative(facing)) || progress == desiredBlockState.getValue(Phasing_Progress)) {
                continue;
            }
            setCurrentBlock = true;
            cascadeCount = this.findNeighborPhasicBlocks(worldIn, pos.relative(facing), desiredBlockState, cascadeCount, cascadedBlockPos, setCurrentBlock);
            if (cascadeCount > 100) {
                break;
            }
        }
    }
    return cascadeCount;
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) Block(net.minecraft.world.level.block.Block) Direction(net.minecraft.core.Direction)

Example 65 with BlockState

use of net.minecraft.world.level.block.state.BlockState in project MinecraftForge by MinecraftForge.

the class ForgeEventFactory method onBlockPlace.

public static boolean onBlockPlace(@Nullable Entity entity, @Nonnull BlockSnapshot blockSnapshot, @Nonnull Direction direction) {
    BlockState placedAgainst = blockSnapshot.getLevel().getBlockState(blockSnapshot.getPos().relative(direction.getOpposite()));
    EntityPlaceEvent event = new BlockEvent.EntityPlaceEvent(blockSnapshot, placedAgainst, entity);
    return MinecraftForge.EVENT_BUS.post(event);
}
Also used : EntityPlaceEvent(net.minecraftforge.event.world.BlockEvent.EntityPlaceEvent) BlockState(net.minecraft.world.level.block.state.BlockState)

Aggregations

BlockState (net.minecraft.world.level.block.state.BlockState)141 BlockPos (net.minecraft.core.BlockPos)75 Direction (net.minecraft.core.Direction)18 Level (net.minecraft.world.level.Level)14 Block (net.minecraft.world.level.block.Block)14 ArrayList (java.util.ArrayList)10 ServerLevel (net.minecraft.server.level.ServerLevel)10 SoundType (net.minecraft.world.level.block.SoundType)10 Vec3 (net.minecraft.world.phys.Vec3)10 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)9 Random (java.util.Random)8 ItemStack (net.minecraft.world.item.ItemStack)7 FluidState (net.minecraft.world.level.material.FluidState)7 AABB (net.minecraft.world.phys.AABB)6 BuildBlock (com.wuest.prefab.structures.base.BuildBlock)5 LevelChunk (net.minecraft.world.level.chunk.LevelChunk)5 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)5 LocationTag (com.denizenscript.denizen.objects.LocationTag)4 FakeBlock (com.denizenscript.denizen.utilities.blocks.FakeBlock)4 HashMap (java.util.HashMap)4