From 9891a7188d50a1c828ab874d30aa371fdacae9e2 Mon Sep 17 00:00:00 2001 From: TONY_All Date: Sun, 3 Jul 2022 22:17:29 +0800 Subject: [PATCH] update --- build.gradle.kts | 5 +- .../sanseyooyea/sansefish/SanseFish.kt | 14 ++++-- .../sansefish/command/GetCommand.kt | 15 ++++-- .../{storage => command}/StorageCommand.kt | 4 +- .../sansefish/listener/FishListener.kt | 47 +++++++++++-------- .../sansefish/manager/BaitManager.kt | 31 ++++++++++-- .../sansefish/manager/RodManager.kt | 2 +- .../sanseyooyea/sansefish/misc/Bait.kt | 6 ++- .../RewardHandler.kt => misc/BaitReward.kt} | 9 ++-- .../sanseyooyea/sansefish/misc/FishRod.kt | 6 +-- .../sansefish/misc/FishingRodInstance.kt | 16 ++++++- .../sansefish/{reward => misc}/Reward.kt | 2 +- .../sanseyooyea/sansefish/ui/BaitUI.kt | 40 ++++++++++++++++ src/main/resources/baits/a.yml | 14 ++++++ src/main/resources/rods/default.yml | 2 + 15 files changed, 162 insertions(+), 51 deletions(-) rename src/main/kotlin/work/microhand/sanseyooyea/sansefish/{storage => command}/StorageCommand.kt (96%) rename src/main/kotlin/work/microhand/sanseyooyea/sansefish/{reward/RewardHandler.kt => misc/BaitReward.kt} (77%) rename src/main/kotlin/work/microhand/sanseyooyea/sansefish/{reward => misc}/Reward.kt (93%) create mode 100644 src/main/kotlin/work/microhand/sanseyooyea/sansefish/ui/BaitUI.kt create mode 100644 src/main/resources/baits/a.yml diff --git a/build.gradle.kts b/build.gradle.kts index 91b9369..203ecef 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -10,6 +10,8 @@ taboolib { install("common-5") install("module-database") install("module-chat") + install("module-ui") + install("module-ui-receptacle") install("module-configuration") install("expansion-player-database") install("platform-bukkit") @@ -23,7 +25,8 @@ repositories { dependencies { compileOnly("ink.ptms:nms-all:1.0.0") - compileOnly("ink.ptms.core:v11200:11200-minimize") + 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/SanseFish.kt b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/SanseFish.kt index 84f6358..2b53cbb 100644 --- a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/SanseFish.kt +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/SanseFish.kt @@ -1,13 +1,14 @@ package work.microhand.sanseyooyea.sansefish +import org.bukkit.NamespacedKey import taboolib.common.platform.Plugin import taboolib.common.platform.function.info import taboolib.module.configuration.Config import taboolib.module.configuration.Configuration -import work.microhand.sanseyooyea.sansefish.command.fishCmd +import taboolib.platform.BukkitPlugin +import work.microhand.sanseyooyea.sansefish.command.setBaitCmd import work.microhand.sanseyooyea.sansefish.command.registerCommand -import work.microhand.sanseyooyea.sansefish.reward.RewardHandler -import work.microhand.sanseyooyea.sansefish.storage.StorageCommand +import work.microhand.sanseyooyea.sansefish.command.StorageCommand object SanseFish : Plugin() { @@ -15,13 +16,16 @@ object SanseFish : Plugin() { lateinit var conf: Configuration private set + val baitKey = NamespacedKey(BukkitPlugin.getInstance(), "bait") + val maxLuckKey = NamespacedKey(BukkitPlugin.getInstance(), "max_luck") + override fun onEnable() { info("§a| §7正在加载钓鱼插件(SanseFish)..") info("§a| §7作者QQ: 1187586838") registerCommand() StorageCommand.registerCommand() - fishCmd() - RewardHandler + 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 f08dbb9..a988b11 100644 --- a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/command/GetCommand.kt +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/command/GetCommand.kt @@ -7,6 +7,7 @@ import taboolib.common.platform.command.command import taboolib.library.xseries.setItemStack import taboolib.platform.util.giveItem import work.microhand.sanseyooyea.sansefish.SanseFish +import work.microhand.sanseyooyea.sansefish.manager.BaitManager import work.microhand.sanseyooyea.sansefish.manager.RodManager fun registerCommand() = command("giveRod", permission = "sansefish.giverod") { @@ -22,10 +23,14 @@ fun registerCommand() = command("giveRod", permission = "sansefish.giverod") { } } -fun fishCmd() = command("setBait", permission = "sansefish.setbait") { - execute { sender, _, _ -> - sender.sendMessage("§a| §7您手中物品已设置为鱼饵.") - SanseFish.conf.setItemStack("target-rod.bait", sender.inventory.itemInMainHand.clone().apply { amount = 1 }) - SanseFish.conf.saveToFile() +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/storage/StorageCommand.kt b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/command/StorageCommand.kt similarity index 96% rename from src/main/kotlin/work/microhand/sanseyooyea/sansefish/storage/StorageCommand.kt rename to src/main/kotlin/work/microhand/sanseyooyea/sansefish/command/StorageCommand.kt index 86afd06..eb9071f 100644 --- a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/storage/StorageCommand.kt +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/command/StorageCommand.kt @@ -1,4 +1,4 @@ -package work.microhand.sanseyooyea.sansefish.storage +package work.microhand.sanseyooyea.sansefish.command import org.bukkit.Bukkit import org.bukkit.entity.Player @@ -62,7 +62,7 @@ object StorageCommand { execute { sender, ctx, arg -> val target = Bukkit.getPlayerExact(ctx.argument(-1)) val item = itemStorage.getItemStack(arg)?: return@execute sender.sendMessage("§c| §7该物品不存在.") - target.giveItem(item) + target!!.giveItem(item) sender.sendMessage("§a| §7已将物品 §a${arg} §7给予 §a${target.name}") } } 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 83f4135..9f10c06 100644 --- a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/listener/FishListener.kt +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/listener/FishListener.kt @@ -1,14 +1,15 @@ package work.microhand.sanseyooyea.sansefish.listener import org.bukkit.Material +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 taboolib.library.xseries.getItemStack -import taboolib.platform.util.hasItem import work.microhand.sanseyooyea.sansefish.SanseFish import work.microhand.sanseyooyea.sansefish.manager.RodManager -import work.microhand.sanseyooyea.sansefish.reward.RewardHandler +import work.microhand.sanseyooyea.sansefish.ui.BaitUI object FishListener { @SubscribeEvent @@ -25,30 +26,36 @@ object FishListener { } var offHand = false - val rod = RodManager.parseRod(player.inventory.itemInMainHand) ?: RodManager.parseRod(player.inventory.itemInOffHand).also { - offHand = true - } ?: throw IllegalStateException("???你咋钓鱼的") + val rod = + RodManager.parseRod(player.inventory.itemInMainHand) ?: RodManager.parseRod(player.inventory.itemInOffHand) + .also { + offHand = true + } ?: throw IllegalStateException("???你咋钓鱼的") + 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)) return + } if (fishEvent.state != PlayerFishEvent.State.BITE) rod.durability-- if (fishEvent.state == PlayerFishEvent.State.CAUGHT_FISH) { - fishEvent.expToDrop = 0 - rod.luck++ - fishEvent.caught.remove() - if (!player.inventory.hasItem(1) { - it.clone().apply { amount = 1 } == SanseFish.conf.getItemStack("target-rod.bait") - }) { - player.sendMessage("§c| §7您没有足够的鱼饵.") - return - } - player.inventory.removeItem(SanseFish.conf.getItemStack("target-rod.bait")) - RewardHandler.doReward(player, rod) + rod.handleFished(fishEvent) } val resultItem = if (rod.durability <= 0) { player.sendMessage("§c| §7您的鱼竿已损坏") ItemStack(Material.AIR) - } else rod.updateFishingRod(if (offHand) player.inventory.itemInOffHand else player.inventory.itemInMainHand) - - if (offHand) player.inventory.itemInOffHand = resultItem else player.inventory.itemInMainHand = resultItem + } else rod.updateFishingRod() + + if (offHand) player.inventory.setItemInOffHand(resultItem) else player.inventory.setItemInMainHand(resultItem) + } + + @SubscribeEvent + fun useBait(event: PlayerInteractEvent) { + 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 + RodManager.parseRod(event.item!!) ?: return + BaitUI(event.player).open() } } \ No newline at end of file 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 1d6da7d..03592b3 100644 --- a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/manager/BaitManager.kt +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/manager/BaitManager.kt @@ -1,21 +1,44 @@ package work.microhand.sanseyooyea.sansefish.manager +import org.bukkit.inventory.ItemStack import taboolib.common.platform.function.getDataFolder +import taboolib.library.xseries.getItemStack import taboolib.module.configuration.Configuration +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() init { + init() + } + + fun parseBait(item: ItemStack): Bait? { + baits.forEach { (_, bait) -> + if (bait.item.isSimilar(item)) { + return bait + } + } + return null + } + + private fun init() { // load baits. File(getDataFolder(), "baits").listFiles()?.forEach { file -> val config = Configuration.loadFromFile(file) - val durability = config.getInt("max-durability") - val luck = config.getInt("luck-def") - val lore = config.getString("lore")!! - baits[file.nameWithoutExtension] = Bait(file.nameWithoutExtension,) + val key = config.getString("item")!! + val item = StorageCommand.itemStorage.getItemStack(key)?: throw IllegalArgumentException("物品 $key 不存在.") + val rewards = BaitReward(config) + baits[file.nameWithoutExtension] = Bait(file.nameWithoutExtension, item, rewards, config) } } + + fun reload() { + baits.clear() + init() + } } \ No newline at end of file diff --git a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/manager/RodManager.kt b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/manager/RodManager.kt index dd8a5a0..a27be58 100644 --- a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/manager/RodManager.kt +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/manager/RodManager.kt @@ -38,6 +38,6 @@ object RodManager { val durability = builder.lore.findLast { it.startsWith("§e耐久: §7") }?.replace("§e耐久: §7", "")?.toInt() ?: SanseFish.conf.getInt("target-rod.max-durability") val luck = builder.lore.findLast { it.startsWith("§d幸运值: §7") }?.replace("§d幸运值: §7", "")?.toInt() ?: 0 - return FishingRodInstance(type!!, durability, luck) + return FishingRodInstance(type!!, item, durability, luck) } } \ No newline at end of file diff --git a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/Bait.kt b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/Bait.kt index a191432..4ca8992 100644 --- a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/Bait.kt +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/Bait.kt @@ -1,4 +1,6 @@ package work.microhand.sanseyooyea.sansefish.misc -data class Bait(val type: String, ) { -} \ No newline at end of file +import org.bukkit.inventory.ItemStack +import taboolib.module.configuration.Configuration + +data class Bait(val type: String, val item: ItemStack, val rewards: BaitReward, val config: Configuration) \ No newline at end of file diff --git a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/reward/RewardHandler.kt b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/BaitReward.kt similarity index 77% rename from src/main/kotlin/work/microhand/sanseyooyea/sansefish/reward/RewardHandler.kt rename to src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/BaitReward.kt index 9e193b8..576b524 100644 --- a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/reward/RewardHandler.kt +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/BaitReward.kt @@ -1,10 +1,9 @@ -package work.microhand.sanseyooyea.sansefish.reward +package work.microhand.sanseyooyea.sansefish.misc import org.bukkit.entity.Player -import work.microhand.sanseyooyea.sansefish.SanseFish -import work.microhand.sanseyooyea.sansefish.misc.FishingRodInstance +import taboolib.library.configuration.ConfigurationSection -object RewardHandler { +class BaitReward(config: ConfigurationSection) { private val rewardList = ArrayList() // load Rewards @@ -20,7 +19,7 @@ object RewardHandler { config["de-luck"] as Int ) - rewardList.addAll(SanseFish.conf.getList("rewards")!!.map { parseReward(it as Map) }) + rewardList.addAll(config.getList("rewards")!!.map { parseReward(it as Map) }) } fun doReward(player: Player, rod: FishingRodInstance) { diff --git a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/FishRod.kt b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/FishRod.kt index ca0edb9..59a15d5 100644 --- a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/FishRod.kt +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/FishRod.kt @@ -1,9 +1,7 @@ package work.microhand.sanseyooyea.sansefish.misc -import org.bukkit.Material import org.bukkit.inventory.ItemStack -import work.microhand.sanseyooyea.sansefish.misc.FishingRodInstance data class FishRod(val type: String, val lore: String, val maxDurability: Int, val defaultLuck: Int) { - fun buildFishingRod(): ItemStack = FishingRodInstance(this).updateFishingRod(ItemStack(Material.FISHING_ROD)) -} + fun buildFishingRod(): ItemStack = FishingRodInstance(this).updateFishingRod() +} \ No newline at end of file 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 fe94e8d..2cd1c93 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,20 @@ package work.microhand.sanseyooyea.sansefish.misc +import org.bukkit.Material +import org.bukkit.event.player.PlayerFishEvent import org.bukkit.inventory.ItemStack +import org.bukkit.persistence.PersistentDataType import taboolib.platform.util.buildItem import work.microhand.sanseyooyea.sansefish.SanseFish +import work.microhand.sanseyooyea.sansefish.manager.BaitManager data class FishingRodInstance( val type: FishRod, + val item: ItemStack = ItemStack(Material.FISHING_ROD), var durability: Int = type.maxDurability, var luck: Int = type.defaultLuck, ) { - fun updateFishingRod(legacy: ItemStack): ItemStack = buildItem(legacy) { + fun updateFishingRod(): ItemStack = buildItem(item) { lore.clear() lore.addAll(listOf("", "", "§8神奇的鱼竿")) lore[0] = "§e耐久: §7$durability" @@ -17,4 +22,13 @@ data class FishingRodInstance( val durabilityPercentage = (durability.toDouble() / SanseFish.conf.getInt("target-rod.max-durability", 100)) damage = (64 * (1 - durabilityPercentage)).toInt() } + + fun handleFished(fishEvent: PlayerFishEvent) { + fishEvent.expToDrop = 0 + luck++ + fishEvent.caught!!.remove() + fishEvent.player.inventory.itemInMainHand + val bait = BaitManager.baits[item.itemMeta!!.persistentDataContainer[SanseFish.baitKey, PersistentDataType.STRING]?: return]!! + bait.rewards.doReward(fishEvent.player, this) + } } diff --git a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/reward/Reward.kt b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/Reward.kt similarity index 93% rename from src/main/kotlin/work/microhand/sanseyooyea/sansefish/reward/Reward.kt rename to src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/Reward.kt index cd162f6..22d4a89 100644 --- a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/reward/Reward.kt +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/Reward.kt @@ -1,4 +1,4 @@ -package work.microhand.sanseyooyea.sansefish.reward +package work.microhand.sanseyooyea.sansefish.misc import org.bukkit.entity.Player import taboolib.common.platform.function.console diff --git a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/ui/BaitUI.kt b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/ui/BaitUI.kt new file mode 100644 index 0000000..1afa558 --- /dev/null +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/ui/BaitUI.kt @@ -0,0 +1,40 @@ +package work.microhand.sanseyooyea.sansefish.ui + +import org.bukkit.entity.Player +import org.bukkit.inventory.PlayerInventory +import org.bukkit.persistence.PersistentDataType +import taboolib.common.platform.function.console +import taboolib.module.ui.buildMenu +import taboolib.module.ui.type.Basic +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 + +class BaitUI(private val player: Player) { + private val baits = HashMap() + + init { + player.inventory.forEach { + val bait = BaitManager.parseBait(it) ?: return@forEach + baits[bait] = baits.getOrDefault(bait, 0) + it.amount + } + } + + fun open() { + val menu = buildMenu> { + rows(6) + baits.onEachIndexed { index, (bait, count) -> + val item = bait.item.clone() + item.amount = if (count >= 64) 64 else count + set(index, item) { + isCancelled = true + player.inventory.removeItem(bait.item) + player.inventory.itemInMainHand.itemMeta!!.persistentDataContainer[SanseFish.baitKey, PersistentDataType.STRING] = bait.type + player.sendMessage("§a| §7成功安装鱼饵") + } + } + } + player.openInventory(menu) + } +} \ No newline at end of file diff --git a/src/main/resources/baits/a.yml b/src/main/resources/baits/a.yml new file mode 100644 index 0000000..1593690 --- /dev/null +++ b/src/main/resources/baits/a.yml @@ -0,0 +1,14 @@ +item: null + +rewards: + - items: + - "物品1" + - "物品2" + commands: + - "mm i give xxx" #钓到鱼时执行命令, %player% 为玩家 + - "broadcast 玩家 %player% 钓到了好东西!" + chance: 0.2 #以小数形式存储 + multiple: 0.01 # 重复出现概率 + isGuarantees: false # 是否为保底物品 + luck: 0 #不受幸运值限制 + de-luck: 0 # 霉运物品扣除幸运值 \ No newline at end of file diff --git a/src/main/resources/rods/default.yml b/src/main/resources/rods/default.yml index 482ad6e..d5867c9 100644 --- a/src/main/resources/rods/default.yml +++ b/src/main/resources/rods/default.yml @@ -1,3 +1,5 @@ lore: "&7指定钓竿" max-durability: 100 luck-def: 0 +compatible-bait: + - "" \ No newline at end of file