Search in sources :

Example 1 with JigsawTemplateProcessor

use of de.industria.structureprocessor.JigsawTemplateProcessor in project MCMOD-Industria by M-Marvin.

the class TileEntityJigsaw method generateStructure.

public void generateStructure(boolean keepJigsaws, int levels, Random rand) {
    if (!this.level.isClientSide()) {
        boolean hasAlreadyGenerated = this.level.getBlockState(worldPosition.relative(this.getFacing())).getBlock() == ModItems.jigsaw;
        ServerWorld world = (ServerWorld) this.level;
        ListNBT list = JigsawFileManager.getPoolList(world, this.poolFile);
        if (list != null && levels > 0 && !hasAlreadyGenerated && !this.targetName.getPath().equals("empty")) {
            HashMap<Integer, Integer> structures = new HashMap<Integer, Integer>();
            int index = 0;
            int structIndex = 0;
            for (int i = 0; i < list.size(); i++) {
                CompoundNBT entry = list.getCompound(i);
                int chance = entry.getInt("chance");
                for (int i1 = 0; i1 < chance; i1++) {
                    structures.put(index++, structIndex);
                }
                structIndex++;
            }
            int randomStructureId = structures.size() > 1 ? structures.get(rand.nextInt(index - 1)) : structures.get(0);
            CompoundNBT structureNBT = list.getCompound(randomStructureId);
            ResourceLocation resourceStructure = ResourceLocation.tryParse(structureNBT.getString("file"));
            JigsawReplacement replaceMode = JigsawReplacement.fromName(structureNBT.getString("replaceBlocks"));
            ListNBT blockList = structureNBT.contains("blocks") ? structureNBT.getList("blocks", 8) : new ListNBT();
            List<BlockState> blocks = new ArrayList<BlockState>();
            for (int i = 0; i < blockList.size(); i++) {
                StringNBT stringNBT = (StringNBT) blockList.get(i);
                BlockStateParser parser = new BlockStateParser(new StringReader(stringNBT.getAsString()), true);
                try {
                    parser.parse(false);
                    blocks.add(parser.getState());
                } catch (CommandSyntaxException e) {
                    Industria.LOGGER.warn("Cant parse replace-list block " + stringNBT.getAsString() + " in jigsaw metafile " + resourceStructure + "!");
                }
            }
            Template template = JigsawFileManager.getTemplate(world, resourceStructure);
            if (template != null) {
                JigsawType alowedType = this.getBlockState().getValue(BlockJigsaw.TYPE).getOppesite();
                List<BlockInfo> jigsawBlocks = template.filterBlocks(BlockPos.ZERO, new PlacementSettings(), ModItems.jigsaw);
                List<BlockInfo> filteredJigsaws = new ArrayList<BlockInfo>();
                for (BlockInfo block : jigsawBlocks) {
                    ResourceLocation jigsawName = ResourceLocation.tryParse(block.nbt.getString("name"));
                    if (jigsawName.toString().equals(this.targetName.toString()) && block.state.getValue(BlockJigsaw.TYPE) == alowedType)
                        filteredJigsaws.add(block);
                }
                if (filteredJigsaws.size() != 0) {
                    BlockInfo randomJigsaw = filteredJigsaws.size() > 1 ? filteredJigsaws.get(rand.nextInt(filteredJigsaws.size())) : filteredJigsaws.get(0);
                    Rotation rotation = Rotation.NONE;
                    if (alowedType != JigsawType.HORIZONTAL && !this.lockOrientation) {
                        rotation = Rotation.getRandom(rand);
                    } else {
                        Direction compare1 = this.getBlockState().getValue(BlockJigsaw.FACING);
                        Direction compare2 = alowedType == JigsawType.HORIZONTAL ? randomJigsaw.state.getValue(BlockJigsaw.FACING).getOpposite() : randomJigsaw.state.getValue(BlockJigsaw.FACING);
                        if (compare1 == compare2) {
                            rotation = Rotation.NONE;
                        } else if (compare1 == compare2.getOpposite()) {
                            rotation = Rotation.CLOCKWISE_180;
                        } else if (compare1.getCounterClockWise() == compare2) {
                            rotation = Rotation.CLOCKWISE_90;
                        } else if (compare1.getClockWise() == compare2) {
                            rotation = Rotation.COUNTERCLOCKWISE_90;
                        }
                    }
                    PlacementSettings placement = (new PlacementSettings()).setMirror(Mirror.NONE).setRotation(rotation).setIgnoreEntities(false).setChunkPos((ChunkPos) null).addProcessor(new JigsawTemplateProcessor(replaceMode, blocks));
                    BlockPos offset = randomJigsaw.pos;
                    offset = Template.transform(offset, Mirror.NONE, rotation, BlockPos.ZERO);
                    offset = offset.relative(this.getFacing().getOpposite());
                    BlockPos generationPos = this.worldPosition.subtract(offset);
                    template.placeInWorldChunk(world, generationPos, placement, rand);
                    for (BlockInfo jigsaw : jigsawBlocks) {
                        BlockPos transformedPos = Template.transform(jigsaw.pos, Mirror.NONE, rotation, BlockPos.ZERO);
                        transformedPos = generationPos.offset(transformedPos);
                        TileEntity tileEntity = world.getBlockEntity(transformedPos);
                        if (tileEntity instanceof TileEntityJigsaw) {
                            ((TileEntityJigsaw) tileEntity).generateStructure(keepJigsaws, levels - 1, rand);
                        }
                    }
                }
            }
        }
        if (!keepJigsaws) {
            world.setBlock(worldPosition, replaceState, 2);
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PlacementSettings(net.minecraft.world.gen.feature.template.PlacementSettings) Direction(net.minecraft.util.Direction) Template(net.minecraft.world.gen.feature.template.Template) ServerWorld(net.minecraft.world.server.ServerWorld) TileEntity(net.minecraft.tileentity.TileEntity) ITickableTileEntity(net.minecraft.tileentity.ITickableTileEntity) BlockInfo(net.minecraft.world.gen.feature.template.Template.BlockInfo) ResourceLocation(net.minecraft.util.ResourceLocation) JigsawType(de.industria.blocks.BlockJigsaw.JigsawType) StringReader(com.mojang.brigadier.StringReader) ChunkPos(net.minecraft.util.math.ChunkPos) JigsawTemplateProcessor(de.industria.structureprocessor.JigsawTemplateProcessor) BlockPos(net.minecraft.util.math.BlockPos) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException) CompoundNBT(net.minecraft.nbt.CompoundNBT) StringNBT(net.minecraft.nbt.StringNBT) BlockStateParser(net.minecraft.command.arguments.BlockStateParser) Rotation(net.minecraft.util.Rotation) ListNBT(net.minecraft.nbt.ListNBT) BlockState(net.minecraft.block.BlockState) JigsawReplacement(de.industria.structureprocessor.JigsawTemplateProcessor.JigsawReplacement)

Aggregations

StringReader (com.mojang.brigadier.StringReader)1 CommandSyntaxException (com.mojang.brigadier.exceptions.CommandSyntaxException)1 JigsawType (de.industria.blocks.BlockJigsaw.JigsawType)1 JigsawTemplateProcessor (de.industria.structureprocessor.JigsawTemplateProcessor)1 JigsawReplacement (de.industria.structureprocessor.JigsawTemplateProcessor.JigsawReplacement)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 BlockState (net.minecraft.block.BlockState)1 BlockStateParser (net.minecraft.command.arguments.BlockStateParser)1 CompoundNBT (net.minecraft.nbt.CompoundNBT)1 ListNBT (net.minecraft.nbt.ListNBT)1 StringNBT (net.minecraft.nbt.StringNBT)1 ITickableTileEntity (net.minecraft.tileentity.ITickableTileEntity)1 TileEntity (net.minecraft.tileentity.TileEntity)1 Direction (net.minecraft.util.Direction)1 ResourceLocation (net.minecraft.util.ResourceLocation)1 Rotation (net.minecraft.util.Rotation)1 BlockPos (net.minecraft.util.math.BlockPos)1 ChunkPos (net.minecraft.util.math.ChunkPos)1 PlacementSettings (net.minecraft.world.gen.feature.template.PlacementSettings)1