mirror of
https://github.com/Auxilor/EcoSkills.git
synced 2026-01-04 15:41:36 +00:00
Code refactor
This commit is contained in:
@@ -1,11 +1,34 @@
|
||||
package com.willfp.ecoskills
|
||||
|
||||
import com.willfp.ecoskills.api.PlayerSkillExpGainEvent
|
||||
import com.willfp.ecoskills.api.PlayerSkillLevelUpEvent
|
||||
import com.willfp.ecoskills.effects.Effect
|
||||
import com.willfp.ecoskills.skills.Skill
|
||||
import com.willfp.ecoskills.stats.Stat
|
||||
import org.bukkit.Bukkit
|
||||
import org.bukkit.entity.Player
|
||||
import org.bukkit.persistence.PersistentDataType
|
||||
|
||||
fun Player.giveSkillExperience(skill: Skill, experience: Double) {
|
||||
val gainEvent = PlayerSkillExpGainEvent(this, skill, experience)
|
||||
Bukkit.getPluginManager().callEvent(gainEvent)
|
||||
|
||||
if (gainEvent.isCancelled) {
|
||||
return
|
||||
}
|
||||
|
||||
val level = this.getSkillLevel(skill)
|
||||
|
||||
this.setSkillProgress(skill, this.getSkillProgress(skill) + experience)
|
||||
|
||||
if (this.getSkillProgress(skill) >= skill.getExpForLevel(level + 1)) {
|
||||
this.setSkillProgress(skill, 0.0)
|
||||
this.setSkillLevel(skill, level + 1)
|
||||
val levelUpEvent = PlayerSkillLevelUpEvent(this, skill, level + 1)
|
||||
Bukkit.getPluginManager().callEvent(levelUpEvent)
|
||||
}
|
||||
}
|
||||
|
||||
fun Player.getSkillLevel(skill: Skill): Int {
|
||||
return this.persistentDataContainer.getOrDefault(skill.key, PersistentDataType.INTEGER, 0)
|
||||
}
|
||||
|
||||
@@ -14,23 +14,6 @@ import org.bukkit.event.Listener
|
||||
class SkillLevellingListener(
|
||||
private val plugin: EcoPlugin
|
||||
) : Listener {
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
fun onProgress(event: PlayerSkillExpGainEvent) {
|
||||
val player = event.player
|
||||
val skill = event.skill
|
||||
val amount = event.amount
|
||||
val level = player.getSkillLevel(skill)
|
||||
|
||||
player.setSkillProgress(skill, player.getSkillProgress(skill) + amount)
|
||||
|
||||
if (player.getSkillProgress(skill) >= skill.getExpForLevel(level + 1)) {
|
||||
player.setSkillProgress(skill, 0.0)
|
||||
player.setSkillLevel(skill, level + 1)
|
||||
val levelUpEvent = PlayerSkillLevelUpEvent(player, skill, level + 1)
|
||||
Bukkit.getPluginManager().callEvent(levelUpEvent)
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
fun onLevelUp(event: PlayerSkillLevelUpEvent) {
|
||||
val player = event.player
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.willfp.ecoskills.skills.skills
|
||||
import com.willfp.eco.core.events.EntityDeathByEntityEvent
|
||||
import com.willfp.ecoskills.api.PlayerSkillExpGainEvent
|
||||
import com.willfp.ecoskills.getStatLevel
|
||||
import com.willfp.ecoskills.giveSkillExperience
|
||||
import com.willfp.ecoskills.skills.Skill
|
||||
import org.bukkit.Bukkit
|
||||
import org.bukkit.Material
|
||||
@@ -54,7 +55,6 @@ class SkillAlchemy : Skill(
|
||||
val type = event.contents.ingredient?.type ?: return
|
||||
|
||||
val toGive = rewards[type] ?: return
|
||||
val gainEvent = PlayerSkillExpGainEvent(player, this, toGive)
|
||||
Bukkit.getPluginManager().callEvent(gainEvent)
|
||||
player.giveSkillExperience(this, toGive)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.willfp.ecoskills.skills.skills
|
||||
|
||||
import com.willfp.ecoskills.api.PlayerSkillExpGainEvent
|
||||
import com.willfp.ecoskills.giveSkillExperience
|
||||
import com.willfp.ecoskills.skills.Skill
|
||||
import org.bukkit.Bukkit
|
||||
import org.bukkit.attribute.Attribute
|
||||
@@ -21,7 +22,6 @@ class SkillArmory : Skill(
|
||||
}
|
||||
|
||||
val xp = event.damage * this.config.getDouble("xp-per-hp")
|
||||
val gainEvent = PlayerSkillExpGainEvent(player, this, xp)
|
||||
Bukkit.getPluginManager().callEvent(gainEvent)
|
||||
player.giveSkillExperience(this, xp)
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.willfp.ecoskills.skills.skills
|
||||
import com.willfp.eco.core.events.EntityDeathByEntityEvent
|
||||
import com.willfp.ecoskills.api.PlayerSkillExpGainEvent
|
||||
import com.willfp.ecoskills.getStatLevel
|
||||
import com.willfp.ecoskills.giveSkillExperience
|
||||
import com.willfp.ecoskills.skills.Skill
|
||||
import org.bukkit.Bukkit
|
||||
import org.bukkit.Material
|
||||
@@ -36,7 +37,6 @@ class SkillCombat : Skill(
|
||||
}
|
||||
|
||||
val xp = event.victim.getAttribute(Attribute.GENERIC_MAX_HEALTH)!!.value * this.config.getDouble("xp-per-heart")
|
||||
val gainEvent = PlayerSkillExpGainEvent(player, this, xp)
|
||||
Bukkit.getPluginManager().callEvent(gainEvent)
|
||||
player.giveSkillExperience(this, xp)
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.willfp.ecoskills.skills.skills
|
||||
import com.willfp.eco.core.events.EntityDeathByEntityEvent
|
||||
import com.willfp.ecoskills.api.PlayerSkillExpGainEvent
|
||||
import com.willfp.ecoskills.getStatLevel
|
||||
import com.willfp.ecoskills.giveSkillExperience
|
||||
import com.willfp.ecoskills.skills.Skill
|
||||
import org.bukkit.Bukkit
|
||||
import org.bukkit.Material
|
||||
@@ -26,7 +27,6 @@ class SkillEnchanting : Skill(
|
||||
val cost = event.expLevelCost
|
||||
|
||||
val xp = cost * this.config.getDouble("xp-per-level")
|
||||
val gainEvent = PlayerSkillExpGainEvent(player, this, xp)
|
||||
Bukkit.getPluginManager().callEvent(gainEvent)
|
||||
player.giveSkillExperience(this, xp)
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import com.willfp.eco.core.events.EntityDeathByEntityEvent
|
||||
import com.willfp.eco.util.NumberUtils
|
||||
import com.willfp.ecoskills.api.PlayerSkillExpGainEvent
|
||||
import com.willfp.ecoskills.getStatLevel
|
||||
import com.willfp.ecoskills.giveSkillExperience
|
||||
import com.willfp.ecoskills.skills.Skill
|
||||
import org.bukkit.Bukkit
|
||||
import org.bukkit.Material
|
||||
@@ -27,8 +28,7 @@ class SkillExploration : Skill(
|
||||
val player = event.player
|
||||
|
||||
if (NumberUtils.randFloat(0.0, 100.0) < this.config.getDouble("xp-on-move-chance")) {
|
||||
val gainEvent = PlayerSkillExpGainEvent(player, this, 1.0)
|
||||
Bukkit.getPluginManager().callEvent(gainEvent)
|
||||
player.giveSkillExperience(this, 1.0)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.willfp.ecoskills.skills.skills
|
||||
|
||||
import com.willfp.ecoskills.api.PlayerSkillExpGainEvent
|
||||
import com.willfp.ecoskills.giveSkillExperience
|
||||
import com.willfp.ecoskills.skills.Skill
|
||||
import org.bukkit.Bukkit
|
||||
import org.bukkit.Material
|
||||
@@ -45,7 +46,6 @@ class SkillFarming : Skill(
|
||||
|
||||
val toGive = rewards[type] ?: return
|
||||
|
||||
val gainEvent = PlayerSkillExpGainEvent(player, this, toGive)
|
||||
Bukkit.getPluginManager().callEvent(gainEvent)
|
||||
player.giveSkillExperience(this, toGive)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.willfp.ecoskills.skills.skills
|
||||
|
||||
import com.willfp.ecoskills.api.PlayerSkillExpGainEvent
|
||||
import com.willfp.ecoskills.giveSkillExperience
|
||||
import com.willfp.ecoskills.skills.Skill
|
||||
import org.bukkit.Bukkit
|
||||
import org.bukkit.attribute.Attribute
|
||||
@@ -19,12 +20,7 @@ class SkillFishing : Skill(
|
||||
val player = event.player
|
||||
val expToDrop = event.expToDrop
|
||||
|
||||
if (player !is Player) {
|
||||
return
|
||||
}
|
||||
|
||||
val xp = expToDrop * this.config.getDouble("xp-per-experience-dropped")
|
||||
val gainEvent = PlayerSkillExpGainEvent(player, this, xp)
|
||||
Bukkit.getPluginManager().callEvent(gainEvent)
|
||||
player.giveSkillExperience(this, xp)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.willfp.ecoskills.skills.skills
|
||||
|
||||
import com.willfp.ecoskills.api.PlayerSkillExpGainEvent
|
||||
import com.willfp.ecoskills.giveSkillExperience
|
||||
import com.willfp.ecoskills.skills.Skill
|
||||
import org.bukkit.Bukkit
|
||||
import org.bukkit.Material
|
||||
@@ -39,7 +40,6 @@ class SkillMining : Skill(
|
||||
return
|
||||
}
|
||||
|
||||
val gainEvent = PlayerSkillExpGainEvent(player, this, toGive)
|
||||
Bukkit.getPluginManager().callEvent(gainEvent)
|
||||
player.giveSkillExperience(this, toGive)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.willfp.ecoskills.skills.skills
|
||||
|
||||
import com.willfp.ecoskills.api.PlayerSkillExpGainEvent
|
||||
import com.willfp.ecoskills.giveSkillExperience
|
||||
import com.willfp.ecoskills.skills.Skill
|
||||
import org.bukkit.Bukkit
|
||||
import org.bukkit.Material
|
||||
@@ -35,7 +36,6 @@ class SkillWoodcutting : Skill(
|
||||
|
||||
val toGive = rewards[type] ?: return
|
||||
|
||||
val gainEvent = PlayerSkillExpGainEvent(player, this, toGive)
|
||||
Bukkit.getPluginManager().callEvent(gainEvent)
|
||||
player.giveSkillExperience(this, toGive)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user