|
| 1 | +/* |
| 2 | + * Scaffolding - Schematic library for Minestom |
| 3 | + * Copyright (c) 2022 SLLCoding <[email protected]> |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the “Software”), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | + * THE SOFTWARE. |
| 22 | + */ |
| 23 | +package dev.hypera.scaffolding; |
| 24 | + |
| 25 | +import java.nio.file.Files; |
| 26 | +import java.nio.file.Path; |
| 27 | +import kotlin.Pair; |
| 28 | +import dev.hypera.scaffolding.schematic.Schematic; |
| 29 | +import dev.hypera.scaffolding.schematic.impl.MCEditSchematic; |
| 30 | +import dev.hypera.scaffolding.schematic.impl.SpongeSchematic; |
| 31 | +import org.jetbrains.annotations.NotNull; |
| 32 | +import org.jetbrains.annotations.Nullable; |
| 33 | +import org.jglrxavpok.hephaistos.nbt.*; |
| 34 | + |
| 35 | +import java.io.*; |
| 36 | + |
| 37 | +public class Scaffolding { |
| 38 | + |
| 39 | + /** |
| 40 | + * Automatically detects the type of schematic and parses the input stream |
| 41 | + * @param inputStream Schematic input |
| 42 | + * @return parsed schematic |
| 43 | + * @throws IOException if the input stream is invalid |
| 44 | + * @throws NBTException if the schematic is invalid |
| 45 | + */ |
| 46 | + public static @Nullable Schematic fromStream(@NotNull InputStream inputStream) throws IOException, NBTException { |
| 47 | + NBTReader reader = new NBTReader(inputStream, CompressedProcesser.GZIP); |
| 48 | + Pair<String, NBT> pair = reader.readNamed(); |
| 49 | + NBTCompound nbtTag = (NBTCompound) pair.getSecond(); |
| 50 | + |
| 51 | + Schematic schematic = null; |
| 52 | + if (nbtTag.contains("Blocks")) schematic = new MCEditSchematic(); |
| 53 | + else if (nbtTag.contains("Palette")) schematic = new SpongeSchematic(); |
| 54 | + |
| 55 | + if (schematic != null) schematic.read(nbtTag); |
| 56 | + return schematic; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Automatically detects the type of schematic and parses the file |
| 61 | + * @param path Schematic path |
| 62 | + * @return parsed schematic |
| 63 | + * @throws IOException if the file is invalid |
| 64 | + * @throws NBTException if the schematic is invalid |
| 65 | + */ |
| 66 | + public static @Nullable Schematic fromPath(@NotNull Path path) throws IOException, NBTException { |
| 67 | + if (!Files.exists(path)) throw new FileNotFoundException("Invalid Schematic: File does not exist"); |
| 68 | + return fromStream(Files.newInputStream(path)); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Automatically detects the type of schematic and parses the file |
| 73 | + * @param file Schematic file |
| 74 | + * @return parsed schematic |
| 75 | + * @throws IOException if the file is invalid |
| 76 | + * @throws NBTException if the schematic is invalid |
| 77 | + */ |
| 78 | + public static @Nullable Schematic fromFile(@NotNull File file) throws IOException, NBTException { |
| 79 | + if (!file.exists()) throw new FileNotFoundException("Invalid Schematic: File does not exist"); |
| 80 | + return fromStream(new FileInputStream(file)); |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments