|
| 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.platform.bukkit.framework |
| 22 | + |
| 23 | +import com.demonwav.mcdev.asset.PlatformAssets |
| 24 | +import com.demonwav.mcdev.util.localFile |
| 25 | +import com.google.gson.Gson |
| 26 | +import com.intellij.framework.library.LibraryVersionProperties |
| 27 | +import com.intellij.openapi.roots.libraries.LibraryPresentationProvider |
| 28 | +import com.intellij.openapi.vfs.VirtualFile |
| 29 | +import java.util.jar.JarFile |
| 30 | + |
| 31 | +class ModernPaperPresentationProvider : LibraryPresentationProvider<LibraryVersionProperties>(PAPER_LIBRARY_KIND) { |
| 32 | + |
| 33 | + override fun getIcon(properties: LibraryVersionProperties?) = PlatformAssets.PAPER_ICON |
| 34 | + |
| 35 | + override fun detect(classesRoots: List<VirtualFile>): LibraryVersionProperties? { |
| 36 | + for (classesRoot in classesRoots) { |
| 37 | + if (!classesRoot.name.endsWith(".jar")) { |
| 38 | + continue |
| 39 | + } |
| 40 | + runCatching { |
| 41 | + JarFile(classesRoot.localFile).use { jar -> |
| 42 | + jar.getEntry("io/papermc/paper/ServerBuildInfo.class") ?: return@runCatching |
| 43 | + val versionJson = jar.getEntry("apiVersioning.json") ?: return@runCatching |
| 44 | + jar.getInputStream(versionJson).use { stream -> |
| 45 | + stream.reader().use { reader -> |
| 46 | + val map = Gson().fromJson(reader, Map::class.java) |
| 47 | + return LibraryVersionProperties(map["currentApiVersion"] as String) |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + return null |
| 54 | + } |
| 55 | +} |
0 commit comments