9
0
mirror of https://github.com/Auxilor/EcoSkills.git synced 2026-01-02 22:02:19 +00:00

Merge remote-tracking branch 'origin/master'

This commit is contained in:
Auxilor
2021-09-12 19:00:05 +01:00
24 changed files with 254 additions and 45 deletions

View File

@@ -137,4 +137,9 @@ public class EcoSkillsPlugin extends EcoPlugin {
new IntegrationLoader("EcoEnchants", () -> this.getEventManager().registerListener(new EcoEnchantsEnchantingLeveller(this)))
);
}
@Override
public String getMinimumEcoVersion() {
return "6.6.0";
}
}

View File

@@ -43,7 +43,7 @@ public interface EcoSkillsAPI {
* @param skill The skill.
* @return The progress.
*/
double getSkillProgressToNextLevel(@NotNull Player player,
double getSkillProgressToNextLevel(@NotNull OfflinePlayer player,
@NotNull Skill skill);
/**
@@ -53,7 +53,7 @@ public interface EcoSkillsAPI {
* @param skill The skill.
* @return The experience required.
*/
int getSkillProgressRequired(@NotNull Player player,
int getSkillProgressRequired(@NotNull OfflinePlayer player,
@NotNull Skill skill);
/**
@@ -63,7 +63,7 @@ public interface EcoSkillsAPI {
* @param skill The skill.
* @return The experience.
*/
double getSkillProgress(@NotNull Player player,
double getSkillProgress(@NotNull OfflinePlayer player,
@NotNull Skill skill);
/**

View File

@@ -3,17 +3,14 @@ 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.effects.Effects
import com.willfp.ecoskills.skills.Skill
import com.willfp.ecoskills.skills.Skills
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.*
val expMultiplierCache = mutableMapOf<UUID, Double>()
@@ -99,31 +96,31 @@ fun OfflinePlayer.getSkillLevel(skill: Skill): Int {
return plugin.dataYml.getInt("player.${this.uniqueId}.${skill.id}", 0)
}
fun Player.setSkillLevel(skill: Skill, level: Int) {
fun OfflinePlayer.setSkillLevel(skill: Skill, level: Int) {
plugin.dataYml.set("player.${this.uniqueId}.${skill.id}", level)
}
fun Player.getSkillProgressToNextLevel(skill: Skill): Double {
fun OfflinePlayer.getSkillProgressToNextLevel(skill: Skill): Double {
return this.getSkillProgress(skill) / this.getSkillProgressRequired(skill)
}
fun Player.getSkillProgressRequired(skill: Skill): Int {
fun OfflinePlayer.getSkillProgressRequired(skill: Skill): Int {
return skill.getExpForLevel(this.getSkillLevel(skill) + 1)
}
fun Player.getSkillProgress(skill: Skill): Double {
return this.persistentDataContainer.getOrDefault(skill.xpKey, PersistentDataType.DOUBLE, 0.0)
fun OfflinePlayer.getSkillProgress(skill: Skill): Double {
return plugin.dataYml.getDoubleOrNull("player.${this.uniqueId}.${skill.xpKey.key}") ?: 0.0
}
fun Player.setSkillProgress(skill: Skill, level: Double) {
this.persistentDataContainer.set(skill.xpKey, PersistentDataType.DOUBLE, level)
fun OfflinePlayer.setSkillProgress(skill: Skill, level: Double) {
plugin.dataYml.set("player.${this.uniqueId}.${skill.xpKey.key}", level)
}
fun OfflinePlayer.getEffectLevel(effect: Effect): Int {
return plugin.dataYml.getInt("player.${this.uniqueId}.${effect.id}", 0)
}
fun Player.setEffectLevel(effect: Effect, level: Int) {
fun OfflinePlayer.setEffectLevel(effect: Effect, level: Int) {
plugin.dataYml.set("player.${this.uniqueId}.${effect.id}", level)
}
@@ -136,18 +133,6 @@ fun Player.setStatLevel(stat: Stat, level: Int) {
stat.updateStatLevel(this)
}
fun Player.convertPersistentToYml() {
for (effect in Effects.values()) {
plugin.dataYml.set("player.${this.uniqueId}.${effect.id}", this.getEffectLevel(effect))
}
for (stat in Stats.values()) {
plugin.dataYml.set("player.${this.uniqueId}.${stat.id}", this.getStatLevel(stat))
}
for (skill in Skills.values()) {
plugin.dataYml.set("player.${this.uniqueId}.${skill.id}", this.getSkillLevel(skill))
}
}
fun Entity.tryAsPlayer(): Player? {
return when(this) {
is Projectile -> {

View File

@@ -20,15 +20,15 @@ object EcoSkillsAPIImpl: EcoSkillsAPI {
player.giveSkillExperience(skill, amount)
}
override fun getSkillProgressToNextLevel(player: Player, skill: Skill): Double {
override fun getSkillProgressToNextLevel(player: OfflinePlayer, skill: Skill): Double {
return player.getSkillProgressToNextLevel(skill)
}
override fun getSkillProgressRequired(player: Player, skill: Skill): Int {
override fun getSkillProgressRequired(player: OfflinePlayer, skill: Skill): Int {
return player.getSkillProgressRequired(skill)
}
override fun getSkillProgress(player: Player, skill: Skill): Double {
override fun getSkillProgress(player: OfflinePlayer, skill: Skill): Double {
return player.getSkillProgress(skill)
}

View File

@@ -0,0 +1,93 @@
package com.willfp.ecoskills.commands
import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.command.CommandHandler
import com.willfp.eco.core.command.TabCompleteHandler
import com.willfp.eco.core.command.impl.Subcommand
import com.willfp.eco.util.StringUtils
import com.willfp.ecoskills.data.LeaderboardHandler
import com.willfp.ecoskills.data.savedDisplayName
import com.willfp.ecoskills.getSkillLevel
import com.willfp.ecoskills.skills.Skills
import com.willfp.ecoskills.util.TabCompleteHelper
import org.bukkit.command.CommandSender
import org.bukkit.util.StringUtil
class CommandRank(plugin: EcoPlugin) :
Subcommand(
plugin,
"rank",
"ecoskills.command.rank",
false
) {
override fun getHandler(): CommandHandler {
return CommandHandler { sender: CommandSender, args: List<String> ->
if (args.isEmpty()) {
sender.sendMessage(plugin.langYml.getMessage("requires-skill"))
return@CommandHandler
}
val skill = Skills.getByID(args[0].lowercase())
if (skill == null) {
sender.sendMessage(plugin.langYml.getMessage("invalid-skill"))
return@CommandHandler
}
val page = if (args.size < 2) 1 else args[1].toIntOrNull() ?: 1
val top = LeaderboardHandler.getPage(page, skill)
val messages = plugin.langYml.getStrings("top", false)
val lines = mutableListOf<String>()
val useDisplayName = plugin.configYml.getBool("commands.rank.use-display-name")
for ((rank, player) in top) {
var line = plugin.langYml.getString("top-line-format", false)
.replace("%rank%", rank.toString())
.replace("%level%", player.getSkillLevel(skill).toString())
var name = player.name!!
if (useDisplayName) {
name = player.savedDisplayName
}
line = line.replace("%playername%", name)
lines.add(line)
}
val linesIndex = messages.indexOf("%lines%")
if (linesIndex != -1) {
messages.removeAt(linesIndex)
messages.addAll(linesIndex, lines)
}
for (message in messages) {
sender.sendMessage(StringUtils.format(message))
}
}
}
override fun getTabCompleter(): TabCompleteHandler {
return TabCompleteHandler { _, args ->
val completions = mutableListOf<String>()
if (args.size == 1) {
StringUtil.copyPartialMatches(
args[0],
TabCompleteHelper.SKILL_NAMES,
completions
)
return@TabCompleteHandler completions
}
return@TabCompleteHandler emptyList()
}
}
}

View File

@@ -17,6 +17,7 @@ class CommandSkills(plugin: EcoPlugin) :
init {
this.addSubcommand(CommandTop(plugin))
.addSubcommand(CommandRank(plugin))
}
override fun getHandler(): CommandHandler {

View File

@@ -5,9 +5,9 @@ import com.willfp.eco.core.command.CommandHandler
import com.willfp.eco.core.command.impl.Subcommand
import com.willfp.eco.util.StringUtils
import com.willfp.ecoskills.data.LeaderboardHandler
import com.willfp.ecoskills.data.savedDisplayName
import com.willfp.ecoskills.getTotalSkillLevel
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
class CommandTop(plugin: EcoPlugin) :
@@ -22,7 +22,7 @@ class CommandTop(plugin: EcoPlugin) :
val page = args.firstOrNull()?.toIntOrNull() ?: 1
val top = LeaderboardHandler.getPage(page)
val messages = plugin.langYml.getStrings("top", false).toMutableList()
val messages = plugin.langYml.getStrings("top", false)
val lines = mutableListOf<String>()
val useDisplayName = plugin.configYml.getBool("commands.top.use-display-name")
@@ -34,8 +34,8 @@ class CommandTop(plugin: EcoPlugin) :
var name = player.name!!
if (useDisplayName && player is Player) {
name = player.displayName
if (useDisplayName) {
name = player.savedDisplayName
}
line = line.replace("%playername%", name)

View File

@@ -1,20 +1,21 @@
package com.willfp.ecoskills.data
import com.willfp.ecoskills.convertPersistentToYml
import com.willfp.ecoskills.*
import com.willfp.ecoskills.effects.Effect
import com.willfp.ecoskills.getSkillLevel
import com.willfp.ecoskills.setEffectLevel
import com.willfp.ecoskills.effects.Effects
import com.willfp.ecoskills.skills.Skills
import com.willfp.ecoskills.stats.Stats
import org.bukkit.attribute.Attribute
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerJoinEvent
import org.bukkit.persistence.PersistentDataType
class DataListener : Listener {
@EventHandler
fun onJoin(event: PlayerJoinEvent) {
event.player.convertPersistentToYml()
event.player.convertFromLegacyData()
for (skill in Skills.values()) {
for (levelUpReward in skill.getLevelUpRewards()) {
@@ -51,4 +52,24 @@ class DataListener : Listener {
stat.updateStatLevel(event.player)
}
}
}
private fun Player.convertFromLegacyData() {
for (effect in Effects.values()) {
plugin.dataYml.set("player.${this.uniqueId}.${effect.id}", this.getEffectLevel(effect))
}
for (stat in Stats.values()) {
plugin.dataYml.set("player.${this.uniqueId}.${stat.id}", this.getStatLevel(stat))
}
for (skill in Skills.values()) {
plugin.dataYml.set("player.${this.uniqueId}.${skill.id}", this.getSkillLevel(skill))
val prog = this.persistentDataContainer.get(skill.xpKey, PersistentDataType.DOUBLE)
if (prog != null) {
plugin.dataYml.set(
"player.${this.uniqueId}.${skill.xpKey.key}",
prog
)
this.persistentDataContainer.remove(skill.xpKey)
}
}
}

View File

@@ -1,6 +1,9 @@
package com.willfp.ecoskills.data
import com.willfp.ecoskills.getSkillLevel
import com.willfp.ecoskills.getTotalSkillLevel
import com.willfp.ecoskills.skills.Skill
import com.willfp.ecoskills.skills.Skills
import org.bukkit.Bukkit
import org.bukkit.OfflinePlayer
import kotlin.math.ceil
@@ -9,16 +12,19 @@ import kotlin.math.min
class LeaderboardHandler {
companion object {
val sortedLeaderboard = mutableListOf<OfflinePlayer>()
private val sortedLeaderboard = mutableListOf<OfflinePlayer>()
private val skillLeaderboards = mutableMapOf<Skill, MutableList<OfflinePlayer>>()
fun getPage(page: Int): MutableMap<Int, OfflinePlayer> {
val maxPage = ceil(sortedLeaderboard.size / 10.0).toInt()
fun getPage(page: Int, skill: Skill? = null): MutableMap<Int, OfflinePlayer> {
val selectedLeaderboard = if (skill == null) sortedLeaderboard else skillLeaderboards[skill]!!
val maxPage = ceil(selectedLeaderboard.size / 10.0).toInt()
val finalPage = max(1, min(page, maxPage))
val startIndex = (finalPage - 1) * 10
val endIndex = min(startIndex + 10, sortedLeaderboard.size - 1)
val endIndex = min(startIndex + 10, selectedLeaderboard.size - 1)
val players = sortedLeaderboard.subList(startIndex, endIndex)
val players = selectedLeaderboard.subList(startIndex, endIndex)
val withRank = mutableMapOf<Int, OfflinePlayer>()
var rank = startIndex + 1
@@ -33,6 +39,23 @@ class LeaderboardHandler {
class Runnable : java.lang.Runnable {
override fun run() {
for (skill in Skills.values()) {
val temp = mutableMapOf<OfflinePlayer, Int>()
val top = mutableListOf<OfflinePlayer>()
for (player in Bukkit.getOfflinePlayers()) {
temp[player] = 10000 - player.getSkillLevel(skill)
}
val temp2 = temp.toList().sortedBy { (_, value) -> value }.toMap()
for (key in temp2.keys) {
top.add(key)
}
skillLeaderboards[skill]?.clear()
skillLeaderboards[skill] = top
}
val temp = mutableMapOf<OfflinePlayer, Int>()
val top = mutableListOf<OfflinePlayer>()

View File

@@ -0,0 +1,36 @@
package com.willfp.ecoskills.data
import com.willfp.eco.core.EcoPlugin
import com.willfp.ecoskills.plugin
import org.bukkit.OfflinePlayer
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerJoinEvent
import org.bukkit.event.player.PlayerQuitEvent
class SavedPlayerNameListener(
private val plugin: EcoPlugin
) : Listener {
@EventHandler
fun onJoin(event: PlayerJoinEvent) {
event.player.savedDisplayName = event.player.displayName
}
@EventHandler
fun onJoin(event: PlayerQuitEvent) {
event.player.savedDisplayName = event.player.displayName
}
}
var OfflinePlayer.savedDisplayName: String
get() {
if (this is Player) {
plugin.dataYml.set("player.${this.uniqueId}.name", this.displayName)
}
return plugin.dataYml.getStringOrNull("player.${this.uniqueId}.name") ?: this.name ?: "Unknown Player"
}
set(value) {
plugin.dataYml.set("player.${this.uniqueId}.name", value)
}

View File

@@ -10,6 +10,7 @@ import com.willfp.ecoskills.config.SkillConfig
import com.willfp.ecoskills.effects.Effect
import com.willfp.ecoskills.effects.Effects
import com.willfp.ecoskills.stats.Stats
import org.bukkit.Bukkit
import org.bukkit.NamespacedKey
import org.bukkit.entity.Player
import org.bukkit.event.Listener
@@ -27,6 +28,7 @@ abstract class Skill(
lateinit var gui: SkillGUI
var maxLevel: Int = 50
private val rewards = mutableListOf<SkillObjectReward>()
private val levelCommands = mutableMapOf<Int, MutableList<String>>()
// Cached values
private val guiLoreCache = mutableMapOf<Int, List<String>>()
@@ -55,6 +57,17 @@ abstract class Skill(
}
}
levelCommands.clear()
for (string in config.getStrings("rewards.level-commands", false)) {
val split = string.split(":")
val level = split[0].toInt()
val command = split[1]
val commands = levelCommands[level] ?: mutableListOf()
commands.add(command)
levelCommands[level] = commands
}
PlaceholderEntry(
id,
{ player -> player.getSkillLevel(this).toString() },
@@ -201,6 +214,14 @@ abstract class Skill(
return lore
}
fun executeLevelCommands(player: Player, level: Int) {
val commands = levelCommands[level] ?: emptyList()
for (command in commands) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command.replace("%player%", player.name))
}
}
open fun postUpdate() {
// Override when needed
}

View File

@@ -31,5 +31,7 @@ class SkillLevellingListener : Listener {
}
}
}
skill.executeLevelCommands(player, to)
}
}

View File

@@ -7,8 +7,8 @@ messages:
requires-player: "&cYou must specify a player!"
invalid-player: "&cInvalid player!"
reset-player: "&fReset player!"
requires-skill-stat: "&cYou must specify a skill or stat!"
invalid-skill-stat: "&cInvalid a skill or stat!"
requires-skill: "&cYou must specify a skill!"
invalid-skill: "&cInvalid skill!"
requires-amount: "&cYou must specify the amount!"
invalid-amount: "&cInvalid amount!"
gave-skill-xp: "Gave %player% %amount% %skill% experience!"

View File

@@ -40,6 +40,7 @@ permissions:
ecoskills.command.skills: true
ecoskills.command.give: true
ecoskills.command.top: true
ecoskills.command.rank: true
ecoskills.command.reload:
description: Allows reloading the config
@@ -56,6 +57,9 @@ permissions:
ecoskills.command.top:
description: Allows the use of /ecoskills top.
default: true
ecoskills.command.rank:
description: Allows the use of /ecoskills rank.
default: true
ecoskills.command.give:
description: Allows the use of /ecoskills give.
default: op

View File

@@ -29,6 +29,8 @@ rewards:
- "efficient_brewing::1:10:100"
- "mystic_resilience::1"
level-commands: []
# The chat messages to send on level up
chat-messages:
1:

View File

@@ -29,6 +29,8 @@ rewards:
- "infernal_resistance::1"
- "bravery::1:10:100"
level-commands: []
# The chat messages to send on level up
chat-messages:
1:

View File

@@ -32,6 +32,8 @@ rewards:
- "strong_impact::1:10:100"
- "endangering::1:20:100"
level-commands: []
# The chat messages to send on level up
chat-messages:
1:

View File

@@ -29,6 +29,8 @@ rewards:
- "reimbursement::1"
- "overcompensation::1:10:100"
level-commands: []
# The chat messages to send on level up
chat-messages:
1:

View File

@@ -29,6 +29,8 @@ rewards:
- "dodging::1"
- "accelerated_escape::1:10:100"
level-commands: []
# The chat messages to send on level up
chat-messages:
1:

View File

@@ -29,6 +29,8 @@ rewards:
- "satiation::1"
- "golden_yield::1:10:100"
level-commands: []
# The chat messages to send on level up
chat-messages:
1:

View File

@@ -23,6 +23,8 @@ rewards:
- "wisdom::1"
- "eye_of_the_depths::1"
level-commands: []
# The chat messages to send on level up
chat-messages:
1:

View File

@@ -29,6 +29,8 @@ rewards:
- "spelunking::1:10:100"
- "dynamic_mining::1"
level-commands: []
# The chat messages to send on level up
chat-messages:
1:

View File

@@ -26,6 +26,8 @@ rewards:
- "craftsmanship::1"
- "master_lumberjack::1"
level-commands: []
# The chat messages to send on level up
chat-messages:
1:

View File

@@ -1,2 +1,2 @@
version = 1.0.6
version = 1.2.0
plugin-name = EcoSkills