| 1 | package net.minecraft.world.level.levelgen.structure.templatesystem;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.ImmutableList.Builder;
|
| 5 | import com.mojang.datafixers.DataFixer;
|
| 6 | import com.mojang.logging.LogUtils;
|
| 7 | import java.io.FileOutputStream;
|
| 8 | import java.io.IOException;
|
| 9 | import java.io.OutputStream;
|
| 10 | import java.nio.file.Path;
|
| 11 | import java.util.List;
|
| 12 | import java.util.Map;
|
| 13 | import java.util.Optional;
|
| 14 | import java.util.concurrent.ConcurrentHashMap;
|
| 15 | import java.util.stream.Stream;
|
| 16 | import net.minecraft.SharedConstants;
|
| 17 | import net.minecraft.core.HolderGetter;
|
| 18 | import net.minecraft.data.CachedOutput;
|
| 19 | import net.minecraft.data.structures.NbtToSnbt;
|
| 20 | import net.minecraft.gametest.framework.StructureUtils;
|
| 21 | import net.minecraft.nbt.CompoundTag;
|
| 22 | import net.minecraft.nbt.NbtIo;
|
| 23 | import net.minecraft.nbt.NbtUtils;
|
| 24 | import net.minecraft.resources.FileToIdConverter;
|
| 25 | import net.minecraft.resources.Identifier;
|
| 26 | import net.minecraft.server.packs.PackType;
|
| 27 | import net.minecraft.server.packs.resources.ResourceManager;
|
| 28 | import net.minecraft.util.FileUtil;
|
| 29 | import net.minecraft.world.level.block.Block;
|
| 30 | import net.minecraft.world.level.levelgen.structure.templatesystem.loader.DirectoryTemplateSource;
|
| 31 | import net.minecraft.world.level.levelgen.structure.templatesystem.loader.ResourceManagerTemplateSource;
|
| 32 | import net.minecraft.world.level.levelgen.structure.templatesystem.loader.TemplatePathFactory;
|
| 33 | import net.minecraft.world.level.levelgen.structure.templatesystem.loader.TemplateSource;
|
| 34 | import net.minecraft.world.level.storage.LevelResource;
|
| 35 | import net.minecraft.world.level.storage.LevelStorageSource;
|
| 36 | import org.jspecify.annotations.Nullable;
|
| 37 | import org.slf4j.Logger;
|
| 38 |
|
| 39 | public class StructureTemplateManager {
|
| 40 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 41 | private static final String STRUCTURE_FILE_EXTENSION = ".nbt";
|
| 42 | private static final String STRUCTURE_TEXT_FILE_EXTENSION = ".snbt";
|
| 43 | public static final String STRUCTURE_DIRECTORY_NAME = "structure";
|
| 44 | public static final FileToIdConverter WORLD_STRUCTURE_LISTER = new FileToIdConverter("structure", ".nbt");
|
| 45 | private static final FileToIdConverter WORLD_TEXT_STRUCTURE_LISTER = new FileToIdConverter("structure", ".snbt");
|
| 46 | private static final FileToIdConverter RESOURCE_STRUCTURE_LISTER = new FileToIdConverter("structure", ".nbt");
|
| 47 | public static final FileToIdConverter RESOURCE_TEXT_STRUCTURE_LISTER = new FileToIdConverter("structure", ".snbt");
|
| 48 | private final Map<Identifier, Optional<StructureTemplate>> structureRepository = new ConcurrentHashMap<>();
|
| 49 | private final ResourceManagerTemplateSource resourceManagerSource;
|
| 50 | private final List<TemplateSource> sources;
|
| 51 | private final TemplatePathFactory worldTemplates;
|
| 52 | private final @Nullable TemplatePathFactory testTemplates;
|
| 53 |
|
| 54 | public StructureTemplateManager(
|
| 55 | final ResourceManager resourceManager, final LevelStorageSource.LevelStorageAccess storage, final DataFixer fixerUpper, final HolderGetter<Block> blockLookup
|
| 56 | ) {
|
| 57 | this.resourceManagerSource = new ResourceManagerTemplateSource(fixerUpper, blockLookup, resourceManager, RESOURCE_STRUCTURE_LISTER);
|
| 58 | Path generatedDir = storage.getLevelPath(LevelResource.GENERATED_DIR).normalize();
|
| 59 | this.worldTemplates = new TemplatePathFactory(generatedDir);
|
| 60 | if (StructureUtils.testStructuresTargetDir != null) {
|
| 61 | this.testTemplates = new TemplatePathFactory(StructureUtils.testStructuresTargetDir, PackType.SERVER_DATA);
|
| 62 | } else {
|
| 63 | this.testTemplates = null;
|
| 64 | }
|
| 65 |
|
| 66 | Builder<TemplateSource> sources = ImmutableList.builder();
|
| 67 | sources.add(new DirectoryTemplateSource(fixerUpper, blockLookup, generatedDir, WORLD_STRUCTURE_LISTER, false));
|
| 68 | if (StructureUtils.testStructuresSourceDir != null) {
|
| 69 | sources.add(
|
| 70 | new DirectoryTemplateSource(fixerUpper, blockLookup, StructureUtils.testStructuresSourceDir, PackType.SERVER_DATA, RESOURCE_TEXT_STRUCTURE_LISTER, true)
|
| 71 | );
|
| 72 | }
|
| 73 |
|
| 74 | sources.add(this.resourceManagerSource);
|
| 75 | this.sources = sources.build();
|
| 76 | }
|
| 77 |
|
| 78 | public StructureTemplate getOrCreate(final Identifier id) {
|
| 79 | Optional<StructureTemplate> cachedTemplate = this.get(id);
|
| 80 | if (cachedTemplate.isPresent()) {
|
| 81 | return cachedTemplate.get();
|
| 82 | }
|
| 83 |
|
| 84 | StructureTemplate template = new StructureTemplate();
|
| 85 | this.structureRepository.put(id, Optional.of(template));
|
| 86 | return template;
|
| 87 | }
|
| 88 |
|
| 89 | public Optional<StructureTemplate> get(final Identifier id) {
|
| 90 | return this.structureRepository.computeIfAbsent(id, this::tryLoad);
|
| 91 | }
|
| 92 |
|
| 93 | public Stream<Identifier> listTemplates() {
|
| 94 | return this.sources.stream().flatMap(TemplateSource::list).distinct();
|
| 95 | }
|
| 96 |
|
| 97 | private Optional<StructureTemplate> tryLoad(final Identifier id) {
|
| 98 | for (TemplateSource source : this.sources) {
|
| 99 | try {
|
| 100 | Optional<StructureTemplate> loaded = source.load(id);
|
| 101 | if (loaded.isPresent()) {
|
| 102 | return loaded;
|
| 103 | }
|
| 104 | } catch (Exception var5) {
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | return Optional.empty();
|
| 109 | }
|
| 110 |
|
| 111 | public void onResourceManagerReload(final ResourceManager resourceManager) {
|
| 112 | this.resourceManagerSource.setResourceManager(resourceManager);
|
| 113 | this.structureRepository.clear();
|
| 114 | }
|
| 115 |
|
| 116 | public boolean save(final Identifier id) {
|
| 117 | Optional<StructureTemplate> structureTemplate = this.structureRepository.get(id);
|
| 118 | if (structureTemplate.isEmpty()) {
|
| 119 | return false;
|
| 120 | }
|
| 121 |
|
| 122 | Path file;
|
| 123 | boolean saveAsText;
|
| 124 | if (SharedConstants.DEBUG_SAVE_STRUCTURES_AS_SNBT) {
|
| 125 | file = this.worldTemplates.createAndValidatePathToStructure(id, WORLD_TEXT_STRUCTURE_LISTER);
|
| 126 | saveAsText = true;
|
| 127 | } else {
|
| 128 | file = this.worldTemplates.createAndValidatePathToStructure(id, WORLD_STRUCTURE_LISTER);
|
| 129 | saveAsText = false;
|
| 130 | }
|
| 131 |
|
| 132 | try {
|
| 133 | return save(file, structureTemplate.get(), saveAsText);
|
| 134 | } catch (Exception e) {
|
| 135 | LOGGER.warn("Failed to save structure file {} to {}", id, file, e);
|
| 136 | return false;
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | public static boolean save(final Path file, final StructureTemplate structureTemplate, final boolean asText) throws IOException {
|
| 141 | Path parent = file.getParent();
|
| 142 | if (parent == null) {
|
| 143 | return false;
|
| 144 | }
|
| 145 |
|
| 146 | FileUtil.createDirectoriesSafe(parent);
|
| 147 | CompoundTag tag = structureTemplate.save(new CompoundTag());
|
| 148 | if (asText) {
|
| 149 | NbtToSnbt.writeSnbt(CachedOutput.NO_CACHE, file, NbtUtils.structureToSnbt(tag));
|
| 150 | } else {
|
| 151 | try (OutputStream output = new FileOutputStream(file.toFile())) {
|
| 152 | NbtIo.writeCompressed(tag, output);
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | return true;
|
| 157 | }
|
| 158 |
|
| 159 | public TemplatePathFactory worldTemplates() {
|
| 160 | return this.worldTemplates;
|
| 161 | }
|
| 162 |
|
| 163 | public @Nullable TemplatePathFactory testTemplates() {
|
| 164 | return this.testTemplates;
|
| 165 | }
|
| 166 |
|
| 167 | public void remove(final Identifier id) {
|
| 168 | this.structureRepository.remove(id);
|
| 169 | }
|
| 170 | }
|
| 171 |
|