Search in sources :

Example 1 with BuildBlock

use of com.wuest.prefab.structures.base.BuildBlock 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 2 with BuildBlock

use of com.wuest.prefab.structures.base.BuildBlock 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 3 with BuildBlock

use of com.wuest.prefab.structures.base.BuildBlock in project MC-Prefab by Brian-Wuest.

the class StructureInstantBridge method setupStructure.

public void setupStructure(InstantBridgeConfiguration configuration, BlockPos originalPos) {
    ArrayList<BuildBlock> buildingBlocks = new ArrayList<BuildBlock>();
    BlockState materialState = configuration.bridgeMaterial.getBlockType();
    Direction facing = Direction.SOUTH;
    BlockState torchState = Blocks.TORCH.defaultBlockState();
    BlockState glassState = Blocks.GLASS_PANE.defaultBlockState();
    int interiorHeightOffSet = configuration.interiorHeight - 3;
    for (int i = 1; i <= configuration.bridgeLength; i++) {
        BlockPos currentPos = originalPos.relative(facing, i);
        // Place the floor
        buildingBlocks.add(Structure.createBuildBlockFromBlockState(materialState, materialState.getBlock(), currentPos.relative(facing.getCounterClockWise(), 2), originalPos));
        buildingBlocks.add(Structure.createBuildBlockFromBlockState(materialState, materialState.getBlock(), currentPos.relative(facing.getCounterClockWise()), originalPos));
        buildingBlocks.add(Structure.createBuildBlockFromBlockState(materialState, materialState.getBlock(), currentPos, originalPos));
        buildingBlocks.add(Structure.createBuildBlockFromBlockState(materialState, materialState.getBlock(), currentPos.relative(facing.getClockWise()), originalPos));
        buildingBlocks.add(Structure.createBuildBlockFromBlockState(materialState, materialState.getBlock(), currentPos.relative(facing.getClockWise(), 2), originalPos));
        // Build the walls.
        buildingBlocks.add(Structure.createBuildBlockFromBlockState(materialState, materialState.getBlock(), currentPos.relative(facing.getCounterClockWise(), 2).above(), originalPos));
        buildingBlocks.add(Structure.createBuildBlockFromBlockState(materialState, materialState.getBlock(), currentPos.relative(facing.getClockWise(), 2).above(), originalPos));
        if (configuration.includeRoof) {
            // Build the roof.
            buildingBlocks.add(Structure.createBuildBlockFromBlockState(materialState, materialState.getBlock(), currentPos.relative(facing.getCounterClockWise(), 2).above(3 + interiorHeightOffSet), originalPos));
            buildingBlocks.add(Structure.createBuildBlockFromBlockState(materialState, materialState.getBlock(), currentPos.relative(facing.getCounterClockWise()).above(4 + interiorHeightOffSet), originalPos));
            buildingBlocks.add(Structure.createBuildBlockFromBlockState(materialState, materialState.getBlock(), currentPos.above(4 + interiorHeightOffSet), originalPos));
            buildingBlocks.add(Structure.createBuildBlockFromBlockState(materialState, materialState.getBlock(), currentPos.relative(facing.getClockWise()).above(4 + interiorHeightOffSet), originalPos));
            buildingBlocks.add(Structure.createBuildBlockFromBlockState(materialState, materialState.getBlock(), currentPos.relative(facing.getClockWise(), 2).above(3 + interiorHeightOffSet), originalPos));
        }
        for (int j = 0; j <= interiorHeightOffSet; j++) {
            if ((i == 1 || i % 6 == 0) && j == 0) {
                // Place torches.
                buildingBlocks.add(Structure.createBuildBlockFromBlockState(torchState, torchState.getBlock(), currentPos.relative(facing.getCounterClockWise(), 2).above(2), originalPos));
                buildingBlocks.add(Structure.createBuildBlockFromBlockState(torchState, torchState.getBlock(), currentPos.relative(facing.getClockWise(), 2).above(2), originalPos));
            } else if (configuration.includeRoof) {
                // Place Glass panes
                buildingBlocks.add(Structure.createBuildBlockFromBlockState(glassState, glassState.getBlock(), currentPos.relative(facing.getCounterClockWise(), 2).above(2 + j), originalPos));
                buildingBlocks.add(Structure.createBuildBlockFromBlockState(glassState, glassState.getBlock(), currentPos.relative(facing.getClockWise(), 2).above(2 + j), originalPos));
            }
        }
    }
    this.setBlocks(buildingBlocks);
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) BuildBlock(com.wuest.prefab.structures.base.BuildBlock) ArrayList(java.util.ArrayList) BlockPos(net.minecraft.core.BlockPos) Direction(net.minecraft.core.Direction)

