| 1 | package net.minecraft.world.level.levelgen.structure.templatesystem;
|
| 2 |
|
| 3 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import net.minecraft.SharedConstants;
|
| 7 | import net.minecraft.commands.arguments.blocks.BlockStateParser;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.core.registries.Registries;
|
| 10 | import net.minecraft.world.level.LevelReader;
|
| 11 | import net.minecraft.world.level.block.Blocks;
|
| 12 | import net.minecraft.world.level.block.state.BlockState;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 | import org.slf4j.Logger;
|
| 15 |
|
| 16 | public class JigsawReplacementProcessor implements StructureProcessor {
|
| 17 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 18 | public static final MapCodec<JigsawReplacementProcessor> MAP_CODEC = MapCodec.unit(() -> JigsawReplacementProcessor.INSTANCE);
|
| 19 | public static final JigsawReplacementProcessor INSTANCE = new JigsawReplacementProcessor();
|
| 20 |
|
| 21 | private JigsawReplacementProcessor() {
|
| 22 | }
|
| 23 |
|
| 24 | @Override
|
| 25 | public StructureTemplate.@Nullable StructureBlockInfo processBlock(
|
| 26 | final LevelReader level,
|
| 27 | final BlockPos targetPosition,
|
| 28 | final BlockPos referencePos,
|
| 29 | final BlockPos templateRelativePos,
|
| 30 | final StructureTemplate.StructureBlockInfo processedBlockInfo,
|
| 31 | final StructurePlaceSettings settings
|
| 32 | ) {
|
| 33 | BlockState state = processedBlockInfo.state();
|
| 34 | if (!state.is(Blocks.JIGSAW) || SharedConstants.DEBUG_KEEP_JIGSAW_BLOCKS_DURING_STRUCTURE_GEN) {
|
| 35 | return processedBlockInfo;
|
| 36 | }
|
| 37 |
|
| 38 | if (processedBlockInfo.nbt() == null) {
|
| 39 | LOGGER.warn("Jigsaw block at {} is missing nbt, will not replace", targetPosition);
|
| 40 | return processedBlockInfo;
|
| 41 | }
|
| 42 |
|
| 43 | String stateString = processedBlockInfo.nbt().getStringOr("final_state", "minecraft:air");
|
| 44 |
|
| 45 | BlockState blockState;
|
| 46 | try {
|
| 47 | BlockStateParser.BlockResult result = BlockStateParser.parseForBlock(level.holderLookup(Registries.BLOCK), stateString, true);
|
| 48 | blockState = result.blockState();
|
| 49 | } catch (CommandSyntaxException e) {
|
| 50 | LOGGER.error("Failed to parse jigsaw replacement state '{}' at {}: {}", stateString, targetPosition, e.getMessage());
|
| 51 | return null;
|
| 52 | }
|
| 53 |
|
| 54 | return blockState.is(Blocks.STRUCTURE_VOID) ? null : new StructureTemplate.StructureBlockInfo(processedBlockInfo.pos(), blockState, null);
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public MapCodec<JigsawReplacementProcessor> codec() {
|
| 59 | return MAP_CODEC;
|
| 60 | }
|
| 61 | }
|
| 62 |
|