From ab4c0fb3fb9193bc5657220e2afee164b15b445b Mon Sep 17 00:00:00 2001 From: Auxilor Date: Thu, 26 Aug 2021 15:45:28 +0100 Subject: [PATCH] Lots more codestyle --- .../com/willfp/ecoskills/EcoSkillsPlugin.java | 2 +- .../EcoSkillsEventModifierHandler.kt | 12 ++-- .../com/willfp/ecoskills/EcoSkillsPlayer.kt | 62 +++++++++---------- .../willfp/ecoskills/commands/CommandTop.kt | 2 +- .../com/willfp/ecoskills/config/DataYml.kt | 3 +- .../com/willfp/ecoskills/config/EffectsYml.kt | 3 +- .../willfp/ecoskills/config/SkillConfig.kt | 3 +- .../com/willfp/ecoskills/data/DataListener.kt | 8 +-- .../ecoskills/data/LeaderboardHandler.kt | 5 +- .../com/willfp/ecoskills/data/SaveHandler.kt | 7 +-- .../com/willfp/ecoskills/effects/Effect.kt | 4 -- .../ecoskills/effects/effects/EffectDazzle.kt | 28 ++------- .../effects/effects/EffectEndangering.kt | 23 +------ .../effects/effects/EffectOvercompensation.kt | 2 - .../effects/effects/EffectPotionmaster.kt | 19 +----- .../effects/effects/EffectReimbursement.kt | 3 - .../effects/effects/EffectSerratedStrikes.kt | 24 +------ .../effects/effects/EffectStrongImpact.kt | 20 +----- .../com/willfp/ecoskills/skills/Skill.kt | 16 ++++- .../skills/SkillLevellingListener.kt | 20 +++--- .../ecoskills/skills/SkillObjectReward.kt | 5 +- .../ecoskills/skills/skills/SkillAlchemy.kt | 15 +---- .../ecoskills/skills/skills/SkillCombat.kt | 17 +---- .../stats/modifier/StatModifierListener.kt | 5 +- .../ecoskills/stats/stats/StatCritChance.kt | 17 +---- .../ecoskills/stats/stats/StatCritDamage.kt | 3 +- .../ecoskills/stats/stats/StatFerocity.kt | 28 ++------- .../ecoskills/stats/stats/StatStrength.kt | 17 +---- 28 files changed, 98 insertions(+), 275 deletions(-) diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoskills/EcoSkillsPlugin.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoskills/EcoSkillsPlugin.java index 6cc7caf..12b4fca 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoskills/EcoSkillsPlugin.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoskills/EcoSkillsPlugin.java @@ -117,7 +117,7 @@ public class EcoSkillsPlugin extends EcoPlugin { new StatModifierListener(this), new DataListener(this), new PlayerBlockListener(this), - EcoSkillsEventModifierHandler.INSTANCE + new EcoSkillsEventModifierHandler(this) ); } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/EcoSkillsEventModifierHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/EcoSkillsEventModifierHandler.kt index cbaf38a..89ee365 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/EcoSkillsEventModifierHandler.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/EcoSkillsEventModifierHandler.kt @@ -5,12 +5,14 @@ import org.bukkit.event.EventPriority import org.bukkit.event.Listener import org.bukkit.event.entity.EntityDamageByEntityEvent -object EcoSkillsEventModifierHandler: Listener { - val critMap: MutableMap = HashMap() +private val critMap = mutableMapOf() +class EcoSkillsEventModifierHandler( + private val plugin: EcoSkillsPlugin +) : Listener { @EventHandler(priority = EventPriority.MONITOR) fun preventLeak(event: EntityDamageByEntityEvent) { - EcoSkillsPlugin.getInstance().scheduler.runLater({ + plugin.scheduler.runLater({ critMap.remove(event) }, 1) } @@ -18,8 +20,8 @@ object EcoSkillsEventModifierHandler: Listener { var EntityDamageByEntityEvent.isCrit: Boolean get() { - return EcoSkillsEventModifierHandler.critMap[this] ?: false + return critMap[this] ?: false } set(isCrit) { - EcoSkillsEventModifierHandler.critMap[this] = isCrit + critMap[this] = isCrit } \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/EcoSkillsPlayer.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/EcoSkillsPlayer.kt index 63b8168..1122f31 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/EcoSkillsPlayer.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/EcoSkillsPlayer.kt @@ -1,7 +1,5 @@ package com.willfp.ecoskills -import com.willfp.eco.core.integrations.placeholder.PlaceholderEntry -import com.willfp.eco.util.NumberUtils import com.willfp.ecoskills.api.PlayerSkillExpGainEvent import com.willfp.ecoskills.api.PlayerSkillLevelUpEvent import com.willfp.ecoskills.effects.Effect @@ -12,35 +10,20 @@ import com.willfp.ecoskills.stats.Stat import com.willfp.ecoskills.stats.Stats import org.bukkit.Bukkit import org.bukkit.OfflinePlayer +import org.bukkit.entity.Entity import org.bukkit.entity.Player +import org.bukkit.entity.Projectile import org.bukkit.persistence.PersistentDataType import java.util.* - -object PlayerHelper { - init { - PlaceholderEntry( - "average_skill_level", - { player -> NumberUtils.format(player.getAverageSkillLevel()) }, - true - ).register() - PlaceholderEntry( - "total_skill_level", - { player -> player.getTotalSkillLevel().toString() }, - true - ).register() - } - - val expMultiplierCache = HashMap() - - val plugin: EcoSkillsPlugin = EcoSkillsPlugin.getInstance() -} +val expMultiplierCache = HashMap() +val plugin: EcoSkillsPlugin = EcoSkillsPlugin.getInstance() fun Player.getSkillExperienceMultiplier(): Double { - if (PlayerHelper.expMultiplierCache.containsKey(this.uniqueId)) { - return PlayerHelper.expMultiplierCache[this.uniqueId]!! + if (expMultiplierCache.containsKey(this.uniqueId)) { + return expMultiplierCache[this.uniqueId]!! } - PlayerHelper.expMultiplierCache[this.uniqueId] = cacheSkillExperienceMultiplier() + expMultiplierCache[this.uniqueId] = cacheSkillExperienceMultiplier() return this.getSkillExperienceMultiplier() } @@ -103,7 +86,7 @@ fun Player.giveSkillExperience(skill: Skill, experience: Double, isOvershoot: Bo this.setSkillProgress(skill, this.getSkillProgress(skill) + exp) if (this.getSkillProgress(skill) >= skill.getExpForLevel(level + 1) && level + 1 <= skill.maxLevel) { - val overshoot = this.getSkillProgress(skill) - skill.getExpForLevel(level + 1); + val overshoot = this.getSkillProgress(skill) - skill.getExpForLevel(level + 1) this.setSkillProgress(skill, 0.0) this.setSkillLevel(skill, level + 1) val levelUpEvent = PlayerSkillLevelUpEvent(this, skill, level + 1) @@ -113,11 +96,11 @@ fun Player.giveSkillExperience(skill: Skill, experience: Double, isOvershoot: Bo } fun OfflinePlayer.getSkillLevel(skill: Skill): Int { - return PlayerHelper.plugin.dataYml.getInt("player.${this.uniqueId}.${skill.id}", 0) + return plugin.dataYml.getInt("player.${this.uniqueId}.${skill.id}", 0) } fun Player.setSkillLevel(skill: Skill, level: Int) { - PlayerHelper.plugin.dataYml.set("player.${this.uniqueId}.${skill.id}", level) + plugin.dataYml.set("player.${this.uniqueId}.${skill.id}", level) } fun Player.getSkillProgressToNextLevel(skill: Skill): Double { @@ -137,30 +120,41 @@ fun Player.setSkillProgress(skill: Skill, level: Double) { } fun OfflinePlayer.getEffectLevel(effect: Effect): Int { - return PlayerHelper.plugin.dataYml.getInt("player.${this.uniqueId}.${effect.id}", 0) + return plugin.dataYml.getInt("player.${this.uniqueId}.${effect.id}", 0) } fun Player.setEffectLevel(effect: Effect, level: Int) { - PlayerHelper.plugin.dataYml.set("player.${this.uniqueId}.${effect.id}", level) + plugin.dataYml.set("player.${this.uniqueId}.${effect.id}", level) } fun OfflinePlayer.getStatLevel(stat: Stat): Int { - return PlayerHelper.plugin.dataYml.getInt("player.${this.uniqueId}.${stat.id}", 0) + return plugin.dataYml.getInt("player.${this.uniqueId}.${stat.id}", 0) } fun Player.setStatLevel(stat: Stat, level: Int) { - PlayerHelper.plugin.dataYml.set("player.${this.uniqueId}.${stat.id}", level) + plugin.dataYml.set("player.${this.uniqueId}.${stat.id}", level) stat.updateStatLevel(this) } fun Player.convertPersistentToYml() { for (effect in Effects.values()) { - PlayerHelper.plugin.dataYml.set("player.${this.uniqueId}.${effect.id}", this.getEffectLevel(effect)) + plugin.dataYml.set("player.${this.uniqueId}.${effect.id}", this.getEffectLevel(effect)) } for (stat in Stats.values()) { - PlayerHelper.plugin.dataYml.set("player.${this.uniqueId}.${stat.id}", this.getStatLevel(stat)) + plugin.dataYml.set("player.${this.uniqueId}.${stat.id}", this.getStatLevel(stat)) } for (skill in Skills.values()) { - PlayerHelper.plugin.dataYml.set("player.${this.uniqueId}.${skill.id}", this.getSkillLevel(skill)) + plugin.dataYml.set("player.${this.uniqueId}.${skill.id}", this.getSkillLevel(skill)) + } +} + +fun Entity.tryAsPlayer(): Player? { + return when(this) { + is Projectile -> { + val shooter = this.shooter + if (shooter is Player) shooter else null + } + is Player -> this + else -> null } } \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandTop.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandTop.kt index 3319b7c..33e2af9 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandTop.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/commands/CommandTop.kt @@ -50,7 +50,7 @@ class CommandTop(plugin: EcoPlugin) : val messages = plugin.langYml.getStrings("top", false) val lines = mutableListOf() - val useDisplayName = plugin.configYml.getBool("commands.top.use-display-name"); + val useDisplayName = plugin.configYml.getBool("commands.top.use-display-name") var rank = start + 1 diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/config/DataYml.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/config/DataYml.kt index df09e36..7bd6c99 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/config/DataYml.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/config/DataYml.kt @@ -9,5 +9,4 @@ class DataYml( "data", false, plugin -) { -} \ No newline at end of file +) \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/config/EffectsYml.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/config/EffectsYml.kt index eef2128..2ae9c37 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/config/EffectsYml.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/config/EffectsYml.kt @@ -9,5 +9,4 @@ class EffectsYml( "effects", true, plugin -) { -} \ No newline at end of file +) \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/config/SkillConfig.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/config/SkillConfig.kt index 86d518b..bc6f6ed 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/config/SkillConfig.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/config/SkillConfig.kt @@ -15,5 +15,4 @@ class SkillConfig( "skills/", "rewards.chat-messages", "rewards.progression-lore" -) { -} \ No newline at end of file +) \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/DataListener.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/DataListener.kt index 724d00a..8e2297b 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/DataListener.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/DataListener.kt @@ -1,21 +1,15 @@ package com.willfp.ecoskills.data -import com.willfp.ecoskills.EcoSkillsPlugin import com.willfp.ecoskills.convertPersistentToYml import com.willfp.ecoskills.effects.Effect -import com.willfp.ecoskills.effects.Effects import com.willfp.ecoskills.getSkillLevel import com.willfp.ecoskills.setEffectLevel import com.willfp.ecoskills.skills.Skills -import org.bukkit.Bukkit import org.bukkit.event.EventHandler import org.bukkit.event.Listener import org.bukkit.event.player.PlayerJoinEvent -import java.net.http.WebSocket -class DataListener( - private val plugin: EcoSkillsPlugin -): Listener { +class DataListener: Listener { @EventHandler fun onJoin(event: PlayerJoinEvent) { event.player.convertPersistentToYml() diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/LeaderboardHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/LeaderboardHandler.kt index b3dacf7..2d09ef2 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/LeaderboardHandler.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/LeaderboardHandler.kt @@ -1,13 +1,10 @@ package com.willfp.ecoskills.data -import com.willfp.ecoskills.EcoSkillsPlugin import com.willfp.ecoskills.getTotalSkillLevel import org.bukkit.Bukkit import org.bukkit.OfflinePlayer -class LeaderboardHandler ( - private val plugin: EcoSkillsPlugin -) { +class LeaderboardHandler { companion object { val sortedLeaderboard = mutableListOf() } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/SaveHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/SaveHandler.kt index d328645..dd0adfb 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/SaveHandler.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/SaveHandler.kt @@ -1,11 +1,8 @@ package com.willfp.ecoskills.data -import com.willfp.eco.core.EcoPlugin import com.willfp.ecoskills.EcoSkillsPlugin -import com.willfp.ecoskills.PlayerHelper -import com.willfp.ecoskills.getTotalSkillLevel +import com.willfp.ecoskills.expMultiplierCache import org.bukkit.Bukkit -import org.bukkit.OfflinePlayer class SaveHandler { companion object { @@ -17,7 +14,7 @@ class SaveHandler { plugin.logger.info("Auto-Saving player data!") } plugin.dataYml.save() - PlayerHelper.expMultiplierCache.clear() + expMultiplierCache.clear() if (plugin.configYml.getBool("log-autosaves")) { plugin.logger.info("Saved data!") } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/Effect.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/Effect.kt index 87bcc3d..6891a5e 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/Effect.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/Effect.kt @@ -1,6 +1,5 @@ package com.willfp.ecoskills.effects -import com.willfp.eco.core.EcoPlugin import com.willfp.eco.core.config.interfaces.Config import com.willfp.eco.core.integrations.placeholder.PlaceholderEntry import com.willfp.eco.util.NumberUtils @@ -10,7 +9,6 @@ import com.willfp.ecoskills.SkillObject import com.willfp.ecoskills.getEffectLevel import org.bukkit.NamespacedKey import org.bukkit.event.Listener -import java.util.* abstract class Effect( id: String @@ -18,13 +16,11 @@ abstract class Effect( protected val plugin: EcoSkillsPlugin = EcoSkillsPlugin.getInstance() val key: NamespacedKey - val uuid: UUID val config: Config init { update() key = plugin.namespacedKeyFactory.create(id) - uuid = UUID.nameUUIDFromBytes(id.toByteArray()) config = plugin.effectsYml.getSubsection(id) Effects.registerNewEffect(this) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectDazzle.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectDazzle.kt index 3e0b22e..bd2b7fd 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectDazzle.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectDazzle.kt @@ -1,19 +1,16 @@ package com.willfp.ecoskills.effects.effects -import com.willfp.eco.core.scheduling.RunnableTask import com.willfp.eco.util.NumberUtils import com.willfp.ecoskills.effects.Effect import com.willfp.ecoskills.getEffectLevel +import com.willfp.ecoskills.tryAsPlayer import org.bukkit.entity.LivingEntity -import org.bukkit.entity.Player -import org.bukkit.entity.Projectile import org.bukkit.event.EventHandler import org.bukkit.event.EventPriority import org.bukkit.event.entity.EntityDamageByEntityEvent import org.bukkit.potion.PotionEffect import org.bukkit.potion.PotionEffectType import org.bukkit.util.Vector -import java.util.concurrent.atomic.AtomicInteger class EffectDazzle : Effect( @@ -26,25 +23,8 @@ class EffectDazzle : Effect( @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) fun handle(event: EntityDamageByEntityEvent) { - var player = event.damager - - if (player is Projectile) { - if (player.shooter !is Player) { - return - } else { - player = player.shooter as Player - } - } - - if (player !is Player) { - return - } - - val victim = event.entity - - if (victim !is LivingEntity) { - return - } + val player = event.damager.tryAsPlayer() ?: return + val victim = if (event.entity is LivingEntity) event.entity as LivingEntity else return val level = player.getEffectLevel(this) @@ -54,7 +34,7 @@ class EffectDazzle : Effect( val duration = (config.getInt("ticks-per-level") * level) + config.getInt("base-ticks") - victim.setVelocity(Vector(0, 0, 0)) + victim.velocity = Vector(0, 0, 0) victim.addPotionEffect(PotionEffect(PotionEffectType.CONFUSION, duration, level)) } } \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectEndangering.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectEndangering.kt index bff597a..6b171e6 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectEndangering.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectEndangering.kt @@ -3,9 +3,8 @@ package com.willfp.ecoskills.effects.effects import com.willfp.eco.util.NumberUtils import com.willfp.ecoskills.effects.Effect import com.willfp.ecoskills.getEffectLevel +import com.willfp.ecoskills.tryAsPlayer import org.bukkit.entity.LivingEntity -import org.bukkit.entity.Player -import org.bukkit.entity.Projectile import org.bukkit.event.EventHandler import org.bukkit.event.EventPriority import org.bukkit.event.entity.EntityDamageByEntityEvent @@ -20,24 +19,8 @@ class EffectEndangering : Effect( @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) fun handle(event: EntityDamageByEntityEvent) { - var player = event.damager - val victim = event.entity - - if (victim !is LivingEntity) { - return - } - - if (player is Projectile) { - if (player.shooter !is Player) { - return - } else { - player = player.shooter as Player - } - } - - if (player !is Player) { - return - } + val player = event.damager.tryAsPlayer() ?: return + val victim = if (event.entity is LivingEntity) event.entity as LivingEntity else return val level = player.getEffectLevel(this) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectOvercompensation.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectOvercompensation.kt index 98761d3..135f23d 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectOvercompensation.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectOvercompensation.kt @@ -3,12 +3,10 @@ package com.willfp.ecoskills.effects.effects import com.willfp.eco.util.NumberUtils import com.willfp.ecoskills.effects.Effect import com.willfp.ecoskills.getEffectLevel -import org.bukkit.Bukkit import org.bukkit.Material import org.bukkit.event.EventHandler import org.bukkit.event.EventPriority import org.bukkit.event.enchantment.EnchantItemEvent -import org.bukkit.inventory.EnchantingInventory import org.bukkit.inventory.ItemStack diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectPotionmaster.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectPotionmaster.kt index d3b7156..a1856dc 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectPotionmaster.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectPotionmaster.kt @@ -27,18 +27,7 @@ class EffectPotionmaster : Effect( @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) fun handle(event: BrewEvent) { - var player: Player? = null - - for (viewer in event.contents.viewers) { - if (viewer is Player) { - player = viewer - break - } - } - - if (player == null) { - return - } + val player = event.contents.viewers.filterIsInstance().firstOrNull() ?: return if (player.getEffectLevel(this) == 0) { return @@ -48,11 +37,7 @@ class EffectPotionmaster : Effect( this.plugin.scheduler.runLater({ for (i in 0..2) { - val item = event.contents.getItem(i) - - if (item == null) { - continue - } + val item = event.contents.getItem(i) ?: continue val meta = item.itemMeta diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectReimbursement.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectReimbursement.kt index efe46bb..1259fe3 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectReimbursement.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectReimbursement.kt @@ -3,12 +3,9 @@ package com.willfp.ecoskills.effects.effects import com.willfp.eco.util.NumberUtils import com.willfp.ecoskills.effects.Effect import com.willfp.ecoskills.getEffectLevel -import com.willfp.ecoskills.giveSkillExperience -import org.bukkit.entity.Player import org.bukkit.event.EventHandler import org.bukkit.event.EventPriority import org.bukkit.event.enchantment.EnchantItemEvent -import org.bukkit.event.entity.EntityDamageEvent class EffectReimbursement : Effect( "reimbursement" diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectSerratedStrikes.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectSerratedStrikes.kt index bf2f660..46d11be 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectSerratedStrikes.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectSerratedStrikes.kt @@ -4,13 +4,11 @@ import com.willfp.eco.core.scheduling.RunnableTask import com.willfp.eco.util.NumberUtils import com.willfp.ecoskills.effects.Effect import com.willfp.ecoskills.getEffectLevel +import com.willfp.ecoskills.tryAsPlayer import org.bukkit.entity.LivingEntity -import org.bukkit.entity.Player -import org.bukkit.entity.Projectile import org.bukkit.event.EventHandler import org.bukkit.event.EventPriority import org.bukkit.event.entity.EntityDamageByEntityEvent -import java.util.concurrent.atomic.AtomicInteger class EffectSerratedStrikes : Effect( @@ -22,24 +20,8 @@ class EffectSerratedStrikes : Effect( @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) fun handle(event: EntityDamageByEntityEvent) { - var player = event.damager - - if (player is Projectile) { - if (player.shooter !is Player) { - return - } else { - player = player.shooter as Player - } - } - - if (player !is Player) { - return - } - val victim = event.entity - - if (victim !is LivingEntity) { - return - } + val player = event.damager.tryAsPlayer() ?: return + val victim = if (event.entity is LivingEntity) event.entity as LivingEntity else return val level = player.getEffectLevel(this) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectStrongImpact.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectStrongImpact.kt index 386bff8..c30640a 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectStrongImpact.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectStrongImpact.kt @@ -1,16 +1,12 @@ package com.willfp.ecoskills.effects.effects -import com.willfp.eco.core.scheduling.RunnableTask import com.willfp.eco.util.NumberUtils import com.willfp.ecoskills.effects.Effect import com.willfp.ecoskills.getEffectLevel -import org.bukkit.entity.LivingEntity -import org.bukkit.entity.Player -import org.bukkit.entity.Projectile +import com.willfp.ecoskills.tryAsPlayer import org.bukkit.event.EventHandler import org.bukkit.event.EventPriority import org.bukkit.event.entity.EntityDamageByEntityEvent -import java.util.concurrent.atomic.AtomicInteger class EffectStrongImpact : Effect( @@ -22,19 +18,7 @@ class EffectStrongImpact : Effect( @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) fun handle(event: EntityDamageByEntityEvent) { - var player = event.damager - - if (player is Projectile) { - if (player.shooter !is Player) { - return - } else { - player = player.shooter as Player - } - } - - if (player !is Player) { - return - } + val player = event.damager.tryAsPlayer() ?: return val level = player.getEffectLevel(this) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/Skill.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/Skill.kt index 277da71..c699aff 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/Skill.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/Skill.kt @@ -5,12 +5,10 @@ import com.willfp.eco.core.config.interfaces.Config import com.willfp.eco.core.integrations.placeholder.PlaceholderEntry import com.willfp.eco.util.NumberUtils import com.willfp.eco.util.StringUtils -import com.willfp.ecoskills.EcoSkillsPlugin -import com.willfp.ecoskills.SkillObject +import com.willfp.ecoskills.* import com.willfp.ecoskills.config.SkillConfig import com.willfp.ecoskills.effects.Effect import com.willfp.ecoskills.effects.Effects -import com.willfp.ecoskills.getSkillLevel import com.willfp.ecoskills.stats.Stats import org.bukkit.NamespacedKey import org.bukkit.entity.Player @@ -68,6 +66,18 @@ abstract class Skill( true ).register() + PlaceholderEntry( + "average_skill_level", + { player -> NumberUtils.format(player.getAverageSkillLevel()) }, + true + ).register() + + PlaceholderEntry( + "total_skill_level", + { player -> player.getTotalSkillLevel().toString() }, + true + ).register() + postUpdate() guiLoreCache.clear() diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/SkillLevellingListener.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/SkillLevellingListener.kt index 59bb93e..d0e212d 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/SkillLevellingListener.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/SkillLevellingListener.kt @@ -1,6 +1,5 @@ package com.willfp.ecoskills.skills -import com.willfp.eco.core.EcoPlugin import com.willfp.ecoskills.api.PlayerSkillLevelUpEvent import com.willfp.ecoskills.effects.Effect import com.willfp.ecoskills.getEffectLevel @@ -12,9 +11,7 @@ import org.bukkit.event.EventHandler import org.bukkit.event.EventPriority import org.bukkit.event.Listener -class SkillLevellingListener( - private val plugin: EcoPlugin -) : Listener { +class SkillLevellingListener : Listener { @EventHandler(priority = EventPriority.HIGHEST) fun onLevelUp(event: PlayerSkillLevelUpEvent) { val player = event.player @@ -22,11 +19,16 @@ class SkillLevellingListener( val to = event.level for (reward in skill.getLevelUpRewards()) { - if (reward.obj is Effect) { - player.setEffectLevel(reward.obj, player.getEffectLevel(reward.obj) + skill.getLevelUpReward(reward.obj, to)) - } - if (reward.obj is Stat) { - player.setStatLevel(reward.obj, player.getStatLevel(reward.obj) + skill.getLevelUpReward(reward.obj, to)) + val obj = reward.obj + val toGive = skill.getLevelUpReward(obj, to) + + when (obj) { + is Effect -> { + player.setEffectLevel(obj, player.getEffectLevel(obj) + toGive) + } + is Stat -> { + player.setStatLevel(obj, player.getStatLevel(obj) + toGive) + } } } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/SkillObjectReward.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/SkillObjectReward.kt index 8dc861d..8ce8d34 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/SkillObjectReward.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/SkillObjectReward.kt @@ -2,8 +2,7 @@ package com.willfp.ecoskills.skills import com.willfp.ecoskills.SkillObject -data class SkillObjectReward( +data class SkillObjectReward ( val obj: SkillObject, val options: SkillObjectOptions -) { -} \ No newline at end of file +) \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/skills/SkillAlchemy.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/skills/SkillAlchemy.kt index 2179762..c20b6e9 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/skills/SkillAlchemy.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/skills/SkillAlchemy.kt @@ -31,24 +31,13 @@ class SkillAlchemy : Skill( @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) fun handleLevelling(event: BrewEvent) { - var player: Player? = null - - for (viewer in event.contents.viewers) { - if (viewer is Player) { - player = viewer - break - } - } - - if (player == null) { - return - } + val player = event.contents.viewers.filterIsInstance().firstOrNull() ?: return if (player.gameMode == GameMode.CREATIVE || player.gameMode == GameMode.SPECTATOR) { return } - var mult = 0; + var mult = 0 for (i in 0..2) { if (event.contents.getItem(i)?.itemMeta is PotionMeta) { diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/skills/SkillCombat.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/skills/SkillCombat.kt index de68cda..5d802cc 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/skills/SkillCombat.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/skills/skills/SkillCombat.kt @@ -3,10 +3,9 @@ package com.willfp.ecoskills.skills.skills import com.willfp.eco.core.events.EntityDeathByEntityEvent import com.willfp.ecoskills.giveSkillExperience import com.willfp.ecoskills.skills.Skill +import com.willfp.ecoskills.tryAsPlayer import org.bukkit.GameMode import org.bukkit.attribute.Attribute -import org.bukkit.entity.Player -import org.bukkit.entity.Projectile import org.bukkit.event.EventHandler import org.bukkit.event.EventPriority @@ -15,19 +14,7 @@ class SkillCombat : Skill( ) { @EventHandler(priority = EventPriority.HIGH) fun handleLevelling(event: EntityDeathByEntityEvent) { - var player = event.killer - - if (player is Projectile) { - if (player.shooter !is Player) { - return - } else { - player = player.shooter as Player - } - } - - if (player !is Player) { - return - } + val player = event.killer.tryAsPlayer() ?: return if (player.gameMode == GameMode.CREATIVE || player.gameMode == GameMode.SPECTATOR) { return diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/stats/modifier/StatModifierListener.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/stats/modifier/StatModifierListener.kt index 6075255..afbd60c 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/stats/modifier/StatModifierListener.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/stats/modifier/StatModifierListener.kt @@ -1,6 +1,5 @@ package com.willfp.ecoskills.stats.modifier -import com.willfp.eco.core.EcoPlugin import com.willfp.eco.core.events.ArmorChangeEvent import com.willfp.ecoskills.addStatModifier import com.willfp.ecoskills.getStatModifiers @@ -12,9 +11,7 @@ import org.bukkit.event.player.PlayerItemHeldEvent import org.bukkit.event.player.PlayerSwapHandItemsEvent import org.bukkit.inventory.EquipmentSlot -class StatModifierListener( - private val plugin: EcoPlugin -): Listener { +class StatModifierListener : Listener { @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) fun onHold(event: PlayerItemHeldEvent) { val player = event.player diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/stats/stats/StatCritChance.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/stats/stats/StatCritChance.kt index da6596b..0a409a1 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/stats/stats/StatCritChance.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/stats/stats/StatCritChance.kt @@ -5,8 +5,7 @@ import com.willfp.ecoskills.getStatLevel import com.willfp.ecoskills.isCrit import com.willfp.ecoskills.stats.Stat import com.willfp.ecoskills.stats.Stats -import org.bukkit.entity.Player -import org.bukkit.entity.Projectile +import com.willfp.ecoskills.tryAsPlayer import org.bukkit.event.EventHandler import org.bukkit.event.EventPriority import org.bukkit.event.entity.EntityDamageByEntityEvent @@ -16,19 +15,7 @@ class StatCritChance : Stat( ) { @EventHandler(priority = EventPriority.LOW) fun handle(event: EntityDamageByEntityEvent) { - var player = event.damager - - if (player is Projectile) { - if (player.shooter !is Player) { - return - } else { - player = player.shooter as Player - } - } - - if (player !is Player) { - return - } + val player = event.damager.tryAsPlayer() ?: return if (NumberUtils.randFloat(0.0, 100.0) >= (this.config.getDouble("chance-per-level") * player.getStatLevel(this))) { return diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/stats/stats/StatCritDamage.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/stats/stats/StatCritDamage.kt index 63013b3..281a4dd 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/stats/stats/StatCritDamage.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/stats/stats/StatCritDamage.kt @@ -4,5 +4,4 @@ import com.willfp.ecoskills.stats.Stat class StatCritDamage : Stat( "crit_damage" -) { -} \ No newline at end of file +) \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/stats/stats/StatFerocity.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/stats/stats/StatFerocity.kt index 4e31fcd..64b837c 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/stats/stats/StatFerocity.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/stats/stats/StatFerocity.kt @@ -2,12 +2,9 @@ package com.willfp.ecoskills.stats.stats import com.willfp.eco.util.NumberUtils import com.willfp.ecoskills.getStatLevel -import com.willfp.ecoskills.isCrit import com.willfp.ecoskills.stats.Stat -import com.willfp.ecoskills.stats.Stats +import com.willfp.ecoskills.tryAsPlayer import org.bukkit.entity.LivingEntity -import org.bukkit.entity.Player -import org.bukkit.entity.Projectile import org.bukkit.event.EventHandler import org.bukkit.event.EventPriority import org.bukkit.event.entity.EntityDamageByEntityEvent @@ -17,30 +14,13 @@ class StatFerocity : Stat( ) { @EventHandler(priority = EventPriority.LOW) fun handle(event: EntityDamageByEntityEvent) { - var player = event.damager - val victim = event.entity - val entity = event.damager + val player = event.damager.tryAsPlayer() ?: return + val victim = if (event.entity is LivingEntity) event.entity as LivingEntity else return if (victim.hasMetadata("ferocity")) { return } - if (victim !is LivingEntity) { - return - } - - if (player is Projectile) { - if (player.shooter !is Player) { - return - } else { - player = player.shooter as Player - } - } - - if (player !is Player) { - return - } - val level = player.getStatLevel(this) if (NumberUtils.randFloat(0.0, 100.0) >= this.config.getDouble("chance-per-level") * level) { @@ -50,7 +30,7 @@ class StatFerocity : Stat( this.plugin.run { victim.setMetadata("ferocity", plugin.metadataValueFactory.create(true)) victim.noDamageTicks = 0 - victim.damage(event.damage, entity) + victim.damage(event.damage, player) } } } \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/stats/stats/StatStrength.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/stats/stats/StatStrength.kt index 1807e26..eb92a76 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/stats/stats/StatStrength.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/stats/stats/StatStrength.kt @@ -2,8 +2,7 @@ package com.willfp.ecoskills.stats.stats import com.willfp.ecoskills.getStatLevel import com.willfp.ecoskills.stats.Stat -import org.bukkit.entity.Player -import org.bukkit.entity.Projectile +import com.willfp.ecoskills.tryAsPlayer import org.bukkit.event.EventHandler import org.bukkit.event.EventPriority import org.bukkit.event.entity.EntityDamageByEntityEvent @@ -13,19 +12,7 @@ class StatStrength : Stat( ) { @EventHandler(priority = EventPriority.LOW) fun handle(event: EntityDamageByEntityEvent) { - var player = event.damager - - if (player is Projectile) { - if (player.shooter !is Player) { - return - } else { - player = player.shooter as Player - } - } - - if (player !is Player) { - return - } + val player = event.damager.tryAsPlayer() ?: return var multiplier = this.config.getDouble("percent-more-damage-per-level") * player.getStatLevel(this) multiplier /= 100