From c8a03c4cb794673a27714470dc3f804462e617c5 Mon Sep 17 00:00:00 2001 From: iGabyTM Date: Mon, 29 Apr 2024 14:30:57 +0300 Subject: [PATCH] feat(plugin): 1.20.5 support --- build.gradle | 2 +- .../minecraft/arcanevouchers/ServerVersion.kt | 22 ++++++++++++++----- .../arcanevouchers/functions/Strings.kt | 11 +++++++--- .../arcanevouchers/voucher/VoucherManager.kt | 2 +- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/build.gradle b/build.gradle index 7ab4102..b9f2d80 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ plugins { } group 'me.gabytm.minecraft' -version '2.3.1-SNAPSHOT' +version '2.3.2-SNAPSHOT' subprojects { if (project.name != 'comet') { diff --git a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/ServerVersion.kt b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/ServerVersion.kt index 165ffc0..3322fe0 100644 --- a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/ServerVersion.kt +++ b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/ServerVersion.kt @@ -1,15 +1,15 @@ package me.gabytm.minecraft.arcanevouchers import org.bukkit.Bukkit +import org.bukkit.Server import java.util.regex.Pattern /** - * @author [Matt (@ipsk)](https://github.com/ipsk) + * @author [Matt (@LichtHund)](https://github.com/LichtHund) */ object ServerVersion { private val VERSION = getCurrentVersion() - private val NMS_VERSION = Bukkit.getServer().javaClass.`package`.name.substringAfterLast('.') private const val V_1_8 = 1_8_0 private const val V_1_9 = 1_9_0 @@ -17,7 +17,7 @@ object ServerVersion { val CURRENT = getCurrentVersionAsString() - val IS_VERY_OLD = VERSION.toString().startsWith("17") + val IS_VERY_OLD = CURRENT.startsWith("1.7") val IS_ANCIENT = VERSION <= V_1_8 /** @@ -61,7 +61,7 @@ object ServerVersion { */ private fun getCurrentVersion(): Int { // No need to cache since will only run once - val matcher = Pattern.compile("(?\\d+\\.\\d+)(?\\.\\d+)?").matcher(Bukkit.getBukkitVersion()) + val matcher = Pattern.compile("(?\\d+\\.\\d+)(?\\.\\d+)?").matcher(getMinecraftVersion()) return buildString { if (matcher.find()) { @@ -72,10 +72,20 @@ object ServerVersion { } private fun getCurrentVersionAsString(): String { - val matcher = Pattern.compile("\\d+\\.\\d+(?:\\.\\d+)?").matcher(Bukkit.getBukkitVersion()) + val matcher = Pattern.compile("\\d+\\.\\d+(?:\\.\\d+)?").matcher(getMinecraftVersion()) return if (matcher.find()) matcher.group() else "unknown" } + private fun getMinecraftVersion(): String { + try { + // Paper method + val method = Server::class.java.getDeclaredMethod("getMinecraftVersion") + return method.invoke(Bukkit.getServer()) as String + } catch (ignored: NoSuchMethodError) { + return Bukkit.getServer().javaClass.`package`.name.substringAfterLast('.'); + } + } + /** * Gets a NMS class by its name * @return class @@ -83,7 +93,7 @@ object ServerVersion { */ @Throws(ClassNotFoundException::class) fun getCraftClass(name: String): Class<*> { - return Class.forName("org.bukkit.craftbukkit.$NMS_VERSION.$name") + return Class.forName("${Bukkit.getServer().javaClass.`package`.name}.$name") } } \ No newline at end of file diff --git a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/functions/Strings.kt b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/functions/Strings.kt index 40744c6..c79633b 100644 --- a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/functions/Strings.kt +++ b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/functions/Strings.kt @@ -33,12 +33,17 @@ fun String.replace(placeholders: Array, values: Array): String { return StringUtils.replaceEach(this, placeholders, values) } -fun String.mini(removeItalic: Boolean = false, vararg tagResolver: TagResolver): Component { +fun String.mini(removeItalic: Boolean = false, customTagResolvers: Set = emptySet()): Component { + val allTagResolvers = TagResolver.builder() + .resolvers(TagResolver.standard()) + .resolvers(customTagResolvers) + .build() + return try { if (removeItalic) { - EMPTY_COMPONENT_WITHOUT_ITALIC.append(Constant.MINI.deserialize(this, *tagResolver)) + EMPTY_COMPONENT_WITHOUT_ITALIC.append(Constant.MINI.deserialize(this, allTagResolvers)) } else { - Constant.MINI.deserialize(this, *tagResolver) + Constant.MINI.deserialize(this, allTagResolvers) } } catch (e: ParsingException) { exception("Could not parse '$this'", e) diff --git a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/VoucherManager.kt b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/VoucherManager.kt index 4fa7563..e2f61a2 100644 --- a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/VoucherManager.kt +++ b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/VoucherManager.kt @@ -78,7 +78,7 @@ class VoucherManager(private val plugin: ArcaneVouchers) { val transformer: (String) -> Component = { it.replace(arguments, values) // Replace arguments .papi(player) // Set PAPI placeholders - .mini(true, PapiMiniMessageTag(player)) // Parse mini tags + .mini(true, setOf(PapiMiniMessageTag(player))) // Parse mini tags } if (voucher.itemName.isNotBlank()) {