|
| 1 | +/* |
| 2 | + * Minecraft Development for IntelliJ |
| 3 | + * |
| 4 | + * https://mcdev.io/ |
| 5 | + * |
| 6 | + * Copyright (C) 2025 minecraft-dev |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Lesser General Public License as published |
| 10 | + * by the Free Software Foundation, version 3.0 only. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public License |
| 18 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +package com.demonwav.mcdev.creator.custom.derivation |
| 22 | + |
| 23 | +import com.demonwav.mcdev.creator.custom.PropertyDerivation |
| 24 | +import com.demonwav.mcdev.creator.custom.TemplateValidationReporter |
| 25 | +import com.demonwav.mcdev.creator.custom.types.CreatorProperty |
| 26 | +import com.demonwav.mcdev.util.SemanticVersion |
| 27 | + |
| 28 | +class ExtractPaperApiVersionPropertyDerivation : ExtractVersionMajorMinorPropertyDerivation() { |
| 29 | + |
| 30 | + override fun derive(parentValues: List<Any?>): Any { |
| 31 | + val from = parentValues[0] as SemanticVersion |
| 32 | + if (from.parts.size < 2) { |
| 33 | + return SemanticVersion(emptyList()) |
| 34 | + } |
| 35 | + |
| 36 | + if (from.parts.size == 3) { |
| 37 | + val (part1, part2, part3) = from.parts |
| 38 | + if (part1 is SemanticVersion.Companion.VersionPart.ReleasePart && |
| 39 | + part2 is SemanticVersion.Companion.VersionPart.ReleasePart && |
| 40 | + part3 is SemanticVersion.Companion.VersionPart.ReleasePart |
| 41 | + ) { |
| 42 | + // From Minecraft version 1.20.5 onwards, the Paper API version also contains the 'minor' number. |
| 43 | + if (part1.version >= 26 || (part2.version >= 21) || (part2.version == 20 && part3.version >= 5)) { |
| 44 | + return SemanticVersion(listOf(part1, part2, part3)) |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + val (part1, part2) = from.parts |
| 50 | + if (part1 is SemanticVersion.Companion.VersionPart.ReleasePart && |
| 51 | + part2 is SemanticVersion.Companion.VersionPart.ReleasePart |
| 52 | + ) { |
| 53 | + return SemanticVersion(listOf(part1, part2)) |
| 54 | + } |
| 55 | + |
| 56 | + return SemanticVersion(emptyList()) |
| 57 | + } |
| 58 | + |
| 59 | + companion object : PropertyDerivationFactory { |
| 60 | + |
| 61 | + override fun create( |
| 62 | + reporter: TemplateValidationReporter, |
| 63 | + parents: List<CreatorProperty<*>?>?, |
| 64 | + derivation: PropertyDerivation |
| 65 | + ): PreparedDerivation? { |
| 66 | + if (parents.isNullOrEmpty()) { |
| 67 | + reporter.error("Expected a parent") |
| 68 | + return null |
| 69 | + } |
| 70 | + |
| 71 | + if (!parents[0]!!.acceptsType(SemanticVersion::class.java)) { |
| 72 | + reporter.error("First parent must produce a semantic version") |
| 73 | + return null |
| 74 | + } |
| 75 | + |
| 76 | + return ExtractPaperApiVersionPropertyDerivation() |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments