Added IntegrationRegistry
This commit is contained in:
@@ -1,13 +1,22 @@
|
||||
package com.willfp.eco.core.integrations;
|
||||
|
||||
import com.willfp.eco.core.registry.Registrable;
|
||||
import com.willfp.eco.core.registry.Registry;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* Abstract class for integrations.
|
||||
*/
|
||||
public interface Integration {
|
||||
public interface Integration extends Registrable {
|
||||
/**
|
||||
* Get the name of integration.
|
||||
*
|
||||
* @return The name.
|
||||
*/
|
||||
String getPluginName();
|
||||
|
||||
@Override
|
||||
default @NotNull String getID() {
|
||||
return Registry.tryFitPattern(this.getPluginName());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
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 java.util.HashSet;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* Registry for integrations.
|
||||
*
|
||||
* @param <T> The type of integration.
|
||||
*/
|
||||
public class IntegrationRegistry<T extends Integration> extends Registry<T> {
|
||||
/**
|
||||
* Iterate over all integrations, safely.
|
||||
*
|
||||
* @param action The action to perform.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If any integrations return true, safely.
|
||||
*
|
||||
* @param predicate The predicate to test.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* If all integrations return true, safely.
|
||||
*
|
||||
* @param predicate The predicate to test.
|
||||
*/
|
||||
public boolean allSafely(@NotNull final Predicate<T> predicate) {
|
||||
return !this.anySafely(predicate.negate());
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,9 @@
|
||||
package com.willfp.eco.core.integrations.afk;
|
||||
|
||||
import com.willfp.eco.core.integrations.IntegrationRegistry;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Class to handle afk integrations.
|
||||
*/
|
||||
@@ -13,7 +11,7 @@ public final class AFKManager {
|
||||
/**
|
||||
* A set of all registered integrations.
|
||||
*/
|
||||
private static final Set<AFKIntegration> REGISTERED = new HashSet<>();
|
||||
private static final IntegrationRegistry<AFKIntegration> REGISTRY = new IntegrationRegistry<>();
|
||||
|
||||
/**
|
||||
* Register a new integration.
|
||||
@@ -21,8 +19,7 @@ public final class AFKManager {
|
||||
* @param integration The integration to register.
|
||||
*/
|
||||
public static void register(@NotNull final AFKIntegration integration) {
|
||||
REGISTERED.removeIf(it -> it.getPluginName().equalsIgnoreCase(integration.getPluginName()));
|
||||
REGISTERED.add(integration);
|
||||
REGISTRY.register(integration);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,13 +29,7 @@ public final class AFKManager {
|
||||
* @return If afk.
|
||||
*/
|
||||
public static boolean isAfk(@NotNull final Player player) {
|
||||
for (AFKIntegration integration : REGISTERED) {
|
||||
if (integration.isAfk(player)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return REGISTRY.anySafely(integration -> integration.isAfk(player));
|
||||
}
|
||||
|
||||
private AFKManager() {
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
package com.willfp.eco.core.integrations.anticheat;
|
||||
|
||||
import com.willfp.eco.core.Eco;
|
||||
import com.willfp.eco.core.integrations.IntegrationRegistry;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Class to handle anticheat integrations.
|
||||
*/
|
||||
@@ -15,7 +13,7 @@ public final class AnticheatManager {
|
||||
/**
|
||||
* A set of all registered anticheats.
|
||||
*/
|
||||
private static final Set<AnticheatIntegration> ANTICHEATS = new HashSet<>();
|
||||
private static final IntegrationRegistry<AnticheatIntegration> REGISTRY = new IntegrationRegistry<>();
|
||||
|
||||
/**
|
||||
* Register a new anticheat.
|
||||
@@ -26,8 +24,7 @@ public final class AnticheatManager {
|
||||
if (anticheat instanceof Listener) {
|
||||
Eco.get().getEcoPlugin().getEventManager().registerListener((Listener) anticheat);
|
||||
}
|
||||
ANTICHEATS.removeIf(it -> it.getPluginName().equalsIgnoreCase(anticheat.getPluginName()));
|
||||
ANTICHEATS.add(anticheat);
|
||||
REGISTRY.register(anticheat);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -36,17 +33,16 @@ public final class AnticheatManager {
|
||||
* @param player The player to exempt.
|
||||
*/
|
||||
public static void exemptPlayer(@NotNull final Player player) {
|
||||
ANTICHEATS.forEach(anticheat -> anticheat.exempt(player));
|
||||
REGISTRY.forEachSafely(anticheat -> anticheat.exempt(player));
|
||||
}
|
||||
|
||||
/**
|
||||
* Unexempt a player from triggering anticheats.
|
||||
* This is ran a tick after it is called to ensure that there are no event timing conflicts.
|
||||
*
|
||||
* @param player The player to remove the exemption.
|
||||
*/
|
||||
public static void unexemptPlayer(@NotNull final Player player) {
|
||||
ANTICHEATS.forEach(anticheat -> anticheat.unexempt(player));
|
||||
REGISTRY.forEachSafely(anticheat -> anticheat.unexempt(player));
|
||||
}
|
||||
|
||||
private AnticheatManager() {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.willfp.eco.core.integrations.antigrief;
|
||||
|
||||
import com.willfp.eco.core.integrations.IntegrationRegistry;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
@@ -16,7 +17,7 @@ public final class AntigriefManager {
|
||||
/**
|
||||
* Registered antigriefs.
|
||||
*/
|
||||
private static final Set<AntigriefIntegration> REGISTERED = new HashSet<>();
|
||||
private static final IntegrationRegistry<AntigriefIntegration> REGISTRY = new IntegrationRegistry<>();
|
||||
|
||||
/**
|
||||
* Register a new AntiGrief/Land Management integration.
|
||||
@@ -24,8 +25,7 @@ public final class AntigriefManager {
|
||||
* @param antigrief The integration to register.
|
||||
*/
|
||||
public static void register(@NotNull final AntigriefIntegration antigrief) {
|
||||
REGISTERED.removeIf(it -> it.getPluginName().equalsIgnoreCase(antigrief.getPluginName()));
|
||||
REGISTERED.add(antigrief);
|
||||
REGISTRY.register(antigrief);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -34,8 +34,7 @@ public final class AntigriefManager {
|
||||
* @param antigrief The integration to unregister.
|
||||
*/
|
||||
public static void unregister(@NotNull final AntigriefIntegration antigrief) {
|
||||
REGISTERED.removeIf(it -> it.getPluginName().equalsIgnoreCase(antigrief.getPluginName()));
|
||||
REGISTERED.remove(antigrief);
|
||||
REGISTRY.remove(antigrief);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,7 +46,7 @@ public final class AntigriefManager {
|
||||
*/
|
||||
public static boolean canPickupItem(@NotNull final Player player,
|
||||
@NotNull final Location location) {
|
||||
return REGISTERED.stream().allMatch(antigriefIntegration -> antigriefIntegration.canPickupItem(player, location));
|
||||
return REGISTRY.allSafely(integration -> integration.canPickupItem(player, location));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,7 +58,7 @@ public final class AntigriefManager {
|
||||
*/
|
||||
public static boolean canBreakBlock(@NotNull final Player player,
|
||||
@NotNull final Block block) {
|
||||
return REGISTERED.stream().allMatch(antigriefIntegration -> antigriefIntegration.canBreakBlock(player, block));
|
||||
return REGISTRY.allSafely(integration -> integration.canBreakBlock(player, block));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,7 +70,7 @@ public final class AntigriefManager {
|
||||
*/
|
||||
public static boolean canCreateExplosion(@NotNull final Player player,
|
||||
@NotNull final Location location) {
|
||||
return REGISTERED.stream().allMatch(antigriefIntegration -> antigriefIntegration.canCreateExplosion(player, location));
|
||||
return REGISTRY.allSafely(integration -> integration.canCreateExplosion(player, location));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,7 +82,7 @@ public final class AntigriefManager {
|
||||
*/
|
||||
public static boolean canPlaceBlock(@NotNull final Player player,
|
||||
@NotNull final Block block) {
|
||||
return REGISTERED.stream().allMatch(antigriefIntegration -> antigriefIntegration.canPlaceBlock(player, block));
|
||||
return REGISTRY.allSafely(integration -> integration.canPlaceBlock(player, block));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -95,7 +94,7 @@ public final class AntigriefManager {
|
||||
*/
|
||||
public static boolean canInjure(@NotNull final Player player,
|
||||
@NotNull final LivingEntity victim) {
|
||||
return REGISTERED.stream().allMatch(antigriefIntegration -> antigriefIntegration.canInjure(player, victim));
|
||||
return REGISTRY.allSafely(integration -> integration.canInjure(player, victim));
|
||||
}
|
||||
|
||||
private AntigriefManager() {
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
package com.willfp.eco.core.integrations.customentities;
|
||||
|
||||
import com.willfp.eco.core.integrations.IntegrationRegistry;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Class to handle custom entity integrations.
|
||||
*/
|
||||
@@ -12,7 +10,7 @@ public final class CustomEntitiesManager {
|
||||
/**
|
||||
* A set of all registered integrations.
|
||||
*/
|
||||
private static final Set<CustomEntitiesIntegration> REGISTERED = new HashSet<>();
|
||||
private static final IntegrationRegistry<CustomEntitiesIntegration> REGISTRY = new IntegrationRegistry<>();
|
||||
|
||||
/**
|
||||
* Register a new integration.
|
||||
@@ -20,8 +18,7 @@ public final class CustomEntitiesManager {
|
||||
* @param integration The integration to register.
|
||||
*/
|
||||
public static void register(@NotNull final CustomEntitiesIntegration integration) {
|
||||
REGISTERED.removeIf(it -> it.getPluginName().equalsIgnoreCase(integration.getPluginName()));
|
||||
REGISTERED.add(integration);
|
||||
REGISTRY.register(integration);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -30,9 +27,7 @@ public final class CustomEntitiesManager {
|
||||
* @see com.willfp.eco.core.entities.Entities
|
||||
*/
|
||||
public static void registerAllEntities() {
|
||||
for (CustomEntitiesIntegration integration : REGISTERED) {
|
||||
integration.registerAllEntities();
|
||||
}
|
||||
REGISTRY.forEachSafely(CustomEntitiesIntegration::registerAllEntities);
|
||||
}
|
||||
|
||||
private CustomEntitiesManager() {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.willfp.eco.core.integrations.customitems;
|
||||
|
||||
import com.willfp.eco.core.integrations.IntegrationRegistry;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashSet;
|
||||
@@ -12,7 +13,7 @@ public final class CustomItemsManager {
|
||||
/**
|
||||
* A set of all registered integrations.
|
||||
*/
|
||||
private static final Set<CustomItemsIntegration> REGISTERED = new HashSet<>();
|
||||
private static final IntegrationRegistry<CustomItemsIntegration> REGISTRY = new IntegrationRegistry<>();
|
||||
|
||||
/**
|
||||
* Register a new integration.
|
||||
@@ -20,8 +21,7 @@ public final class CustomItemsManager {
|
||||
* @param integration The integration to register.
|
||||
*/
|
||||
public static void register(@NotNull final CustomItemsIntegration integration) {
|
||||
REGISTERED.removeIf(it -> it.getPluginName().equalsIgnoreCase(integration.getPluginName()));
|
||||
REGISTERED.add(integration);
|
||||
REGISTRY.register(integration);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -30,9 +30,7 @@ public final class CustomItemsManager {
|
||||
* @see com.willfp.eco.core.items.Items
|
||||
*/
|
||||
public static void registerAllItems() {
|
||||
for (CustomItemsIntegration customItemsIntegration : REGISTERED) {
|
||||
customItemsIntegration.registerAllItems();
|
||||
}
|
||||
REGISTRY.forEachSafely(CustomItemsIntegration::registerAllItems);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -41,9 +39,7 @@ public final class CustomItemsManager {
|
||||
* @see com.willfp.eco.core.items.Items
|
||||
*/
|
||||
public static void registerProviders() {
|
||||
for (CustomItemsIntegration customItemsIntegration : REGISTERED) {
|
||||
customItemsIntegration.registerProvider();
|
||||
}
|
||||
REGISTRY.forEachSafely(CustomItemsIntegration::registerProvider);
|
||||
}
|
||||
|
||||
private CustomItemsManager() {
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
package com.willfp.eco.core.integrations.economy;
|
||||
|
||||
import com.willfp.eco.core.integrations.IntegrationRegistry;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Class to handle economy.
|
||||
@@ -14,7 +13,7 @@ public final class EconomyManager {
|
||||
/**
|
||||
* A set of all registered integrations.
|
||||
*/
|
||||
private static final Set<EconomyIntegration> REGISTERED = new HashSet<>();
|
||||
private static final IntegrationRegistry<EconomyIntegration> REGISTRY = new IntegrationRegistry<>();
|
||||
|
||||
/**
|
||||
* Register a new integration.
|
||||
@@ -22,8 +21,7 @@ public final class EconomyManager {
|
||||
* @param integration The integration to register.
|
||||
*/
|
||||
public static void register(@NotNull final EconomyIntegration integration) {
|
||||
REGISTERED.removeIf(it -> it.getPluginName().equalsIgnoreCase(integration.getPluginName()));
|
||||
REGISTERED.add(integration);
|
||||
REGISTRY.register(integration);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,7 +30,7 @@ public final class EconomyManager {
|
||||
* @return If any economy.
|
||||
*/
|
||||
public static boolean hasRegistrations() {
|
||||
return !REGISTERED.isEmpty();
|
||||
return !REGISTRY.values().isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,7 +54,7 @@ public final class EconomyManager {
|
||||
*/
|
||||
public static boolean hasAmount(@NotNull final OfflinePlayer player,
|
||||
final BigDecimal amount) {
|
||||
for (EconomyIntegration integration : REGISTERED) {
|
||||
for (EconomyIntegration integration : REGISTRY) {
|
||||
return integration.hasAmount(player, amount);
|
||||
}
|
||||
|
||||
@@ -84,7 +82,7 @@ public final class EconomyManager {
|
||||
*/
|
||||
public static boolean giveMoney(@NotNull final OfflinePlayer player,
|
||||
@NotNull final BigDecimal amount) {
|
||||
for (EconomyIntegration integration : REGISTERED) {
|
||||
for (EconomyIntegration integration : REGISTRY) {
|
||||
return integration.giveMoney(player, amount);
|
||||
}
|
||||
|
||||
@@ -112,7 +110,7 @@ public final class EconomyManager {
|
||||
*/
|
||||
public static boolean removeMoney(@NotNull final OfflinePlayer player,
|
||||
@NotNull final BigDecimal amount) {
|
||||
for (EconomyIntegration integration : REGISTERED) {
|
||||
for (EconomyIntegration integration : REGISTRY) {
|
||||
return integration.removeMoney(player, amount);
|
||||
}
|
||||
|
||||
@@ -136,7 +134,7 @@ public final class EconomyManager {
|
||||
* @return The balance.
|
||||
*/
|
||||
public static BigDecimal getExactBalance(@NotNull final OfflinePlayer player) {
|
||||
for (EconomyIntegration integration : REGISTERED) {
|
||||
for (EconomyIntegration integration : REGISTRY) {
|
||||
return integration.getExactBalance(player);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
package com.willfp.eco.core.integrations.guidetection;
|
||||
|
||||
import com.willfp.eco.core.integrations.IntegrationRegistry;
|
||||
import com.willfp.eco.util.MenuUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Class to handle GUI detection.
|
||||
*/
|
||||
@@ -14,7 +12,7 @@ public final class GUIDetectionManager {
|
||||
/**
|
||||
* A set of all registered integrations.
|
||||
*/
|
||||
private static final Set<GUIDetectionIntegration> REGISTERED = new HashSet<>();
|
||||
private static final IntegrationRegistry<GUIDetectionIntegration> REGISTRY = new IntegrationRegistry<>();
|
||||
|
||||
/**
|
||||
* Register a new integration.
|
||||
@@ -22,8 +20,7 @@ public final class GUIDetectionManager {
|
||||
* @param integration The integration to register.
|
||||
*/
|
||||
public static void register(@NotNull final GUIDetectionIntegration integration) {
|
||||
REGISTERED.removeIf(it -> it.getPluginName().equalsIgnoreCase(integration.getPluginName()));
|
||||
REGISTERED.add(integration);
|
||||
REGISTRY.register(integration);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -37,13 +34,7 @@ public final class GUIDetectionManager {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (GUIDetectionIntegration integration : REGISTERED) {
|
||||
if (integration.hasGUIOpen(player)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return REGISTRY.anySafely(integration -> integration.hasGUIOpen(player));
|
||||
}
|
||||
|
||||
private GUIDetectionManager() {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.willfp.eco.core.integrations.mcmmo;
|
||||
|
||||
import com.willfp.eco.core.integrations.IntegrationRegistry;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.event.Event;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -14,7 +15,7 @@ public final class McmmoManager {
|
||||
/**
|
||||
* A set of all registered integrations.
|
||||
*/
|
||||
private static final Set<McmmoIntegration> REGISTERED = new HashSet<>();
|
||||
private static final IntegrationRegistry<McmmoIntegration> REGISTERED = new IntegrationRegistry<>();
|
||||
|
||||
/**
|
||||
* Register a new integration.
|
||||
@@ -22,8 +23,7 @@ public final class McmmoManager {
|
||||
* @param integration The integration to register.
|
||||
*/
|
||||
public static void register(@NotNull final McmmoIntegration integration) {
|
||||
REGISTERED.removeIf(it -> it.getPluginName().equalsIgnoreCase(integration.getPluginName()));
|
||||
REGISTERED.add(integration);
|
||||
REGISTERED.register(integration);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -34,13 +34,11 @@ public final class McmmoManager {
|
||||
*/
|
||||
public static int getBonusDropCount(@NotNull final Block block) {
|
||||
int finalValue = 0;
|
||||
|
||||
for (McmmoIntegration mcmmoIntegration : REGISTERED) {
|
||||
finalValue += mcmmoIntegration.getBonusDropCount(block);
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
return finalValue;
|
||||
}
|
||||
|
||||
@@ -51,13 +49,7 @@ public final class McmmoManager {
|
||||
* @return If the event is fake.
|
||||
*/
|
||||
public static boolean isFake(@NotNull final Event event) {
|
||||
for (McmmoIntegration mcmmoIntegration : REGISTERED) {
|
||||
if (mcmmoIntegration.isFake(event)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
return false;
|
||||
return REGISTERED.anySafely(integration -> integration.isFake(event));
|
||||
}
|
||||
|
||||
private McmmoManager() {
|
||||
|
||||
Reference in New Issue
Block a user