From 5473bb8ef80c6500c31e9d78703c36d2fafb1af6 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Thu, 20 Apr 2023 17:34:18 +0100 Subject: [PATCH] Cleaned up IntegrationRegistry --- .../integrations/IntegrationRegistry.java | 90 ++++++++++++------- .../integrations/economy/EconomyManager.java | 16 ++-- .../hologram/HologramManager.java | 4 +- .../core/integrations/shop/ShopManager.java | 9 +- .../willfp/eco/core/registry/Registry.java | 18 ++++ 5 files changed, 90 insertions(+), 47 deletions(-) diff --git a/eco-api/src/main/java/com/willfp/eco/core/integrations/IntegrationRegistry.java b/eco-api/src/main/java/com/willfp/eco/core/integrations/IntegrationRegistry.java index e9401667..cc52a735 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/integrations/IntegrationRegistry.java +++ b/eco-api/src/main/java/com/willfp/eco/core/integrations/IntegrationRegistry.java @@ -3,11 +3,13 @@ package com.willfp.eco.core.integrations; import com.willfp.eco.core.Eco; import com.willfp.eco.core.registry.Registry; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.HashSet; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; +import java.util.function.Supplier; /** * Registry for integrations. @@ -17,15 +19,7 @@ import java.util.function.Predicate; public class IntegrationRegistry extends Registry { @Override public @NotNull T register(@NotNull final T element) { - try { - return super.register(element); - } catch (final Exception e) { - Eco.get().getEcoPlugin().getLogger().warning("Integration for " + element.getPluginName() + " threw an exception!"); - Eco.get().getEcoPlugin().getLogger().warning("The integration will be not be registered."); - e.printStackTrace(); - } - - return element; + return executeSafely(() -> super.register(element), element); } /** @@ -35,14 +29,7 @@ public class IntegrationRegistry extends Registry { */ public void forEachSafely(@NotNull final Consumer action) { for (T integration : new HashSet<>(this.values())) { - try { - action.accept(integration); - } catch (final Exception e) { - Eco.get().getEcoPlugin().getLogger().warning("Integration for " + integration.getPluginName() + " threw an exception!"); - Eco.get().getEcoPlugin().getLogger().warning("The integration will be disabled."); - e.printStackTrace(); - this.remove(integration); - } + executeSafely(() -> action.accept(integration), integration); } } @@ -53,18 +40,11 @@ public class IntegrationRegistry extends Registry { */ public boolean anySafely(@NotNull final Predicate predicate) { for (T integration : new HashSet<>(this.values())) { - try { - if (predicate.test(integration)) { - return true; - } - } catch (final Exception e) { - Eco.get().getEcoPlugin().getLogger().warning("Integration for " + integration.getPluginName() + " threw an exception!"); - Eco.get().getEcoPlugin().getLogger().warning("The integration will be disabled."); - e.printStackTrace(); - this.remove(integration); + Boolean result = executeSafely(() -> predicate.test(integration), integration); + if (result != null && result) { + return true; } } - return false; } @@ -76,24 +56,66 @@ public class IntegrationRegistry extends Registry { * @param The type of value. * @return The first value that returns a value. */ - public R firstSafely(@NotNull final R defaultValue, - @NotNull final Function function) { - if (this.values().isEmpty()) { + @NotNull + public R firstSafely(@NotNull final Function function, + @NotNull final R defaultValue) { + if (this.isEmpty()) { return defaultValue; } T integration = this.iterator().next(); + return executeSafely(() -> function.apply(integration), integration, defaultValue); + } + + /** + * Executes a given action safely, catching any exceptions and logging the issue. + * + * @param action The action to execute. + * @param integration The integration to apply the action on. + */ + private void executeSafely(@NotNull final Runnable action, + @NotNull final T integration) { + executeSafely(() -> { + action.run(); + return null; + }, integration); + } + + /** + * Executes a given action safely, catching any exceptions and logging the issue. + * + * @param action The action to execute. + * @param integration The integration to apply the action on. + * @param The return type of the action. + * @return The result of the action, or null if an exception was thrown. + */ + private R executeSafely(@NotNull final Supplier action, + @NotNull final T integration) { + return executeSafely(action, integration, null); + } + + /** + * Executes a given action safely, catching any exceptions and logging the issue. + * + * @param action The action to execute. + * @param integration The integration to apply the action on. + * @param defaultValue The default value to return if an exception is thrown. + * @param The return type of the action. + * @return The result of the action, or the default value if an exception was thrown. + */ + private R executeSafely(@NotNull final Supplier action, + @NotNull final T integration, + @Nullable final R defaultValue) { try { - return function.apply(integration); + return action.get(); } catch (final Exception e) { Eco.get().getEcoPlugin().getLogger().warning("Integration for " + integration.getPluginName() + " threw an exception!"); Eco.get().getEcoPlugin().getLogger().warning("The integration will be disabled."); e.printStackTrace(); - this.remove(integration); + this.remove(integration.getPluginName()); + return defaultValue; } - - return defaultValue; } /** diff --git a/eco-api/src/main/java/com/willfp/eco/core/integrations/economy/EconomyManager.java b/eco-api/src/main/java/com/willfp/eco/core/integrations/economy/EconomyManager.java index 060ceabc..3b32401b 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/integrations/economy/EconomyManager.java +++ b/eco-api/src/main/java/com/willfp/eco/core/integrations/economy/EconomyManager.java @@ -55,8 +55,8 @@ public final class EconomyManager { public static boolean hasAmount(@NotNull final OfflinePlayer player, final BigDecimal amount) { return REGISTRY.firstSafely( - false, - integration -> integration.hasAmount(player, amount) + integration -> integration.hasAmount(player, amount), + false ); } @@ -82,8 +82,8 @@ public final class EconomyManager { public static boolean giveMoney(@NotNull final OfflinePlayer player, @NotNull final BigDecimal amount) { return REGISTRY.firstSafely( - false, - integration -> integration.giveMoney(player, amount) + integration -> integration.giveMoney(player, amount), + false ); } @@ -109,8 +109,8 @@ public final class EconomyManager { public static boolean removeMoney(@NotNull final OfflinePlayer player, @NotNull final BigDecimal amount) { return REGISTRY.firstSafely( - false, - integration -> integration.removeMoney(player, amount) + integration -> integration.removeMoney(player, amount), + false ); } @@ -132,8 +132,8 @@ public final class EconomyManager { */ public static BigDecimal getExactBalance(@NotNull final OfflinePlayer player) { return REGISTRY.firstSafely( - BigDecimal.ZERO, - integration -> integration.getExactBalance(player) + integration -> integration.getExactBalance(player), + BigDecimal.ZERO ); } diff --git a/eco-api/src/main/java/com/willfp/eco/core/integrations/hologram/HologramManager.java b/eco-api/src/main/java/com/willfp/eco/core/integrations/hologram/HologramManager.java index 9f1df8d7..0dc000fa 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/integrations/hologram/HologramManager.java +++ b/eco-api/src/main/java/com/willfp/eco/core/integrations/hologram/HologramManager.java @@ -34,8 +34,8 @@ public final class HologramManager { public static Hologram createHologram(@NotNull final Location location, @NotNull final List contents) { return REGISTRY.firstSafely( - new DummyHologram(), - integration -> integration.createHologram(location, contents) + integration -> integration.createHologram(location, contents), + new DummyHologram() ); } diff --git a/eco-api/src/main/java/com/willfp/eco/core/integrations/shop/ShopManager.java b/eco-api/src/main/java/com/willfp/eco/core/integrations/shop/ShopManager.java index f09838e8..4b0bff11 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/integrations/shop/ShopManager.java +++ b/eco-api/src/main/java/com/willfp/eco/core/integrations/shop/ShopManager.java @@ -69,7 +69,10 @@ public final class ShopManager { return new PriceFree(); } - return REGISTRY.firstSafely(new PriceFree(), integration -> integration.getUnitValue(itemStack, player)); + return REGISTRY.firstSafely( + integration -> integration.getUnitValue(itemStack, player), + new PriceFree() + ); } /** @@ -100,8 +103,8 @@ public final class ShopManager { } return REGISTRY.firstSafely( - 0.0, - integration -> integration.getUnitValue(itemStack, player).getValue(player, itemStack.getAmount()) + integration -> integration.getUnitValue(itemStack, player).getValue(player, itemStack.getAmount()), + 0.0 ); } diff --git a/eco-api/src/main/java/com/willfp/eco/core/registry/Registry.java b/eco-api/src/main/java/com/willfp/eco/core/registry/Registry.java index d4f8e8c4..5b6b1ad1 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/registry/Registry.java +++ b/eco-api/src/main/java/com/willfp/eco/core/registry/Registry.java @@ -150,6 +150,24 @@ public class Registry implements Iterable { isLocked = false; } + /** + * Get if the registry is empty. + * + * @return If the registry is empty. + */ + public boolean isEmpty() { + return registry.isEmpty(); + } + + /** + * Get if the registry is not empty. + * + * @return If the registry is not empty. + */ + public boolean isNotEmpty() { + return !isEmpty(); + } + @NotNull @Override public Iterator iterator() {