From 9e15bdced5b43598248f316f650511446ef038d3 Mon Sep 17 00:00:00 2001 From: tony_all Date: Mon, 27 Jun 2022 17:31:13 +0800 Subject: [PATCH] update --- .../sanseyooyea/sansefish/ItemUtils.kt | 38 ---------- .../sanseyooyea/sansefish/SanseFish.kt | 2 + .../sansefish/command/GetCommand.kt | 12 ++-- .../sansefish/listener/FishListener.kt | 11 +-- .../sansefish/manager/BaitManager.kt | 21 ++++++ .../sansefish/manager/RodManager.kt | 43 +++++++++++ .../sanseyooyea/sansefish/misc/Bait.kt | 4 ++ .../sanseyooyea/sansefish/misc/FishRod.kt | 9 +++ .../sansefish/misc/FishingRodInstance.kt | 20 ++++++ .../sansefish/reward/RewardHandler.kt | 5 +- .../sansefish/storage/StorageCommand.kt | 71 +++++++++++++++++++ src/main/resources/ItemStorage.yaml | 0 src/main/resources/rods/default.yml | 3 + src/main/resources/settings.yml | 9 ++- 14 files changed, 194 insertions(+), 54 deletions(-) delete mode 100644 src/main/kotlin/work/microhand/sanseyooyea/sansefish/ItemUtils.kt create mode 100644 src/main/kotlin/work/microhand/sanseyooyea/sansefish/manager/BaitManager.kt create mode 100644 src/main/kotlin/work/microhand/sanseyooyea/sansefish/manager/RodManager.kt create mode 100644 src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/Bait.kt create mode 100644 src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/FishRod.kt create mode 100644 src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/FishingRodInstance.kt create mode 100644 src/main/kotlin/work/microhand/sanseyooyea/sansefish/storage/StorageCommand.kt create mode 100644 src/main/resources/ItemStorage.yaml create mode 100644 src/main/resources/rods/default.yml diff --git a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/ItemUtils.kt b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/ItemUtils.kt deleted file mode 100644 index 88f3236..0000000 --- a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/ItemUtils.kt +++ /dev/null @@ -1,38 +0,0 @@ -package work.microhand.sanseyooyea.sansefish - -import org.bukkit.Material -import org.bukkit.inventory.ItemStack -import taboolib.platform.util.ItemBuilder -import taboolib.platform.util.buildItem - -data class FishingRod( - var durability: Int = SanseFish.conf.getInt("target-rod.max-durability", 100), - var luck: Int = 0, -) { - fun buildFishingRod(): ItemStack = buildItem(Material.FISHING_ROD) { - lore.clear() - lore.addAll(listOf("", "", "§8神奇的鱼竿")) - 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() - } -} - -/** - * FishingRod - * - * §e耐久: §7 - * §d幸运值: §7 - * §8神奇的鱼竿 - */ -fun parseRod(item: ItemStack): FishingRod? { - val builder = ItemBuilder(item) - if (!builder.lore.contains("§8神奇的鱼竿")) { - return null - } - 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 FishingRod(durability, luck) -} \ No newline at end of file diff --git a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/SanseFish.kt b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/SanseFish.kt index 9923065..84f6358 100644 --- a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/SanseFish.kt +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/SanseFish.kt @@ -7,6 +7,7 @@ import taboolib.module.configuration.Configuration import work.microhand.sanseyooyea.sansefish.command.fishCmd import work.microhand.sanseyooyea.sansefish.command.registerCommand import work.microhand.sanseyooyea.sansefish.reward.RewardHandler +import work.microhand.sanseyooyea.sansefish.storage.StorageCommand object SanseFish : Plugin() { @@ -18,6 +19,7 @@ object SanseFish : Plugin() { info("§a| §7正在加载钓鱼插件(SanseFish)..") info("§a| §7作者QQ: 1187586838") registerCommand() + StorageCommand.registerCommand() fishCmd() RewardHandler 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 073a9ba..f08dbb9 100644 --- a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/command/GetCommand.kt +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/command/GetCommand.kt @@ -6,14 +6,18 @@ import org.bukkit.entity.Player import taboolib.common.platform.command.command import taboolib.library.xseries.setItemStack import taboolib.platform.util.giveItem -import work.microhand.sanseyooyea.sansefish.FishingRod import work.microhand.sanseyooyea.sansefish.SanseFish +import work.microhand.sanseyooyea.sansefish.manager.RodManager fun registerCommand() = command("giveRod", permission = "sansefish.giverod") { dynamic { - execute { sender, _, argument -> - val target = Bukkit.getPlayerExact(argument) ?: return@execute sender.sendMessage("§c| §7该玩家不在线") - target.giveItem(FishingRod().buildFishingRod()) + dynamic { + execute { sender, ctx, arg -> + val target = + Bukkit.getPlayerExact(ctx.argument(-1)) ?: return@execute sender.sendMessage("§c| §7该玩家不在线") + val rod = RodManager.rods[arg]?: return@execute sender.sendMessage("§c| §7该鱼竿不存在") + target.giveItem(rod.buildFishingRod()) + } } } } 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 6b136e9..83f4135 100644 --- a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/listener/FishListener.kt +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/listener/FishListener.kt @@ -7,7 +7,7 @@ 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.parseRod +import work.microhand.sanseyooyea.sansefish.manager.RodManager import work.microhand.sanseyooyea.sansefish.reward.RewardHandler object FishListener { @@ -15,7 +15,7 @@ object FishListener { fun onFish(fishEvent: PlayerFishEvent) { val player = fishEvent.player // Disable Vanilla Fishing Rod - if (fishEvent.state == PlayerFishEvent.State.FISHING && parseRod(player.inventory.itemInMainHand) == null && parseRod( + if (fishEvent.state == PlayerFishEvent.State.FISHING && RodManager.parseRod(player.inventory.itemInMainHand) == null && RodManager.parseRod( player.inventory.itemInOffHand ) == null ) { @@ -25,13 +25,14 @@ object FishListener { } var offHand = false - val rod = parseRod(player.inventory.itemInMainHand) ?: parseRod(player.inventory.itemInOffHand).also { + val rod = RodManager.parseRod(player.inventory.itemInMainHand) ?: RodManager.parseRod(player.inventory.itemInOffHand).also { offHand = true } ?: throw IllegalStateException("???你咋钓鱼的") 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) { @@ -46,8 +47,8 @@ object FishListener { val resultItem = if (rod.durability <= 0) { player.sendMessage("§c| §7您的鱼竿已损坏") ItemStack(Material.AIR) - } else rod.buildFishingRod() - + } else rod.updateFishingRod(if (offHand) player.inventory.itemInOffHand else player.inventory.itemInMainHand) + if (offHand) player.inventory.itemInOffHand = resultItem else player.inventory.itemInMainHand = resultItem } } \ 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 new file mode 100644 index 0000000..1d6da7d --- /dev/null +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/manager/BaitManager.kt @@ -0,0 +1,21 @@ +package work.microhand.sanseyooyea.sansefish.manager + +import taboolib.common.platform.function.getDataFolder +import taboolib.module.configuration.Configuration +import work.microhand.sanseyooyea.sansefish.misc.Bait +import java.io.File + +object BaitManager { + val baits = HashMap() + + 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,) + } + } +} \ 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 new file mode 100644 index 0000000..dd8a5a0 --- /dev/null +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/manager/RodManager.kt @@ -0,0 +1,43 @@ +package work.microhand.sanseyooyea.sansefish.manager + +import org.bukkit.inventory.ItemStack +import taboolib.common.platform.function.getDataFolder +import taboolib.module.configuration.Configuration +import taboolib.platform.util.ItemBuilder +import work.microhand.sanseyooyea.sansefish.SanseFish +import work.microhand.sanseyooyea.sansefish.misc.FishingRodInstance +import work.microhand.sanseyooyea.sansefish.misc.FishRod +import java.io.File + +object RodManager { + val rods = HashMap() + + init { + // load fish rods. + File(getDataFolder(), "rods").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")!! + rods[file.nameWithoutExtension] = FishRod(file.nameWithoutExtension, lore, durability, luck) + } + } + + fun parseRod(item: ItemStack): FishingRodInstance? { + val builder = ItemBuilder(item) + var type: FishRod? = null + rods.forEach { (_, rod) -> + if (builder.lore.contains(rod.lore)) { + type = rod + } + } + + // null if not target fishing rod. + if (type == null) return null + + 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) + } +} \ 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 new file mode 100644 index 0000000..a191432 --- /dev/null +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/Bait.kt @@ -0,0 +1,4 @@ +package work.microhand.sanseyooyea.sansefish.misc + +data class Bait(val type: String, ) { +} \ No newline at end of file diff --git a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/FishRod.kt b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/FishRod.kt new file mode 100644 index 0000000..ca0edb9 --- /dev/null +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/FishRod.kt @@ -0,0 +1,9 @@ +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)) +} diff --git a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/FishingRodInstance.kt b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/FishingRodInstance.kt new file mode 100644 index 0000000..fe94e8d --- /dev/null +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/misc/FishingRodInstance.kt @@ -0,0 +1,20 @@ +package work.microhand.sanseyooyea.sansefish.misc + +import org.bukkit.inventory.ItemStack +import taboolib.platform.util.buildItem +import work.microhand.sanseyooyea.sansefish.SanseFish + +data class FishingRodInstance( + val type: FishRod, + var durability: Int = type.maxDurability, + var luck: Int = type.defaultLuck, +) { + fun updateFishingRod(legacy: ItemStack): ItemStack = buildItem(legacy) { + lore.clear() + lore.addAll(listOf("", "", "§8神奇的鱼竿")) + 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() + } +} diff --git a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/reward/RewardHandler.kt b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/reward/RewardHandler.kt index df20739..9e193b8 100644 --- a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/reward/RewardHandler.kt +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/reward/RewardHandler.kt @@ -1,14 +1,15 @@ package work.microhand.sanseyooyea.sansefish.reward import org.bukkit.entity.Player -import work.microhand.sanseyooyea.sansefish.FishingRod import work.microhand.sanseyooyea.sansefish.SanseFish +import work.microhand.sanseyooyea.sansefish.misc.FishingRodInstance object RewardHandler { private val rewardList = ArrayList() // load Rewards init { + @Suppress("UNCHECKED_CAST") fun parseReward(config: Map) = Reward( config["commands"] as List, @@ -22,7 +23,7 @@ object RewardHandler { rewardList.addAll(SanseFish.conf.getList("rewards")!!.map { parseReward(it as Map) }) } - fun doReward(player: Player, rod: FishingRod) { + fun doReward(player: Player, rod: FishingRodInstance) { rewardList.forEach { // handle deLuck if (it.deLuck >= 0) { diff --git a/src/main/kotlin/work/microhand/sanseyooyea/sansefish/storage/StorageCommand.kt b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/storage/StorageCommand.kt new file mode 100644 index 0000000..86afd06 --- /dev/null +++ b/src/main/kotlin/work/microhand/sanseyooyea/sansefish/storage/StorageCommand.kt @@ -0,0 +1,71 @@ +package work.microhand.sanseyooyea.sansefish.storage + +import org.bukkit.Bukkit +import org.bukkit.entity.Player +import taboolib.common.platform.command.CommandBuilder +import taboolib.common.platform.command.command +import taboolib.library.xseries.getItemStack +import taboolib.library.xseries.setItemStack +import taboolib.module.configuration.Config +import taboolib.module.configuration.Configuration +import taboolib.platform.util.giveItem + +object StorageCommand { + @Config("ItemStorage.yaml") + lateinit var itemStorage: Configuration + private set + + fun registerCommand() { + command("istorage", permission = "sansefish.istorage") { + commandSet() + commandRemove() + commandList() + commandGive() + } + } + + private fun CommandBuilder.CommandBase.commandSet() = literal("set") { + dynamic { + execute { sender, _, arg -> + val item = sender.inventory.itemInMainHand + itemStorage.setItemStack(arg, item) + itemStorage.saveToFile() + sender.sendMessage("§a| §7已将该物品设置为 §a${arg}") + } + } + } + + private fun CommandBuilder.CommandBase.commandRemove() = literal("remove") { + dynamic { + execute { sender, _, arg -> + itemStorage[arg] = null + itemStorage.saveToFile() + sender.sendMessage("§a| §7已移除物品 §a${arg}") + } + } + } + + private fun CommandBuilder.CommandBase.commandList() = literal("list") { + execute { sender, _, _ -> + val items = itemStorage.getKeys(false).joinToString(", ") + sender.sendMessage("§e| §7目前存储的物品有: §e${items}") + } + + } + + //istorage give xxx Item + private fun CommandBuilder.CommandBase.commandGive() = literal("give") { + dynamic { + restrict { _, _, arg -> Bukkit.getPlayerExact(arg) != null } + dynamic { + restrict { _, _, arg -> itemStorage.contains(arg) } + 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) + sender.sendMessage("§a| §7已将物品 §a${arg} §7给予 §a${target.name}") + } + } + } + } +} \ No newline at end of file diff --git a/src/main/resources/ItemStorage.yaml b/src/main/resources/ItemStorage.yaml new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/rods/default.yml b/src/main/resources/rods/default.yml new file mode 100644 index 0000000..482ad6e --- /dev/null +++ b/src/main/resources/rods/default.yml @@ -0,0 +1,3 @@ +lore: "&7指定钓竿" +max-durability: 100 +luck-def: 0 diff --git a/src/main/resources/settings.yml b/src/main/resources/settings.yml index 7dbe9b8..9c8b447 100644 --- a/src/main/resources/settings.yml +++ b/src/main/resources/settings.yml @@ -1,9 +1,8 @@ -target-rod: - lore: "&7指定钓竿" - max-durability: 100 - rewards: - - commands: + - items: + - "物品1" + - "物品2" + commands: - "mm i give xxx" #钓到鱼时执行命令, %player% 为玩家 - "broadcast 玩家 %player% 钓到了好东西!" chance: 0.2 #以小数形式存储