From b96d6c3388dabb4e2563e89671dccdeb984da5ad Mon Sep 17 00:00:00 2001 From: Auxilor Date: Wed, 14 Sep 2022 13:26:59 +0100 Subject: [PATCH] Added leave price --- .../com/willfp/ecojobs/EcoJobsPlugin.kt | 4 +- .../kotlin/com/willfp/ecojobs/jobs/Job.kt | 4 +- .../willfp/ecojobs/jobs/JoinPriceHandler.kt | 25 ---------- .../com/willfp/ecojobs/jobs/PriceHandler.kt | 50 +++++++++++++++++++ .../ecojobs/jobs/ResetOnQuitListener.kt | 6 ++- .../core-plugin/src/main/resources/config.yml | 4 +- .../src/main/resources/jobs/_example.yml | 3 +- .../src/main/resources/jobs/builder.yml | 2 + .../src/main/resources/jobs/farmer.yml | 2 + .../src/main/resources/jobs/miner.yml | 2 + .../src/main/resources/jobs/slayer.yml | 2 + 11 files changed, 72 insertions(+), 32 deletions(-) delete mode 100644 eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/jobs/JoinPriceHandler.kt create mode 100644 eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/jobs/PriceHandler.kt 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 0dccf3b..cb0a6d0 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,7 +7,7 @@ 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.PriceHandler import com.willfp.ecojobs.jobs.ResetOnQuitListener import com.willfp.ecojobs.jobs.activeJob import com.willfp.ecojobs.jobs.activeJobLevel @@ -46,7 +46,7 @@ class EcoJobsPlugin : LibReforgePlugin() { JobLevelListener(this), JobTriggerXPGainListener, ResetOnQuitListener, - JoinPriceHandler + PriceHandler ) } 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 217dff2..cdbf828 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 @@ -42,6 +42,7 @@ class Job( val isUnlockedByDefault = config.getBool("unlocked-by-default") val resetsOnQuit = config.getBool("reset-on-quit") val joinPrice = config.getDouble("join-price") + val leavePrice = config.getDouble("leave-price") val levelKey: PersistentDataKey = PersistentDataKey( EcoJobsPlugin.instance.namespacedKeyFactory.create("${id}_level"), @@ -255,7 +256,8 @@ class Job( .replace("%description%", this.description) .replace("%job%", this.name) .replace("%level%", (forceLevel ?: player.getJobLevel(this)).toString()) - .replace("%cost%", NumberUtils.format(this.joinPrice)) + .replace("%join_price%", NumberUtils.format(this.joinPrice)) + .replace("%leave_price%", NumberUtils.format(this.leavePrice)) } .toMutableList() 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 deleted file mode 100644 index d2cdc3c..0000000 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/jobs/JoinPriceHandler.kt +++ /dev/null @@ -1,25 +0,0 @@ -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/PriceHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/jobs/PriceHandler.kt new file mode 100644 index 0000000..d054faf --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/jobs/PriceHandler.kt @@ -0,0 +1,50 @@ +package com.willfp.ecojobs.jobs + +import com.willfp.eco.core.integrations.economy.balance +import com.willfp.ecojobs.api.event.PlayerJobJoinEvent +import com.willfp.ecojobs.api.event.PlayerJobLeaveEvent +import org.bukkit.event.EventHandler +import org.bukkit.event.EventPriority +import org.bukkit.event.Listener + +object PriceHandler : Listener { + @EventHandler( + priority = EventPriority.LOW, + ignoreCancelled = true + ) + 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 + } + } + + @EventHandler( + priority = EventPriority.LOW, + ignoreCancelled = true + ) + fun onLeave(event: PlayerJobLeaveEvent) { + val player = event.player + val job = event.job + val price = job.leavePrice + + 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 index 5d5a901..083307b 100644 --- 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 @@ -4,8 +4,10 @@ import com.willfp.ecojobs.api.event.PlayerJobLeaveEvent import org.bukkit.event.EventHandler import org.bukkit.event.Listener -object ResetOnQuitListener: Listener { - @EventHandler +object ResetOnQuitListener : Listener { + @EventHandler( + ignoreCancelled = true + ) fun onQuit(event: PlayerJobLeaveEvent) { val player = event.player val job = event.job diff --git a/eco-core/core-plugin/src/main/resources/config.yml b/eco-core/core-plugin/src/main/resources/config.yml index 53b89e1..3cc6030 100644 --- a/eco-core/core-plugin/src/main/resources/config.yml +++ b/eco-core/core-plugin/src/main/resources/config.yml @@ -48,6 +48,8 @@ gui: - "" item: player_head texture:eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYmFkYzA0OGE3Y2U3OGY3ZGFkNzJhMDdkYTI3ZDg1YzA5MTY4ODFlNTUyMmVlZWQxZTNkYWYyMTdhMzhjMWEifX19 + # %join_price% and %leave_price% are also available as placeholders + active: name: "%job% &fLvl. &a%level%" lore: @@ -78,7 +80,7 @@ gui: - "" - "&cThis job is already active!" - not-active-lore: # If using join cost, you can use %cost% as a placeholder. + not-active-lore: - "" - "&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 f30a9ab..223128f 100644 --- a/eco-core/core-plugin/src/main/resources/jobs/_example.yml +++ b/eco-core/core-plugin/src/main/resources/jobs/_example.yml @@ -16,8 +16,9 @@ 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) +# The price to join or leave this job (set to 0 to disable) join-price: 0 +leave-price: 0 # The xp requirements for each job level - add new levels by adding more to this list level-xp-requirements: 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 a6f9e07..4b0b501 100644 --- a/eco-core/core-plugin/src/main/resources/jobs/builder.yml +++ b/eco-core/core-plugin/src/main/resources/jobs/builder.yml @@ -4,7 +4,9 @@ description: "&8&oLevel up by placing blocks" unlocked-by-default: true reset-on-quit: false + join-price: 0 +leave-price: 0 level-xp-requirements: - 100 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 de38379..70b3385 100644 --- a/eco-core/core-plugin/src/main/resources/jobs/farmer.yml +++ b/eco-core/core-plugin/src/main/resources/jobs/farmer.yml @@ -4,7 +4,9 @@ description: "&8&oLevel up by farming crops" unlocked-by-default: true reset-on-quit: false + join-price: 0 +leave-price: 0 level-xp-requirements: - 100 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 de313f3..c8ed79d 100644 --- a/eco-core/core-plugin/src/main/resources/jobs/miner.yml +++ b/eco-core/core-plugin/src/main/resources/jobs/miner.yml @@ -4,7 +4,9 @@ description: "&8&oLevel up by mining blocks" unlocked-by-default: true reset-on-quit: false + join-price: 0 +leave-price: 0 level-xp-requirements: - 100 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 e4576ba..fc523bb 100644 --- a/eco-core/core-plugin/src/main/resources/jobs/slayer.yml +++ b/eco-core/core-plugin/src/main/resources/jobs/slayer.yml @@ -4,7 +4,9 @@ description: "&8&oLevel up by killing mobs" unlocked-by-default: true reset-on-quit: false + join-price: 0 +leave-price: 0 level-xp-requirements: - 100