use of de.industria.structureprocessor.JigsawTemplateProcessor.JigsawReplacement 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);
}
}
}
Aggregations