diff --git a/eco-api/src/main/java/com/willfp/eco/core/integrations/price/PriceManager.java b/eco-api/src/main/java/com/willfp/eco/core/integrations/price/PriceManager.java deleted file mode 100644 index 9537de3d..00000000 --- a/eco-api/src/main/java/com/willfp/eco/core/integrations/price/PriceManager.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.willfp.eco.core.integrations.price; - -import com.willfp.eco.core.price.PriceFactory; -import org.jetbrains.annotations.NotNull; - -import java.util.HashSet; -import java.util.Set; - -/** - * Class to handle afk integrations. - */ -public final class PriceManager { - /** - * A set of all registered integrations. - */ - private static final Set REGISTERED = new HashSet<>(); - - /** - * Register a new integration. - * - * @param integration The integration to register. - */ - public static void register(@NotNull final PriceFactory integration) { - REGISTERED.removeIf(it -> new HashSet<>(integration.getNames()).containsAll(it.getNames())); - REGISTERED.add(integration); - } - - private PriceManager() { - throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); - } -} diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoSpigotPlugin.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoSpigotPlugin.kt index 03e2115f..bbfc7d4d 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoSpigotPlugin.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoSpigotPlugin.kt @@ -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)) } }, diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/integrations/price/PriceFactoryUltraEconomy.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/integrations/price/PriceFactoryUltraEconomy.kt new file mode 100644 index 00000000..43b23476 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/integrations/price/PriceFactoryUltraEconomy.kt @@ -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 { + return "ultraeconomy:${currency.name.lowercase()}".toSingletonList() + } + + override fun create(baseContext: MathContext, function: Function): 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() + 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 + } + } +} diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/integrations/price/PriceUltraEconomy.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/integrations/price/PriceUltraEconomy.kt deleted file mode 100644 index d39a5ef8..00000000 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/integrations/price/PriceUltraEconomy.kt +++ /dev/null @@ -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 { - 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): Price { - return UEPriceObject(currency, function, baseContext) - } - - class UEPriceObject( - private val currency: Currency, private val function: Function, - 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()) - } - - } -} \ No newline at end of file