Updated price API

This commit is contained in:
Auxilor
2022-10-29 16:28:48 +01:00
parent cf347de4b8
commit ec8936b765
9 changed files with 117 additions and 37 deletions

View File

@@ -3,6 +3,7 @@ package com.willfp.eco.internal.price
import com.willfp.eco.core.price.Price
import com.willfp.eco.core.price.PriceFactory
import com.willfp.eco.core.price.impl.PriceEconomy
import java.util.function.Supplier
object PriceFactoryEconomy : PriceFactory {
override fun getNames() = listOf(
@@ -10,5 +11,7 @@ object PriceFactoryEconomy : PriceFactory {
"$"
)
override fun create(value: Double): Price = PriceEconomy(value)
override fun create(function: Supplier<Double>): Price {
return PriceEconomy(function)
}
}

View File

@@ -3,6 +3,7 @@ package com.willfp.eco.internal.price
import com.willfp.eco.core.price.Price
import com.willfp.eco.core.price.PriceFactory
import org.bukkit.entity.Player
import java.util.function.Supplier
import kotlin.math.roundToInt
object PriceFactoryXP : PriceFactory {
@@ -12,15 +13,21 @@ object PriceFactoryXP : PriceFactory {
"experience"
)
override fun create(value: Double): Price = PriceXP(value.roundToInt())
override fun create(function: Supplier<Double>): Price {
return PriceXP { function.get().roundToInt() }
}
private class PriceXP(
private val xp: Int
private val xp: () -> Int
) : Price {
override fun canAfford(player: Player) = player.totalExperience >= xp
override fun canAfford(player: Player) = player.totalExperience >= xp()
override fun pay(player: Player) {
player.totalExperience -= xp
player.totalExperience -= xp()
}
override fun giveTo(player: Player) {
player.totalExperience += xp()
}
}
}

View File

@@ -3,6 +3,7 @@ package com.willfp.eco.internal.price
import com.willfp.eco.core.price.Price
import com.willfp.eco.core.price.PriceFactory
import org.bukkit.entity.Player
import java.util.function.Supplier
import kotlin.math.roundToInt
object PriceFactoryXPLevels : PriceFactory {
@@ -13,15 +14,21 @@ object PriceFactoryXPLevels : PriceFactory {
"explevels",
)
override fun create(value: Double): Price = PriceXPLevel(value.roundToInt())
override fun create(function: Supplier<Double>): Price {
return PriceXPLevel { function.get().roundToInt() }
}
private class PriceXPLevel(
private val levels: Int
private val levels: () -> Int
) : Price {
override fun canAfford(player: Player) = player.level >= levels
override fun canAfford(player: Player) = player.level >= levels()
override fun pay(player: Player) {
player.level -= levels
player.level -= levels()
}
override fun giveTo(player: Player) {
player.level += levels()
}
}
}