9
0
mirror of https://github.com/Auxilor/EcoQuests.git synced 2025-12-28 03:19:13 +00:00

Added /ecoquests reset, improved GUIs, fixed bugs

This commit is contained in:
Auxilor
2023-08-08 21:08:44 +01:00
parent e71cb57e89
commit c74a429e73
11 changed files with 124 additions and 18 deletions

View File

@@ -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<String>) {

View File

@@ -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<String>) {
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<String>): List<String> {
val completions = mutableListOf<String>()
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
}
}

View File

@@ -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<String>): List<String> {
val completions = mutableListOf<String>()
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
}
}

View File

@@ -63,7 +63,9 @@ object PreviousQuestsGUI {
}
onClose { event, _ ->
QuestsGUI.open(event.player as Player)
plugin.scheduler.run {
QuestsGUI.open(event.player as Player)
}
}
}
}

View File

@@ -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")))

View File

@@ -14,7 +14,7 @@ class BackButton(
Items.lookup(config.getString("item"))
) {
onLeftClick { player, _, _, _ ->
player.closeInventory()
QuestsGUI.open(player)
}
}

View File

@@ -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%")) {

View File

@@ -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 {

View File

@@ -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

View File

@@ -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!"

View File

@@ -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