Example 4 with BuildBlock

use of com.wuest.prefab.structures.base.BuildBlock in project MC-Prefab by Brian-Wuest.

the class StructureGuiWorld method setupBlocks.

/**
 * Call this function after setting the clear shape and the blocks for the class.
 */
public void setupBlocks() {
    if (this.buildShape != null && this.blocks != null && this.blocks.size() > 0) {
        BlockPos startPosition = new BlockPos(0, 0, 0);
        this.blocksByPosition.clear();
        HashMap<Long, BlockState> blocksByPosition = new HashMap<>();
        for (BuildBlock buildBlock : this.blocks) {
            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(startPosition, this.buildShape.getDirection(), structureConfiguration.houseFacing);
                // Get the unique block state for this block.
                BlockState blockState = foundBlock.defaultBlockState();
                BuildBlock updatedBuildBlock = BuildBlock.SetBlockState(this.structureConfiguration, startPosition, buildBlock, foundBlock, blockState, this.buildShape.getDirection());
                blocksByPosition.put(pos.asLong(), updatedBuildBlock.getBlockState());
                // If there is a sub block (like for doors), do that now.
                BuildBlock subBlock = buildBlock.getSubBlock();
                if (subBlock != null) {
                    // In order to get the proper relative position I also need the structure's original facing.
                    BlockPos subPos = subBlock.getStartingPosition().getRelativePosition(startPosition, this.buildShape.getDirection(), structureConfiguration.houseFacing);
                    // Get the unique block state for this block.
                    BlockState subBlockState = foundBlock.defaultBlockState();
                    BuildBlock updatedSubBuildBlock = BuildBlock.SetBlockState(this.structureConfiguration, startPosition, subBlock, foundBlock, subBlockState, this.buildShape.getDirection());
                    blocksByPosition.put(subPos.asLong(), updatedSubBuildBlock.getBlockState());
                }
            }
        }
        // Don't do this until the end when we're ready to render the blocks.
        this.blocksByPosition = blocksByPosition;
    }
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) BuildBlock(com.wuest.prefab.structures.base.BuildBlock) HashMap(java.util.HashMap) BuildBlock(com.wuest.prefab.structures.base.BuildBlock) Block(net.minecraft.world.level.block.Block) BlockPos(net.minecraft.core.BlockPos)

Example 5 with BuildBlock

use of com.wuest.prefab.structures.base.BuildBlock in project MC-Prefab by Brian-Wuest.

the class StructureEventHandler method setBlock.

