From c74a429e73a8fb878d73cd10acbf2d9804ad81ac Mon Sep 17 00:00:00 2001 From: Auxilor Date: Tue, 8 Aug 2023 21:08:44 +0100 Subject: [PATCH] Added /ecoquests reset, improved GUIs, fixed bugs --- .../ecoquests/commands/CommandEcoQuests.kt | 1 + .../willfp/ecoquests/commands/CommandReset.kt | 54 +++++++++++++++++++ .../willfp/ecoquests/commands/CommandStart.kt | 30 +++++++++++ .../willfp/ecoquests/gui/PreviousQuestsGUI.kt | 4 +- .../com/willfp/ecoquests/gui/QuestsGUI.kt | 2 +- .../ecoquests/gui/components/BackButton.kt | 2 +- .../com/willfp/ecoquests/quests/Quest.kt | 16 ++++-- .../kotlin/com/willfp/ecoquests/tasks/Task.kt | 8 ++- .../core-plugin/src/main/resources/config.yml | 18 +++---- .../core-plugin/src/main/resources/lang.yml | 3 ++ .../core-plugin/src/main/resources/plugin.yml | 4 ++ 11 files changed, 124 insertions(+), 18 deletions(-) create mode 100644 eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/commands/CommandReset.kt diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/commands/CommandEcoQuests.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/commands/CommandEcoQuests.kt index 684761e..10efa70 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/commands/CommandEcoQuests.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/commands/CommandEcoQuests.kt @@ -13,6 +13,7 @@ class CommandEcoQuests(plugin: EcoPlugin) : PluginCommand( init { this.addSubcommand(CommandReload(plugin)) .addSubcommand(CommandStart(plugin)) + .addSubcommand(CommandReset(plugin)) } override fun onExecute(sender: CommandSender, args: List) { diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/commands/CommandReset.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/commands/CommandReset.kt new file mode 100644 index 0000000..9ee61a7 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/commands/CommandReset.kt @@ -0,0 +1,54 @@ +package com.willfp.ecoquests.commands + +import com.willfp.eco.core.EcoPlugin +import com.willfp.eco.core.command.impl.PluginCommand +import com.willfp.eco.util.StringUtils +import com.willfp.ecoquests.gui.QuestsGUI +import com.willfp.ecoquests.quests.Quests +import com.willfp.libreforge.commands.CommandReload +import org.bukkit.command.CommandSender +import org.bukkit.entity.Player +import org.bukkit.util.StringUtil + +class CommandReset(plugin: EcoPlugin) : PluginCommand( + plugin, + "reset", + "ecoquests.command.reset", + false +) { + override fun onExecute(sender: CommandSender, args: List) { + val player = notifyPlayerRequired(args.getOrNull(0), "invalid-player") + val quest = notifyNull(Quests[args.getOrNull(1)], "invalid-quest") + + quest.reset(player) + + sender.sendMessage( + plugin.langYml.getMessage("reset-quest", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS) + .replace("%quest%", quest.name) + .replace("%player%", player.name) + ) + } + + override fun tabComplete(sender: CommandSender, args: List): List { + val completions = mutableListOf() + + if (args.size == 1) { + StringUtil.copyPartialMatches( + args[0], + plugin.server.onlinePlayers.map { it.name }, + completions + ) + } + + if (args.size == 2) { + StringUtil.copyPartialMatches( + args[1], + Quests.values().map { it.name }, + completions + ) + } + + return completions + } +} + diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/commands/CommandStart.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/commands/CommandStart.kt index 8feef79..5b8c6cb 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/commands/CommandStart.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/commands/CommandStart.kt @@ -2,11 +2,13 @@ package com.willfp.ecoquests.commands import com.willfp.eco.core.EcoPlugin import com.willfp.eco.core.command.impl.PluginCommand +import com.willfp.eco.util.StringUtils import com.willfp.ecoquests.gui.QuestsGUI import com.willfp.ecoquests.quests.Quests import com.willfp.libreforge.commands.CommandReload import org.bukkit.command.CommandSender import org.bukkit.entity.Player +import org.bukkit.util.StringUtil class CommandStart(plugin: EcoPlugin) : PluginCommand( plugin, @@ -24,6 +26,34 @@ class CommandStart(plugin: EcoPlugin) : PluginCommand( } quest.start(player) + + sender.sendMessage( + plugin.langYml.getMessage("started-quest", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS) + .replace("%quest%", quest.name) + .replace("%player%", player.name) + ) + } + + override fun tabComplete(sender: CommandSender, args: List): List { + val completions = mutableListOf() + + if (args.size == 1) { + StringUtil.copyPartialMatches( + args[0], + plugin.server.onlinePlayers.map { it.name }, + completions + ) + } + + if (args.size == 2) { + StringUtil.copyPartialMatches( + args[1], + Quests.values().map { it.name }, + completions + ) + } + + return completions } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/gui/PreviousQuestsGUI.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/gui/PreviousQuestsGUI.kt index 3cae7c5..732ac19 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/gui/PreviousQuestsGUI.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/gui/PreviousQuestsGUI.kt @@ -63,7 +63,9 @@ object PreviousQuestsGUI { } onClose { event, _ -> - QuestsGUI.open(event.player as Player) + plugin.scheduler.run { + QuestsGUI.open(event.player as Player) + } } } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/gui/QuestsGUI.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/gui/QuestsGUI.kt index a3d0858..760e70a 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/gui/QuestsGUI.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/gui/QuestsGUI.kt @@ -35,7 +35,7 @@ object QuestsGUI { ) ) - addComponent(QuestInfoComponent(plugin.configYml.getSubsection("gui.gui-info"))) + addComponent(QuestInfoComponent(plugin.configYml.getSubsection("gui.quest-info"))) addComponent(CloseButton(plugin.configYml.getSubsection("gui.close"))) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/gui/components/BackButton.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/gui/components/BackButton.kt index 254bfd4..1ef6ba5 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/gui/components/BackButton.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/gui/components/BackButton.kt @@ -14,7 +14,7 @@ class BackButton( Items.lookup(config.getString("item")) ) { onLeftClick { player, _, _, _ -> - player.closeInventory() + QuestsGUI.open(player) } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/quests/Quest.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/quests/Quest.kt index 4275f0f..1927363 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/quests/Quest.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/quests/Quest.kt @@ -35,7 +35,7 @@ class Quest( guiItem.clone().modify { addLoreLines( addPlaceholdersInto( - config.getStrings("gui.quest-area.quest-icon.lore"), + plugin.configYml.getStrings("quests.icon.lore"), player ) ) @@ -90,6 +90,15 @@ class Quest( return player.profile.read(hasStartedKey) } + fun reset(player: Player) { + player.profile.write(hasStartedKey, false) + player.profile.write(hasCompletedKey, false) + + for (task in tasks) { + task.reset(player) + } + } + fun start(player: Player) { if (hasStarted(player)) { return @@ -131,11 +140,8 @@ class Quest( fun String.addPlaceholders() = this .replace("%quest%", quest.name) - // Replace placeholders in the strings with their actual values. - val withPlaceholders = strings.map { it.addPlaceholders() } - // Replace multi-line placeholders. - val processed = withPlaceholders.flatMap { s -> + val processed = strings.flatMap { s -> val margin = s.length - s.trimStart().length if (s.contains("%rewards%")) { diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/tasks/Task.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/tasks/Task.kt index b200b51..b6b9dc9 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/tasks/Task.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoquests/tasks/Task.kt @@ -16,6 +16,7 @@ import com.willfp.libreforge.counters.Accumulator import com.willfp.libreforge.counters.Counters import org.bukkit.Bukkit import org.bukkit.entity.Player +import kotlin.math.min class Task( private val plugin: EcoPlugin, @@ -75,12 +76,17 @@ class Task( } } + fun reset(player: Player) { + player.profile.write(xpKey, 0.0) + player.profile.write(hasCompletedKey, false) + } + fun hasCompleted(player: Player): Boolean { return player.profile.read(hasCompletedKey) } fun getExperience(player: Player): Double { - return player.profile.read(xpKey) + return min(player.profile.read(xpKey), getExperienceRequired(player)) } fun getExperienceRequired(player: Player): Double { diff --git a/eco-core/core-plugin/src/main/resources/config.yml b/eco-core/core-plugin/src/main/resources/config.yml index 603d7fd..6790a12 100644 --- a/eco-core/core-plugin/src/main/resources/config.yml +++ b/eco-core/core-plugin/src/main/resources/config.yml @@ -51,15 +51,6 @@ gui: row: 5 column: 8 - quest-icon: - lore: - - "" - - "&fTasks:" - - " %tasks%" - - "" - - "&fRewards:" - - " %rewards%" - prev-page: item: arrow name:"&fPrevious Page" location: @@ -144,6 +135,15 @@ tasks: not-completed: "&c&l✘ &r&f%description%" quests: + icon: + lore: + - "" + - "&fTasks:" + - " %tasks%" + - "" + - "&fRewards:" + - " %rewards%" + complete: message: enabled: true diff --git a/eco-core/core-plugin/src/main/resources/lang.yml b/eco-core/core-plugin/src/main/resources/lang.yml index 2821041..314db9c 100644 --- a/eco-core/core-plugin/src/main/resources/lang.yml +++ b/eco-core/core-plugin/src/main/resources/lang.yml @@ -8,3 +8,6 @@ messages: invalid-player: "&cInvalid player!" invalid-quest: "&cInvalid quest!" already-started: "&cThe player has already started this quest!" + + started-quest: "&fStarted the &a%quest% &fquest for &a%player%&f!" + reset-quest: "&fReset the &a%quest% &fquest for &a%player%&f!" \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/resources/plugin.yml b/eco-core/core-plugin/src/main/resources/plugin.yml index 4064213..6538d71 100644 --- a/eco-core/core-plugin/src/main/resources/plugin.yml +++ b/eco-core/core-plugin/src/main/resources/plugin.yml @@ -33,6 +33,7 @@ permissions: ecoquests.command.reload: true ecoquests.command.quests: true ecoquests.command.start: op + ecoquests.command.reset: op ecoquests.command.reload: description: Allows reloading the config @@ -45,4 +46,7 @@ permissions: default: true ecoquests.command.start: description: Allows using /ecoquests start. + default: op + ecoquests.command.reset: + description: Allows using /ecoquests reset. default: op \ No newline at end of file