001package net.tnemc.plugincore.core.id;
002
003/*
004 * This class provides UUID-related functions, and has a concept of
005 * "live" UUIDs and "stored" UUIDs.
006 *
007 * live UUIDs are any UUID that is directly associated with a Player account.
008 *
009 * stored UUIDs are any UUID that is generated for use inside the plugin
010 * only. This could be a fake account related to an NPC account, or related
011 * to a name that has never been registered on Minecraft.net.
012 */
013
014import java.nio.charset.StandardCharsets;
015import java.util.Optional;
016import java.util.UUID;
017import java.util.concurrent.ConcurrentHashMap;
018
019import static net.tnemc.plugincore.core.utils.PlayerHelper.playerMatcher;
020
021/**
022 * Represents a provider that is used to resolve and store UUID<->Username Pairs.
023 *
024 * @author creatorfromhell
025 * @since 0.1.2.0
026 */
027public interface UUIDProvider {
028
029  /**
030   * Used to determine if a string is a valid minecraft username or not.
031   *
032   * @param name The name to check.
033   *
034   * @return True if the name is a valid minecraft username, otherwise false.
035   */
036  static boolean validate(final String name) {
037
038    if(name.length() >= 3 && name.length() <= 16) {
039      return playerMatcher().matcher(name).matches();
040    }
041    return false;
042  }
043
044  /**
045   * Used to determine if a string is a valid UUID.
046   *
047   * @param identifier The string to check.
048   *
049   * @return True if the name is a valid UUID, otherwise false.
050   */
051  static boolean isUUID(final String identifier) {
052
053    try {
054      UUID.fromString(identifier);
055      return true;
056    } catch(final Exception ignore) {
057      return false;
058    }
059  }
060
061  /**
062   * Returns the associated {@link UUIDAPI uuid api} associated with this provider.
063   *
064   * @return The associated {@link UUIDAPI uuid api} associated with this provider.
065   *
066   * @see UUIDAPI
067   */
068  UUIDAPI api();
069
070  /**
071   * Used to retrieve a {@link UUIDPair}.
072   *
073   * @param name The username of the pair.
074   *
075   * @return An optional containing the pair if found, otherwise an empty optional.
076   */
077  Optional<UUIDPair> retrieve(final String name);
078
079  /**
080   * Used to retrieve a name from its associated {@link UUID}.
081   *
082   * @param id The {@link UUID} to use in the search.
083   *
084   * @return An optional containing the name if found, otherwise an empty optional.
085   */
086  Optional<String> retrieveName(final UUID id);
087
088  /**
089   * Used to store a Username & UUID pair. This could be to a map, or to a database for persistent
090   * usage or to both.
091   *
092   * @param pair The {@link UUIDPair}
093   */
094  void store(final UUIDPair pair);
095
096  default UUID generateOffline(final String name) {
097
098    return UUID.nameUUIDFromBytes(("Offline:" + name).getBytes(StandardCharsets.UTF_8));
099  }
100
101  /**
102   * Retrieves the map containing all stored UUID pairs.
103   *
104   * @return A map where the keys are UUIDs and the values are {@link UUIDPair} instances.
105   */
106  ConcurrentHashMap<UUID, UUIDPair> pairs();
107}