diff --git a/build.gradle.kts b/build.gradle.kts index e5e6fc56..f076b138 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -77,6 +77,8 @@ allprojects { // LibsDisguises maven("https://repo.md-5.net/content/groups/public/") + + maven("https://repo.techscode.com/repository/maven-releases/") } dependencies { diff --git a/eco-api/src/main/java/com/willfp/eco/core/Eco.java b/eco-api/src/main/java/com/willfp/eco/core/Eco.java index a3743070..50da2693 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/Eco.java +++ b/eco-api/src/main/java/com/willfp/eco/core/Eco.java @@ -71,7 +71,7 @@ public interface Eco { /** * Create an event manager. * - * @param plugin The plugin. + * @param plugin The plugin.F * @return The event manager. */ @NotNull 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 new file mode 100644 index 00000000..9537de3d --- /dev/null +++ b/eco-api/src/main/java/com/willfp/eco/core/integrations/price/PriceManager.java @@ -0,0 +1,31 @@ +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/build.gradle b/eco-core/core-plugin/build.gradle index 47415c1e..36eb6aa9 100644 --- a/eco-core/core-plugin/build.gradle +++ b/eco-core/core-plugin/build.gradle @@ -50,6 +50,7 @@ dependencies { compileOnly 'com.github.Gypopo:EconomyShopGUI-API:1.1.0' compileOnly 'com.github.N0RSKA:ScytherAPI:55a' compileOnly 'com.ticxo.modelengine:api:R3.0.1' + compileOnly 'me.TechsCode:UltraEconomyAPI:1.0.0' // MythicMobs compileOnly 'io.lumine:Mythic:5.0.1' 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 bd219305..03e2115f 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,6 +15,7 @@ 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 @@ -114,6 +115,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.shop.ShopDeluxeSellwands import com.willfp.eco.internal.spigot.integrations.shop.ShopEconomyShopGUI import com.willfp.eco.internal.spigot.integrations.shop.ShopShopGuiPlus @@ -125,6 +127,7 @@ import com.willfp.eco.internal.spigot.recipes.listeners.ComplexInComplex import com.willfp.eco.internal.spigot.recipes.listeners.ComplexInVanilla import com.willfp.eco.internal.spigot.recipes.stackhandlers.ShapedCraftingRecipeStackHandler import com.willfp.eco.internal.spigot.recipes.stackhandlers.ShapelessCraftingRecipeStackHandler +import me.TechsCode.UltraEconomy.UltraEconomy import net.kyori.adventure.platform.bukkit.BukkitAudiences import net.milkbowl.vault.economy.Economy import org.bukkit.Bukkit @@ -331,6 +334,13 @@ abstract class EcoSpigotPlugin : EcoPlugin() { } }, + // Price + IntegrationLoader("UltraEconomy") { + UltraEconomy.getAPI().currencies.forEach { + PriceManager.register(PriceUltraEconomy(it)) + } + }, + // Placeholder IntegrationLoader("PlaceholderAPI") { PlaceholderManager.addIntegration(PlaceholderIntegrationPAPI()) }, 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 new file mode 100644 index 00000000..d39a5ef8 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/integrations/price/PriceUltraEconomy.kt @@ -0,0 +1,70 @@ +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