Skip to content

Commit

Permalink
feat(smItems): Added full xp implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ixhbinphoenix committed Apr 10, 2022
1 parent 95f36e6 commit 79f6cec
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import org.bukkit.NamespacedKey
import org.bukkit.entity.Player
import org.bukkit.Sound
import org.bukkit.persistence.PersistentDataType
import kotlin.math.floor


fun getPrefix(player: Player, message: Component): Component {
Expand Down Expand Up @@ -116,29 +115,30 @@ fun getRankColor(rank: Rank): TextColor {
}
}

fun createRankInfoText(rank: Rank, category: String, specific: String): Component {
return Component.text(category).color(getRankColor(rank))
.append(Component.text(" ($specific)").color(NamedTextColor.DARK_GRAY))
}

fun createProgressBar(bars: Int, progress: Double): Component {
val barPercent: Double = (100 / bars).toDouble()
var prog = progress
var barsWritten = bars
// Shadowing is useful here, since params cannot be vars
@Suppress("NAME_SHADOWING") var bars = bars
val barPercent = 100 / bars
var prog = progress + 0.001

var bar = Component.text("[").color(NamedTextColor.DARK_GRAY)
var comp = Component.text("[").color(NamedTextColor.DARK_GRAY)

while (prog > barPercent) {
prog -= barPercent
bar = bar.append(Component.text("|").color(NamedTextColor.GREEN))
barsWritten -= 1
}
while (barsWritten > 0) {
bar = bar.append(Component.text("|").color(NamedTextColor.GRAY))
barsWritten -= 1
comp = comp.append(Component.text("|").color(NamedTextColor.GREEN))
bars -= 1
}

return bar.append(Component.text("]").color(NamedTextColor.DARK_GRAY))
}

fun createRankInfoText(rank: Rank, category: String, specific: String): Component {
return Component.text(category).color(getRankColor(rank))
.append(Component.text(" ($specific)").color(NamedTextColor.DARK_GRAY))
while (bars > 0) {
comp = comp.append(Component.text("|").color(NamedTextColor.DARK_GREEN))
bars -= 1
}
return comp.append(Component.text("]").color(NamedTextColor.DARK_GRAY))
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ open class ItemHandler(val item: ItemStack, private val player: Player) {
this.stats[stat.key] = pdc.getOrDefault(NamespacedKey.fromString(stat.value)!!, PersistentDataType.INTEGER, 0)
}
this.xp = getItemXP()
this.level = calcLevel()
this.level = itemUtils.calcRarityLevel(xp, itemUtils.getRarityMultiplier(rarity))
}

fun updateLore() {
Expand Down Expand Up @@ -72,7 +72,7 @@ open class ItemHandler(val item: ItemStack, private val player: Player) {
}
}
val im = item.itemMeta
im.lore(itemUtils.genLore(rarity, type, statTexts, this.level, SetHelper.getCompletion(player, set), setHelper.setObjects[set]))
im.lore(itemUtils.genLore(rarity, type, statTexts, xp, SetHelper.getCompletion(player, set), setHelper.setObjects[set]))
item.itemMeta = im
}

Expand Down Expand Up @@ -117,39 +117,14 @@ open class ItemHandler(val item: ItemStack, private val player: Player) {
}

