From 8d8a8045c056ff94fbb4244e9e4a7365aa8cecb4 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Thu, 29 Sep 2022 09:51:43 +0100 Subject: [PATCH] Moved frontend -> backend communication to be purely via the handler --- .../java/com/willfp/eco/core/Handler.java | 54 +++++++++++++++++++ .../java/com/willfp/eco/util/MenuUtils.java | 20 +------ .../java/com/willfp/eco/util/NumberUtils.java | 41 +------------- .../java/com/willfp/eco/util/SkullUtils.java | 47 ++-------------- .../willfp/eco/internal/spigot/EcoHandler.kt | 28 ++++++++++ .../eco/internal/spigot/EcoSpigotPlugin.kt | 21 -------- 6 files changed, 89 insertions(+), 122 deletions(-) diff --git a/eco-api/src/main/java/com/willfp/eco/core/Handler.java b/eco-api/src/main/java/com/willfp/eco/core/Handler.java index 4cef801a..fed4df98 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/Handler.java +++ b/eco-api/src/main/java/com/willfp/eco/core/Handler.java @@ -14,8 +14,11 @@ import com.willfp.eco.core.factory.NamespacedKeyFactory; import com.willfp.eco.core.factory.RunnableFactory; import com.willfp.eco.core.fast.FastItemStack; import com.willfp.eco.core.gui.GUIFactory; +import com.willfp.eco.core.gui.menu.Menu; import com.willfp.eco.core.integrations.placeholder.PlaceholderIntegration; import com.willfp.eco.core.items.SNBTHandler; +import com.willfp.eco.core.placeholder.AdditionalPlayer; +import com.willfp.eco.core.placeholder.PlaceholderInjectable; import com.willfp.eco.core.proxy.Cleaner; import com.willfp.eco.core.proxy.ProxyFactory; import com.willfp.eco.core.scheduling.Scheduler; @@ -24,12 +27,15 @@ import org.bukkit.Location; import org.bukkit.NamespacedKey; import org.bukkit.entity.Entity; import org.bukkit.entity.Mob; +import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.SkullMeta; import org.bukkit.persistence.PersistentDataContainer; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Collection; import java.util.List; import java.util.logging.Logger; @@ -309,4 +315,52 @@ public interface Handler { */ @NotNull SNBTHandler getSNBTHandler(); + + /** + * Get the texture of a skull. + * + * @param meta The skull meta. + * @return The texture, or null if not found. + */ + @Nullable + String getSkullTexture(@NotNull SkullMeta meta); + + /** + * Set the texture of a skull. + * + * @param meta The skull meta. + * @param base64 The texture. + */ + void setSkullTexture(@NotNull SkullMeta meta, + @NotNull String base64); + + /** + * Get the current server TPS. + * + * @return The TPS. + */ + double getTPS(); + + /** + * Evaluate an expression. + * + * @param expression The expression. + * @param player The player. + * @param injectable The injectable placeholders. + * @param additionalPlayers The additional players. + * @return The value of the expression, or zero if invalid. + */ + double evaluate(@NotNull String expression, + @Nullable Player player, + @NotNull PlaceholderInjectable injectable, + @NotNull Collection additionalPlayers); + + /** + * Get the menu a player currently has open. + * + * @param player The player. + * @return The menu, or null if no menu open. + */ + @Nullable + Menu getOpenMenu(@NotNull Player player); } diff --git a/eco-api/src/main/java/com/willfp/eco/util/MenuUtils.java b/eco-api/src/main/java/com/willfp/eco/util/MenuUtils.java index 100ee4d1..98165f8d 100644 --- a/eco-api/src/main/java/com/willfp/eco/util/MenuUtils.java +++ b/eco-api/src/main/java/com/willfp/eco/util/MenuUtils.java @@ -1,5 +1,6 @@ package com.willfp.eco.util; +import com.willfp.eco.core.Eco; import com.willfp.eco.core.gui.menu.Menu; import com.willfp.eco.core.tuples.Pair; import org.apache.commons.lang.Validate; @@ -14,11 +15,6 @@ import java.util.function.Function; * Utilities / API methods for menus. */ public final class MenuUtils { - /** - * The menu supplier. - */ - private static Function menuGetter = null; - /** * Convert 0-53 slot to row and column pair. * @@ -51,19 +47,7 @@ public final class MenuUtils { */ @Nullable public static Menu getOpenMenu(@NotNull final Player player) { - return menuGetter.apply(player); - } - - /** - * Initialize the tps supplier function. - * - * @param function The function. - */ - @ApiStatus.Internal - public static void initialize(@NotNull final Function function) { - Validate.isTrue(menuGetter == null, "Already initialized!"); - - menuGetter = function; + return Eco.getHandler().getOpenMenu(player); } private MenuUtils() { diff --git a/eco-api/src/main/java/com/willfp/eco/util/NumberUtils.java b/eco-api/src/main/java/com/willfp/eco/util/NumberUtils.java index 1ff026b2..235780a1 100644 --- a/eco-api/src/main/java/com/willfp/eco/util/NumberUtils.java +++ b/eco-api/src/main/java/com/willfp/eco/util/NumberUtils.java @@ -1,12 +1,11 @@ package com.willfp.eco.util; +import com.willfp.eco.core.Eco; import com.willfp.eco.core.placeholder.AdditionalPlayer; import com.willfp.eco.core.placeholder.InjectablePlaceholder; import com.willfp.eco.core.placeholder.PlaceholderInjectable; import com.willfp.eco.core.placeholder.StaticPlaceholder; -import org.apache.commons.lang.Validate; import org.bukkit.entity.Player; -import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -28,11 +27,6 @@ public final class NumberUtils { */ private static final double[] SIN_LOOKUP = new double[65536]; - /** - * Crunch handler. - */ - private static CrunchHandler crunch = null; - /** * Set of roman numerals to look up. */ @@ -329,38 +323,7 @@ public final class NumberUtils { @Nullable final Player player, @NotNull final PlaceholderInjectable context, @NotNull final Collection additionalPlayers) { - return crunch.evaluate(expression, player, context, additionalPlayers); - } - - /** - * Init crunch handler. - * - * @param handler The handler. - */ - @ApiStatus.Internal - public static void initCrunch(@NotNull final CrunchHandler handler) { - Validate.isTrue(crunch == null, "Already initialized!"); - crunch = handler; - } - - /** - * Bridge component for crunch. - */ - @ApiStatus.Internal - public interface CrunchHandler { - /** - * Evaluate an expression. - * - * @param expression The expression. - * @param player The player. - * @param injectable The injectable placeholders. - * @param additionalPlayers The additional players. - * @return The value of the expression, or zero if invalid. - */ - double evaluate(@NotNull String expression, - @Nullable Player player, - @NotNull PlaceholderInjectable injectable, - @NotNull Collection additionalPlayers); + return Eco.getHandler().evaluate(expression, player, context, additionalPlayers); } private NumberUtils() { diff --git a/eco-api/src/main/java/com/willfp/eco/util/SkullUtils.java b/eco-api/src/main/java/com/willfp/eco/util/SkullUtils.java index 24e40911..5eeb2ee4 100644 --- a/eco-api/src/main/java/com/willfp/eco/util/SkullUtils.java +++ b/eco-api/src/main/java/com/willfp/eco/util/SkullUtils.java @@ -1,33 +1,14 @@ package com.willfp.eco.util; -import org.apache.commons.lang.Validate; +import com.willfp.eco.core.Eco; import org.bukkit.inventory.meta.SkullMeta; -import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.function.BiConsumer; -import java.util.function.Function; - /** * Utilities / API methods for player heads. */ public final class SkullUtils { - /** - * If the meta set function has been set. - */ - private static boolean initialized = false; - - /** - * The meta set function. - */ - private static BiConsumer metaSetConsumer = null; - - /** - * The meta get function. - */ - private static Function metaGetConsumer = null; - /** * Set the texture of a skull from base64. * @@ -36,10 +17,7 @@ public final class SkullUtils { */ public static void setSkullTexture(@NotNull final SkullMeta meta, @NotNull final String base64) { - Validate.isTrue(initialized, "Must be initialized!"); - Validate.notNull(metaSetConsumer, "Must be initialized!"); - - metaSetConsumer.accept(meta, base64); + Eco.getHandler().setSkullTexture(meta, base64); } /** @@ -50,26 +28,7 @@ public final class SkullUtils { */ @Nullable public static String getSkullTexture(@NotNull final SkullMeta meta) { - Validate.isTrue(initialized, "Must be initialized!"); - Validate.notNull(metaGetConsumer, "Must be initialized!"); - - return metaGetConsumer.apply(meta); - } - - /** - * Initialize the skull texture function. - * - * @param function The function. - * @param function2 Get function. - */ - @ApiStatus.Internal - public static void initialize(@NotNull final BiConsumer function, - @NotNull final Function function2) { - Validate.isTrue(!initialized, "Already initialized!"); - - metaSetConsumer = function; - metaGetConsumer = function2; - initialized = true; + return Eco.getHandler().getSkullTexture(meta); } private SkullUtils() { diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoHandler.kt index 7c743466..7a55a3e4 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoHandler.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoHandler.kt @@ -6,8 +6,11 @@ import com.willfp.eco.core.PluginProps import com.willfp.eco.core.data.ExtendedPersistentDataContainer import com.willfp.eco.core.entities.ai.EntityController import com.willfp.eco.core.fast.FastItemStack +import com.willfp.eco.core.gui.menu.Menu import com.willfp.eco.core.integrations.placeholder.PlaceholderIntegration import com.willfp.eco.core.items.SNBTHandler +import com.willfp.eco.core.placeholder.AdditionalPlayer +import com.willfp.eco.core.placeholder.PlaceholderInjectable import com.willfp.eco.internal.EcoCleaner import com.willfp.eco.internal.EcoPropsParser import com.willfp.eco.internal.Plugins @@ -23,6 +26,7 @@ import com.willfp.eco.internal.fast.FastInternalNamespacedKeyFactory import com.willfp.eco.internal.fast.InternalNamespacedKeyFactory import com.willfp.eco.internal.fast.SafeInternalNamespacedKeyFactory import com.willfp.eco.internal.gui.EcoGUIFactory +import com.willfp.eco.internal.gui.menu.getMenu import com.willfp.eco.internal.integrations.PlaceholderIntegrationPAPI import com.willfp.eco.internal.logging.EcoLogger import com.willfp.eco.internal.proxy.EcoProxyFactory @@ -33,18 +37,23 @@ import com.willfp.eco.internal.spigot.data.EcoProfileHandler import com.willfp.eco.internal.spigot.data.storage.HandlerType import com.willfp.eco.internal.spigot.integrations.bstats.MetricHandler import com.willfp.eco.internal.spigot.items.EcoSNBTHandler +import com.willfp.eco.internal.spigot.math.evaluateExpression import com.willfp.eco.internal.spigot.proxy.CommonsInitializerProxy import com.willfp.eco.internal.spigot.proxy.DummyEntityFactoryProxy import com.willfp.eco.internal.spigot.proxy.EntityControllerFactoryProxy import com.willfp.eco.internal.spigot.proxy.ExtendedPersistentDataContainerFactoryProxy import com.willfp.eco.internal.spigot.proxy.FastItemStackFactoryProxy import com.willfp.eco.internal.spigot.proxy.MiniMessageTranslatorProxy +import com.willfp.eco.internal.spigot.proxy.SkullProxy +import com.willfp.eco.internal.spigot.proxy.TPSProxy import net.kyori.adventure.platform.bukkit.BukkitAudiences import org.bukkit.Location import org.bukkit.NamespacedKey import org.bukkit.entity.Entity import org.bukkit.entity.Mob +import org.bukkit.entity.Player import org.bukkit.inventory.ItemStack +import org.bukkit.inventory.meta.SkullMeta import org.bukkit.persistence.PersistentDataContainer import java.util.logging.Logger @@ -172,4 +181,23 @@ class EcoHandler : EcoSpigotPlugin(), Handler { override fun getSNBTHandler(): SNBTHandler = snbtHandler + + override fun getSkullTexture(meta: SkullMeta): String? = + getProxy(SkullProxy::class.java).getSkullTexture(meta) + + override fun setSkullTexture(meta: SkullMeta, base64: String) = + getProxy(SkullProxy::class.java).setSkullTexture(meta, base64) + + override fun getTPS(): Double = + getProxy(TPSProxy::class.java).getTPS() + + override fun evaluate( + expression: String, + player: Player?, + injectable: PlaceholderInjectable, + additionalPlayers: MutableCollection + ): Double = evaluateExpression(expression, player, injectable, additionalPlayers) + + override fun getOpenMenu(player: Player): Menu? = + player.openInventory.topInventory.getMenu() } 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 0bbffbcb..3fc90d28 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 @@ -38,7 +38,6 @@ import com.willfp.eco.internal.entities.EntityArgParserSilent import com.willfp.eco.internal.entities.EntityArgParserSize import com.willfp.eco.internal.entities.EntityArgParserSpawnReinforcements import com.willfp.eco.internal.entities.EntityArgParserSpeed -import com.willfp.eco.internal.gui.menu.getMenu import com.willfp.eco.internal.items.ArgParserColor import com.willfp.eco.internal.items.ArgParserCustomModelData import com.willfp.eco.internal.items.ArgParserEnchantment @@ -113,21 +112,14 @@ 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 import com.willfp.eco.internal.spigot.integrations.shop.ShopZShop -import com.willfp.eco.internal.spigot.math.evaluateExpression import com.willfp.eco.internal.spigot.player.PlayerHealthFixer import com.willfp.eco.internal.spigot.proxy.FastItemStackFactoryProxy -import com.willfp.eco.internal.spigot.proxy.SkullProxy -import com.willfp.eco.internal.spigot.proxy.TPSProxy import com.willfp.eco.internal.spigot.recipes.CraftingRecipeListener import com.willfp.eco.internal.spigot.recipes.StackedRecipeListener 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 com.willfp.eco.util.MenuUtils -import com.willfp.eco.util.NumberUtils -import com.willfp.eco.util.ServerUtils -import com.willfp.eco.util.SkullUtils import net.kyori.adventure.platform.bukkit.BukkitAudiences import net.milkbowl.vault.economy.Economy import org.bukkit.Bukkit @@ -176,19 +168,6 @@ abstract class EcoSpigotPlugin : EcoPlugin() { SegmentParserGroup().register() SegmentParserUseIfPresent().register() - val skullProxy = getProxy(SkullProxy::class.java) - SkullUtils.initialize( - { meta, base64 -> skullProxy.setSkullTexture(meta, base64) }, - { meta -> skullProxy.getSkullTexture(meta) } - ) - - val tpsProxy = getProxy(TPSProxy::class.java) - ServerUtils.initialize { tpsProxy.getTPS() } - - NumberUtils.initCrunch(::evaluateExpression) - - MenuUtils.initialize { it.openInventory.topInventory.getMenu() } - CustomItemsManager.registerProviders() postInit()