diff --git a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/SanseFish.kt b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/SanseFish.kt index 2b53cbb..8409731 100644 --- a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/SanseFish.kt +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/SanseFish.kt @@ -6,7 +6,6 @@ import taboolib.common.platform.function.info import taboolib.module.configuration.Config import taboolib.module.configuration.Configuration import taboolib.platform.BukkitPlugin -import work.microhand.sanseyooyea.sansefish.command.setBaitCmd import work.microhand.sanseyooyea.sansefish.command.registerCommand import work.microhand.sanseyooyea.sansefish.command.StorageCommand @@ -16,15 +15,14 @@ object SanseFish : Plugin() { lateinit var conf: Configuration private set - val baitKey = NamespacedKey(BukkitPlugin.getInstance(), "bait") - val maxLuckKey = NamespacedKey(BukkitPlugin.getInstance(), "max_luck") + val baitKey by lazy { NamespacedKey(BukkitPlugin.getInstance(), "bait") } + val maxLuckKey by lazy { NamespacedKey(BukkitPlugin.getInstance(), "max_luck") } override fun onEnable() { info("§a| §7正在加载钓鱼插件(SanseFish)..") info("§a| §7作者QQ: 1187586838") registerCommand() StorageCommand.registerCommand() - setBaitCmd() info("§a| §7插件加载完成.") } diff --git a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/command/GetCommand.kt b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/command/GetCommand.kt index a988b11..9e5fa6b 100644 --- a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/command/GetCommand.kt +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/command/GetCommand.kt @@ -21,16 +21,4 @@ fun registerCommand() = command("giveRod", permission = "sansefish.giverod") { } } } -} - -fun setBaitCmd() = command("setBait", permission = "sansefish.setbait") { - dynamic { - execute { sender, _, arg -> - val bait = BaitManager.baits[arg]?: return@execute sender.sendMessage("§a| §7未找到该鱼饵") - sender.sendMessage("§a| §7您手中物品已设置为鱼饵.") - - SanseFish.conf.setItemStack("target-rod.bait", sender.inventory.itemInMainHand.clone().apply { amount = 1 }) - SanseFish.conf.saveToFile() - } - } } \ No newline at end of file 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 7c66fa2..cce3781 100644 --- a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/listener/FishListener.kt +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/listener/FishListener.kt @@ -7,6 +7,7 @@ import org.bukkit.event.player.PlayerInteractEvent import org.bukkit.inventory.ItemStack import org.bukkit.persistence.PersistentDataType import taboolib.common.platform.event.SubscribeEvent +import taboolib.platform.util.ItemBuilder import work.microhand.sanseyooyea.sansefish.SanseFish import work.microhand.sanseyooyea.sansefish.manager.RodManager import work.microhand.sanseyooyea.sansefish.ui.BaitUI @@ -34,6 +35,7 @@ object FishListener { val rodItem = if (offHand) player.inventory.itemInOffHand else player.inventory.itemInMainHand if (fishEvent.state == PlayerFishEvent.State.FISHING) { + player.sendMessage(rodItem.itemMeta!!.persistentDataContainer.keys.toString()) if (!rodItem.itemMeta!!.persistentDataContainer.has( SanseFish.baitKey, PersistentDataType.STRING @@ -57,9 +59,11 @@ object FishListener { @SubscribeEvent fun useBait(event: PlayerInteractEvent) { + println(event.action) 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 RodManager.parseRod(event.item!!) ?: return BaitUI(event.player).open() } diff --git a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/manager/BaitManager.kt b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/manager/BaitManager.kt index 03592b3..5eb1256 100644 --- a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/manager/BaitManager.kt +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/manager/BaitManager.kt @@ -8,7 +8,6 @@ import work.microhand.sanseyooyea.sansefish.command.StorageCommand import work.microhand.sanseyooyea.sansefish.misc.Bait import work.microhand.sanseyooyea.sansefish.misc.BaitReward import java.io.File -import java.lang.IllegalArgumentException object BaitManager { val baits = HashMap() @@ -30,7 +29,7 @@ object BaitManager { // load baits. File(getDataFolder(), "baits").listFiles()?.forEach { file -> val config = Configuration.loadFromFile(file) - val key = config.getString("item")!! + val key = config.getString("item")?: throw IllegalArgumentException("鱼饵 ${file.nameWithoutExtension} 未配置物品.") val item = StorageCommand.itemStorage.getItemStack(key)?: throw IllegalArgumentException("物品 $key 不存在.") val rewards = BaitReward(config) baits[file.nameWithoutExtension] = Bait(file.nameWithoutExtension, item, rewards, config) 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 fd39a63..00253ae 100644 --- a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/FishingRodInstance.kt +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/FishingRodInstance.kt @@ -1,9 +1,12 @@ 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.persistence.PersistentDataType +import taboolib.platform.BukkitPlugin +import taboolib.platform.util.ItemBuilder import taboolib.platform.util.buildItem import work.microhand.sanseyooyea.sansefish.SanseFish import work.microhand.sanseyooyea.sansefish.manager.BaitManager @@ -16,23 +19,32 @@ data class FishingRodInstance( ) { fun updateFishingRod(): ItemStack = buildItem(item) { lore.clear() - lore.addAll(listOf("", "", "§8神奇的鱼竿")) + lore.addAll(listOf("", "", type.lore)) lore[0] = "§e耐久: §7$durability" lore[1] = "§d幸运值: §7$luck" val durabilityPercentage = (durability.toDouble() / SanseFish.conf.getInt("target-rod.max-durability", 100)) damage = (64 * (1 - durabilityPercentage)).toInt() - } + println("Damage Before: $damage") + }.also { println("Damage After: ${ItemBuilder(it).damage}") } fun handleFished(fishEvent: PlayerFishEvent) { fishEvent.expToDrop = 0 fishEvent.caught!!.remove() - fishEvent.player.inventory.itemInMainHand val bait = BaitManager.baits[item.itemMeta!!.persistentDataContainer[SanseFish.baitKey, PersistentDataType.STRING]?: return]!! luck++ val maxLuck = item.itemMeta!!.persistentDataContainer.getOrDefault(SanseFish.maxLuckKey, PersistentDataType.INTEGER, type.defaultLuck) if (luck > maxLuck) { + val meta = item.itemMeta!! + meta.persistentDataContainer[SanseFish.maxLuckKey, PersistentDataType.INTEGER] = luck + item.itemMeta = meta bait.rewards.doGuarantee(fishEvent.player, luck, maxLuck) } + val meta = item.itemMeta!! + Bukkit.getScheduler().runTask(BukkitPlugin.getInstance()) { _ -> + println("remove bait") + meta.persistentDataContainer.remove(SanseFish.baitKey) + fishEvent.player.inventory.itemInMainHand.itemMeta = meta + } bait.rewards.doReward(fishEvent.player, this) } } 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 1afa558..6deb681 100644 --- a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/ui/BaitUI.kt +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/ui/BaitUI.kt @@ -16,6 +16,7 @@ class BaitUI(private val player: Player) { init { player.inventory.forEach { + if (it == null) return@forEach val bait = BaitManager.parseBait(it) ?: return@forEach baits[bait] = baits.getOrDefault(bait, 0) + it.amount } @@ -30,7 +31,10 @@ class BaitUI(private val player: Player) { set(index, item) { isCancelled = true player.inventory.removeItem(bait.item) - player.inventory.itemInMainHand.itemMeta!!.persistentDataContainer[SanseFish.baitKey, PersistentDataType.STRING] = bait.type + val meta = player.inventory.itemInMainHand.itemMeta!! + meta.persistentDataContainer[SanseFish.baitKey, PersistentDataType.STRING] = bait.type + player.inventory.itemInMainHand.itemMeta = meta + player.sendMessage(player.inventory.itemInMainHand.itemMeta!!.persistentDataContainer.keys.toString()) player.sendMessage("§a| §7成功安装鱼饵") } }