From f6a2b94943538ebefe0a7ec341bdbed56fe563a5 Mon Sep 17 00:00:00 2001 From: TONY_All Date: Thu, 6 Oct 2022 12:43:39 +0800 Subject: [PATCH] support for legacy Minecraft --- build.gradle.kts | 14 +++--- .../sansefish/listener/FishListener.kt | 15 +++--- .../sansefish/misc/FishingRodInstance.kt | 30 ++++-------- .../sanseyooyea/sansefish/nms/NMSAccess.kt | 19 ++++++++ .../sanseyooyea/sansefish/nms/NMSBase.kt | 23 +++++++++ .../sanseyooyea/sansefish/nms/NMSBaseImpl.kt | 47 +++++++++++++++++++ .../sanseyooyea/sansefish/ui/BaitUI.kt | 7 ++- 7 files changed, 117 insertions(+), 38 deletions(-) create mode 100644 src/main/kotlin/work/microhand/sanseyooyea/sansefish/nms/NMSAccess.kt create mode 100644 src/main/kotlin/work/microhand/sanseyooyea/sansefish/nms/NMSBase.kt create mode 100644 src/main/kotlin/work/microhand/sanseyooyea/sansefish/nms/NMSBaseImpl.kt diff --git a/build.gradle.kts b/build.gradle.kts index 203ecef..b3d6be9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,22 +1,21 @@ plugins { `java-library` `maven-publish` - id("io.izzel.taboolib") version "1.40" - id("org.jetbrains.kotlin.jvm") version "1.6.0" + id("io.izzel.taboolib") version "1.42" + id("org.jetbrains.kotlin.jvm") version "1.7.10" } taboolib { install("common") install("common-5") - install("module-database") install("module-chat") install("module-ui") install("module-ui-receptacle") install("module-configuration") - install("expansion-player-database") + install("module-nms") install("platform-bukkit") classifier = null - version = "6.0.9-5" + version = "6.0.9-111" } repositories { @@ -25,8 +24,9 @@ repositories { dependencies { compileOnly("ink.ptms:nms-all:1.0.0") - compileOnly("ink.ptms.core:v11802:11802-minimize:mapped") - compileOnly("ink.ptms.core:v11802:11802-minimize:universal") + implementation("ink.ptms.core:v11200:11200") +// compileOnly("ink.ptms.core:v11802:11802-minimize:mapped") +// compileOnly("ink.ptms.core:v11802:11802-minimize:universal") compileOnly(kotlin("stdlib")) compileOnly(fileTree("libs")) } diff --git a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/listener/FishListener.kt b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/listener/FishListener.kt index 0a3cde2..d0246a5 100644 --- a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/listener/FishListener.kt +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/listener/FishListener.kt @@ -5,10 +5,10 @@ import org.bukkit.event.block.Action import org.bukkit.event.player.PlayerFishEvent import org.bukkit.event.player.PlayerInteractEvent import org.bukkit.inventory.ItemStack -import org.bukkit.persistence.PersistentDataType import taboolib.common.platform.event.SubscribeEvent import work.microhand.sanseyooyea.sansefish.SanseFish import work.microhand.sanseyooyea.sansefish.manager.RodManager +import work.microhand.sanseyooyea.sansefish.nms.hasNBTTag import work.microhand.sanseyooyea.sansefish.ui.BaitUI object FishListener { @@ -34,11 +34,12 @@ object FishListener { val rodItem = if (offHand) player.inventory.itemInOffHand else player.inventory.itemInMainHand if (fishEvent.state == PlayerFishEvent.State.FISHING) { - if (!rodItem.itemMeta!!.persistentDataContainer.has( - SanseFish.baitKey, - PersistentDataType.STRING + + if (!rodItem.hasNBTTag( + SanseFish.baitKey.toString() ) - ) return player.sendMessage("§e| §7该鱼竿未安装鱼饵. 请手持鱼竿左键安装.").also { fishEvent.isCancelled = true } + ) return player.sendMessage("§e| §7该鱼竿未安装鱼饵. 请手持鱼竿左键安装.") + .also { fishEvent.isCancelled = true } } if (fishEvent.state == PlayerFishEvent.State.CAUGHT_FISH) { @@ -51,7 +52,7 @@ object FishListener { ItemStack(Material.AIR) } else rod.updateFishingRod() - if (offHand) player.inventory.setItemInOffHand(resultItem) else player.inventory.setItemInMainHand(resultItem) + if (offHand) player.inventory.itemInOffHand = resultItem else player.inventory.itemInMainHand = resultItem } @SubscribeEvent @@ -59,7 +60,7 @@ object FishListener { if (event.action != Action.LEFT_CLICK_AIR && event.action != Action.LEFT_CLICK_BLOCK) return if (!event.hasItem()) return if (event.item!!.type != Material.FISHING_ROD) return - if (event.item!!.itemMeta!!.persistentDataContainer.has(SanseFish.baitKey, PersistentDataType.STRING)) return + if (event.item!!.hasNBTTag(SanseFish.baitKey.toString())) return RodManager.parseRod(event.item!!) ?: return BaitUI(event.player).open() } diff --git a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/FishingRodInstance.kt b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/FishingRodInstance.kt index d538567..9e3ca1f 100644 --- a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/FishingRodInstance.kt +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/FishingRodInstance.kt @@ -1,15 +1,15 @@ package work.microhand.sanseyooyea.sansefish.misc -import org.bukkit.Bukkit import org.bukkit.Material import org.bukkit.event.player.PlayerFishEvent import org.bukkit.inventory.ItemStack -import org.bukkit.inventory.meta.Damageable -import org.bukkit.persistence.PersistentDataType -import taboolib.platform.BukkitPlugin import taboolib.platform.util.buildItem import work.microhand.sanseyooyea.sansefish.SanseFish import work.microhand.sanseyooyea.sansefish.manager.BaitManager +import work.microhand.sanseyooyea.sansefish.nms.getNBTInt +import work.microhand.sanseyooyea.sansefish.nms.getNBTString +import work.microhand.sanseyooyea.sansefish.nms.removeNBTTag +import work.microhand.sanseyooyea.sansefish.nms.setNBTInt data class FishingRodInstance( val type: FishRod, @@ -25,7 +25,7 @@ data class FishingRodInstance( }.let { val durabilityPercentage = (durability.toDouble() / type.maxDurability) val meta = it.itemMeta - (meta as Damageable).damage = (64 * (1 - durabilityPercentage)).toInt() + it.durability = (64 * (1 - durabilityPercentage)).toInt().toShort() it.itemMeta = meta it } @@ -34,25 +34,15 @@ data class FishingRodInstance( fishEvent.expToDrop = 0 fishEvent.caught!!.remove() val bait = - BaitManager.baits[item.itemMeta!!.persistentDataContainer[SanseFish.baitKey, PersistentDataType.STRING] - ?: return]!! + BaitManager.baits[item.getNBTString(SanseFish.baitKey.toString()) ?: return]!! luck++ - val maxLuck = item.itemMeta!!.persistentDataContainer.getOrDefault( - SanseFish.maxLuckKey, - PersistentDataType.INTEGER, - type.defaultLuck - ) + val maxLuck = item.getNBTInt(SanseFish.maxLuckKey.toString()) ?: type.defaultLuck + if (luck > maxLuck) { - val meta = item.itemMeta!! - meta.persistentDataContainer[SanseFish.maxLuckKey, PersistentDataType.INTEGER] = luck - item.itemMeta = meta + item.setNBTInt(SanseFish.maxLuckKey.toString(), luck) bait.rewards.doGuarantee(fishEvent.player, luck, maxLuck) } - val meta = item.itemMeta!! - Bukkit.getScheduler().runTask(BukkitPlugin.getInstance()) { _ -> - meta.persistentDataContainer.remove(SanseFish.baitKey) - fishEvent.player.inventory.itemInMainHand.itemMeta = meta - } + item.removeNBTTag(SanseFish.baitKey.toString()) bait.rewards.doReward(fishEvent.player, this) } } diff --git a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/nms/NMSAccess.kt b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/nms/NMSAccess.kt new file mode 100644 index 0000000..3566cf1 --- /dev/null +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/nms/NMSAccess.kt @@ -0,0 +1,19 @@ +package work.microhand.sanseyooyea.sansefish.nms + +import org.bukkit.inventory.ItemStack + +fun ItemStack.setNBTString(key: String, value: String) { + NMSBase.impl.setTagString(this, key, value) +} + +fun ItemStack.setNBTInt(key: String, value: Int) { + NMSBase.impl.setTagInt(this, key, value) +} + +fun ItemStack.getNBTString(key: String): String? = NMSBase.impl.getTagString(this, key) + +fun ItemStack.getNBTInt(key: String): Int? = NMSBase.impl.getTagInt(this, key) + +fun ItemStack.hasNBTTag(key: String): Boolean = NMSBase.impl.hasTag(this, key) + +fun ItemStack.removeNBTTag(key: String) = NMSBase.impl.removeTag(this, key) \ No newline at end of file diff --git a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/nms/NMSBase.kt b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/nms/NMSBase.kt new file mode 100644 index 0000000..517afcc --- /dev/null +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/nms/NMSBase.kt @@ -0,0 +1,23 @@ +package work.microhand.sanseyooyea.sansefish.nms + +import org.bukkit.inventory.ItemStack +import taboolib.module.nms.nmsProxy + +abstract class NMSBase { + abstract fun hasTag(item: ItemStack, key: String): Boolean + + abstract fun removeTag(item: ItemStack, key: String) + + abstract fun setTagString(item: ItemStack, key: String, value: String) + + abstract fun getTagString(item: ItemStack, key: String): String? + + abstract fun setTagInt(item: ItemStack, key: String, value: Int) + + abstract fun getTagInt(item: ItemStack, key: String): Int? + + companion object { + val impl = nmsProxy() + } + +} \ No newline at end of file diff --git a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/nms/NMSBaseImpl.kt b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/nms/NMSBaseImpl.kt new file mode 100644 index 0000000..5fab1d7 --- /dev/null +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/nms/NMSBaseImpl.kt @@ -0,0 +1,47 @@ +package work.microhand.sanseyooyea.sansefish.nms + +import net.minecraft.server.v1_12_R1.NBTTagCompound +import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack +import org.bukkit.inventory.ItemStack +import taboolib.library.reflex.Reflex.Companion.getProperty + +class NMSBaseImpl : NMSBase() { + override fun setTagString(item: ItemStack, key: String, value: String) { + val tag = item.toNBT() + tag.setString(key, value) + } + + override fun hasTag(item: ItemStack, key: String): Boolean { + val tag = item.toNBT() + return tag.hasKey(key) + } + + override fun removeTag(item: ItemStack, key: String) { + val tag = item.toNBT() + println("removing") + tag.remove(key) + + } + + override fun getTagString(item: ItemStack, key: String): String? { + val tag = item.toNBT() + return tag.getString(key) + } + + override fun setTagInt(item: ItemStack, key: String, value: Int) { + val tag = item.toNBT() + tag.setInt(key, value) + } + + override fun getTagInt(item: ItemStack, key: String): Int? { + val tag = item.toNBT() + return if (hasTag(item, key)) { + tag.getInt(key) + } else null + } + + private fun ItemStack.toNBT(): NBTTagCompound { + val handle = (this as CraftItemStack).getProperty("handle")!! + return handle.c("sansefish") + } +} \ No newline at end of file diff --git a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/ui/BaitUI.kt b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/ui/BaitUI.kt index 4bda8dd..0fff2bd 100644 --- a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/ui/BaitUI.kt +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/ui/BaitUI.kt @@ -1,12 +1,12 @@ package work.microhand.sanseyooyea.sansefish.ui import org.bukkit.entity.Player -import org.bukkit.persistence.PersistentDataType import taboolib.module.ui.buildMenu import taboolib.module.ui.type.Linked import work.microhand.sanseyooyea.sansefish.SanseFish import work.microhand.sanseyooyea.sansefish.manager.BaitManager import work.microhand.sanseyooyea.sansefish.misc.Bait +import work.microhand.sanseyooyea.sansefish.nms.setNBTString class BaitUI(private val player: Player) { private val baits = HashMap() @@ -28,9 +28,8 @@ class BaitUI(private val player: Player) { set(index, item) { isCancelled = true player.inventory.removeItem(bait.item) - val meta = player.inventory.itemInMainHand.itemMeta!! - meta.persistentDataContainer[SanseFish.baitKey, PersistentDataType.STRING] = bait.type - player.inventory.itemInMainHand.itemMeta = meta + val item = player.inventory.itemInMainHand + item.setNBTString(SanseFish.baitKey.toString(), bait.type) player.sendMessage("§a| §7成功安装鱼饵") player.closeInventory() }