Fixed UltraEconomy integration
This commit is contained in:
@@ -15,7 +15,6 @@ import com.willfp.eco.core.integrations.economy.EconomyManager
|
||||
import com.willfp.eco.core.integrations.hologram.HologramManager
|
||||
import com.willfp.eco.core.integrations.mcmmo.McmmoManager
|
||||
import com.willfp.eco.core.integrations.placeholder.PlaceholderManager
|
||||
import com.willfp.eco.core.integrations.price.PriceManager
|
||||
import com.willfp.eco.core.integrations.shop.ShopManager
|
||||
import com.willfp.eco.core.items.Items
|
||||
import com.willfp.eco.core.particle.Particles
|
||||
@@ -115,7 +114,7 @@ import com.willfp.eco.internal.spigot.integrations.hologram.HologramHolographicD
|
||||
import com.willfp.eco.internal.spigot.integrations.mcmmo.McmmoIntegrationImpl
|
||||
import com.willfp.eco.internal.spigot.integrations.multiverseinventories.MultiverseInventoriesIntegration
|
||||
import com.willfp.eco.internal.spigot.integrations.placeholder.PlaceholderIntegrationPAPI
|
||||
import com.willfp.eco.internal.spigot.integrations.price.PriceUltraEconomy
|
||||
import com.willfp.eco.internal.spigot.integrations.price.PriceFactoryUltraEconomy
|
||||
import com.willfp.eco.internal.spigot.integrations.shop.ShopDeluxeSellwands
|
||||
import com.willfp.eco.internal.spigot.integrations.shop.ShopEconomyShopGUI
|
||||
import com.willfp.eco.internal.spigot.integrations.shop.ShopShopGuiPlus
|
||||
@@ -336,8 +335,8 @@ abstract class EcoSpigotPlugin : EcoPlugin() {
|
||||
|
||||
// Price
|
||||
IntegrationLoader("UltraEconomy") {
|
||||
UltraEconomy.getAPI().currencies.forEach {
|
||||
PriceManager.register(PriceUltraEconomy(it))
|
||||
for (currency in UltraEconomy.getAPI().currencies) {
|
||||
Prices.registerPriceFactory(PriceFactoryUltraEconomy(currency))
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.willfp.eco.internal.spigot.integrations.price
|
||||
|
||||
import com.willfp.eco.core.math.MathContext
|
||||
import com.willfp.eco.core.price.Price
|
||||
import com.willfp.eco.core.price.PriceFactory
|
||||
import com.willfp.eco.util.toSingletonList
|
||||
import me.TechsCode.UltraEconomy.UltraEconomy
|
||||
import me.TechsCode.UltraEconomy.objects.Account
|
||||
import me.TechsCode.UltraEconomy.objects.Currency
|
||||
import org.bukkit.entity.Player
|
||||
import java.util.UUID
|
||||
import java.util.function.Function
|
||||
|
||||
class PriceFactoryUltraEconomy(private val currency: Currency) : PriceFactory {
|
||||
override fun getNames(): List<String> {
|
||||
return "ultraeconomy:${currency.name.lowercase()}".toSingletonList()
|
||||
}
|
||||
|
||||
override fun create(baseContext: MathContext, function: Function<MathContext, Double>): Price {
|
||||
return PriceUltraEconomy(currency, baseContext) { function.apply(it) }
|
||||
}
|
||||
|
||||
private class PriceUltraEconomy(
|
||||
private val currency: Currency,
|
||||
private val baseContext: MathContext,
|
||||
private val function: (MathContext) -> Double
|
||||
) : Price {
|
||||
private val multipliers = mutableMapOf<UUID, Double>()
|
||||
private val api = UltraEconomy.getAPI()
|
||||
|
||||
private val Player.account: Account?
|
||||
get() = api.accounts.uuid(this.uniqueId).orElse(null)
|
||||
|
||||
override fun canAfford(player: Player): Boolean {
|
||||
return (player.account?.getBalance(currency)?.onHand ?: 0f) >= getValue(player)
|
||||
}
|
||||
|
||||
override fun pay(player: Player) {
|
||||
player.account?.getBalance(currency)?.removeHand(getValue(player).toFloat())
|
||||
}
|
||||
|
||||
override fun giveTo(player: Player) {
|
||||
player.account?.getBalance(currency)?.addHand(getValue(player).toFloat())
|
||||
}
|
||||
|
||||
override fun getValue(player: Player): Double {
|
||||
return function(MathContext.copyWithPlayer(baseContext, player)) * getMultiplier(player)
|
||||
}
|
||||
|
||||
override fun getMultiplier(player: Player): Double {
|
||||
return multipliers[player.uniqueId] ?: 1.0
|
||||
}
|
||||
|
||||
override fun setMultiplier(player: Player, multiplier: Double) {
|
||||
multipliers[player.uniqueId] = multiplier
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
package com.willfp.eco.internal.spigot.integrations.price
|
||||
|
||||
import com.willfp.eco.core.math.MathContext
|
||||
import com.willfp.eco.core.price.Price
|
||||
import com.willfp.eco.core.price.PriceFactory
|
||||
import com.willfp.eco.util.toSingletonList
|
||||
import me.TechsCode.UltraEconomy.UltraEconomy
|
||||
import me.TechsCode.UltraEconomy.objects.Currency
|
||||
import org.bukkit.entity.Player
|
||||
import java.util.function.Function
|
||||
|
||||
class PriceUltraEconomy(private val currency: Currency): PriceFactory {
|
||||
|
||||
/**
|
||||
* Get the names (how the price looks in lookup strings).
|
||||
*
|
||||
*
|
||||
* For example, for XP Levels this would be 'l', 'xpl', 'levels', etc.
|
||||
*
|
||||
* @return The allowed names.
|
||||
*/
|
||||
override fun getNames(): MutableList<String> {
|
||||
return "ultraeconomy:${currency.name.lowercase()}".toSingletonList().toMutableList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the price.
|
||||
*
|
||||
* @param baseContext The base MathContext.
|
||||
* @param function The function to use. Should use {@link MathContext#copyWithPlayer(MathContext, Player)}
|
||||
* on calls.
|
||||
* @return The price.
|
||||
*/
|
||||
override fun create(baseContext: MathContext, function: Function<MathContext, Double>): Price {
|
||||
return UEPriceObject(currency, function, baseContext)
|
||||
}
|
||||
|
||||
class UEPriceObject(
|
||||
private val currency: Currency, private val function: Function<MathContext, Double>,
|
||||
private val baseContext: MathContext): Price {
|
||||
private val api = UltraEconomy.getAPI()
|
||||
|
||||
/**
|
||||
* Get if the player can afford the price.
|
||||
*
|
||||
* @param player The player.
|
||||
* @return If the player can afford.
|
||||
*/
|
||||
override fun canAfford(player: Player): Boolean {
|
||||
return (api.accounts.firstOrNull { it.uuid == player.uniqueId }
|
||||
?.getBalance(currency)?.onHand?.toDouble()?: 0.0) >= (function.apply(
|
||||
MathContext.copyWithPlayer(baseContext, player)))
|
||||
}
|
||||
|
||||
/**
|
||||
* Make the player pay the price.
|
||||
*
|
||||
*
|
||||
* Only run this if the player can afford the price.
|
||||
*
|
||||
* @param player The player.
|
||||
*/
|
||||
override fun pay(player: Player) {
|
||||
api.accounts.firstOrNull { it.uuid == player.uniqueId }
|
||||
?.getBalance(currency)
|
||||
?.removeHand(function.apply(MathContext.copyWithPlayer(baseContext, player)).toFloat())
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user