Merge remote-tracking branch 'origin/master'

# Conflicts:
#	gradle.properties
This commit is contained in:
Auxilor
2023-04-24 22:12:19 +01:00
33 changed files with 597 additions and 173 deletions

View File

@@ -19,9 +19,10 @@ import com.willfp.eco.core.proxy.ProxyFactory;
import com.willfp.eco.core.registry.Registrable;
import com.willfp.eco.core.registry.Registry;
import com.willfp.eco.core.scheduling.Scheduler;
import com.willfp.eco.core.version.OutdatedEcoVersionError;
import com.willfp.eco.core.version.Version;
import com.willfp.eco.core.web.UpdateChecker;
import org.apache.commons.lang.Validate;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.file.FileConfiguration;
@@ -347,14 +348,14 @@ public abstract class EcoPlugin extends JavaPlugin implements PluginLike, Regist
they have an outdated version of eco installed.
*/
DefaultArtifactVersion runningVersion = new DefaultArtifactVersion(Eco.get().getEcoPlugin().getDescription().getVersion());
DefaultArtifactVersion requiredVersion = new DefaultArtifactVersion(this.getMinimumEcoVersion());
Version runningVersion = new Version(Eco.get().getEcoPlugin().getDescription().getVersion());
Version requiredVersion = new Version(this.getMinimumEcoVersion());
if (!(runningVersion.compareTo(requiredVersion) > 0 || runningVersion.equals(requiredVersion))) {
this.getLogger().severe("You are running an outdated version of eco!");
this.getLogger().severe("You must be on at least" + this.getMinimumEcoVersion());
this.getLogger().severe("Download the newest version here:");
this.getLogger().severe("https://polymart.org/download/773/recent/JSpprMspkuyecf5y1wQ2Jn8OoLQSQ_IW");
Bukkit.getPluginManager().disablePlugin(this);
throw new OutdatedEcoVersionError("This plugin requires at least eco version " + this.getMinimumEcoVersion() + " to run.");
}
}
@@ -370,8 +371,8 @@ public abstract class EcoPlugin extends JavaPlugin implements PluginLike, Regist
if (this.getResourceId() != 0 && !Eco.get().getEcoPlugin().getConfigYml().getBool("no-update-checker")) {
new UpdateChecker(this).getVersion(version -> {
DefaultArtifactVersion currentVersion = new DefaultArtifactVersion(this.getDescription().getVersion());
DefaultArtifactVersion mostRecentVersion = new DefaultArtifactVersion(version);
Version currentVersion = new Version(this.getDescription().getVersion());
Version mostRecentVersion = new Version(version);
if (!(currentVersion.compareTo(mostRecentVersion) > 0 || currentVersion.equals(mostRecentVersion))) {
this.outdated = true;
this.getLogger().warning(this.getName() + " is out of date! (Version " + this.getDescription().getVersion() + ")");

View File

@@ -3,17 +3,26 @@ package com.willfp.eco.core.data;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
/**
* A simple store key-value store for data to be stored outside of plugins.
*/
@SuppressWarnings("unchecked")
public final class ExternalDataStore {
/**
* The store.
*/
private static final HashMap<String, Object> data = new HashMap<>();
private static final Map<String, Object> DATA = new HashMap<>();
/**
* The store adapters.
*/
private static final List<ExternalDataStoreObjectAdapter<?, ?>> STORE_ADAPTERS = new ArrayList<>();
/**
* Put data into the store.
@@ -23,7 +32,29 @@ public final class ExternalDataStore {
*/
public static void put(@NotNull final String key,
@NotNull final Object value) {
data.put(key, value);
doPut(key, value);
}
/**
* Put data into the store.
*
* @param key The key.
* @param value The value.
* @param <A> The stored type.
*/
private static <A> void doPut(@NotNull final String key,
@NotNull final A value) {
Object storedValue = value;
for (ExternalDataStoreObjectAdapter<?, ?> unknownAdapter : STORE_ADAPTERS) {
if (unknownAdapter.getAccessedClass().isInstance(value)) {
ExternalDataStoreObjectAdapter<A, ?> adapter = (ExternalDataStoreObjectAdapter<A, ?>) unknownAdapter;
storedValue = adapter.toStoredObject(value);
break;
}
}
DATA.put(key, storedValue);
}
/**
@@ -37,7 +68,30 @@ public final class ExternalDataStore {
@Nullable
public static <T> T get(@NotNull final String key,
@NotNull final Class<T> clazz) {
Object value = data.get(key);
return doGet(key, clazz);
}
/**
* Get data from the store.
*
* @param key The key.
* @param clazz The class.
* @param <A> The accessed type.
* @param <S> The stored type.
* @return The value.
*/
@Nullable
private static <A, S> A doGet(@NotNull final String key,
@NotNull final Class<A> clazz) {
Object value = DATA.get(key);
for (ExternalDataStoreObjectAdapter<?, ?> unknownAdapter : STORE_ADAPTERS) {
if (unknownAdapter.getStoredClass().isInstance(value) && unknownAdapter.getAccessedClass().equals(clazz)) {
ExternalDataStoreObjectAdapter<A, S> adapter = (ExternalDataStoreObjectAdapter<A, S>) unknownAdapter;
value = adapter.toAccessedObject((S) value);
break;
}
}
if (clazz.isInstance(value)) {
return clazz.cast(value);
@@ -79,6 +133,15 @@ public final class ExternalDataStore {
return get(key, clazz, defaultValue.get());
}
/**
* Register a new adapter.
*
* @param adapter The adapter.
*/
public static void registerAdapter(@NotNull final ExternalDataStoreObjectAdapter<?, ?> adapter) {
STORE_ADAPTERS.add(adapter);
}
private ExternalDataStore() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}

View File

@@ -0,0 +1,69 @@
package com.willfp.eco.core.data;
import org.jetbrains.annotations.NotNull;
/**
* An adapter for objects stored in {@link ExternalDataStore}.
*
* @param <A> The accessed class.
* @param <S> The stored class.
*/
public abstract class ExternalDataStoreObjectAdapter<A, S> {
/**
* The class that is accessed (read / written).
*/
private final Class<? extends A> accessedClass;
/**
* The class that is stored internally.
*/
private final Class<? extends S> storedClass;
/**
* Create a new adapter.
*
* @param accessedClass The class that is accessed (read / written).
* @param storedClass The class that is stored internally.
*/
protected ExternalDataStoreObjectAdapter(@NotNull final Class<? extends A> accessedClass,
@NotNull final Class<? extends S> storedClass) {
this.accessedClass = accessedClass;
this.storedClass = storedClass;
}
/**
* Convert an object to the stored object.
*
* @param obj The object.
* @return The stored object.
*/
@NotNull
public abstract S toStoredObject(@NotNull final A obj);
/**
* Convert an object to the accessed object.
*
* @param obj The object.
* @return The accessed object.
*/
@NotNull
public abstract A toAccessedObject(@NotNull final S obj);
/**
* Get the class that is accessed (read / written).
*
* @return The class.
*/
public Class<? extends A> getAccessedClass() {
return accessedClass;
}
/**
* Get the class that is stored internally.
*
* @return The class.
*/
public Class<? extends S> getStoredClass() {
return storedClass;
}
}

View File

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

View File

@@ -0,0 +1,131 @@
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.
*
* @param <T> The type of integration.
*/
public class IntegrationRegistry<T extends Integration> extends Registry<T> {
@Override
public @NotNull T register(@NotNull final T element) {
return executeSafely(() -> super.register(element), element);
}
/**
* 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())) {
executeSafely(() -> action.accept(integration), integration);
}
}
/**
* If any integrations return true, safely.
*
* @param predicate The predicate to test.
* @return If any integrations return true.
*/
public boolean anySafely(@NotNull final Predicate<T> predicate) {
for (T integration : new HashSet<>(this.values())) {
Boolean result = executeSafely(() -> predicate.test(integration), integration);
if (result != null && result) {
return true;
}
}
return false;
}
/**
* Get the first integration that returns a value, safely.
*
* @param function The function to apply.
* @param defaultValue The default value.
* @param <R> The type of value.
* @return The first value that returns a value.
*/
@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 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.getPluginName());
return defaultValue;
}
}
/**
* If all integrations return true, safely.
*
* @param predicate The predicate to test.
* @return If all integrations return true.
*/
public boolean allSafely(@NotNull final Predicate<T> predicate) {
return !this.anySafely(predicate.negate());
}
}

View File

@@ -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() {

View File

@@ -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() {

View File

@@ -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() {

View File

@@ -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() {

View File

@@ -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() {

View File

@@ -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.isNotEmpty();
}
/**
@@ -56,11 +54,10 @@ public final class EconomyManager {
*/
public static boolean hasAmount(@NotNull final OfflinePlayer player,
final BigDecimal amount) {
for (EconomyIntegration integration : REGISTERED) {
return integration.hasAmount(player, amount);
}
return false;
return REGISTRY.firstSafely(
integration -> integration.hasAmount(player, amount),
false
);
}
/**
@@ -84,11 +81,10 @@ public final class EconomyManager {
*/
public static boolean giveMoney(@NotNull final OfflinePlayer player,
@NotNull final BigDecimal amount) {
for (EconomyIntegration integration : REGISTERED) {
return integration.giveMoney(player, amount);
}
return false;
return REGISTRY.firstSafely(
integration -> integration.giveMoney(player, amount),
false
);
}
/**
@@ -112,11 +108,10 @@ public final class EconomyManager {
*/
public static boolean removeMoney(@NotNull final OfflinePlayer player,
@NotNull final BigDecimal amount) {
for (EconomyIntegration integration : REGISTERED) {
return integration.removeMoney(player, amount);
}
return false;
return REGISTRY.firstSafely(
integration -> integration.removeMoney(player, amount),
false
);
}
/**
@@ -136,11 +131,10 @@ public final class EconomyManager {
* @return The balance.
*/
public static BigDecimal getExactBalance(@NotNull final OfflinePlayer player) {
for (EconomyIntegration integration : REGISTERED) {
return integration.getExactBalance(player);
}
return BigDecimal.ZERO;
return REGISTRY.firstSafely(
integration -> integration.getExactBalance(player),
BigDecimal.ZERO
);
}
private EconomyManager() {

View File

@@ -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() {

View File

@@ -1,11 +1,10 @@
package com.willfp.eco.core.integrations.hologram;
import com.willfp.eco.core.integrations.IntegrationRegistry;
import org.bukkit.Location;
import org.jetbrains.annotations.NotNull;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Class to handle hologram integrations.
@@ -14,7 +13,7 @@ public final class HologramManager {
/**
* A set of all registered integrations.
*/
private static final Set<HologramIntegration> REGISTERED = new HashSet<>();
private static final IntegrationRegistry<HologramIntegration> REGISTRY = new IntegrationRegistry<>();
/**
* Register a new integration.
@@ -22,8 +21,7 @@ public final class HologramManager {
* @param integration The integration to register.
*/
public static void register(@NotNull final HologramIntegration integration) {
REGISTERED.removeIf(it -> it.getPluginName().equalsIgnoreCase(integration.getPluginName()));
REGISTERED.add(integration);
REGISTRY.register(integration);
}
/**
@@ -35,11 +33,10 @@ public final class HologramManager {
*/
public static Hologram createHologram(@NotNull final Location location,
@NotNull final List<String> contents) {
for (HologramIntegration integration : REGISTERED) {
return integration.createHologram(location, contents);
}
return new DummyHologram();
return REGISTRY.firstSafely(
integration -> integration.createHologram(location, contents),
new DummyHologram()
);
}
private HologramManager() {

View File

@@ -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() {

View File

@@ -1,5 +1,6 @@
package com.willfp.eco.core.integrations.shop;
import com.willfp.eco.core.integrations.IntegrationRegistry;
import com.willfp.eco.core.price.Price;
import com.willfp.eco.core.price.impl.PriceFree;
import org.bukkit.entity.Player;
@@ -13,11 +14,12 @@ import java.util.Set;
/**
* Class to handle shop integrations.
*/
@SuppressWarnings("DeprecatedIsStillUsed")
public final class ShopManager {
/**
* A set of all registered integrations.
*/
private static final Set<ShopIntegration> REGISTERED = new HashSet<>();
private static final IntegrationRegistry<ShopIntegration> REGISTRY = new IntegrationRegistry<>();
/**
* Register a new integration.
@@ -25,17 +27,14 @@ public final class ShopManager {
* @param integration The integration to register.
*/
public static void register(@NotNull final ShopIntegration integration) {
REGISTERED.removeIf(it -> it.getPluginName().equalsIgnoreCase(integration.getPluginName()));
REGISTERED.add(integration);
REGISTRY.register(integration);
}
/**
* Register eco item provider for shop plugins.
*/
public static void registerEcoProvider() {
for (ShopIntegration shopIntegration : REGISTERED) {
shopIntegration.registerEcoProvider();
}
REGISTRY.forEachSafely(ShopIntegration::registerEcoProvider);
}
/**
@@ -51,11 +50,7 @@ public final class ShopManager {
return false;
}
for (ShopIntegration integration : REGISTERED) {
return integration.isSellable(itemStack, player);
}
return false;
return REGISTRY.anySafely(integration -> integration.isSellable(itemStack, player));
}
/**
@@ -74,11 +69,10 @@ public final class ShopManager {
return new PriceFree();
}
for (ShopIntegration integration : REGISTERED) {
return integration.getUnitValue(itemStack, player);
}
return new PriceFree();
return REGISTRY.firstSafely(
integration -> integration.getUnitValue(itemStack, player),
new PriceFree()
);
}
/**
@@ -108,11 +102,10 @@ public final class ShopManager {
return 0.0;
}
for (ShopIntegration shopIntegration : REGISTERED) {
return shopIntegration.getUnitValue(itemStack, player).getValue(player, itemStack.getAmount());
}
return 0.0;
return REGISTRY.firstSafely(
integration -> integration.getUnitValue(itemStack, player).getValue(player, itemStack.getAmount()),
0.0
);
}
/**
@@ -121,7 +114,7 @@ public final class ShopManager {
* @return The integrations.
*/
public static Set<ShopIntegration> getRegisteredIntegrations() {
return new HashSet<>(REGISTERED);
return new HashSet<>(REGISTRY.values());
}
private ShopManager() {

View File

@@ -5,6 +5,8 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
@@ -14,7 +16,7 @@ import java.util.regex.Pattern;
*
* @param <T> The type of {@link Registrable}.
*/
public class Registry<T extends Registrable> {
public class Registry<T extends Registrable> implements Iterable<T> {
/**
* The ID pattern.
*/
@@ -25,6 +27,17 @@ public class Registry<T extends Registrable> {
*/
private final Map<String, T> registry = new HashMap<>();
/**
* If the registry is locked.
*/
private boolean isLocked = false;
/**
* The locker, used to 'secure' registries and prevent random unlocking.
*/
@Nullable
private Object locker = null;
/**
* Instantiate a new registry.
*/
@@ -40,6 +53,10 @@ public class Registry<T extends Registrable> {
*/
@NotNull
public T register(@NotNull final T element) {
if (this.isLocked) {
throw new IllegalStateException("Cannot add to locked registry! (ID: " + element.getID() + ")");
}
Validate.isTrue(ID_PATTERN.matcher(element.getID()).matches(), "ID must match pattern: " + ID_PATTERN.pattern() + " (was " + element.getID() + ")");
registry.put(element.getID(), element);
@@ -56,6 +73,10 @@ public class Registry<T extends Registrable> {
* @return The element.
*/
public T remove(@NotNull final T element) {
if (this.isLocked) {
throw new IllegalStateException("Cannot remove from locked registry! (ID: " + element.getID() + ")");
}
element.onRemove();
registry.remove(element.getID());
@@ -71,6 +92,10 @@ public class Registry<T extends Registrable> {
*/
@Nullable
public T remove(@NotNull final String id) {
if (this.isLocked) {
throw new IllegalStateException("Cannot remove from locked registry! (ID: " + id + ")");
}
T element = registry.get(id);
if (element != null) {
@@ -109,6 +134,61 @@ public class Registry<T extends Registrable> {
return Set.copyOf(registry.values());
}
/**
* Get if the registry is locked.
*
* @return If the registry is locked.
*/
public boolean isLocked() {
return isLocked;
}
/**
* Lock the registry.
*
* @param locker The locker.
*/
public void lock(@Nullable final Object locker) {
this.locker = locker;
isLocked = true;
}
/**
* Unlock the registry.
*
* @param locker The locker.
*/
public void unlock(@Nullable final Object locker) {
if (this.locker != locker) {
throw new IllegalArgumentException("Cannot unlock registry!");
}
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() {
return values().iterator();
}
/**
* Try to fit a string to the ID pattern.
*
@@ -120,6 +200,6 @@ public class Registry<T extends Registrable> {
return string.replace(" ", "_")
.replace(".", "_")
.replace("-", "_")
.toLowerCase();
.toLowerCase(Locale.ENGLISH);
}
}

View File

@@ -0,0 +1,17 @@
package com.willfp.eco.core.version;
import org.jetbrains.annotations.NotNull;
/**
* An error thrown when eco is outdated.
*/
public class OutdatedEcoVersionError extends Error {
/**
* Create a new OutdatedEcoVersionError.
*
* @param message The message.
*/
public OutdatedEcoVersionError(@NotNull final String message) {
super(message);
}
}

View File

@@ -0,0 +1,53 @@
package com.willfp.eco.core.version;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.jetbrains.annotations.NotNull;
/**
* A minimal class to represent a version, uses DefaultArtifactVersion under the hood.
* <p>
* This class exists to resolve issues where 1.17 doesn't include DefaultArtifactVersion.
*/
public class Version implements Comparable<Version> {
/**
* The version.
*/
private final DefaultArtifactVersion version;
/**
* Create a new version.
*
* @param version The version.
*/
public Version(@NotNull final String version) {
this.version = new DefaultArtifactVersion(version);
}
@Override
public int compareTo(@NotNull final Version o) {
return this.version.compareTo(o.version);
}
@Override
public String toString() {
return this.version.toString();
}
@Override
public boolean equals(@NotNull final Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof Version other)) {
return false;
}
return this.version.equals(other.version);
}
@Override
public int hashCode() {
return this.version.hashCode();
}
}

View File

@@ -0,0 +1,13 @@
package com.willfp.eco.core.registry
/**
* A registrable that has a string ID, for use with Kotlin.
*/
interface KRegistrable : Registrable {
/**
* The ID of the registrable.
*/
val id: String
override fun getID() = id
}