Cleaned up IntegrationRegistry

This commit is contained in:
Auxilor
2023-04-20 17:34:18 +01:00
parent 90c55849ae
commit 5473bb8ef8
5 changed files with 90 additions and 47 deletions

View File

@@ -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<T extends Integration> extends Registry<T> {
@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<T extends Integration> extends Registry<T> {
*/
public void forEachSafely(@NotNull final Consumer<T> 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<T extends Integration> extends Registry<T> {
*/
public boolean anySafely(@NotNull final Predicate<T> 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<T extends Integration> extends Registry<T> {
* @param <R> The type of value.
* @return The first value that returns a value.
*/
public <R> R firstSafely(@NotNull final R defaultValue,
@NotNull final Function<T, R> function) {
if (this.values().isEmpty()) {
@NotNull
public <R> R firstSafely(@NotNull final Function<T, R> 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 <R> The return type of the action.
* @return The result of the action, or null if an exception was thrown.
*/
private <R> R executeSafely(@NotNull final Supplier<R> 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 <R> The return type of the action.
* @return The result of the action, or the default value if an exception was thrown.
*/
private <R> R executeSafely(@NotNull final Supplier<R> 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;
}
/**

View File

@@ -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
);
}

View File

@@ -34,8 +34,8 @@ public final class HologramManager {
public static Hologram createHologram(@NotNull final Location location,
@NotNull final List<String> contents) {
return REGISTRY.firstSafely(
new DummyHologram(),
integration -> integration.createHologram(location, contents)
integration -> integration.createHologram(location, contents),
new DummyHologram()
);
}

View File

@@ -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
);
}

View File

@@ -150,6 +150,24 @@ public class Registry<T extends Registrable> implements Iterable<T> {
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<T> iterator() {