fun setXP(xp: Int) {
pdc.set(
val im = item.itemMeta
im.persistentDataContainer.set(
NamespacedKey.fromString("smitems:item.xp.int")!!,
PersistentDataType.INTEGER,
0
xp
)
item.itemMeta = im
this.xp = xp
this.level = calcLevel()
updateLore()
}

private fun calcLevel(): Double {
var num = 0.0
when (rarity) {
Rarity.COMMON -> {
num = sqrt(this.xp.toDouble() / 500)
}
Rarity.UNCOMMON -> {
num = sqrt(this.xp.toDouble() / 750)
}
Rarity.RARE -> {
num = sqrt(this.xp.toDouble() / 1250)
}
Rarity.EPIC -> {
num = sqrt(this.xp.toDouble() / 2000)
}
Rarity.LEGENDARY -> {
num = sqrt(this.xp.toDouble() / 3000)
}
Rarity.MYTHIC -> {
num = sqrt(this.xp.toDouble() / 5000)
}
}
return if (num > 20.0) 20.0 else num
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import me.ixhbinphoenix.smPl.smItems.events.StatsCalculation
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.format.NamedTextColor
import net.kyori.adventure.text.format.TextDecoration
import org.bukkit.Bukkit
import org.bukkit.Material
import org.bukkit.NamespacedKey
import org.bukkit.entity.Player
Expand All @@ -17,8 +18,9 @@ import org.bukkit.persistence.PersistentDataType
import org.bukkit.scheduler.BukkitRunnable
import java.util.*
import kotlin.collections.ArrayList
import kotlin.math.floor
import kotlin.math.roundToInt
import kotlin.math.floor
import kotlin.math.pow

object SetBonus {
var set: String = "NONE"
Expand Down Expand Up @@ -69,7 +71,7 @@ class ItemUtils {
im.persistentDataContainer.set(NamespacedKey.fromString("smitems:weapon.type.str")!!, PersistentDataType.STRING, weaponType.name)
im.persistentDataContainer.set(NamespacedKey.fromString("smitems:item.id.str")!!, PersistentDataType.STRING, id)
im.persistentDataContainer.set(NamespacedKey.fromString("smitems:item.uuid.str")!!, PersistentDataType.STRING, UUID.randomUUID().toString())
im.lore(genLore(rarity, Types.valueOf(weaponType.name), statTexts, 0.0, 0, set))
im.lore(genLore(rarity, Types.valueOf(weaponType.name), statTexts, 0, 0, set))
im.addItemFlags(ItemFlag.HIDE_ATTRIBUTES)
im.addItemFlags(ItemFlag.HIDE_UNBREAKABLE)
im.addItemFlags(ItemFlag.HIDE_DYE)
Expand All @@ -80,7 +82,7 @@ class ItemUtils {
fun createArmor(mat: Material, name: String, id: String, rarity: Rarity, armorType: ArmorTypes, defence: Int = 0, maxHealth: Int = 0, set: SetBonus?): ItemStack {
val item = ItemStack(mat, 1)
val im = item.itemMeta
im.isUnbreakable = false
im.isUnbreakable = true
im.displayName(Component.text(name).color(RarityColor.valueOf(rarity.name).color).decoration(TextDecoration.ITALIC, false))
val statTexts = ArrayList<Component>()
if (defence > 0) {
Expand All @@ -99,26 +101,27 @@ class ItemUtils {
im.persistentDataContainer.set(NamespacedKey.fromString("smitems:armor.type.str")!!, PersistentDataType.STRING, armorType.name)
im.persistentDataContainer.set(NamespacedKey.fromString("smitems:item.id.str")!!, PersistentDataType.STRING, id)
im.persistentDataContainer.set(NamespacedKey.fromString("smitems:item.uuid.str")!!, PersistentDataType.STRING, UUID.randomUUID().toString())
im.lore(genLore(rarity, Types.valueOf(armorType.name), statTexts, 0.0, 0, set))
im.lore(genLore(rarity, Types.valueOf(armorType.name), statTexts, 0, 0, set))
im.addItemFlags(ItemFlag.HIDE_ATTRIBUTES)
im.addItemFlags(ItemFlag.HIDE_UNBREAKABLE)
im.addItemFlags(ItemFlag.HIDE_DYE)
item.itemMeta = im
return item
}

fun genLore(rarity: Rarity, type: Types, stats: ArrayList<Component>, level: Double, setCompletion: Int = 0, set: SetBonus?): ArrayList<Component> {
fun genLore(rarity: Rarity, type: Types, stats: ArrayList<Component>, xp: Int, setCompletion: Int = 0, set: SetBonus?): ArrayList<Component> {
val lore = ArrayList<Component>()
lore.addAll(stats)
lore.add(Component.text("").decoration(TextDecoration.ITALIC, false))
lore.add(Component.text("Level: ").color(NamedTextColor.GRAY).decoration(TextDecoration.ITALIC, false))
val level = calcRarityLevel(xp, getRarityMultiplier(rarity))
val percent = "%.2f".format((level - floor(level)) * 100).toDouble()
lore.add(
Component.text(floor(level).roundToInt()).color(NamedTextColor.GREEN)
.append(Component.text("").color(NamedTextColor.GRAY))
.append(Component.text(floor(level).roundToInt() + 1).color(NamedTextColor.DARK_GREEN))
.append(Component.text(" "))
.append(createProgressBar(20, percent))
.append(createProgressBar(20, level))
.append(Component.text(" ${percent}%").color(NamedTextColor.GREEN))
.decoration(TextDecoration.ITALIC, false)
)
Expand All @@ -138,4 +141,33 @@ class ItemUtils {
lore.add(Component.text("${rarity.name} ${type.name}").color(RarityColor.valueOf(rarity.name).color).decoration(TextDecoration.ITALIC, false))
return lore
}

fun getRarityMultiplier(rarity: Rarity): Int {
return when (rarity) {
Rarity.COMMON -> 500
Rarity.UNCOMMON -> 750
Rarity.RARE -> 1500
Rarity.EPIC -> 2250
Rarity.LEGENDARY -> 3500
Rarity.MYTHIC -> 5000
}
}

// this is a terrible way of doing this
fun calcRarityLevel(xp: Int, a: Int): Double {
// These ARE constants that could have been pre-calculated, however that would look ugly as fuck and it takes
// literal nanoseconds to calculate this (i hope lmao)
if (xp < 1) return 0.0
val levelReqs = ArrayList<Double>()
for (x in 0..19) {
levelReqs.add(a * x.toDouble().pow(2))
}
for (x in 0 until levelReqs.size) {
val fraction = xp / levelReqs[x]
if (x == 19 && fraction > 1) return 20.0
if (fraction > 1) continue
return fraction + x - 1
}
throw IllegalStateException("calcRarityLevel could not calculate level and exited without returning")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ class SetHelper {
return comp
}
}
val Handlers = HashMap<String, BaseSetHandler>()
private val handlers = HashMap<String, BaseSetHandler>()
val setObjects = HashMap<String, SetBonus>()
init {
Handlers["PROGRAMMER"] = ProgrammerSet()
handlers["PROGRAMMER"] = ProgrammerSet()
setObjects["PROGRAMMER"] = ProgrammerSet.getBonus()
}

fun calcSet(armor: ArrayList<ItemStack?>, player: Player): Boolean {
val set = getSet(armor, player)
return if (Handlers.containsKey(set)) {
Handlers[set]?.onRecalc(player)
return if (handlers.containsKey(set)) {
handlers[set]?.onRecalc(player)
true
} else {
false
Expand Down
2 changes: 1 addition & 1 deletion smItems/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ commands:
description: Gives a test item
setitemxp:
usage: /<command> xp
description: Sets xp of the currently equiped item
description: Sets xp of the held item
depend:
- smCore

0 comments on commit 79f6cec

Please sign in to comment.