diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/EcoJobsPlugin.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/EcoJobsPlugin.kt index 6d69eb3..0dccf3b 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/EcoJobsPlugin.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/EcoJobsPlugin.kt @@ -7,6 +7,8 @@ import com.willfp.ecojobs.commands.CommandEcojobs import com.willfp.ecojobs.commands.CommandJobs import com.willfp.ecojobs.jobs.JobLevelListener import com.willfp.ecojobs.jobs.JobTriggerXPGainListener +import com.willfp.ecojobs.jobs.JoinPriceHandler +import com.willfp.ecojobs.jobs.ResetOnQuitListener import com.willfp.ecojobs.jobs.activeJob import com.willfp.ecojobs.jobs.activeJobLevel import com.willfp.libreforge.LibReforgePlugin @@ -42,7 +44,9 @@ class EcoJobsPlugin : LibReforgePlugin() { override fun loadListeners(): List { return listOf( JobLevelListener(this), - JobTriggerXPGainListener + JobTriggerXPGainListener, + ResetOnQuitListener, + JoinPriceHandler ) } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/commands/CommandReset.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/commands/CommandReset.kt index 373d6c7..aac1df3 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/commands/CommandReset.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/commands/CommandReset.kt @@ -7,6 +7,7 @@ import com.willfp.eco.util.savedDisplayName import com.willfp.ecojobs.jobs.Jobs import com.willfp.ecojobs.jobs.activeJob import com.willfp.ecojobs.jobs.hasJob +import com.willfp.ecojobs.jobs.resetJob import com.willfp.ecojobs.jobs.setJobLevel import com.willfp.ecojobs.jobs.setJobXP import org.bukkit.Bukkit @@ -48,8 +49,7 @@ class CommandReset(plugin: EcoPlugin) : Subcommand(plugin, "reset", "ecojobs.com if (player.activeJob == job) { player.activeJob = null } - player.setJobXP(job, 0.0) - player.setJobLevel(job, 1) + player.resetJob(job) sender.sendMessage( plugin.langYml.getMessage("reset-xp", StringUtils.FormatOption.WITHOUT_PLACEHOLDERS) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/jobs/Job.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/jobs/Job.kt index 162454d..217dff2 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/jobs/Job.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/jobs/Job.kt @@ -40,6 +40,8 @@ class Job( val name = config.getFormattedString("name") val description = config.getFormattedString("description") val isUnlockedByDefault = config.getBool("unlocked-by-default") + val resetsOnQuit = config.getBool("reset-on-quit") + val joinPrice = config.getDouble("join-price") val levelKey: PersistentDataKey = PersistentDataKey( EcoJobsPlugin.instance.namespacedKeyFactory.create("${id}_level"), @@ -253,6 +255,7 @@ class Job( .replace("%description%", this.description) .replace("%job%", this.name) .replace("%level%", (forceLevel ?: player.getJobLevel(this)).toString()) + .replace("%cost%", NumberUtils.format(this.joinPrice)) } .toMutableList() @@ -407,6 +410,11 @@ fun OfflinePlayer.getJobLevel(job: Job): Int = fun OfflinePlayer.setJobLevel(job: Job, level: Int) = this.profile.write(job.levelKey, level) +fun OfflinePlayer.resetJob(job: Job) { + this.setJobLevel(job, 1) + this.setJobXP(job, 0.0) +} + fun OfflinePlayer.getJobProgress(job: Job): Double { val currentXP = this.getJobXP(job) val requiredXP = job.getExpForLevel(this.getJobLevel(job) + 1) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/jobs/JoinPriceHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/jobs/JoinPriceHandler.kt new file mode 100644 index 0000000..d2cdc3c --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/jobs/JoinPriceHandler.kt @@ -0,0 +1,25 @@ +package com.willfp.ecojobs.jobs + +import com.willfp.eco.core.integrations.economy.balance +import com.willfp.ecojobs.api.event.PlayerJobJoinEvent +import org.bukkit.event.EventHandler +import org.bukkit.event.Listener + +object JoinPriceHandler : Listener { + @EventHandler + fun onJoin(event: PlayerJobJoinEvent) { + val player = event.player + val job = event.job + val price = job.joinPrice + + if (price > 0) { + val hasMoney = player.balance >= price + + if (!hasMoney) { + event.isCancelled = true + } + + player.balance -= price + } + } +} diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/jobs/ResetOnQuitListener.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/jobs/ResetOnQuitListener.kt new file mode 100644 index 0000000..5d5a901 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/jobs/ResetOnQuitListener.kt @@ -0,0 +1,17 @@ +package com.willfp.ecojobs.jobs + +import com.willfp.ecojobs.api.event.PlayerJobLeaveEvent +import org.bukkit.event.EventHandler +import org.bukkit.event.Listener + +object ResetOnQuitListener: Listener { + @EventHandler + fun onQuit(event: PlayerJobLeaveEvent) { + val player = event.player + val job = event.job + + if (job.resetsOnQuit) { + player.resetJob(job) + } + } +} diff --git a/eco-core/core-plugin/src/main/resources/config.yml b/eco-core/core-plugin/src/main/resources/config.yml index fb4096d..53b89e1 100644 --- a/eco-core/core-plugin/src/main/resources/config.yml +++ b/eco-core/core-plugin/src/main/resources/config.yml @@ -53,7 +53,7 @@ gui: lore: - "%description%" - "&f" - - "&fEffects:" + - "&fJob Rewards:" - "%effects%" - "" - "&fProgress:" @@ -67,7 +67,7 @@ gui: lore: - "%description%" - "&f" - - "&fEffects:" + - "&fJob Rewards:" - "%effects%" - "" - "&fProgress:" @@ -78,7 +78,7 @@ gui: - "" - "&cThis job is already active!" - not-active-lore: + not-active-lore: # If using join cost, you can use %cost% as a placeholder. - "" - "&eClick to activate this job!" diff --git a/eco-core/core-plugin/src/main/resources/jobs/_example.yml b/eco-core/core-plugin/src/main/resources/jobs/_example.yml index 4c0a502..f30a9ab 100644 --- a/eco-core/core-plugin/src/main/resources/jobs/_example.yml +++ b/eco-core/core-plugin/src/main/resources/jobs/_example.yml @@ -13,6 +13,12 @@ description: "&8&oLevel up by mining blocks" # If the job should be unlocked by default unlocked-by-default: true +# If job progress should be reset when quitting +reset-on-quit: false + +# The price to join this job (set to 0 to disable) +join-price: 0 + # The xp requirements for each job level - add new levels by adding more to this list level-xp-requirements: - 100 diff --git a/eco-core/core-plugin/src/main/resources/jobs/builder.yml b/eco-core/core-plugin/src/main/resources/jobs/builder.yml index f4a3f42..a6f9e07 100644 --- a/eco-core/core-plugin/src/main/resources/jobs/builder.yml +++ b/eco-core/core-plugin/src/main/resources/jobs/builder.yml @@ -3,6 +3,9 @@ description: "&8&oLevel up by placing blocks" unlocked-by-default: true +reset-on-quit: false +join-price: 0 + level-xp-requirements: - 100 - 120 diff --git a/eco-core/core-plugin/src/main/resources/jobs/farmer.yml b/eco-core/core-plugin/src/main/resources/jobs/farmer.yml index 940a800..de38379 100644 --- a/eco-core/core-plugin/src/main/resources/jobs/farmer.yml +++ b/eco-core/core-plugin/src/main/resources/jobs/farmer.yml @@ -3,6 +3,9 @@ description: "&8&oLevel up by farming crops" unlocked-by-default: true +reset-on-quit: false +join-price: 0 + level-xp-requirements: - 100 - 120 diff --git a/eco-core/core-plugin/src/main/resources/jobs/miner.yml b/eco-core/core-plugin/src/main/resources/jobs/miner.yml index 3bbca28..de313f3 100644 --- a/eco-core/core-plugin/src/main/resources/jobs/miner.yml +++ b/eco-core/core-plugin/src/main/resources/jobs/miner.yml @@ -3,6 +3,9 @@ description: "&8&oLevel up by mining blocks" unlocked-by-default: true +reset-on-quit: false +join-price: 0 + level-xp-requirements: - 100 - 120 diff --git a/eco-core/core-plugin/src/main/resources/jobs/slayer.yml b/eco-core/core-plugin/src/main/resources/jobs/slayer.yml index 1f6294a..e4576ba 100644 --- a/eco-core/core-plugin/src/main/resources/jobs/slayer.yml +++ b/eco-core/core-plugin/src/main/resources/jobs/slayer.yml @@ -3,6 +3,9 @@ description: "&8&oLevel up by killing mobs" unlocked-by-default: true +reset-on-quit: false +join-price: 0 + level-xp-requirements: - 100 - 120