9
0
mirror of https://github.com/Auxilor/EcoJobs.git synced 2025-12-21 16:09:18 +00:00

Added PlayerJobJoinEvent and PlayerJobLeaveEvent

This commit is contained in:
Auxilor
2022-09-14 13:12:47 +01:00
parent 3780370caf
commit e33d3264d8
3 changed files with 88 additions and 1 deletions

View File

@@ -0,0 +1,30 @@
package com.willfp.ecojobs.api.event
import com.willfp.ecojobs.jobs.Job
import org.bukkit.entity.Player
import org.bukkit.event.Cancellable
import org.bukkit.event.HandlerList
import org.bukkit.event.player.PlayerEvent
class PlayerJobJoinEvent(
who: Player,
val job: Job,
val oldJob: Job?
) : PlayerEvent(who), Cancellable {
private var cancelled = false
override fun isCancelled() = this.cancelled
override fun setCancelled(cancelled: Boolean) {
this.cancelled = cancelled
}
override fun getHandlers(): HandlerList {
return handlerList
}
companion object {
@JvmStatic
val handlerList = HandlerList()
}
}

View File

@@ -0,0 +1,29 @@
package com.willfp.ecojobs.api.event
import com.willfp.ecojobs.jobs.Job
import org.bukkit.entity.Player
import org.bukkit.event.Cancellable
import org.bukkit.event.HandlerList
import org.bukkit.event.player.PlayerEvent
class PlayerJobLeaveEvent(
who: Player,
val job: Job
) : PlayerEvent(who), Cancellable {
private var cancelled = false
override fun isCancelled() = this.cancelled
override fun setCancelled(cancelled: Boolean) {
this.cancelled = cancelled
}
override fun getHandlers(): HandlerList {
return handlerList
}
companion object {
@JvmStatic
val handlerList = HandlerList()
}
}

View File

@@ -15,6 +15,8 @@ import com.willfp.eco.util.formatEco
import com.willfp.eco.util.toNiceString
import com.willfp.ecojobs.EcoJobsPlugin
import com.willfp.ecojobs.api.event.PlayerJobExpGainEvent
import com.willfp.ecojobs.api.event.PlayerJobJoinEvent
import com.willfp.ecojobs.api.event.PlayerJobLeaveEvent
import com.willfp.ecojobs.api.event.PlayerJobLevelUpEvent
import com.willfp.libreforge.conditions.Conditions
import com.willfp.libreforge.conditions.ConfiguredCondition
@@ -365,7 +367,33 @@ private val activeJobKey: PersistentDataKey<String> = PersistentDataKey(
var OfflinePlayer.activeJob: Job?
get() = Jobs.getByID(this.profile.read(activeJobKey))
set(value) = this.profile.write(activeJobKey, value?.id ?: "")
set(job) {
val oldJob = this.activeJob
if (oldJob != job) {
// Have to check for oldJob too to have null safety
if (job == null && oldJob != null) {
val event = PlayerJobLeaveEvent(player, oldJob)
Bukkit.getPluginManager().callEvent(event)
if (event.isCancelled) {
return
}
}
// Not using else because null safety as well
if (job != null) {
val event = PlayerJobJoinEvent(player, job, oldJob)
Bukkit.getPluginManager().callEvent(event)
if (event.isCancelled) {
return
}
}
}
this.profile.write(activeJobKey, job?.id ?: "")
}
val OfflinePlayer.activeJobLevel: JobLevel?
get() {