private static int setBlock(int i, Structure structure, ArrayList<Structure> structuresToRemove) {
    // Anything which should be air will be air
    if (structure.clearedBlockPos.size() > 0 && !structure.hasAirBlocks) {
        BlockPos currentPos = structure.clearedBlockPos.get(0);
        structure.clearedBlockPos.remove(0);
        BlockState clearBlockState = structure.world.getBlockState(currentPos);
        // generation anyways.
        if (clearBlockState.getMaterial() != Material.AIR) {
            structure.BeforeClearSpaceBlockReplaced(currentPos);
            for (Direction adjacentBlock : Direction.values()) {
                BlockPos tempPos = currentPos.relative(adjacentBlock);
                BlockState foundState = structure.world.getBlockState(tempPos);
                Block foundBlock = foundState.getBlock();
                // Check if this block is one that is attached to a facing, if it is, remove it first.
                if (foundBlock instanceof TorchBlock || foundBlock instanceof SignBlock || foundBlock instanceof LeverBlock || foundBlock instanceof ButtonBlock || foundBlock instanceof BedBlock || foundBlock instanceof CarpetBlock || foundBlock instanceof FlowerPotBlock || foundBlock instanceof SugarCaneBlock || foundBlock instanceof BasePressurePlateBlock || foundBlock instanceof DoorBlock || foundBlock instanceof LadderBlock || foundBlock instanceof VineBlock || foundBlock instanceof RedStoneWireBlock || foundBlock instanceof DiodeBlock || foundBlock instanceof AbstractBannerBlock || foundBlock instanceof LanternBlock || foundBlock instanceof BaseRailBlock) {
                    structure.BeforeClearSpaceBlockReplaced(currentPos);
                    if (!(foundBlock instanceof BedBlock)) {
                        structure.world.removeBlock(tempPos, false);
                    } else if (foundBlock instanceof DoorBlock) {
                        // Make sure to remove both parts before going on.
                        DoubleBlockHalf currentHalf = foundState.getValue(BlockStateProperties.DOUBLE_BLOCK_HALF);
                        BlockPos otherHalfPos = currentHalf == DoubleBlockHalf.LOWER ? tempPos.above() : tempPos.below();
                        structure.world.setBlock(tempPos, Blocks.AIR.defaultBlockState(), 35);
                        structure.world.setBlock(otherHalfPos, Blocks.AIR.defaultBlockState(), 35);
                    } else {
                        // Found a bed, try to find the other part of the bed and remove it.
                        for (Direction currentDirection : Direction.values()) {
                            BlockPos bedPos = tempPos.relative(currentDirection);
                            BlockState bedState = structure.world.getBlockState(bedPos);
                            if (bedState.getBlock() instanceof BedBlock) {
                                // found the other part of the bed. Remove the current block and this one.
                                structure.world.setBlock(tempPos, Blocks.AIR.defaultBlockState(), 35);
                                structure.world.setBlock(bedPos, Blocks.AIR.defaultBlockState(), 35);
                                break;
                            }
                        }
                    }
                }
            }
            structure.world.removeBlock(currentPos, false);
        } else {
            // This is just an air block, move onto the next block don't need to wait for the next tick.
            i--;
        }
        return i;
    }
    BuildBlock currentBlock = null;
    if (structure.priorityOneBlocks.size() > 0) {
        currentBlock = structure.priorityOneBlocks.get(0);
        structure.priorityOneBlocks.remove(0);
    } else {
        // There are no more blocks to set.
        structuresToRemove.add(structure);
        return 999;
    }
    BlockState state = currentBlock.getBlockState();
    BlockPos setBlockPos = currentBlock.getStartingPosition().getRelativePosition(structure.originalPos, structure.getClearSpace().getShape().getDirection(), structure.configuration.houseFacing);
    BuildingMethods.ReplaceBlock(structure.world, setBlockPos, state, 2);
    // correct order.
    if (currentBlock.getSubBlock() != null) {
        BuildBlock subBlock = currentBlock.getSubBlock();
        BuildingMethods.ReplaceBlock(structure.world, subBlock.getStartingPosition().getRelativePosition(structure.originalPos, structure.getClearSpace().getShape().getDirection(), structure.configuration.houseFacing), subBlock.getBlockState(), BlockFlags.DEFAULT_AND_RERENDER);
    }
    return i;
}
Also used : Direction(net.minecraft.core.Direction) NetworkDirection(net.minecraftforge.network.NetworkDirection) BlockState(net.minecraft.world.level.block.state.BlockState) BuildBlock(com.wuest.prefab.structures.base.BuildBlock) DoubleBlockHalf(net.minecraft.world.level.block.state.properties.DoubleBlockHalf) BuildBlock(com.wuest.prefab.structures.base.BuildBlock) BlockPos(net.minecraft.core.BlockPos)

Aggregations

BuildBlock (com.wuest.prefab.structures.base.BuildBlock)5 BlockPos (net.minecraft.core.BlockPos)5 BlockState (net.minecraft.world.level.block.state.BlockState)5 Block (net.minecraft.world.level.block.Block)3 Direction (net.minecraft.core.Direction)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Minecraft (net.minecraft.client.Minecraft)1 MultiBufferSource (net.minecraft.client.renderer.MultiBufferSource)1 Frustum (net.minecraft.client.renderer.culling.Frustum)1 TranslatableComponent (net.minecraft.network.chat.TranslatableComponent)1 RenderShape (net.minecraft.world.level.block.RenderShape)1 DoubleBlockHalf (net.minecraft.world.level.block.state.properties.DoubleBlockHalf)1 AABB (net.minecraft.world.phys.AABB)1 NetworkDirection (net.minecraftforge.network.NetworkDirection)1