| 1 | package net.minecraft.world.level.levelgen.structure.templatesystem.loader;
|
| 2 |
|
| 3 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 4 | import com.mojang.datafixers.DataFixer;
|
| 5 | import java.io.FileNotFoundException;
|
| 6 | import java.io.IOException;
|
| 7 | import java.io.InputStream;
|
| 8 | import java.io.InputStreamReader;
|
| 9 | import java.io.Reader;
|
| 10 | import java.nio.charset.StandardCharsets;
|
| 11 | import java.util.Optional;
|
| 12 | import java.util.function.Consumer;
|
| 13 | import java.util.stream.Stream;
|
| 14 | import net.minecraft.core.HolderGetter;
|
| 15 | import net.minecraft.nbt.CompoundTag;
|
| 16 | import net.minecraft.nbt.NbtAccounter;
|
| 17 | import net.minecraft.nbt.NbtIo;
|
| 18 | import net.minecraft.nbt.NbtUtils;
|
| 19 | import net.minecraft.resources.Identifier;
|
| 20 | import net.minecraft.server.packs.resources.IoSupplier;
|
| 21 | import net.minecraft.util.FastBufferedInputStream;
|
| 22 | import net.minecraft.util.datafix.DataFixTypes;
|
| 23 | import net.minecraft.world.level.block.Block;
|
| 24 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
|
| 25 |
|
| 26 | public abstract class TemplateSource {
|
| 27 | private final DataFixer fixerUpper;
|
| 28 | private final HolderGetter<Block> blockLookup;
|
| 29 |
|
| 30 | protected TemplateSource(final DataFixer fixerUpper, final HolderGetter<Block> blockLookup) {
|
| 31 | this.fixerUpper = fixerUpper;
|
| 32 | this.blockLookup = blockLookup;
|
| 33 | }
|
| 34 |
|
| 35 | public abstract Optional<StructureTemplate> load(Identifier id);
|
| 36 |
|
| 37 | public abstract Stream<Identifier> list();
|
| 38 |
|
| 39 | protected Optional<StructureTemplate> load(final IoSupplier<InputStream> opener, final boolean asText, final Consumer<Throwable> onError) {
|
| 40 | try (
|
| 41 | InputStream rawInput = opener.get();
|
| 42 | InputStream input = new FastBufferedInputStream(rawInput);
|
| 43 | ) {
|
| 44 | CompoundTag structureTag;
|
| 45 | if (asText) {
|
| 46 | structureTag = readTextStructure(input);
|
| 47 | } else {
|
| 48 | structureTag = readStructure(input);
|
| 49 | }
|
| 50 |
|
| 51 | return Optional.of(this.readStructure(structureTag));
|
| 52 | } catch (FileNotFoundException e) {
|
| 53 | return Optional.empty();
|
| 54 | } catch (Throwable e) {
|
| 55 | onError.accept(e);
|
| 56 | return Optional.empty();
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | private static CompoundTag readStructure(final InputStream input) throws IOException {
|
| 61 | return NbtIo.readCompressed(input, NbtAccounter.unlimitedHeap());
|
| 62 | }
|
| 63 |
|
| 64 | private static CompoundTag readTextStructure(final InputStream input) throws IOException, CommandSyntaxException {
|
| 65 | try (Reader reader = new InputStreamReader(input, StandardCharsets.UTF_8)) {
|
| 66 | String contents = reader.readAllAsString();
|
| 67 | return NbtUtils.snbtToStructure(contents);
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | private StructureTemplate readStructure(final CompoundTag tag) {
|
| 72 | StructureTemplate structureTemplate = new StructureTemplate();
|
| 73 | int version = NbtUtils.getDataVersion(tag, 500);
|
| 74 | structureTemplate.load(this.blockLookup, DataFixTypes.STRUCTURE.updateToCurrentVersion(this.fixerUpper, tag, version));
|
| 75 | return structureTemplate;
|
| 76 | }
|
| 77 | }
|
| 78 |
|