diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/api/EcoSkillsAPI.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/api/EcoSkillsAPI.kt index ef65e74..489a7a4 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/api/EcoSkillsAPI.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/api/EcoSkillsAPI.kt @@ -4,11 +4,14 @@ package com.willfp.ecoskills.api import com.willfp.ecoskills.api.modifiers.StatModifier import com.willfp.ecoskills.effects.Effect +import com.willfp.ecoskills.effects.Effects import com.willfp.ecoskills.effects.effects import com.willfp.ecoskills.skills.Skill +import com.willfp.ecoskills.skills.SkillLevel import com.willfp.ecoskills.skills.Skills import com.willfp.ecoskills.skills.skills import com.willfp.ecoskills.stats.Stat +import com.willfp.ecoskills.stats.Stats import com.willfp.ecoskills.stats.statModifiers import com.willfp.ecoskills.stats.stats import org.bukkit.OfflinePlayer @@ -21,6 +24,18 @@ Skills */ +fun OfflinePlayer.resetSkills() { + for (skill in Skills.values()) { + this.skills.reset(skill) + } + for (stat in Stats.values()) { + this.stats.reset(stat) + } + for (effect in Effects.values()) { + this.effects.reset(effect) + } +} + fun OfflinePlayer.getSkillXP(skill: Skill): Double = this.skills[skill].xp diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandEcoSkills.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandEcoSkills.kt index b855950..c589cbf 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandEcoSkills.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandEcoSkills.kt @@ -12,6 +12,8 @@ class CommandEcoSkills(plugin: EcoPlugin) : PluginCommand( ) { init { this.addSubcommand(CommandReload(plugin)) + .addSubcommand(CommandGive(plugin)) + .addSubcommand(CommandReset(plugin)) } override fun onExecute(sender: CommandSender, args: List) { diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandGive.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandGive.kt new file mode 100644 index 0000000..8cc122a --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandGive.kt @@ -0,0 +1,102 @@ +package com.willfp.ecoskills.commands + +import com.willfp.eco.core.EcoPlugin +import com.willfp.eco.core.command.impl.Subcommand +import com.willfp.eco.util.StringUtils +import com.willfp.eco.util.formatEco +import com.willfp.ecoskills.api.giveSkillXP +import com.willfp.ecoskills.skills.Skills +import org.bukkit.Bukkit +import org.bukkit.command.CommandSender +import org.bukkit.util.StringUtil + + +class CommandGive(plugin: EcoPlugin) : + Subcommand( + plugin, + "give", + "ecoskills.command.give", + false + ) { + + override fun onExecute(sender: CommandSender, args: List) { + if (args.isEmpty()) { + sender.sendMessage(plugin.langYml.getMessage("requires-player")) + return + } + + if (args.size == 1) { + sender.sendMessage(plugin.langYml.getMessage("requires-skill")) + return + } + + if (args.size == 2) { + sender.sendMessage(plugin.langYml.getMessage("requires-amount")) + return + } + + val player = Bukkit.getPlayer(args[0]) + + if (player == null) { + sender.sendMessage(plugin.langYml.getMessage("invalid-player")) + return + } + + val obj = Skills.getByID(args[1].lowercase()) + + if (obj == null) { + sender.sendMessage(plugin.langYml.getMessage("invalid-skill")) + return + } + + val amount = args[2].toIntOrNull() + + if (amount == null) { + sender.sendMessage(plugin.langYml.getMessage("invalid-amount")) + return + } + + player.giveSkillXP(obj, amount.toDouble()) + + sender.sendMessage( + this.plugin.langYml.getMessage("gave-skill-xp", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS) + .replace("%player%", player.name) + .replace("%amount%", amount.toString()) + .replace("%skill%", obj.name) + .formatEco() + ) + } + + override fun tabComplete(sender: CommandSender, args: List): List { + val completions = mutableListOf() + + if (args.size == 1) { + StringUtil.copyPartialMatches( + args[0], + Bukkit.getOnlinePlayers().map { player -> player.name }.toCollection(ArrayList()), + completions + ) + return completions + } + + if (args.size == 2) { + StringUtil.copyPartialMatches( + args[1], + Skills.values().map { it.id }, + completions + ) + return completions + } + + if (args.size == 3) { + StringUtil.copyPartialMatches( + args[2], + listOf(1, 2, 5, 10, 100).map { it.toString() }, + completions + ) + return completions + } + + return emptyList() + } +} diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandReset.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandReset.kt new file mode 100644 index 0000000..7da7705 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandReset.kt @@ -0,0 +1,56 @@ +@file:Suppress("DEPRECATION") + +package com.willfp.ecoskills.commands + +import com.willfp.eco.core.EcoPlugin +import com.willfp.eco.core.command.impl.Subcommand +import com.willfp.ecoskills.api.resetSkills +import org.bukkit.Bukkit +import org.bukkit.command.CommandSender +import org.bukkit.util.StringUtil + +class CommandReset(plugin: EcoPlugin) : + Subcommand( + plugin, + "reset", + "ecoskills.command.reset", + false + ) { + + override fun onExecute(sender: CommandSender, args: List) { + if (args.isEmpty()) { + sender.sendMessage(plugin.langYml.getMessage("requires-player")) + return + } + + if (args[0].equals("all", ignoreCase = true)) { + sender.sendMessage(plugin.langYml.getMessage("resetting-all-players")) + Bukkit.getOfflinePlayers().forEach { it.resetSkills() } + sender.sendMessage(plugin.langYml.getMessage("reset-all-players")) + } else { + val player = Bukkit.getOfflinePlayer(args[0]) + if (!player.hasPlayedBefore()) { + sender.sendMessage(plugin.langYml.getMessage("invalid-player")) + return + } + + sender.sendMessage(plugin.langYml.getMessage("reset-player")) + player.resetSkills() + } + } + + override fun tabComplete(sender: CommandSender, args: List): List { + val completions = mutableListOf() + + if (args.size == 1) { + StringUtil.copyPartialMatches( + args[0], + Bukkit.getOnlinePlayers().map { player -> player.name }, + completions + ) + return completions + } + + return emptyList() + } +} \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/obj/Levellable.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/obj/Levellable.kt index 9c4c706..b23b6e9 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/obj/Levellable.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/obj/Levellable.kt @@ -30,11 +30,13 @@ abstract class Levellable( val config: Config, protected val plugin: EcoSkillsPlugin ) : KRegistrable { + open val startLevel = 1 + private val key by lazy { PersistentDataKey( plugin.createNamespacedKey(id), PersistentDataKeyType.INT, - 1 + startLevel ) } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/SkillLevelMap.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/SkillLevelMap.kt index 7e89ac1..e97a649 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/SkillLevelMap.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/SkillLevelMap.kt @@ -77,4 +77,11 @@ class SkillLevelMap( giveXP(skill, xp) } } + + fun reset(skill: Skill) { + this[skill] = SkillLevel( + skill.startLevel, + 0.0 + ) + } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/util/LevelMap.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/util/LevelMap.kt index 0868b2c..63dffc4 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/util/LevelMap.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/util/LevelMap.kt @@ -15,4 +15,8 @@ class LevelMap( levellable.setSavedLevel(player, level) } -} \ No newline at end of file + + fun reset(levellable: T) { + levellable.setSavedLevel(player, levellable.startLevel) + } +}